Merge "Updating aai adapter to v14 model"
[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.onap.aai.inventory.v14.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 = targetUri+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                     } else     if(child.has("generic-vnf")) {
142                         JsonNode innerChild =  child.get("generic-vnf");
143                         if(innerChild != null) {
144                             if(innerChild instanceof ObjectNode) {
145                                 ObjectNode on = ObjectNode.class.cast(innerChild);
146                                 List<String> namesToDelete = new ArrayList<String>();
147                                 Iterator<String> names = on.fieldNames();
148                                 while(names.hasNext()) {
149                                     String name = names.next();
150                                     if(name != null && name.startsWith("is-")) {
151                                         namesToDelete.add(name);
152                                     } else if(name != null && name.startsWith("in-maint")) {
153                                         namesToDelete.add(name);
154                                     }
155                                 }
156                                 for(String nameToDelete : namesToDelete) {
157                                     on.remove(nameToDelete);
158                                 }
159                             }
160                         }
161                     }
162                 }
163             }
164             json_text = node.toString();
165             if(json_text == null)
166             json_text = mapper.writeValueAsString(tenant);
167         } catch (JsonProcessingException exc) {
168             handleException(this, exc);
169             return null;
170         }
171         return json_text;
172     }
173
174
175     @Override
176     public String[] getArgsList() {
177         String[] args = {NAMED_QUERY_UUID, PREFIX};
178         return args;
179     }
180
181
182     @Override
183     public Class<? extends AAIDatum> getModelClass() {
184         return InventoryResponseItems.class;
185     }
186
187
188     public static String processPathData(String request_url, Properties requestProperties) throws UnsupportedEncodingException {
189
190
191         String encoded_vnf ;
192         String key = NAMED_QUERY_UUID;
193
194         if(requestProperties.containsKey(key)) {
195             encoded_vnf = encodeQuery(requestProperties.getProperty(key));
196             request_url = request_url.replace("{named-query-uuid}", encoded_vnf) ;
197             aaiService.LOGwriteDateTrace("named-query-uuid", requestProperties.getProperty(key));
198         }
199
200         key = PREFIX;
201
202         if(requestProperties.containsKey(key)) {
203             encoded_vnf = encodeQuery(requestProperties.getProperty(key));
204             request_url = request_url.replace("{prefix}", encoded_vnf) ;
205             aaiService.LOGwriteDateTrace("prefix", requestProperties.getProperty(key));
206         }
207
208         return request_url;
209     }
210 }