Update sli-adaptor/aai-service package names
[ccsdk/sli/adaptors.git] / aai-service / provider / src / main / java / org / onap / sli / adaptors / aai / GenericRequest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 ONAP Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.ccsdk.sli.adaptors.aai;
23
24 import java.io.UnsupportedEncodingException;
25 import java.lang.reflect.Field;
26 import java.net.MalformedURLException;
27 import java.net.URL;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.Set;
31
32 import javax.xml.bind.annotation.XmlElement;
33
34 import org.onap.ccsdk.sli.adaptors.aai.data.AAIDatum;
35 import org.openecomp.aai.inventory.v10.L3Network;
36 import org.openecomp.aai.inventory.v10.L3Networks;
37
38 import com.fasterxml.jackson.core.JsonProcessingException;
39 import com.fasterxml.jackson.databind.ObjectMapper;
40 import com.google.common.base.Joiner;
41
42 public class GenericRequest extends AAIRequest {
43
44
45         protected Class<? extends AAIDatum> model;
46
47         public GenericRequest(Class<? extends AAIDatum> clazz) {
48                 model = clazz;
49         }
50
51         @Override
52         public URL getRequestUrl(String method, String resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
53
54                 String request_url = null;
55                 String originalPath = null;
56                 String pathSubstitute = null;
57
58                 request_url = target_uri + getRequestPath();
59
60                 Map<String, String> queryParams = new HashMap<String, String> ();
61                 if(resourceVersion != null) {
62                         queryParams.put("resource-version",resourceVersion);
63                 }
64
65                 Set<String> uniqueResources = extractUniqueResourceSetFromKeys(requestProperties.stringPropertyNames());
66
67                 String[] keys = requestProperties.keySet().toArray(new String[0]);
68                 for(String key : keys) {
69                         if("cloud-region.cloud-region-id".equals(key))
70                                 continue;
71                         if("entitlement.resource-uuid".equals(key))
72                                 continue;
73                         if("license.resource-uuid".equals(key))
74                                 continue;
75
76
77                         String value = requestProperties.getProperty(key);
78                         if(key.contains(".")) {
79                                 String[] splitKey = key.split("\\.");
80                                 if("cloud-region".equals(splitKey[0])){
81                                         String cloudRegionId =  requestProperties.getProperty("cloud-region.cloud-region-id");
82                                         aaiService.LOGwriteDateTrace("cloud-region-id", cloudRegionId);
83                                         String token = String.format("%s/{%s}/{cloud-region-id}", splitKey[0], splitKey[1] );
84                                         String encoded_owner = encodeQuery(value);
85                                         String encoded_region = encodeQuery(cloudRegionId);
86                                         request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
87                                 } else  if("entitlement".equals(splitKey[0])){
88                                         String cloudRegionId =  requestProperties.getProperty("entitlement.resource-uuid");
89                                         aaiService.LOGwriteDateTrace("resource-uuid", cloudRegionId);
90                                         String token = String.format("%s/{%s}/{resource-uuid}", splitKey[0], splitKey[1] );
91                                         String encoded_owner = encodeQuery(value);
92                                         String encoded_region = encodeQuery(cloudRegionId);
93                                         request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
94                                 } else  if("license".equals(splitKey[0])){
95                                         String cloudRegionId =  requestProperties.getProperty("license.resource-uuid");
96                                         aaiService.LOGwriteDateTrace("resource-uuid", cloudRegionId);
97                                         String token = String.format("%s/{%s}/{resource-uuid}", splitKey[0], splitKey[1] );
98                                         String encoded_owner = encodeQuery(value);
99                                         String encoded_region = encodeQuery(cloudRegionId);
100                                         request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
101                                 } else {
102                                         Class<? extends AAIDatum> clazz = null;
103                                         try {
104                                                 clazz = getClassFromResource(splitKey[0]);
105                                         } catch (ClassNotFoundException exc) {
106                                                 LOG.warn("AAIRequest does not support class: " + exc.getMessage());
107                                                 return null;
108                                         }
109
110                                         if(clazz != null) {
111                                                 if(clazz == this.model) {
112                                                         Field[] fields = this.model.getDeclaredFields();
113                                                         Field field = fields[0];
114                                                         String fieldName = field.getName();
115                                                         XmlElement annotation = field.getAnnotation(XmlElement.class);
116                                                         String primaryId = annotation.name();
117                                                         if("##default".equals(primaryId)) {
118                                                                 primaryId = fieldName;
119                                                         }
120
121                                                         String token = String.format("%s/{%s}", splitKey[0], primaryId);
122
123                                                         if(splitKey[1].equals(primaryId)) {
124                                                                 String encoded_vnf = encodeQuery(value);
125                                                                 request_url = request_url.replace(token, String.format("%s/%s", splitKey[0], encoded_vnf));
126                                                         } else {
127                                                                 queryParams.put(splitKey[1], encodeQuery(value));
128                                                                 originalPath = token;
129                                                                 pathSubstitute = String.format("%s", splitKey[0]);
130                                                         }
131                                                 } else if(L3Networks.class == this.model) {
132                                                         Field[] fields = L3Network.class.getDeclaredFields();
133                                                         Field field = fields[0];
134                                                         String fieldName = field.getName();
135                                                         XmlElement annotation = field.getAnnotation(XmlElement.class);
136                                                         String primaryId = annotation.name();
137                                                         if("##default".equals(primaryId)) {
138                                                                 primaryId = fieldName;
139                                                         }
140
141                                                         String token = String.format("%s/{%s}", splitKey[0], primaryId);
142                                                         originalPath = token;
143                                                         pathSubstitute = String.format("");
144
145                                                         queryParams.put(splitKey[1], encodeQuery(value));
146                                                 } else {
147                                                         String token = String.format("%s/{%s}", splitKey[0], splitKey[1]);
148                                                         String encoded_vnf = encodeQuery(value);
149                                                         request_url = request_url.replace(token, String.format("%s/%s", splitKey[0], encoded_vnf));
150                                                 }
151                                         }
152
153                                 }
154                                 aaiService.LOGwriteDateTrace(splitKey[1], value);
155                         }
156                 }
157
158                 if(originalPath != null && pathSubstitute != null)
159                         request_url = request_url.replace(originalPath, pathSubstitute);
160
161                 if(!queryParams.isEmpty()) {
162                         Joiner.MapJoiner mapJoiner = Joiner.on("&").withKeyValueSeparator("=");
163                         String queryString = mapJoiner.join(queryParams);
164                         request_url = String.format("%s?%s", request_url, queryString);
165                 }
166
167                 URL http_req_url =      new URL(request_url);
168
169                 aaiService.LOGwriteFirstTrace(method, http_req_url.toString());
170
171
172                 return http_req_url;
173         }
174
175
176         public URL OriginalgetRequestUrl(String method, String resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
177
178                 String request_url = null;
179
180                 request_url = target_uri + getRequestPath();
181
182                 Map<String, String> keyValuepairs = new HashMap<String, String> ();
183                 Set<String> uniqueResources = extractUniqueResourceSetFromKeys(requestProperties.stringPropertyNames());
184
185                 String[] keys = requestProperties.keySet().toArray(new String[0]);
186                 for(String key : keys) {
187                         if("cloud-region.cloud-region-id".equals(key))
188                                 continue;
189                         if("entitlement.resource-uuid".equals(key))
190                                 continue;
191                         if("license.resource-uuid".equals(key))
192                                 continue;
193
194
195                         String value = requestProperties.getProperty(key);
196                         if(key.contains(".")) {
197                                 String[] splitKey = key.split("\\.");
198                                 if("cloud-region".equals(splitKey[0])){
199                                         String cloudRegionId =  requestProperties.getProperty("cloud-region.cloud-region-id");
200                                         aaiService.LOGwriteDateTrace("cloud-region-id", cloudRegionId);
201                                         String token = String.format("%s/{%s}/{cloud-region-id}", splitKey[0], splitKey[1] );
202                                         String encoded_owner = encodeQuery(value);
203                                         String encoded_region = encodeQuery(cloudRegionId);
204                                         request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
205                                 } else  if("entitlement".equals(splitKey[0])){
206                                         String cloudRegionId =  requestProperties.getProperty("entitlement.resource-uuid");
207                                         aaiService.LOGwriteDateTrace("resource-uuid", cloudRegionId);
208                                         String token = String.format("%s/{%s}/{resource-uuid}", splitKey[0], splitKey[1] );
209                                         String encoded_owner = encodeQuery(value);
210                                         String encoded_region = encodeQuery(cloudRegionId);
211                                         request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
212                                 } else  if("license".equals(splitKey[0])){
213                                         String cloudRegionId =  requestProperties.getProperty("license.resource-uuid");
214                                         aaiService.LOGwriteDateTrace("resource-uuid", cloudRegionId);
215                                         String token = String.format("%s/{%s}/{resource-uuid}", splitKey[0], splitKey[1] );
216                                         String encoded_owner = encodeQuery(value);
217                                         String encoded_region = encodeQuery(cloudRegionId);
218                                         request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
219                                 } else {
220                                         Class<? extends AAIDatum> clazz = null;
221                                         try {
222                                                 clazz = getClassFromResource(splitKey[0]);
223                                         } catch (ClassNotFoundException exc) {
224                                                 LOG.warn("AAIRequest does not support class: " + exc.getMessage());
225                                                 return null;
226                                         }
227
228                                         if(clazz != null) {
229                                                 if(clazz == this.model) {
230                                                         Field[] fields = this.model.getDeclaredFields();
231                                                         Field field = fields[0];
232                                                         String fieldName = field.getName();
233                                                         XmlElement annotation = field.getAnnotation(XmlElement.class);
234                                                         String primaryId = annotation.name();
235                                                         if("##default".equals(primaryId)) {
236                                                                 primaryId = fieldName;
237                                                         }
238
239                                                         String token = String.format("%s/{%s}", splitKey[0], primaryId);
240
241                                                         if(splitKey[1].equals(primaryId)) {
242                                                                 String encoded_vnf = encodeQuery(value);
243                                                                 request_url = request_url.replace(token, String.format("%s/%s", splitKey[0], encoded_vnf));
244                                                         } else {
245                                                                 String replacement = String.format("%s?%s=%s", splitKey[0], splitKey[1], encodeQuery(value));
246                                                                 if(request_url.contains(token))
247                                                                         request_url = request_url.replace(token, replacement);
248                                                         }
249                                                 } else {
250                                                         String token = String.format("%s/{%s}", splitKey[0], splitKey[1]);
251                                                         String encoded_vnf = encodeQuery(value);
252                                                         request_url = request_url.replace(token, String.format("%s/%s", splitKey[0], encoded_vnf));
253                                                 }
254                                         }
255
256                                 }
257                                 aaiService.LOGwriteDateTrace(splitKey[1], value);
258                         }
259                 }
260
261
262                 if(resourceVersion != null) {
263                         request_url = request_url +"?resource-version="+resourceVersion;
264                 }
265                 URL http_req_url =      new URL(request_url);
266
267                 aaiService.LOGwriteFirstTrace(method, http_req_url.toString());
268
269
270                 return http_req_url;
271         }
272
273         @Override
274         public URL getRequestQueryUrl(String method) throws UnsupportedEncodingException, MalformedURLException {
275                 return this.getRequestUrl(method, null);
276         }
277
278
279         @Override
280         public String toJSONString() {
281                 ObjectMapper mapper = getObjectMapper();
282
283                 String json_text = null;
284                 try {
285                         json_text = mapper.writeValueAsString(requestDatum);
286                 } catch (JsonProcessingException exc) {
287                         handleException(this, exc);
288                         return null;
289                 }
290                 return json_text;
291         }
292
293         @Override
294         public String[] getArgsList() {
295                 String[] args = {};
296                 return args;
297         }
298
299         @Override
300         public Class<? extends AAIDatum> getModelClass() {
301                 return model;
302         }
303
304         public void processRequestPathValues(Map<String, String> nameValues) {
305                 // identify unique resources
306                 Set<String> uniqueResources = AAIRequest.extractUniqueResourceSetFromKeys(nameValues.keySet());
307
308                 String[] arguments = nameValues.keySet().toArray(new String[0]);
309                 for(String name : arguments) {
310                         String tmpName = name.replaceAll("-", "_");
311                         String value = nameValues.get(tmpName);
312                         if(value != null && !value.isEmpty()) {
313                                 value = value.trim().replace("'", "").replace("$", "").replace("'", "");
314                                 tmpName = name.replaceAll("_", "-");
315                                 this.addRequestProperty(tmpName, value);
316                         }
317                 }
318         }
319 }