Merge "Move RefreshConfigTask under tasks"
[nonrtric.git] / sdnc-a1-controller / northbound / nonrt-ric-api / provider / src / main / java / org / onap / sdnc / northbound / restadpter / NearRicUrlProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.sdnc.northbound.restadpter;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.ArrayList;
26 import java.util.Enumeration;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Properties;
30 import org.onap.sdnc.northbound.exceptions.NearRtRicNotFoundException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.web.util.UriComponentsBuilder;
34
35 /**
36  * This class provides Near-RIC api to invoke the A1 interface
37  *
38  * @author lathishbabu.ganesan@est.tech
39  *
40  */
41
42 public class NearRicUrlProvider {
43
44   // nearRicMap provides mapping from nearRtRicId to domainname:port of nearRTRics
45   private HashMap<String, String> nearRicMap = new HashMap<>();
46   private static final String NEAR_RIC_LIST_FILE = "NearRtRicList.properties";
47   private final Logger log = LoggerFactory.getLogger(NearRicUrlProvider.class);
48
49   public NearRicUrlProvider() {
50       try {
51         readNearRtRicConfigFile();
52       } catch (IOException ex) {
53         log.error("Exception while reading nearRtRicConfigFile: {}", ex);
54       }
55   }
56
57   private void readNearRtRicConfigFile() throws IOException {
58       InputStream inputStream = NearRicUrlProvider.class.getClassLoader().getResourceAsStream(NEAR_RIC_LIST_FILE);
59       if (inputStream == null) {
60           log.error("The file {} not found in classpath", NEAR_RIC_LIST_FILE);
61       } else {
62           Properties properties = new Properties();
63           properties.load(inputStream);
64           Enumeration<?> keys = properties.propertyNames();
65           while (keys.hasMoreElements()) {
66               String key = (String) keys.nextElement();
67               nearRicMap.put(key, properties.getProperty(key));
68           }
69           inputStream.close();
70       }
71   }
72
73   /**
74    * Retrieve the list of Near-RICs
75    *
76    * @return the list of Near-RICs
77    */
78   public List<String> getNearRTRicIdsList () {
79       return new ArrayList<>(nearRicMap.keySet());
80   }
81
82   /**
83    * Retrieve the base url of the Near-RIC
84    *
85    * @return the base url
86    */
87   public String getBaseUrl(final String nearRtRicId) {
88     if (!nearRicMap.containsKey(nearRtRicId)) {
89         throw new NearRtRicNotFoundException("NearRtRic with this ID is not known by the A1 controller");
90     }
91     String baseUrl = "http://" + nearRicMap.get(nearRtRicId) + "/a1-p/";
92     return UriComponentsBuilder.fromUriString(baseUrl).build().toString();
93   }
94
95   /**
96    * Retrieve the url of A1 healthcheck
97    *
98    * @return the health check url
99    */
100   public String getHealthCheck(final String nearRtRicId) {
101     return UriComponentsBuilder.fromUriString(getBaseUrl(nearRtRicId)).pathSegment("healthcheck").build()
102         .toString();
103   }
104
105   /**
106    * Retrieve the policy type url
107    *
108    * @return the base url with the policytypes
109    */
110   public String getPolicyTypes(final String nearRtRicId) {
111     return UriComponentsBuilder.fromUriString(getBaseUrl(nearRtRicId)).pathSegment("policytypes/").build()
112         .toString();
113   }
114
115   /**
116    * Retrieve the url of policy type id
117    *
118    * @param policyTypeId Policy Type Id
119    * @return the policy type id url
120    */
121   public String getPolicyTypeId(final String nearRtRicId,
122           final String policyTypeId) {
123     return UriComponentsBuilder.fromUriString(getBaseUrl(nearRtRicId)).pathSegment("policytypes")
124         .pathSegment(policyTypeId).build().toString();
125   }
126
127   /**
128    * Retrieve the url of the policy instances
129    *
130    * @param policyTypeId Policy Type Id
131    * @return the policy instances for the given policy type
132    */
133   public String getPolicyInstances(final String nearRtRicId,
134           final String policyTypeId) {
135     return UriComponentsBuilder.fromUriString(getPolicyTypeId(nearRtRicId, policyTypeId)).pathSegment("policies")
136         .build().toString();
137   }
138
139   /**
140    * Retrieve the url of the policy instance id
141    *
142    * @param policyTypeId Policy Type Id
143    * @param policyInstanceId Policy Instance Id
144    * @return the policy instance id for the given policy type
145    */
146   public String getPolicyInstanceId(final String nearRtRicId, final String policyTypeId,
147           final String policyInstanceId) {
148     return UriComponentsBuilder.fromUriString(getPolicyTypeId(nearRtRicId, policyTypeId)).pathSegment("policies")
149         .pathSegment(policyInstanceId).build().toString();
150   }
151
152   /**
153    * Retrieve the url of the policy instance id status
154    *
155    * @param policyTypeId Policy Type Id
156    * @param policyInstanceId Policy Instance Id
157    * @return the policy instance id status for the given policy type
158    */
159   public String getPolicyInstanceIdStatus(final String nearRtRicId, final String policyTypeId,
160       final String policyInstanceId) {
161     return UriComponentsBuilder.fromUriString(getPolicyInstanceId(nearRtRicId, policyTypeId, policyInstanceId))
162         .pathSegment("status").build().toString();
163   }
164 }