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.net.MalformedURLException;
32 import java.util.ArrayList;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Properties;
37 import org.onap.ccsdk.sli.adaptors.aai.data.AAIDatum;
38 import org.onap.aai.inventory.v14.InventoryResponseItems;
40 import com.fasterxml.jackson.annotation.JsonInclude.Include;
41 import com.fasterxml.jackson.core.JsonProcessingException;
42 import com.fasterxml.jackson.databind.AnnotationIntrospector;
43 import com.fasterxml.jackson.databind.DeserializationFeature;
44 import com.fasterxml.jackson.databind.JsonNode;
45 import com.fasterxml.jackson.databind.ObjectMapper;
46 import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
47 import com.fasterxml.jackson.databind.node.ObjectNode;
48 import com.fasterxml.jackson.databind.type.TypeFactory;
49 import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
51 public class NamedQueryRequest extends AAIRequest {
53 public static final String NAMED_SEARCH_PATH = "org.onap.ccsdk.sli.adaptors.aai.query.named";
55 private final String named_search_path;
57 public static final String NAMED_QUERY_UUID = "named-query-uuid";
58 public static final String PREFIX = "prefix";
61 public NamedQueryRequest() {
62 named_search_path = configProperties.getProperty(NAMED_SEARCH_PATH);
66 public URL getRequestUrl(String method, String resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
68 String request_url = targetUri+named_search_path;
70 request_url = processPathData(request_url, requestProperties);
72 if(resourceVersion != null) {
73 request_url = request_url +"?resource-version="+resourceVersion;
75 URL http_req_url = new URL(request_url);
77 aaiService.LOGwriteFirstTrace(method, http_req_url.toString());
83 public URL getRequestQueryUrl(String method) throws UnsupportedEncodingException, MalformedURLException {
84 return getRequestUrl(method, null);
89 public String toJSONString() {
90 ObjectMapper mapper = AAIService.getObjectMapper();
91 mapper.setSerializationInclusion(Include.NON_NULL);
92 mapper.setSerializationInclusion(Include.NON_EMPTY);
93 mapper.setSerializationInclusion(Include.NON_DEFAULT);
95 AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
96 AnnotationIntrospector secondary = new JacksonAnnotationIntrospector();
97 mapper.setAnnotationIntrospector(AnnotationIntrospector.pair(introspector, secondary));
98 mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
100 AAIDatum tenant = (AAIDatum)requestDatum;
101 String json_text = null;
103 ObjectNode node = mapper.valueToTree(tenant);
104 Iterator<JsonNode> it = node.elements();
106 JsonNode jn = it.next();
107 JsonNode child = jn.get("instance-filter");
109 child = child.get(0);
110 if(child.has("l3-network")) {
111 JsonNode innerChild = child.get("l3-network");
112 if(innerChild != null) {
113 if(innerChild instanceof ObjectNode) {
114 ObjectNode on = ObjectNode.class.cast(innerChild);
115 List<String> namesToDelete = new ArrayList<>();
116 Iterator<String> names = on.fieldNames();
117 while(names.hasNext()) {
118 String name = names.next();
119 if(name != null && name.startsWith("is-")) {
120 namesToDelete.add(name);
123 for(String nameToDelete : namesToDelete) {
124 on.remove(nameToDelete);
128 } else if(child.has("pnf")) {
129 JsonNode innerChild = child.get("pnf");
130 if(innerChild != null) {
131 if(innerChild instanceof ObjectNode) {
132 ObjectNode on = ObjectNode.class.cast(innerChild);
133 List<String> namesToDelete = new ArrayList<>();
134 Iterator<String> names = on.fieldNames();
135 while(names.hasNext()) {
136 String name = names.next();
137 if(name != null && name.startsWith("in-maint")) {
138 namesToDelete.add(name);
141 for(String nameToDelete : namesToDelete) {
142 on.remove(nameToDelete);
146 } else if(child.has("generic-vnf")) {
147 JsonNode innerChild = child.get("generic-vnf");
148 if(innerChild != null) {
149 if(innerChild instanceof ObjectNode) {
150 ObjectNode on = ObjectNode.class.cast(innerChild);
151 List<String> namesToDelete = new ArrayList<>();
152 Iterator<String> names = on.fieldNames();
153 while(names.hasNext()) {
154 String name = names.next();
155 if(name != null && name.startsWith("is-")) {
156 namesToDelete.add(name);
157 } else if(name != null && name.startsWith("in-maint")) {
158 namesToDelete.add(name);
161 for(String nameToDelete : namesToDelete) {
162 on.remove(nameToDelete);
169 json_text = node.toString();
170 if(json_text == null)
171 json_text = mapper.writeValueAsString(tenant);
172 } catch (JsonProcessingException exc) {
173 handleException(this, exc);
181 public String[] getArgsList() {
182 String[] args = {NAMED_QUERY_UUID, PREFIX};
188 public Class<? extends AAIDatum> getModelClass() {
189 return InventoryResponseItems.class;
193 public static String processPathData(String request_url, Properties requestProperties) throws UnsupportedEncodingException {
197 String key = NAMED_QUERY_UUID;
199 if(requestProperties.containsKey(key)) {
200 encoded_vnf = encodeQuery(requestProperties.getProperty(key));
201 request_url = request_url.replace("{named-query-uuid}", encoded_vnf) ;
202 aaiService.LOGwriteDateTrace(NAMED_QUERY_UUID, requestProperties.getProperty(key));
207 if(requestProperties.containsKey(key)) {
208 encoded_vnf = encodeQuery(requestProperties.getProperty(key));
209 request_url = request_url.replace("{prefix}", encoded_vnf) ;
210 aaiService.LOGwriteDateTrace(PREFIX, requestProperties.getProperty(key));