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.annotation.Annotation;
26 import java.lang.reflect.Field;
27 import java.net.MalformedURLException;
29 import java.util.HashMap;
33 import javax.xml.bind.annotation.XmlElement;
34 import javax.xml.bind.annotation.XmlRootElement;
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;
40 import com.fasterxml.jackson.core.JsonProcessingException;
41 import com.fasterxml.jackson.databind.ObjectMapper;
42 import com.google.common.base.Joiner;
44 public class GenericRequest extends AAIRequest {
47 protected Class<? extends AAIDatum> model;
49 public GenericRequest(Class<? extends AAIDatum> clazz) {
54 public String updatePathDataValues(Object resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
56 String originalPath = null;
57 String pathSubstitute = null;
58 String resourceName = null;
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();
70 String request_url = getRequestPath(resourceName);
72 Map<String, String> queryParams = new HashMap<String, String> ();
73 if(resourceVersion != null) {
74 queryParams.put("resource-version", resourceVersion.toString());
77 Set<String> uniqueResources = extractUniqueResourceSetFromKeys(requestProperties.stringPropertyNames());
79 String[] keys = requestProperties.keySet().toArray(new String[0]);
80 for(String key : keys) {
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":
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);
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));
142 Class<? extends AAIDatum> clazz = null;
144 clazz = getClassFromResource(splitKey[0]);
145 } catch (ClassNotFoundException exc) {
146 LOG.warn("AAIRequest does not support class: " + exc.getMessage());
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;
161 String token = String.format("%s/{%s}", splitKey[0], primaryId);
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));
167 queryParams.put(splitKey[1], encodeQuery(value));
168 originalPath = token;
169 pathSubstitute = String.format("%s", splitKey[0]);
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;
181 String token = String.format("%s/{%s}", splitKey[0], primaryId);
182 originalPath = token;
183 pathSubstitute = String.format("");
185 queryParams.put(splitKey[1], encodeQuery(value));
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));
194 aaiService.LOGwriteDateTrace(splitKey[1], value);
198 if(originalPath != null && pathSubstitute != null)
199 request_url = request_url.replace(originalPath, pathSubstitute);
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);
211 public URL getRequestQueryUrl(String method) throws UnsupportedEncodingException, MalformedURLException {
212 return this.getRequestUrl(method, null);
217 public String toJSONString() {
218 ObjectMapper mapper = getObjectMapper();
220 String json_text = null;
222 json_text = mapper.writeValueAsString(requestDatum);
223 } catch (JsonProcessingException exc) {
224 handleException(this, exc);
231 public String[] getArgsList() {
237 public Class<? extends AAIDatum> getModelClass() {
241 public void processRequestPathValues(Map<String, String> nameValues) {
242 // identify unique resources
243 Set<String> uniqueResources = AAIRequest.extractUniqueResourceSetFromKeys(nameValues.keySet());
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);