d61a4ea89a5d02d0bca6480b1eddd209e5bc06e3
[ccsdk/sli/adaptors.git] / aai-service / provider / src / main / java / org / openecomp / sdnc / sli / aai / CustomQueryRequest.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.IOException;
25 import java.io.UnsupportedEncodingException;
26 import java.net.MalformedURLException;
27 import java.net.URL;
28 import java.util.Properties;
29
30 import org.openecomp.sdnc.sli.aai.data.AAIDatum;
31 import org.openecomp.sdnc.sli.aai.query.FormattedQueryRequestData;
32 import org.openecomp.sdnc.sli.aai.query.FormattedQueryResultList;
33
34 import com.fasterxml.jackson.core.JsonParseException;
35 import com.fasterxml.jackson.core.JsonProcessingException;
36 import com.fasterxml.jackson.databind.JsonMappingException;
37 import com.fasterxml.jackson.databind.ObjectMapper;
38
39
40 public class CustomQueryRequest extends AAIRequest {
41
42         public static final String GENERIC_SEARCH_PATH                  = "org.openecomp.sdnc.sli.aai.query.generic";
43
44         private final String generic_search_path;
45
46         public static final String FORMAT = "format";
47
48
49         public CustomQueryRequest() {
50                 String tmp_generic_search_path = configProperties.getProperty(GENERIC_SEARCH_PATH);
51                 tmp_generic_search_path = tmp_generic_search_path.split("search")[0];
52                 generic_search_path = tmp_generic_search_path +"query";
53         }
54
55
56         @Override
57         public URL getRequestUrl(String method, String resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
58
59                 String request_url = target_uri+generic_search_path;
60
61                 request_url = processPathData(request_url, requestProperties);
62
63                 String formatQuery = requestProperties.getProperty(FORMAT);
64
65                 if(formatQuery != null) {
66                         request_url = request_url +"?format="+formatQuery;
67                 }
68                 URL http_req_url =      new URL(request_url);
69
70                 aaiService.LOGwriteFirstTrace(method, http_req_url.toString());
71
72                 return http_req_url;
73         }
74
75         @Override
76         public URL getRequestQueryUrl(String method) throws UnsupportedEncodingException, MalformedURLException {
77                 return getRequestUrl(method, null);
78         }
79
80
81         @Override
82         public String toJSONString() {
83                 ObjectMapper mapper = getObjectMapper();
84                 FormattedQueryRequestData tenant = (FormattedQueryRequestData)requestDatum;
85                 String json_text = null;
86                 try {
87                         json_text = mapper.writeValueAsString(tenant);
88                 } catch (JsonProcessingException exc) {
89                         handleException(this, exc);
90                         return null;
91                 }
92                 return json_text;
93         }
94
95
96         @Override
97         public String[] getArgsList() {
98                 String[] args = {FORMAT};
99                 return args;
100         }
101
102
103         @Override
104         public Class<? extends AAIDatum> getModelClass() {
105                 return FormattedQueryRequestData.class;
106         }
107
108
109         public static String processPathData(String request_url, Properties requestProperties) throws UnsupportedEncodingException {
110
111                 String key = FORMAT;
112
113                 String encoded_vnf = encodeQuery(requestProperties.getProperty(key));
114                 request_url = request_url.replace("{identifier}", encoded_vnf) ;
115                 aaiService.LOGwriteDateTrace("identifier", requestProperties.getProperty(key));
116
117                 return request_url;
118         }
119
120         public AAIDatum jsonStringToObject(String jsonData) throws JsonParseException, JsonMappingException, IOException {
121                 if(jsonData == null) {
122                         return null;
123                 }
124
125                 AAIDatum response = null;
126                 ObjectMapper mapper = getObjectMapper();
127                 response = mapper.readValue(jsonData, FormattedQueryResultList.class);
128                 return response;
129         }
130
131         protected boolean expectsDataFromPUTRequest() {
132                 return true;
133         }
134 }