0f1dda0e517877a2b99c548f3f73d64cbbbf7d42
[ccsdk/sli/adaptors.git] / aai-service / provider / src / main / java / org / openecomp / sdnc / sli / aai / GenericQueryRequest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 ONAP 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.openecomp.sdnc.sli.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.openecomp.aai.inventory.v10.ResultData;
30 import org.openecomp.sdnc.sli.aai.data.AAIDatum;
31
32 import com.fasterxml.jackson.core.JsonProcessingException;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34
35 public class GenericQueryRequest extends AAIRequest {
36
37         public static final String GENERIC_SEARCH_PATH                  = "org.openecomp.sdnc.sli.aai.query.generic";
38         
39         private final String generic_search_path;
40         
41         public static final String START_NODE_TYPE = "start-node-type";
42         public static final String IDENTIFIER = "identifier";
43         public static final String VALUE = "value";
44
45
46         public GenericQueryRequest() {
47                 generic_search_path = configProperties.getProperty(GENERIC_SEARCH_PATH);
48         }
49
50         
51 //      @Override
52 //      public URL getRequestUrl(String method, String resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
53 //
54 //              String request_url = target_uri+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 = target_uri+generic_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                 ResultData tenant = (ResultData)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 = {START_NODE_TYPE, IDENTIFIER, VALUE};
112                 return args;
113         }
114
115
116         @Override
117         public Class<? extends AAIDatum> getModelClass() {
118                 return ResultData.class;
119         }
120
121
122         public static String processPathData(String request_url, Properties requestProperties) throws UnsupportedEncodingException {
123
124                 String key = IDENTIFIER;
125
126                 String encoded_vnf = encodeQuery(requestProperties.getProperty(key));
127                 request_url = request_url.replace("{identifier}", encoded_vnf) ;
128                 aaiService.LOGwriteDateTrace("identifier", requestProperties.getProperty(key));
129                 
130                 key = VALUE;
131
132                 encoded_vnf = encodeQuery(requestProperties.getProperty(key));
133                 request_url = request_url.replace("{value}", encoded_vnf) ;
134                 aaiService.LOGwriteDateTrace("value", requestProperties.getProperty(key));
135                 
136                 key = START_NODE_TYPE;
137
138                 encoded_vnf = encodeQuery(requestProperties.getProperty(key));
139                 request_url = request_url.replace("{start-node-type}", encoded_vnf) ;
140                 aaiService.LOGwriteDateTrace("start-node-type", requestProperties.getProperty(key));
141                 
142                 return request_url;
143         }
144 }