2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.sli.adaptors.aai;
24 import java.io.UnsupportedEncodingException;
25 import java.lang.reflect.Field;
26 import java.net.MalformedURLException;
28 import java.util.HashMap;
32 import javax.xml.bind.annotation.XmlElement;
34 import org.onap.ccsdk.sli.adaptors.aai.data.AAIDatum;
35 import org.openecomp.aai.inventory.v11.L3Network;
36 import org.openecomp.aai.inventory.v11.L3Networks;
38 import com.fasterxml.jackson.core.JsonProcessingException;
39 import com.fasterxml.jackson.databind.ObjectMapper;
40 import com.google.common.base.Joiner;
42 public class GenericRequest extends AAIRequest {
45 protected Class<? extends AAIDatum> model;
47 public GenericRequest(Class<? extends AAIDatum> clazz) {
52 public URL getRequestUrl(String method, String resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
54 String request_url = null;
55 String originalPath = null;
56 String pathSubstitute = null;
58 request_url = target_uri + getRequestPath();
60 Map<String, String> queryParams = new HashMap<String, String> ();
61 if(resourceVersion != null) {
62 queryParams.put("resource-version",resourceVersion);
65 Set<String> uniqueResources = extractUniqueResourceSetFromKeys(requestProperties.stringPropertyNames());
67 String[] keys = requestProperties.keySet().toArray(new String[0]);
68 for(String key : keys) {
69 if("cloud-region.cloud-region-id".equals(key))
71 if("entitlement.resource-uuid".equals(key))
73 if("license.resource-uuid".equals(key))
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));
102 Class<? extends AAIDatum> clazz = null;
104 clazz = getClassFromResource(splitKey[0]);
105 } catch (ClassNotFoundException exc) {
106 LOG.warn("AAIRequest does not support class: " + exc.getMessage());
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;
121 String token = String.format("%s/{%s}", splitKey[0], primaryId);
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));
127 queryParams.put(splitKey[1], encodeQuery(value));
128 originalPath = token;
129 pathSubstitute = String.format("%s", splitKey[0]);
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;
141 String token = String.format("%s/{%s}", splitKey[0], primaryId);
142 originalPath = token;
143 pathSubstitute = String.format("");
145 queryParams.put(splitKey[1], encodeQuery(value));
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));
154 aaiService.LOGwriteDateTrace(splitKey[1], value);
158 if(originalPath != null && pathSubstitute != null)
159 request_url = request_url.replace(originalPath, pathSubstitute);
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);
167 URL http_req_url = new URL(request_url);
169 aaiService.LOGwriteFirstTrace(method, http_req_url.toString());
176 public URL OriginalgetRequestUrl(String method, String resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
178 String request_url = null;
180 request_url = target_uri + getRequestPath();
182 Map<String, String> keyValuepairs = new HashMap<String, String> ();
183 Set<String> uniqueResources = extractUniqueResourceSetFromKeys(requestProperties.stringPropertyNames());
185 String[] keys = requestProperties.keySet().toArray(new String[0]);
186 for(String key : keys) {
187 if("cloud-region.cloud-region-id".equals(key))
189 if("entitlement.resource-uuid".equals(key))
191 if("license.resource-uuid".equals(key))
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));
220 Class<? extends AAIDatum> clazz = null;
222 clazz = getClassFromResource(splitKey[0]);
223 } catch (ClassNotFoundException exc) {
224 LOG.warn("AAIRequest does not support class: " + exc.getMessage());
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;
239 String token = String.format("%s/{%s}", splitKey[0], primaryId);
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));
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);
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));
257 aaiService.LOGwriteDateTrace(splitKey[1], value);
262 if(resourceVersion != null) {
263 request_url = request_url +"?resource-version="+resourceVersion;
265 URL http_req_url = new URL(request_url);
267 aaiService.LOGwriteFirstTrace(method, http_req_url.toString());
274 public URL getRequestQueryUrl(String method) throws UnsupportedEncodingException, MalformedURLException {
275 return this.getRequestUrl(method, null);
280 public String toJSONString() {
281 ObjectMapper mapper = getObjectMapper();
283 String json_text = null;
285 json_text = mapper.writeValueAsString(requestDatum);
286 } catch (JsonProcessingException exc) {
287 handleException(this, exc);
294 public String[] getArgsList() {
300 public Class<? extends AAIDatum> getModelClass() {
304 public void processRequestPathValues(Map<String, String> nameValues) {
305 // identify unique resources
306 Set<String> uniqueResources = AAIRequest.extractUniqueResourceSetFromKeys(nameValues.keySet());
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);