Fixing trustStore issues in AAI Client
[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.URISyntaxException;
29 import java.net.URL;
30 import java.util.HashMap;
31 import java.util.Map;
32 import java.util.Set;
33
34 import javax.xml.bind.annotation.XmlElement;
35 import javax.xml.bind.annotation.XmlRootElement;
36
37 import org.onap.ccsdk.sli.adaptors.aai.data.AAIDatum;
38 import org.openecomp.aai.inventory.v13.L3Network;
39 import org.openecomp.aai.inventory.v13.L3Networks;
40
41 import com.fasterxml.jackson.core.JsonProcessingException;
42 import com.fasterxml.jackson.databind.ObjectMapper;
43 import com.google.common.base.Joiner;
44
45 public class GenericRequest extends AAIRequest {
46
47
48     protected Class<? extends AAIDatum> model;
49
50     public GenericRequest(Class<? extends AAIDatum> clazz) {
51         model = clazz;
52     }
53
54     @Override
55     public String updatePathDataValues(Object resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
56
57         String originalPath = null;
58         String pathSubstitute = null;
59         String resourceName = null;
60
61         Annotation[] annotations = model.getAnnotations();
62         for(Annotation annotation : annotations) {
63             Class<? extends Annotation> anotationType = annotation.annotationType();
64             String annotationName = anotationType.getName();
65             if("javax.xml.bind.annotation.XmlRootElement".equals(annotationName)){
66                 XmlRootElement order = (XmlRootElement)annotation;
67                 resourceName = order.name();
68             }
69         }
70
71         String request_url = getRequestPath(resourceName);
72
73         Map<String, String> queryParams = new HashMap<String, String> ();
74         if(resourceVersion != null) {
75             queryParams.put("resource-version", resourceVersion.toString());
76         }
77
78         Set<String> uniqueResources = extractUniqueResourceSetFromKeys(requestProperties.stringPropertyNames());
79
80         String[] keys = requestProperties.keySet().toArray(new String[0]);
81         for(String key : keys) {
82             switch(key) {
83             case "cloud-region.cloud-region-id":
84             case "entitlement.resource-uuid":
85             case "license.resource-uuid":
86             case "route-target.route-target-role":
87             case "service-capability.vnf-type":
88             case "ctag-pool.availability-zone-name":
89                 continue;
90             }
91
92             String value = requestProperties.getProperty(key);
93             if(key.contains(".")) {
94                 String[] splitKey = key.split("\\.");
95                 if("cloud-region".equals(splitKey[0])){
96                     String cloudRegionId =  requestProperties.getProperty("cloud-region.cloud-region-id");
97                     aaiService.LOGwriteDateTrace("cloud-region-id", cloudRegionId);
98                     String token = String.format("%s/{%s}/{cloud-region-id}", splitKey[0], splitKey[1] );
99                     String encoded_owner = encodeQuery(value);
100                     String encoded_region = encodeQuery(cloudRegionId);
101                     request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
102                 } else     if("entitlement".equals(splitKey[0])){
103                     String cloudRegionId =  requestProperties.getProperty("entitlement.resource-uuid");
104                     aaiService.LOGwriteDateTrace("resource-uuid", cloudRegionId);
105                     String token = String.format("%s/{%s}/{resource-uuid}", splitKey[0], splitKey[1] );
106                     String encoded_owner = encodeQuery(value);
107                     String encoded_region = encodeQuery(cloudRegionId);
108                     request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
109                 } else     if("license".equals(splitKey[0])){
110                     String cloudRegionId =  requestProperties.getProperty("license.resource-uuid");
111                     aaiService.LOGwriteDateTrace("resource-uuid", cloudRegionId);
112                     String token = String.format("%s/{%s}/{resource-uuid}", splitKey[0], splitKey[1] );
113                     String encoded_owner = encodeQuery(value);
114                     String encoded_region = encodeQuery(cloudRegionId);
115                     request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
116                 } else     if("route-target".equals(splitKey[0])){
117                     String cloudRegionId =  requestProperties.getProperty("route-target.route-target-role");
118                     aaiService.LOGwriteDateTrace("route-target-role", cloudRegionId);
119                     String token = String.format("%s/{%s}/{route-target-role}", splitKey[0], splitKey[1] );
120                     String encoded_owner = encodeQuery(value);
121                     String encoded_region = encodeQuery(cloudRegionId);
122                     request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
123                 } else     if("service-capability".equals(splitKey[0])){
124                     String vnfType =  requestProperties.getProperty("service-capability.vnf-type");
125                     aaiService.LOGwriteDateTrace("vnf-type", vnfType);
126                     String token = String.format("%s/{%s}/{vnf-type}", splitKey[0], splitKey[1] );
127                     String encoded_service_type = encodeQuery(value);
128                     String encoded_vnf_type = encodeQuery(vnfType);
129                     if("service-capability".equals(resourceName)) {
130                         request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_service_type, encoded_vnf_type));
131                     } else if("service-capabilities".equals(resourceName)) {
132                         queryParams.put("service-type", encoded_service_type);
133                         queryParams.put("vnf-type", encoded_vnf_type);
134                     }
135                 } else     if("ctag-pool".equals(splitKey[0])){
136                     String cloudRegionId =  requestProperties.getProperty("ctag-pool.availability-zone-name");
137                     aaiService.LOGwriteDateTrace("availability-zone-name", cloudRegionId);
138                     String token = String.format("%s/{%s}/{availability-zone-name}", splitKey[0], splitKey[1] );
139                     String encoded_owner = encodeQuery(value);
140                     String encoded_region = encodeQuery(cloudRegionId);
141                     request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
142                 } else {
143                     Class<? extends AAIDatum> clazz = getClassFromResource(splitKey[0]);
144
145                     if(clazz != null) {
146                         if(clazz == this.model) {
147                             Field[] fields = this.model.getDeclaredFields();
148                             Field field = fields[0];
149                             String fieldName = field.getName();
150                             XmlElement annotation = field.getAnnotation(XmlElement.class);
151                             String primaryId = null;
152                             if(annotation != null) {
153                                 primaryId = annotation.name();
154                             if("##default".equals(primaryId)) {
155                                 primaryId = fieldName;
156                             }
157                             } else {
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, URISyntaxException {
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 }