Isolate deprecated code in CCSDK aai-service
[ccsdk/sli/adaptors.git] / aai-service / provider / src / main / java / org / onap / ccsdk / sli / adaptors / aai / GenericRequest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T 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.annotation.Annotation;
26 import java.lang.reflect.Field;
27 import java.lang.reflect.Method;
28 import java.net.MalformedURLException;
29 import java.net.URISyntaxException;
30 import java.net.URL;
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.Set;
34
35 import javax.xml.bind.annotation.XmlElement;
36 import javax.xml.bind.annotation.XmlRootElement;
37 import javax.xml.bind.annotation.XmlType;
38 import org.onap.ccsdk.sli.adaptors.aai.data.AAIDatum;
39 import org.openecomp.aai.inventory.v11.L3Network;
40 import org.openecomp.aai.inventory.v11.L3Networks;
41
42 import com.fasterxml.jackson.core.JsonProcessingException;
43 import com.fasterxml.jackson.databind.ObjectMapper;
44 import com.google.common.base.Joiner;
45
46 public class GenericRequest extends AAIRequest {
47
48
49     protected Class<? extends AAIDatum> model;
50
51     public GenericRequest(Class<? extends AAIDatum> clazz) {
52         model = clazz;
53     }
54
55     @Override
56     public String updatePathDataValues(Object resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
57
58         String originalPath = null;
59         String pathSubstitute = null;
60         String resourceName = null;
61
62         Annotation[] annotations = model.getAnnotations();
63         for(Annotation annotation : annotations) {
64             Class<? extends Annotation> anotationType = annotation.annotationType();
65             String annotationName = anotationType.getName();
66             if("javax.xml.bind.annotation.XmlRootElement".equals(annotationName)){
67                 XmlRootElement order = (XmlRootElement)annotation;
68                 resourceName = order.name();
69             }
70         }
71
72         String request_url = getRequestPath(resourceName);
73
74         Map<String, String> queryParams = new HashMap<String, String> ();
75         if(resourceVersion != null) {
76             queryParams.put("resource-version", resourceVersion.toString());
77         }
78
79         Set<String> uniqueResources = extractUniqueResourceSetFromKeys(requestProperties.stringPropertyNames());
80
81         String[] keys = requestProperties.keySet().toArray(new String[0]);
82         for(String key : keys) {
83             switch(key) {
84             case "cloud-region.cloud-region-id":
85             case "entitlement.resource-uuid":
86             case "license.resource-uuid":
87             case "route-target.route-target-role":
88             case "service-capability.vnf-type":
89             case "ctag-pool.availability-zone-name":
90                 continue;
91             }
92
93             String value = requestProperties.getProperty(key);
94             if(key.contains(".")) {
95                 String[] splitKey = key.split("\\.");
96                 if("cloud-region".equals(splitKey[0])){
97                     String cloudRegionId =  requestProperties.getProperty("cloud-region.cloud-region-id");
98                     aaiService.LOGwriteDateTrace("cloud-region-id", cloudRegionId);
99                     String token = String.format("%s/{%s}/{cloud-region-id}", splitKey[0], splitKey[1] );
100                     String encoded_owner = encodeQuery(value);
101                     String encoded_region = encodeQuery(cloudRegionId);
102                     request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
103                 } else     if("entitlement".equals(splitKey[0])){
104                     String cloudRegionId =  requestProperties.getProperty("entitlement.resource-uuid");
105                     aaiService.LOGwriteDateTrace("resource-uuid", cloudRegionId);
106                     String token = String.format("%s/{%s}/{resource-uuid}", splitKey[0], splitKey[1] );
107                     String encoded_owner = encodeQuery(value);
108                     String encoded_region = encodeQuery(cloudRegionId);
109                     request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
110                 } else     if("license".equals(splitKey[0])){
111                     String cloudRegionId =  requestProperties.getProperty("license.resource-uuid");
112                     aaiService.LOGwriteDateTrace("resource-uuid", cloudRegionId);
113                     String token = String.format("%s/{%s}/{resource-uuid}", splitKey[0], splitKey[1] );
114                     String encoded_owner = encodeQuery(value);
115                     String encoded_region = encodeQuery(cloudRegionId);
116                     request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
117                 } else     if("route-target".equals(splitKey[0])){
118                     String cloudRegionId =  requestProperties.getProperty("route-target.route-target-role");
119                     aaiService.LOGwriteDateTrace("route-target-role", cloudRegionId);
120                     String token = String.format("%s/{%s}/{route-target-role}", splitKey[0], splitKey[1] );
121                     String encoded_owner = encodeQuery(value);
122                     String encoded_region = encodeQuery(cloudRegionId);
123                     request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
124                 } else     if("service-capability".equals(splitKey[0])){
125                     String vnfType =  requestProperties.getProperty("service-capability.vnf-type");
126                     aaiService.LOGwriteDateTrace("vnf-type", vnfType);
127                     String token = String.format("%s/{%s}/{vnf-type}", splitKey[0], splitKey[1] );
128                     String encoded_service_type = encodeQuery(value);
129                     String encoded_vnf_type = encodeQuery(vnfType);
130                     if("service-capability".equals(resourceName)) {
131                         request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_service_type, encoded_vnf_type));
132                     } else if("service-capabilities".equals(resourceName)) {
133                         queryParams.put("service-type", encoded_service_type);
134                         queryParams.put("vnf-type", encoded_vnf_type);
135                     }
136                 } else     if("ctag-pool".equals(splitKey[0])){
137                     String cloudRegionId =  requestProperties.getProperty("ctag-pool.availability-zone-name");
138                     aaiService.LOGwriteDateTrace("availability-zone-name", cloudRegionId);
139                     String token = String.format("%s/{%s}/{availability-zone-name}", splitKey[0], splitKey[1] );
140                     String encoded_owner = encodeQuery(value);
141                     String encoded_region = encodeQuery(cloudRegionId);
142                     request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
143                 } else {
144                     Class<? extends AAIDatum> clazz = null;
145                     try {
146                         clazz = getClassFromResource(splitKey[0]);
147                     } catch (ClassNotFoundException exc) {
148                         LOG.warn("AAIRequest does not support class: " + exc.getMessage());
149                         return null;
150                     }
151
152                     if(clazz != null) {
153                         if(clazz == this.model) {
154                             Field[] fields = this.model.getDeclaredFields();
155                             Field field = fields[0];
156                             String fieldName = field.getName();
157                             XmlElement annotation = field.getAnnotation(XmlElement.class);
158                                                         String primaryId = null;
159                                                         if(annotation != null) {
160                                                                 primaryId = annotation.name();
161                             if("##default".equals(primaryId)) {
162                                 primaryId = fieldName;
163                             }
164                                                         } else {
165                                                                 primaryId = fieldName;
166                                                         }
167
168                             String token = String.format("%s/{%s}", splitKey[0], primaryId);
169
170                             if(splitKey[1].equals(primaryId)) {
171                                 String encoded_vnf = encodeQuery(value);
172                                 request_url = request_url.replace(token, String.format("%s/%s", splitKey[0], encoded_vnf));
173                             } else {
174                                 queryParams.put(splitKey[1], encodeQuery(value));
175                                 originalPath = token;
176                                 pathSubstitute = String.format("%s", splitKey[0]);
177                             }
178                         } else if(L3Networks.class == this.model) {
179                             Field[] fields = L3Network.class.getDeclaredFields();
180                             Field field = fields[0];
181                             String fieldName = field.getName();
182                             XmlElement annotation = field.getAnnotation(XmlElement.class);
183                             String primaryId = annotation.name();
184                             if("##default".equals(primaryId)) {
185                                 primaryId = fieldName;
186                             }
187
188                             String token = String.format("%s/{%s}", splitKey[0], primaryId);
189                             originalPath = token;
190                             pathSubstitute = String.format("");
191
192                             queryParams.put(splitKey[1], encodeQuery(value));
193                         } else {
194                             String token = String.format("%s/{%s}", splitKey[0], splitKey[1]);
195                             String encoded_vnf = encodeQuery(value);
196                             request_url = request_url.replace(token, String.format("%s/%s", splitKey[0], encoded_vnf));
197                         }
198                     }
199
200                 }
201                 aaiService.LOGwriteDateTrace(splitKey[1], value);
202             }
203         }
204
205         if(originalPath != null && pathSubstitute != null)
206             request_url = request_url.replace(originalPath, pathSubstitute);
207
208         if(!queryParams.isEmpty()) {
209             Joiner.MapJoiner mapJoiner = Joiner.on("&").withKeyValueSeparator("=");
210             String queryString = mapJoiner.join(queryParams);
211             request_url = String.format("%s?%s", request_url, queryString);
212         }
213
214         return request_url;
215     }
216
217     @Override
218         public URL getRequestQueryUrl(String method) throws UnsupportedEncodingException, MalformedURLException, URISyntaxException {
219         return this.getRequestUrl(method, null);
220     }
221
222
223     @Override
224     public String toJSONString() {
225         ObjectMapper mapper = getObjectMapper();
226
227         String json_text = null;
228         try {
229             json_text = mapper.writeValueAsString(requestDatum);
230         } catch (JsonProcessingException exc) {
231             handleException(this, exc);
232             return null;
233         }
234         return json_text;
235     }
236
237     @Override
238     public String[] getArgsList() {
239         String[] args = {};
240         return args;
241     }
242
243     @Override
244     public Class<? extends AAIDatum> getModelClass() {
245         return model;
246     }
247
248     public void processRequestPathValues(Map<String, String> nameValues) {
249         // identify unique resources
250         Set<String> uniqueResources = AAIRequest.extractUniqueResourceSetFromKeys(nameValues.keySet());
251
252         String[] arguments = nameValues.keySet().toArray(new String[0]);
253         for(String name : arguments) {
254             String tmpName = name.replaceAll("-", "_");
255             String value = nameValues.get(tmpName);
256             if(value != null && !value.isEmpty()) {
257                 value = value.trim().replace("'", "").replace("$", "").replace("'", "");
258                 tmpName = name.replaceAll("_", "-");
259                 this.addRequestProperty(tmpName, value);
260             }
261         }
262     }
263 }