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.lang.reflect.Method;
28 import java.net.MalformedURLException;
29 import java.net.URISyntaxException;
31 import java.util.HashMap;
35 import javax.xml.bind.annotation.XmlElement;
36 import javax.xml.bind.annotation.XmlRootElement;
37 import javax.xml.bind.annotation.XmlType;
38 import org.onap.ccsdk.sli.adaptors.aai.data.AAIDatum;
39 import org.openecomp.aai.inventory.v11.L3Network;
40 import org.openecomp.aai.inventory.v11.L3Networks;
42 import com.fasterxml.jackson.core.JsonProcessingException;
43 import com.fasterxml.jackson.databind.ObjectMapper;
44 import com.google.common.base.Joiner;
46 public class GenericRequest extends AAIRequest {
49 protected Class<? extends AAIDatum> model;
51 public GenericRequest(Class<? extends AAIDatum> clazz) {
56 public String updatePathDataValues(Object resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
58 String originalPath = null;
59 String pathSubstitute = null;
60 String resourceName = null;
62 Annotation[] annotations = model.getAnnotations();
63 for(Annotation annotation : annotations) {
64 Class<? extends Annotation> anotationType = annotation.annotationType();
65 String annotationName = anotationType.getName();
66 if("javax.xml.bind.annotation.XmlRootElement".equals(annotationName)){
67 XmlRootElement order = (XmlRootElement)annotation;
68 resourceName = order.name();
72 String request_url = getRequestPath(resourceName);
74 Map<String, String> queryParams = new HashMap<String, String> ();
75 if(resourceVersion != null) {
76 queryParams.put("resource-version", resourceVersion.toString());
79 Set<String> uniqueResources = extractUniqueResourceSetFromKeys(requestProperties.stringPropertyNames());
81 String[] keys = requestProperties.keySet().toArray(new String[0]);
82 for(String key : keys) {
84 case "cloud-region.cloud-region-id":
85 case "entitlement.resource-uuid":
86 case "license.resource-uuid":
87 case "route-target.route-target-role":
88 case "service-capability.vnf-type":
89 case "ctag-pool.availability-zone-name":
93 String value = requestProperties.getProperty(key);
94 if(key.contains(".")) {
95 String[] splitKey = key.split("\\.");
96 if("cloud-region".equals(splitKey[0])){
97 String cloudRegionId = requestProperties.getProperty("cloud-region.cloud-region-id");
98 aaiService.LOGwriteDateTrace("cloud-region-id", cloudRegionId);
99 String token = String.format("%s/{%s}/{cloud-region-id}", splitKey[0], splitKey[1] );
100 String encoded_owner = encodeQuery(value);
101 String encoded_region = encodeQuery(cloudRegionId);
102 request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
103 } else if("entitlement".equals(splitKey[0])){
104 String cloudRegionId = requestProperties.getProperty("entitlement.resource-uuid");
105 aaiService.LOGwriteDateTrace("resource-uuid", cloudRegionId);
106 String token = String.format("%s/{%s}/{resource-uuid}", splitKey[0], splitKey[1] );
107 String encoded_owner = encodeQuery(value);
108 String encoded_region = encodeQuery(cloudRegionId);
109 request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
110 } else if("license".equals(splitKey[0])){
111 String cloudRegionId = requestProperties.getProperty("license.resource-uuid");
112 aaiService.LOGwriteDateTrace("resource-uuid", cloudRegionId);
113 String token = String.format("%s/{%s}/{resource-uuid}", splitKey[0], splitKey[1] );
114 String encoded_owner = encodeQuery(value);
115 String encoded_region = encodeQuery(cloudRegionId);
116 request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
117 } else if("route-target".equals(splitKey[0])){
118 String cloudRegionId = requestProperties.getProperty("route-target.route-target-role");
119 aaiService.LOGwriteDateTrace("route-target-role", cloudRegionId);
120 String token = String.format("%s/{%s}/{route-target-role}", splitKey[0], splitKey[1] );
121 String encoded_owner = encodeQuery(value);
122 String encoded_region = encodeQuery(cloudRegionId);
123 request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
124 } else if("service-capability".equals(splitKey[0])){
125 String vnfType = requestProperties.getProperty("service-capability.vnf-type");
126 aaiService.LOGwriteDateTrace("vnf-type", vnfType);
127 String token = String.format("%s/{%s}/{vnf-type}", splitKey[0], splitKey[1] );
128 String encoded_service_type = encodeQuery(value);
129 String encoded_vnf_type = encodeQuery(vnfType);
130 if("service-capability".equals(resourceName)) {
131 request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_service_type, encoded_vnf_type));
132 } else if("service-capabilities".equals(resourceName)) {
133 queryParams.put("service-type", encoded_service_type);
134 queryParams.put("vnf-type", encoded_vnf_type);
136 } else if("ctag-pool".equals(splitKey[0])){
137 String cloudRegionId = requestProperties.getProperty("ctag-pool.availability-zone-name");
138 aaiService.LOGwriteDateTrace("availability-zone-name", cloudRegionId);
139 String token = String.format("%s/{%s}/{availability-zone-name}", splitKey[0], splitKey[1] );
140 String encoded_owner = encodeQuery(value);
141 String encoded_region = encodeQuery(cloudRegionId);
142 request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
144 Class<? extends AAIDatum> clazz = null;
146 clazz = getClassFromResource(splitKey[0]);
147 } catch (ClassNotFoundException exc) {
148 LOG.warn("AAIRequest does not support class: " + exc.getMessage());
153 if(clazz == this.model) {
154 Field[] fields = this.model.getDeclaredFields();
155 Field field = fields[0];
156 String fieldName = field.getName();
157 XmlElement annotation = field.getAnnotation(XmlElement.class);
158 String primaryId = null;
159 if(annotation != null) {
160 primaryId = annotation.name();
161 if("##default".equals(primaryId)) {
162 primaryId = fieldName;
165 primaryId = fieldName;
168 String token = String.format("%s/{%s}", splitKey[0], primaryId);
170 if(splitKey[1].equals(primaryId)) {
171 String encoded_vnf = encodeQuery(value);
172 request_url = request_url.replace(token, String.format("%s/%s", splitKey[0], encoded_vnf));
174 queryParams.put(splitKey[1], encodeQuery(value));
175 originalPath = token;
176 pathSubstitute = String.format("%s", splitKey[0]);
178 } else if(L3Networks.class == this.model) {
179 Field[] fields = L3Network.class.getDeclaredFields();
180 Field field = fields[0];
181 String fieldName = field.getName();
182 XmlElement annotation = field.getAnnotation(XmlElement.class);
183 String primaryId = annotation.name();
184 if("##default".equals(primaryId)) {
185 primaryId = fieldName;
188 String token = String.format("%s/{%s}", splitKey[0], primaryId);
189 originalPath = token;
190 pathSubstitute = String.format("");
192 queryParams.put(splitKey[1], encodeQuery(value));
194 String token = String.format("%s/{%s}", splitKey[0], splitKey[1]);
195 String encoded_vnf = encodeQuery(value);
196 request_url = request_url.replace(token, String.format("%s/%s", splitKey[0], encoded_vnf));
201 aaiService.LOGwriteDateTrace(splitKey[1], value);
205 if(originalPath != null && pathSubstitute != null)
206 request_url = request_url.replace(originalPath, pathSubstitute);
208 if(!queryParams.isEmpty()) {
209 Joiner.MapJoiner mapJoiner = Joiner.on("&").withKeyValueSeparator("=");
210 String queryString = mapJoiner.join(queryParams);
211 request_url = String.format("%s?%s", request_url, queryString);
218 public URL getRequestQueryUrl(String method) throws UnsupportedEncodingException, MalformedURLException, URISyntaxException {
219 return this.getRequestUrl(method, null);
224 public String toJSONString() {
225 ObjectMapper mapper = getObjectMapper();
227 String json_text = null;
229 json_text = mapper.writeValueAsString(requestDatum);
230 } catch (JsonProcessingException exc) {
231 handleException(this, exc);
238 public String[] getArgsList() {
244 public Class<? extends AAIDatum> getModelClass() {
248 public void processRequestPathValues(Map<String, String> nameValues) {
249 // identify unique resources
250 Set<String> uniqueResources = AAIRequest.extractUniqueResourceSetFromKeys(nameValues.keySet());
252 String[] arguments = nameValues.keySet().toArray(new String[0]);
253 for(String name : arguments) {
254 String tmpName = name.replaceAll("-", "_");
255 String value = nameValues.get(tmpName);
256 if(value != null && !value.isEmpty()) {
257 value = value.trim().replace("'", "").replace("$", "").replace("'", "");
258 tmpName = name.replaceAll("_", "-");
259 this.addRequestProperty(tmpName, value);