37410e00dd86bba8daff3c3e084d9731a8ab102f
[ccsdk/sli/adaptors.git] / aai-service / provider / src / main / java / org / onap / ccsdk / sli / adaptors / aai / NamedQueryRequest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                      reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.onap.ccsdk.sli.adaptors.aai;
23
24 import java.io.UnsupportedEncodingException;
25 import java.net.MalformedURLException;
26 import java.net.URL;
27 import java.util.ArrayList;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Properties;
31
32 import org.onap.ccsdk.sli.adaptors.aai.data.AAIDatum;
33 import org.openecomp.aai.inventory.v11.InventoryResponseItems;
34
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;
45
46 public class NamedQueryRequest extends AAIRequest {
47
48         public static final String NAMED_SEARCH_PATH                    = "org.onap.ccsdk.sli.adaptors.aai.query.named";
49
50         private final String named_search_path;
51
52         public static final String NAMED_QUERY_UUID = "named-query-uuid";
53         public static final String PREFIX = "prefix";
54
55
56         public NamedQueryRequest() {
57                 named_search_path = configProperties.getProperty(NAMED_SEARCH_PATH);
58         }
59
60         @Override
61         public URL getRequestUrl(String method, String resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
62
63                 String request_url = target_uri+named_search_path;
64
65                 request_url = processPathData(request_url, requestProperties);
66
67                 if(resourceVersion != null) {
68                         request_url = request_url +"?resource-version="+resourceVersion;
69                 }
70                 URL http_req_url =      new URL(request_url);
71
72                 aaiService.LOGwriteFirstTrace(method, http_req_url.toString());
73
74                 return http_req_url;
75         }
76
77         @Override
78         public URL getRequestQueryUrl(String method) throws UnsupportedEncodingException, MalformedURLException {
79                 return getRequestUrl(method, null);
80         }
81
82
83         @Override
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);
89
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);
94
95         AAIDatum tenant = (AAIDatum)requestDatum;
96                 String json_text = null;
97                 try {
98                         ObjectNode node = mapper.valueToTree(tenant);
99                         Iterator<JsonNode> it = node.elements();
100                         while(it.hasNext()){
101                                 JsonNode jn = it.next();
102                                 JsonNode child = jn.get("instance-filter");
103                                 if(child != null) {
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);
116                                                                         }
117                                                                 }
118                                                                 for(String nameToDelete : namesToDelete) {
119                                                                         on.remove(nameToDelete);
120                                                                 }
121                                                         }
122                                                 }
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);
134                                                                         }
135                                                                 }
136                                                                 for(String nameToDelete : namesToDelete) {
137                                                                         on.remove(nameToDelete);
138                                                                 }
139                                                         }
140                                                 }
141                                         }
142                                 }
143                         }
144                         json_text = node.toString();
145                         if(json_text == null)
146                         json_text = mapper.writeValueAsString(tenant);
147                 } catch (JsonProcessingException exc) {
148                         handleException(this, exc);
149                         return null;
150                 }
151                 return json_text;
152         }
153
154
155         @Override
156         public String[] getArgsList() {
157                 String[] args = {NAMED_QUERY_UUID, PREFIX};
158                 return args;
159         }
160
161
162         @Override
163         public Class<? extends AAIDatum> getModelClass() {
164                 return InventoryResponseItems.class;
165         }
166
167
168         public static String processPathData(String request_url, Properties requestProperties) throws UnsupportedEncodingException {
169
170
171                 String encoded_vnf ;
172                 String key = NAMED_QUERY_UUID;
173
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));
178                 }
179
180                 key = PREFIX;
181
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));
186                 }
187
188                 return request_url;
189         }
190 }