2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Modifications Copyright (C) 2018 IBM.
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 * @author Rich Tabedzki
27 package org.onap.ccsdk.sli.adaptors.aai;
29 import java.io.UnsupportedEncodingException;
30 import java.lang.annotation.Annotation;
31 import java.lang.reflect.Field;
32 import java.net.MalformedURLException;
33 import java.net.URISyntaxException;
35 import java.util.HashMap;
39 import javax.xml.bind.annotation.XmlElement;
40 import javax.xml.bind.annotation.XmlRootElement;
42 import org.onap.ccsdk.sli.adaptors.aai.data.AAIDatum;
43 import org.onap.aai.inventory.v14.L3Network;
44 import org.onap.aai.inventory.v14.L3Networks;
46 import com.fasterxml.jackson.core.JsonProcessingException;
47 import com.fasterxml.jackson.databind.ObjectMapper;
48 import com.google.common.base.Joiner;
50 public class GenericRequest extends AAIRequest {
53 protected Class<? extends AAIDatum> model;
55 public GenericRequest(Class<? extends AAIDatum> clazz) {
60 public String updatePathDataValues(Object resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
62 String originalPath = null;
63 String pathSubstitute = null;
64 String resourceName = null;
66 Annotation[] annotations = model.getAnnotations();
67 for(Annotation annotation : annotations) {
68 Class<? extends Annotation> anotationType = annotation.annotationType();
69 String annotationName = anotationType.getName();
70 if("javax.xml.bind.annotation.XmlRootElement".equals(annotationName)){
71 XmlRootElement order = (XmlRootElement)annotation;
72 resourceName = order.name();
76 String request_url = getRequestPath(resourceName);
78 Map<String, String> queryParams = new HashMap<> ();
79 if(resourceVersion != null) {
80 queryParams.put("resource-version", resourceVersion.toString());
83 Set<String> uniqueResources = extractUniqueResourceSetFromKeys(requestProperties.stringPropertyNames());
85 String[] keys = requestProperties.keySet().toArray(new String[0]);
86 for(String key : keys) {
88 case "cloud-region.cloud-region-id":
89 case "entitlement.resource-uuid":
90 case "license.resource-uuid":
91 case "route-target.route-target-role":
92 case "service-capability.vnf-type":
93 case "ctag-pool.availability-zone-name":
97 String value = requestProperties.getProperty(key);
98 if(key.contains(".")) {
99 String[] splitKey = key.split("\\.");
100 if("cloud-region".equals(splitKey[0])){
101 String cloudRegionId = requestProperties.getProperty("cloud-region.cloud-region-id");
102 aaiService.LOGwriteDateTrace("cloud-region-id", cloudRegionId);
103 String token = String.format("%s/{%s}/{cloud-region-id}", splitKey[0], splitKey[1] );
104 String encoded_owner = encodeQuery(value);
105 String encoded_region = encodeQuery(cloudRegionId);
106 request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
107 } else if("entitlement".equals(splitKey[0])){
108 String cloudRegionId = requestProperties.getProperty("entitlement.resource-uuid");
109 aaiService.LOGwriteDateTrace("resource-uuid", cloudRegionId);
110 String token = String.format("%s/{%s}/{resource-uuid}", splitKey[0], splitKey[1] );
111 String encoded_owner = encodeQuery(value);
112 String encoded_region = encodeQuery(cloudRegionId);
113 request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
114 } else if("license".equals(splitKey[0])){
115 String cloudRegionId = requestProperties.getProperty("license.resource-uuid");
116 aaiService.LOGwriteDateTrace("resource-uuid", cloudRegionId);
117 String token = String.format("%s/{%s}/{resource-uuid}", splitKey[0], splitKey[1] );
118 String encoded_owner = encodeQuery(value);
119 String encoded_region = encodeQuery(cloudRegionId);
120 request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
121 } else if("route-target".equals(splitKey[0])){
122 String cloudRegionId = requestProperties.getProperty("route-target.route-target-role");
123 aaiService.LOGwriteDateTrace("route-target-role", cloudRegionId);
124 String token = String.format("%s/{%s}/{route-target-role}", splitKey[0], splitKey[1] );
125 String encoded_owner = encodeQuery(value);
126 String encoded_region = encodeQuery(cloudRegionId);
127 request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
128 } else if("service-capability".equals(splitKey[0])){
129 String vnfType = requestProperties.getProperty("service-capability.vnf-type");
130 aaiService.LOGwriteDateTrace("vnf-type", vnfType);
131 String token = String.format("%s/{%s}/{vnf-type}", splitKey[0], splitKey[1] );
132 String encoded_service_type = encodeQuery(value);
133 String encoded_vnf_type = encodeQuery(vnfType);
134 if("service-capability".equals(resourceName)) {
135 request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_service_type, encoded_vnf_type));
136 } else if("service-capabilities".equals(resourceName)) {
137 queryParams.put("service-type", encoded_service_type);
138 queryParams.put("vnf-type", encoded_vnf_type);
140 } else if("ctag-pool".equals(splitKey[0])){
141 String cloudRegionId = requestProperties.getProperty("ctag-pool.availability-zone-name");
142 aaiService.LOGwriteDateTrace("availability-zone-name", cloudRegionId);
143 String token = String.format("%s/{%s}/{availability-zone-name}", splitKey[0], splitKey[1] );
144 String encoded_owner = encodeQuery(value);
145 String encoded_region = encodeQuery(cloudRegionId);
146 request_url = request_url.replace(token, String.format("%s/%s/%s", splitKey[0], encoded_owner, encoded_region));
148 Class<? extends AAIDatum> clazz = getClassFromResource(splitKey[0]);
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 = null;
157 if(annotation != null) {
158 primaryId = annotation.name();
159 if("##default".equals(primaryId)) {
160 primaryId = fieldName;
163 primaryId = fieldName;
166 String token = String.format("%s/{%s}", splitKey[0], primaryId);
168 if(splitKey[1].equals(primaryId)) {
169 String encoded_vnf = encodeQuery(value);
170 request_url = request_url.replace(token, String.format("%s/%s", splitKey[0], encoded_vnf));
172 queryParams.put(splitKey[1], encodeQuery(value));
173 originalPath = token;
174 pathSubstitute = String.format("%s", splitKey[0]);
176 } else if(L3Networks.class == this.model) {
177 Field[] fields = L3Network.class.getDeclaredFields();
178 Field field = fields[0];
179 String fieldName = field.getName();
180 XmlElement annotation = field.getAnnotation(XmlElement.class);
181 String primaryId = annotation.name();
182 if("##default".equals(primaryId)) {
183 primaryId = fieldName;
186 String token = String.format("%s/{%s}", splitKey[0], primaryId);
187 originalPath = token;
188 pathSubstitute = String.format("");
190 queryParams.put(splitKey[1], encodeQuery(value));
192 String token = String.format("%s/{%s}", splitKey[0], splitKey[1]);
193 String encoded_vnf = encodeQuery(value);
194 request_url = request_url.replace(token, String.format("%s/%s", splitKey[0], encoded_vnf));
199 aaiService.LOGwriteDateTrace(splitKey[1], value);
203 if(originalPath != null && pathSubstitute != null)
204 request_url = request_url.replace(originalPath, pathSubstitute);
206 if(!queryParams.isEmpty()) {
207 Joiner.MapJoiner mapJoiner = Joiner.on("&").withKeyValueSeparator("=");
208 String queryString = mapJoiner.join(queryParams);
209 request_url = String.format("%s?%s", request_url, queryString);
216 public URL getRequestQueryUrl(String method) throws UnsupportedEncodingException, MalformedURLException, URISyntaxException {
217 return this.getRequestUrl(method, null);
222 public String toJSONString() {
223 ObjectMapper mapper = getObjectMapper();
225 String json_text = null;
227 json_text = mapper.writeValueAsString(requestDatum);
228 } catch (JsonProcessingException exc) {
229 handleException(this, exc);
236 public String[] getArgsList() {
242 public Class<? extends AAIDatum> getModelClass() {
246 public void processRequestPathValues(Map<String, String> nameValues) {
247 // identify unique resources
248 Set<String> uniqueResources = AAIRequest.extractUniqueResourceSetFromKeys(nameValues.keySet());
250 String[] arguments = nameValues.keySet().toArray(new String[0]);
251 for(String name : arguments) {
252 String tmpName = name.replaceAll("-", "_");
253 String value = nameValues.get(tmpName);
254 if(value != null && !value.isEmpty()) {
255 value = value.trim().replace("'", "").replace("$", "").replace("'", "");
256 tmpName = name.replaceAll("_", "-");
257 this.addRequestProperty(tmpName, value);