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