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