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