27e4567ae57a43398015069c81202698d129f9c5
[ccsdk/sli/adaptors.git] / aai-service / provider / src / main / java / org / onap / ccsdk / sli / adaptors / aai / NodesQueryRequest.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.Properties;
28
29 import org.onap.ccsdk.sli.adaptors.aai.data.AAIDatum;
30 import org.openecomp.aai.inventory.v13.SearchResults;
31
32 import com.fasterxml.jackson.core.JsonProcessingException;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34
35 public class NodesQueryRequest extends AAIRequest {
36
37     public static final String NODES_SEARCH_PATH            = "org.onap.ccsdk.sli.adaptors.aai.query.nodes";
38
39     private final String nodes_search_path;
40
41     public static final String NODE_TYPE = "node-type";
42     public static final String ENTITY_IDENTIFIER = "entity-identifier";
43     public static final String ENTITY_VALUE = "entity-value";
44
45
46     public NodesQueryRequest() {
47         nodes_search_path = configProperties.getProperty(NODES_SEARCH_PATH);
48     }
49
50
51 //    @Override
52 //    public URL getRequestUrl(String method, String resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
53 //
54 //        String request_url = targetUri+generic_search_path;
55 //        String key = START_NODE_TYPE;
56 //
57 //        String encoded_vnf = encodeQuery(requestProperties.getProperty(key));
58 //        request_url = request_url.replace("{vnf-id}", encoded_vnf) ;
59 //
60 //        if(resourceVersion != null) {
61 //            request_url = request_url +"?resource-version="+resourceVersion;
62 //        }
63 //        URL http_req_url =    new URL(request_url);
64 //
65 //        aaiService.LOGwriteFirstTrace(method, http_req_url.toString());
66 //
67 //
68 //        return http_req_url;
69 //    }
70
71     @Override
72     public URL getRequestUrl(String method, String resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
73
74         String request_url = targetUri+nodes_search_path;
75
76         request_url = processPathData(request_url, requestProperties);
77
78         if(resourceVersion != null) {
79             request_url = request_url +"?resource-version="+resourceVersion;
80         }
81         URL http_req_url =    new URL(request_url);
82
83         aaiService.LOGwriteFirstTrace(method, http_req_url.toString());
84
85         return http_req_url;
86     }
87
88     @Override
89     public URL getRequestQueryUrl(String method) throws UnsupportedEncodingException, MalformedURLException {
90         return getRequestUrl(method, null);
91     }
92
93
94     @Override
95     public String toJSONString() {
96         ObjectMapper mapper = getObjectMapper();
97         SearchResults tenant = (SearchResults)requestDatum;
98         String json_text = null;
99         try {
100             json_text = mapper.writeValueAsString(tenant);
101         } catch (JsonProcessingException exc) {
102             handleException(this, exc);
103             return null;
104         }
105         return json_text;
106     }
107
108
109     @Override
110     public String[] getArgsList() {
111         String[] args = {NODE_TYPE, ENTITY_IDENTIFIER, ENTITY_VALUE};
112         return args;
113     }
114
115
116     @Override
117     public Class<? extends AAIDatum> getModelClass() {
118         return SearchResults.class;
119     }
120
121
122     public static String processPathData(String request_url, Properties requestProperties) throws UnsupportedEncodingException {
123
124         String key = ENTITY_IDENTIFIER;
125
126         String encoded_vnf = encodeQuery(requestProperties.getProperty(key));
127         request_url = request_url.replace("{entity-identifier}", encoded_vnf) ;
128         aaiService.LOGwriteDateTrace("entity-identifier", requestProperties.getProperty(key));
129
130         key = ENTITY_VALUE;
131
132         encoded_vnf = encodeQuery(requestProperties.getProperty(key));
133         request_url = request_url.replace("{entity-name}", encoded_vnf) ;
134         aaiService.LOGwriteDateTrace("entity-name", requestProperties.getProperty(key));
135
136         key = NODE_TYPE;
137
138         encoded_vnf = encodeQuery(requestProperties.getProperty(key));
139         request_url = request_url.replace("{node-type}", encoded_vnf) ;
140         aaiService.LOGwriteDateTrace("node-type", requestProperties.getProperty(key));
141
142         return request_url;
143     }
144 }