2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 ONAP 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.net.MalformedURLException;
27 import java.util.ArrayList;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Properties;
32 import org.onap.ccsdk.sli.adaptors.aai.data.AAIDatum;
33 import org.openecomp.aai.inventory.v11.InventoryResponseItems;
35 import com.fasterxml.jackson.annotation.JsonInclude.Include;
36 import com.fasterxml.jackson.core.JsonProcessingException;
37 import com.fasterxml.jackson.databind.AnnotationIntrospector;
38 import com.fasterxml.jackson.databind.DeserializationFeature;
39 import com.fasterxml.jackson.databind.JsonNode;
40 import com.fasterxml.jackson.databind.ObjectMapper;
41 import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
42 import com.fasterxml.jackson.databind.node.ObjectNode;
43 import com.fasterxml.jackson.databind.type.TypeFactory;
44 import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
46 public class NamedQueryRequest extends AAIRequest {
48 public static final String NAMED_SEARCH_PATH = "org.onap.ccsdk.sli.adaptors.aai.query.named";
50 private final String named_search_path;
52 public static final String NAMED_QUERY_UUID = "named-query-uuid";
53 public static final String PREFIX = "prefix";
56 public NamedQueryRequest() {
57 named_search_path = configProperties.getProperty(NAMED_SEARCH_PATH);
61 public URL getRequestUrl(String method, String resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
63 String request_url = target_uri+named_search_path;
65 request_url = processPathData(request_url, requestProperties);
67 if(resourceVersion != null) {
68 request_url = request_url +"?resource-version="+resourceVersion;
70 URL http_req_url = new URL(request_url);
72 aaiService.LOGwriteFirstTrace(method, http_req_url.toString());
78 public URL getRequestQueryUrl(String method) throws UnsupportedEncodingException, MalformedURLException {
79 return getRequestUrl(method, null);
84 public String toJSONString() {
85 ObjectMapper mapper = AAIService.getObjectMapper();
86 mapper.setSerializationInclusion(Include.NON_NULL);
87 mapper.setSerializationInclusion(Include.NON_EMPTY);
88 mapper.setSerializationInclusion(Include.NON_DEFAULT);
90 AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
91 AnnotationIntrospector secondary = new JacksonAnnotationIntrospector();
92 mapper.setAnnotationIntrospector(AnnotationIntrospector.pair(introspector, secondary));
93 mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
95 AAIDatum tenant = (AAIDatum)requestDatum;
96 String json_text = null;
98 ObjectNode node = mapper.valueToTree(tenant);
99 Iterator<JsonNode> it = node.elements();
101 JsonNode jn = it.next();
102 JsonNode child = jn.get("instance-filter");
104 child = child.get(0);
105 if(child.has("l3-network")) {
106 JsonNode innerChild = child.get("l3-network");
107 if(innerChild != null) {
108 if(innerChild instanceof ObjectNode) {
109 ObjectNode on = ObjectNode.class.cast(innerChild);
110 List<String> namesToDelete = new ArrayList<String>();
111 Iterator<String> names = on.fieldNames();
112 while(names.hasNext()) {
113 String name = names.next();
114 if(name != null && name.startsWith("is-")) {
115 namesToDelete.add(name);
118 for(String nameToDelete : namesToDelete) {
119 on.remove(nameToDelete);
123 } else if(child.has("pnf")) {
124 JsonNode innerChild = child.get("pnf");
125 if(innerChild != null) {
126 if(innerChild instanceof ObjectNode) {
127 ObjectNode on = ObjectNode.class.cast(innerChild);
128 List<String> namesToDelete = new ArrayList<String>();
129 Iterator<String> names = on.fieldNames();
130 while(names.hasNext()) {
131 String name = names.next();
132 if(name != null && name.startsWith("in-maint")) {
133 namesToDelete.add(name);
136 for(String nameToDelete : namesToDelete) {
137 on.remove(nameToDelete);
144 json_text = node.toString();
145 if(json_text == null)
146 json_text = mapper.writeValueAsString(tenant);
147 } catch (JsonProcessingException exc) {
148 handleException(this, exc);
156 public String[] getArgsList() {
157 String[] args = {NAMED_QUERY_UUID, PREFIX};
163 public Class<? extends AAIDatum> getModelClass() {
164 return InventoryResponseItems.class;
168 public static String processPathData(String request_url, Properties requestProperties) throws UnsupportedEncodingException {
172 String key = NAMED_QUERY_UUID;
174 if(requestProperties.containsKey(key)) {
175 encoded_vnf = encodeQuery(requestProperties.getProperty(key));
176 request_url = request_url.replace("{named-query-uuid}", encoded_vnf) ;
177 aaiService.LOGwriteDateTrace("named-query-uuid", requestProperties.getProperty(key));
182 if(requestProperties.containsKey(key)) {
183 encoded_vnf = encodeQuery(requestProperties.getProperty(key));
184 request_url = request_url.replace("{prefix}", encoded_vnf) ;
185 aaiService.LOGwriteDateTrace("prefix", requestProperties.getProperty(key));