14c34e4f517c747778d813f15e94d831dfc7d853
[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.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.web.util.UriComponentsBuilder;
33
34 /**
35  * This class provides Near-RIC api to invoke the A1 interface
36  *
37  * @author lathishbabu.ganesan@est.tech
38  *
39  */
40
41 public class NearRicUrlProvider {
42
43   // nearRicMap provides mapping from nearRtRicId to domainname:port of nearRTRics
44   private HashMap<String, String> nearRicMap = new HashMap<>();
45   private static final String NEAR_RIC_LIST_FILE = "NearRtRicList.properties";
46   private final Logger log = LoggerFactory.getLogger(NearRicUrlProvider.class);
47
48   public NearRicUrlProvider() {
49       try {
50         readNearRtRicConfigFile();
51       } catch (IOException ex) {
52         log.error("Exception while reading nearRtRicConfigFile: {}", ex);
53       }
54   }
55
56   private void readNearRtRicConfigFile() throws IOException {
57       InputStream inputStream = NearRicUrlProvider.class.getClassLoader().getResourceAsStream(NEAR_RIC_LIST_FILE);
58       if (inputStream == null) {
59           log.error("The file {} not found in classpath", NEAR_RIC_LIST_FILE);
60       } else {
61           Properties properties = new Properties();
62           properties.load(inputStream);
63           Enumeration<?> keys = properties.propertyNames();
64           while (keys.hasMoreElements()) {
65               String key = (String) keys.nextElement();
66               nearRicMap.put(key, properties.getProperty(key));
67           }
68           inputStream.close();
69       }
70   }
71
72   /**
73    * Retrieve the list of Near-RICs
74    *
75    * @return the list of Near-RICs
76    */
77   public List<String> getNearRTRicIdsList () {
78       return new ArrayList<>(nearRicMap.keySet());
79   }
80
81   /**
82    * Retrieve the base url of the Near-RIC
83    *
84    * @return the base url
85    */
86   public String getBaseUrl(final String nearRtRicId) {
87     String baseUrl = "http://" + nearRicMap.get(nearRtRicId) + "/a1-p/";
88     return UriComponentsBuilder.fromUriString(baseUrl).build().toString();
89   }
90
91   /**
92    * Retrieve the url of A1 healthcheck
93    *
94    * @return the health check url
95    */
96   public String getHealthCheck(final String nearRtRicId) {
97     return UriComponentsBuilder.fromUriString(getBaseUrl(nearRtRicId)).pathSegment("healthcheck").build()
98         .toString();
99   }
100
101   /**
102    * Retrieve the policy type url
103    *
104    * @return the base url with the policytypes
105    */
106   public String getPolicyTypes(final String nearRtRicId) {
107     return UriComponentsBuilder.fromUriString(getBaseUrl(nearRtRicId)).pathSegment("policytypes/").build()
108         .toString();
109   }
110
111   /**
112    * Retrieve the url of policy type id
113    *
114    * @param policyTypeId Policy Type Id
115    * @return the policy type id url
116    */
117   public String getPolicyTypeId(final String nearRtRicId, final String policyTypeId) {
118     return UriComponentsBuilder.fromUriString(getBaseUrl(nearRtRicId)).pathSegment("policytypes")
119         .pathSegment(policyTypeId).build().toString();
120   }
121
122   /**
123    * Retrieve the url of the policy instances
124    *
125    * @param policyTypeId Policy Type Id
126    * @return the policy instances for the given policy type
127    */
128   public String getPolicyInstances(final String nearRtRicId, final String policyTypeId) {
129     return UriComponentsBuilder.fromUriString(getPolicyTypeId(nearRtRicId, policyTypeId)).pathSegment("policies")
130         .build().toString();
131   }
132
133   /**
134    * Retrieve the url of the policy instance id
135    *
136    * @param policyTypeId Policy Type Id
137    * @param policyInstanceId Policy Instance Id
138    * @return the policy instance id for the given policy type
139    */
140   public String getPolicyInstanceId(final String nearRtRicId, final String policyTypeId, final String policyInstanceId) {
141     return UriComponentsBuilder.fromUriString(getPolicyTypeId(nearRtRicId, policyTypeId)).pathSegment("policies")
142         .pathSegment(policyInstanceId).build().toString();
143   }
144
145   /**
146    * Retrieve the url of the policy instance id status
147    *
148    * @param policyTypeId Policy Type Id
149    * @param policyInstanceId Policy Instance Id
150    * @return the policy instance id status for the given policy type
151    */
152   public String getPolicyInstanceIdStatus(final String nearRtRicId, final String policyTypeId,
153       final String policyInstanceId) {
154     return UriComponentsBuilder.fromUriString(getPolicyInstanceId(nearRtRicId, policyTypeId, policyInstanceId))
155         .pathSegment("status").build().toString();
156   }
157 }