improved handling of indexed data
[ccsdk/sli/adaptors.git] / aai-service / provider / src / main / java / org / onap / ccsdk / sli / adaptors / aai / CustomQueryRequest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                      reserved.
7  * Modifications Copyright (C) 2018 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22 /**
23  * @author Rich Tabedzki
24  *
25  */
26 package org.onap.ccsdk.sli.adaptors.aai;
27
28 import java.io.IOException;
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.ccsdk.sli.adaptors.aai.query.FormattedQueryRequestData;
36 import org.onap.ccsdk.sli.adaptors.aai.query.FormattedQueryResultList;
37
38 import com.fasterxml.jackson.core.JsonProcessingException;
39 import com.fasterxml.jackson.databind.ObjectMapper;
40
41
42 public class CustomQueryRequest extends AAIRequest {
43
44         public static final String GENERIC_SEARCH_PATH_CONST                    = "org.onap.ccsdk.sli.adaptors.aai.query.generic";
45
46         private final String generic_search_path;
47
48         public static final String FORMAT = "format";
49
50
51         public CustomQueryRequest() {
52                 String tmpGenericSearchPath = configProperties.getProperty(GENERIC_SEARCH_PATH_CONST);
53                 tmpGenericSearchPath = tmpGenericSearchPath.split("search")[0];
54                 generic_search_path = tmpGenericSearchPath +"query";
55         }
56
57
58         @Override
59         public URL getRequestUrl(String method, String resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
60
61                 String requestUrl = targetUri+generic_search_path;
62
63                 requestUrl = processPathData(requestUrl, requestProperties);
64
65                 String formatQuery = requestProperties.getProperty(FORMAT);
66
67                 if(formatQuery != null) {
68                         requestUrl = requestUrl +"?format="+formatQuery;
69                 }
70                 URL httpReqUrl =        new URL(requestUrl);
71
72                 aaiService.LOGwriteFirstTrace(method, httpReqUrl.toString());
73
74                 return httpReqUrl;
75         }
76
77         @Override
78         public URL getRequestQueryUrl(String method) throws UnsupportedEncodingException, MalformedURLException {
79                 return getRequestUrl(method, null);
80         }
81
82
83         @Override
84         public String toJSONString() {
85                 ObjectMapper mapper = getObjectMapper();
86                 FormattedQueryRequestData tenant = (FormattedQueryRequestData)requestDatum;
87                 String jsonText = null;
88                 try {
89                         jsonText = mapper.writeValueAsString(tenant);
90                 } catch (JsonProcessingException exc) {
91                         handleException(this, exc);
92                         return null;
93                 }
94                 return jsonText;
95         }
96
97
98         @Override
99         public String[] getArgsList() {
100                 String[] args = {FORMAT};
101                 return args;
102         }
103
104
105         @Override
106         public Class<? extends AAIDatum> getModelClass() {
107                 return FormattedQueryRequestData.class;
108         }
109
110
111         public static String processPathData(String requestUrl, Properties requestProperties) throws UnsupportedEncodingException {
112
113                 String key = FORMAT;
114
115                 String encodedVnf = encodeQuery(requestProperties.getProperty(key));
116                 requestUrl = requestUrl.replace("{identifier}", encodedVnf) ;
117                 aaiService.LOGwriteDateTrace("identifier", requestProperties.getProperty(key));
118
119                 return requestUrl;
120         }
121
122         public AAIDatum jsonStringToObject(String jsonData) throws IOException {
123                 if(jsonData == null) {
124                         return null;
125                 }
126
127                 AAIDatum response = null;
128                 ObjectMapper mapper = getObjectMapper();
129                 response = mapper.readValue(jsonData, FormattedQueryResultList.class);
130                 return response;
131         }
132
133         protected boolean expectsDataFromPUTRequest() {
134                 return true;
135         }
136 }