Expose gRPC service in OSGi service registry
[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  * 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
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
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=========================================================
22  */
23 /**
24  * @author Rich Tabedzki
25  *
26  */
27 package org.onap.ccsdk.sli.adaptors.aai;
28
29 import java.io.UnsupportedEncodingException;
30 import java.net.MalformedURLException;
31 import java.net.URL;
32 import java.util.ArrayList;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Properties;
36
37 import org.onap.ccsdk.sli.adaptors.aai.data.AAIDatum;
38 import org.onap.aai.inventory.v14.InventoryResponseItems;
39
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;
50
51 public class NamedQueryRequest extends AAIRequest {
52
53     public static final String NAMED_SEARCH_PATH            = "org.onap.ccsdk.sli.adaptors.aai.query.named";
54
55     private final String named_search_path;
56
57     public static final String NAMED_QUERY_UUID = "named-query-uuid";
58     public static final String PREFIX = "prefix";
59
60
61     public NamedQueryRequest() {
62         named_search_path = configProperties.getProperty(NAMED_SEARCH_PATH);
63     }
64
65     @Override
66     public URL getRequestUrl(String method, String resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
67
68         String request_url = targetUri+named_search_path;
69
70         request_url = processPathData(request_url, requestProperties);
71
72         if(resourceVersion != null) {
73             request_url = request_url +"?resource-version="+resourceVersion;
74         }
75         URL http_req_url =    new URL(request_url);
76
77         aaiService.LOGwriteFirstTrace(method, http_req_url.toString());
78
79         return http_req_url;
80     }
81
82     @Override
83     public URL getRequestQueryUrl(String method) throws UnsupportedEncodingException, MalformedURLException {
84         return getRequestUrl(method, null);
85     }
86
87
88     @Override
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);
94
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);
99
100         AAIDatum tenant = (AAIDatum)requestDatum;
101         String json_text = null;
102         try {
103             ObjectNode node = mapper.valueToTree(tenant);
104             Iterator<JsonNode> it = node.elements();
105             while(it.hasNext()){
106                 JsonNode jn = it.next();
107                 JsonNode child = jn.get("instance-filter");
108                 if(child != null) {
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);
121                                     }
122                                 }
123                                 for(String nameToDelete : namesToDelete) {
124                                     on.remove(nameToDelete);
125                                 }
126                             }
127                         }
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);
139                                     }
140                                 }
141                                 for(String nameToDelete : namesToDelete) {
142                                     on.remove(nameToDelete);
143                                 }
144                             }
145                         }
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);
159                                     }
160                                 }
161                                 for(String nameToDelete : namesToDelete) {
162                                     on.remove(nameToDelete);
163                                 }
164                             }
165                         }
166                     }
167                 }
168             }
169             json_text = node.toString();
170             if(json_text == null)
171             json_text = mapper.writeValueAsString(tenant);
172         } catch (JsonProcessingException exc) {
173             handleException(this, exc);
174             return null;
175         }
176         return json_text;
177     }
178
179
180     @Override
181     public String[] getArgsList() {
182         String[] args = {NAMED_QUERY_UUID, PREFIX};
183         return args;
184     }
185
186
187     @Override
188     public Class<? extends AAIDatum> getModelClass() {
189         return InventoryResponseItems.class;
190     }
191
192
193     public static String processPathData(String request_url, Properties requestProperties) throws UnsupportedEncodingException {
194
195
196         String encoded_vnf ;
197         String key = NAMED_QUERY_UUID;
198
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));
203         }
204
205         key = PREFIX;
206
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));
211         }
212
213         return request_url;
214     }
215 }