Updating aai adapter to v16 model
[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  * Modifications Copyright (C) 2018 IBM.
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23  /**
24  * @author Rich Tabedzki
25  *
26  */
27 package org.onap.ccsdk.sli.adaptors.aai;
28
29 import java.io.UnsupportedEncodingException;
30 import java.lang.annotation.Annotation;
31 import java.lang.reflect.Field;
32 import java.net.MalformedURLException;
33 import java.net.URISyntaxException;
34 import java.net.URL;
35 import java.util.HashMap;
36 import java.util.Map;
37 import java.util.Set;
38
39 import javax.xml.bind.annotation.XmlElement;
40 import javax.xml.bind.annotation.XmlRootElement;
41
42 import org.onap.ccsdk.sli.adaptors.aai.data.AAIDatum;
43 import org.onap.aai.inventory.v16.L3Network;
44 import org.onap.aai.inventory.v16.L3Networks;
45
46 import com.fasterxml.jackson.core.JsonProcessingException;
47 import com.fasterxml.jackson.databind.ObjectMapper;
48 import com.google.common.base.Joiner;
49
50 public class GenericRequest extends AAIRequest {
51
52
53     protected Class<? extends AAIDatum> model;
54
55     public GenericRequest(Class<? extends AAIDatum> clazz) {
56         model = clazz;
57     }
58
59     @Override
60     public String updatePathDataValues(Object resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
61
62         String originalPath = null;
63         String pathSubstitute = null;
64         String resourceName = null;
65
66         Annotation[] annotations = model.getAnnotations();
67         for(Annotation annotation : annotations) {
68             Class<? extends Annotation> anotationType = annotation.annotationType();
69             String annotationName = anotationType.getName();
70             if("javax.xml.bind.annotation.XmlRootElement".equals(annotationName)){
71                 XmlRootElement order = (XmlRootElement)annotation;
72                 resourceName = order.name();
73             }
74         }
75
76         String request_url = getRequestPath(resourceName);
77
78         Map<String, String> queryParams = new HashMap<> ();
79         if(resourceVersion != null) {
80             queryParams.put("resource-version", resourceVersion.toString());
81         }
82
83         Set<String> uniqueResources = extractUniqueResourceSetFromKeys(requestProperties.stringPropertyNames());
84
85         String[] keys = requestProperties.keySet().toArray(new String[0]);
86         for(String key : keys) {
87             switch(key) {
88             case "cloud-region.cloud-region-id":
89             case "entitlement.resource-uuid":
90             case "license.resource-uuid":
91             case "route-target.route-target-role":
92             case "service-capability.vnf-type":
93             case "ctag-pool.availability-zone-name":
94                 continue;
95             }
96
97             String value = requestProperties.getProperty(key);
98             if(key.contains(".")) {
99                 String[] splitKey = key.split("\\.");
100                 if("cloud-region".equals(splitKey[0])){
101                     String cloudRegionId =  requestProperties.getProperty("cloud-region.cloud-region-id");
102                     aaiService.LOGwriteDateTrace("cloud-region-id", cloudRegionId);
103                     String token = String.format("%s/{%s}/{cloud-region-id}", splitKey[0], splitKey[1] );
104                     String encoded_owner = encodeQuery(value);
105                     String encoded_region = encodeQuery(cloudRegionId);
106                     request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
107                 } else     if("entitlement".equals(splitKey[0])){
108                     String cloudRegionId =  requestProperties.getProperty("entitlement.resource-uuid");
109                     aaiService.LOGwriteDateTrace("resource-uuid", cloudRegionId);
110                     String token = String.format("%s/{%s}/{resource-uuid}", splitKey[0], splitKey[1] );
111                     String encoded_owner = encodeQuery(value);
112                     String encoded_region = encodeQuery(cloudRegionId);
113                     request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
114                 } else     if("license".equals(splitKey[0])){
115                     String cloudRegionId =  requestProperties.getProperty("license.resource-uuid");
116                     aaiService.LOGwriteDateTrace("resource-uuid", cloudRegionId);
117                     String token = String.format("%s/{%s}/{resource-uuid}", splitKey[0], splitKey[1] );
118                     String encoded_owner = encodeQuery(value);
119                     String encoded_region = encodeQuery(cloudRegionId);
120                     request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
121                 } else     if("route-target".equals(splitKey[0])){
122                     String cloudRegionId =  requestProperties.getProperty("route-target.route-target-role");
123                     aaiService.LOGwriteDateTrace("route-target-role", cloudRegionId);
124                     String token = String.format("%s/{%s}/{route-target-role}", splitKey[0], splitKey[1] );
125                     String encoded_owner = encodeQuery(value);
126                     String encoded_region = encodeQuery(cloudRegionId);
127                     request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
128                 } else     if("service-capability".equals(splitKey[0])){
129                     String vnfType =  requestProperties.getProperty("service-capability.vnf-type");
130                     aaiService.LOGwriteDateTrace("vnf-type", vnfType);
131                     String token = String.format("%s/{%s}/{vnf-type}", splitKey[0], splitKey[1] );
132                     String encoded_service_type = encodeQuery(value);
133                     String encoded_vnf_type = encodeQuery(vnfType);
134                     if("service-capability".equals(resourceName)) {
135                         request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_service_type, encoded_vnf_type));
136                     } else if("service-capabilities".equals(resourceName)) {
137                         queryParams.put("service-type", encoded_service_type);
138                         queryParams.put("vnf-type", encoded_vnf_type);
139                     }
140                 } else     if("ctag-pool".equals(splitKey[0])){
141                     String cloudRegionId =  requestProperties.getProperty("ctag-pool.availability-zone-name");
142                     aaiService.LOGwriteDateTrace("availability-zone-name", cloudRegionId);
143                     String token = String.format("%s/{%s}/{availability-zone-name}", splitKey[0], splitKey[1] );
144                     String encoded_owner = encodeQuery(value);
145                     String encoded_region = encodeQuery(cloudRegionId);
146                     request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
147                 } else {
148                     Class<? extends AAIDatum> clazz = getClassFromResource(splitKey[0]);
149
150                     if(clazz != null) {
151                         if(clazz == this.model) {
152                             Field[] fields = this.model.getDeclaredFields();
153                             Field field = fields[0];
154                             String fieldName = field.getName();
155                             XmlElement annotation = field.getAnnotation(XmlElement.class);
156                             String primaryId = null;
157                             if(annotation != null) {
158                                 primaryId = annotation.name();
159                             if("##default".equals(primaryId)) {
160                                 primaryId = fieldName;
161                             }
162                             } else {
163                                 primaryId = fieldName;
164                             }
165
166                             String token = String.format("%s/{%s}", splitKey[0], primaryId);
167
168                             if(splitKey[1].equals(primaryId)) {
169                                 String encoded_vnf = encodeQuery(value);
170                                 request_url = request_url.replace(token, String.format("%s/%s", splitKey[0], encoded_vnf));
171                             } else {
172                                 queryParams.put(splitKey[1], encodeQuery(value));
173                                 originalPath = token;
174                                 pathSubstitute = String.format("%s", splitKey[0]);
175                             }
176                         } else if(L3Networks.class == this.model) {
177                             Field[] fields = L3Network.class.getDeclaredFields();
178                             Field field = fields[0];
179                             String fieldName = field.getName();
180                             XmlElement annotation = field.getAnnotation(XmlElement.class);
181                             String primaryId = annotation.name();
182                             if("##default".equals(primaryId)) {
183                                 primaryId = fieldName;
184                             }
185
186                             String token = String.format("%s/{%s}", splitKey[0], primaryId);
187                             originalPath = token;
188                             pathSubstitute = String.format("");
189
190                             queryParams.put(splitKey[1], encodeQuery(value));
191                         } else {
192                             String token = String.format("%s/{%s}", splitKey[0], splitKey[1]);
193                             String encoded_vnf = encodeQuery(value);
194                             request_url = request_url.replace(token, String.format("%s/%s", splitKey[0], encoded_vnf));
195                         }
196                     }
197
198                 }
199                 aaiService.LOGwriteDateTrace(splitKey[1], value);
200             }
201         }
202
203         if(originalPath != null && pathSubstitute != null)
204             request_url = request_url.replace(originalPath, pathSubstitute);
205
206         if(!queryParams.isEmpty()) {
207             Joiner.MapJoiner mapJoiner = Joiner.on("&").withKeyValueSeparator("=");
208             String queryString = mapJoiner.join(queryParams);
209             request_url = String.format("%s?%s", request_url, queryString);
210         }
211
212         return request_url;
213     }
214
215     @Override
216     public URL getRequestQueryUrl(String method) throws UnsupportedEncodingException, MalformedURLException, URISyntaxException {
217         return this.getRequestUrl(method, null);
218     }
219
220
221     @Override
222     public String toJSONString() {
223         ObjectMapper mapper = getObjectMapper();
224
225         String json_text = null;
226         try {
227             json_text = mapper.writeValueAsString(requestDatum);
228         } catch (JsonProcessingException exc) {
229             handleException(this, exc);
230             return null;
231         }
232         return json_text;
233     }
234
235     @Override
236     public String[] getArgsList() {
237         String[] args = {};
238         return args;
239     }
240
241     @Override
242     public Class<? extends AAIDatum> getModelClass() {
243         return model;
244     }
245
246     public void processRequestPathValues(Map<String, String> nameValues) {
247         // identify unique resources
248         Set<String> uniqueResources = AAIRequest.extractUniqueResourceSetFromKeys(nameValues.keySet());
249
250         String[] arguments = nameValues.keySet().toArray(new String[0]);
251         for(String name : arguments) {
252             String tmpName = name.replaceAll("-", "_");
253             String value = nameValues.get(tmpName);
254             if(value != null && !value.isEmpty()) {
255                 value = value.trim().replace("'", "").replace("$", "").replace("'", "");
256                 tmpName = name.replaceAll("_", "-");
257                 this.addRequestProperty(tmpName, value);
258             }
259         }
260     }
261 }