Merge "fixed sonar issue in GenericRequest.java"
[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  * 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 package org.onap.ccsdk.sli.adaptors.aai;
25
26 import java.io.UnsupportedEncodingException;
27 import java.net.MalformedURLException;
28 import java.net.URL;
29 import java.util.Properties;
30
31 import org.onap.ccsdk.sli.adaptors.aai.data.AAIDatum;
32 import org.onap.aai.inventory.v14.SearchResults;
33
34 import com.fasterxml.jackson.core.JsonProcessingException;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36
37 public class NodesQueryRequest extends AAIRequest {
38
39     public static final String NODES_SEARCH_PATH            = "org.onap.ccsdk.sli.adaptors.aai.query.nodes";
40
41     private final String nodes_search_path;
42
43     public static final String NODE_TYPE = "node-type";
44     public static final String ENTITY_IDENTIFIER = "entity-identifier";
45     public static final String ENTITY_VALUE = "entity-value";
46
47
48     public NodesQueryRequest() {
49         nodes_search_path = configProperties.getProperty(NODES_SEARCH_PATH);
50     }
51
52
53     @Override
54     public URL getRequestUrl(String method, String resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
55
56         String request_url = targetUri+nodes_search_path;
57
58         request_url = processPathData(request_url, requestProperties);
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         return http_req_url;
68     }
69
70     @Override
71     public URL getRequestQueryUrl(String method) throws UnsupportedEncodingException, MalformedURLException {
72         return getRequestUrl(method, null);
73     }
74
75
76     @Override
77     public String toJSONString() {
78         ObjectMapper mapper = getObjectMapper();
79         SearchResults tenant = (SearchResults)requestDatum;
80         String json_text = null;
81         try {
82             json_text = mapper.writeValueAsString(tenant);
83         } catch (JsonProcessingException exc) {
84             handleException(this, exc);
85             return null;
86         }
87         return json_text;
88     }
89
90
91     @Override
92     public String[] getArgsList() {
93         String[] args = {NODE_TYPE, ENTITY_IDENTIFIER, ENTITY_VALUE};
94         return args;
95     }
96
97
98     @Override
99     public Class<? extends AAIDatum> getModelClass() {
100         return SearchResults.class;
101     }
102
103
104     public static String processPathData(String request_url, Properties requestProperties) throws UnsupportedEncodingException {
105
106         String key = ENTITY_IDENTIFIER;
107
108         String encoded_vnf = encodeQuery(requestProperties.getProperty(key));
109         request_url = request_url.replace("{entity-identifier}", encoded_vnf) ;
110         aaiService.LOGwriteDateTrace(ENTITY_IDENTIFIER, requestProperties.getProperty(key));
111
112         key = ENTITY_VALUE;
113
114         encoded_vnf = encodeQuery(requestProperties.getProperty(key));
115         request_url = request_url.replace("{entity-name}", encoded_vnf) ;
116         aaiService.LOGwriteDateTrace("entity-name", requestProperties.getProperty(key));
117
118         key = NODE_TYPE;
119
120         encoded_vnf = encodeQuery(requestProperties.getProperty(key));
121         request_url = request_url.replace("{node-type}", encoded_vnf) ;
122         aaiService.LOGwriteDateTrace(NODE_TYPE, requestProperties.getProperty(key));
123
124         return request_url;
125     }
126 }