Convert tabs to spaces
[ccsdk/sli/adaptors.git] / aai-service / provider / src / main / java / org / openecomp / sdnc / sli / aai / GenericVnfRequest.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.GenericVnf;
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 GenericVnfRequest extends AAIRequest {
36
37         // tenant (1602)
38         public static final String GENERIC_VNF_PATH                     = "org.openecomp.sdnc.sli.aai.path.generic.vnf";
39         public static final String GENERIC_VNF_QUERY_PATH       = "org.openecomp.sdnc.sli.aai.path.generic.vnf.query";
40         
41         private final String generic_vnf_path;
42         private final String generic_vnf_query_path;
43         
44         public static final String GENERIC_VNF_ID = "generic_vnf.vnf_id";
45         public static final String VNF_ID = "vnf_id";
46
47
48         public GenericVnfRequest() {
49                 generic_vnf_path = configProperties.getProperty(GENERIC_VNF_PATH);
50                 generic_vnf_query_path = configProperties.getProperty(GENERIC_VNF_QUERY_PATH);
51         }
52
53         
54         @Override
55         public URL getRequestUrl(String method, String resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
56
57                 String request_url = target_uri+generic_vnf_path;
58                 String key = VNF_ID;
59                 if(requestProperties.containsKey(GENERIC_VNF_ID)) {
60                         key = GENERIC_VNF_ID;
61                 }
62                 
63                 if(!requestProperties.containsKey(key)) {
64                         aaiService.logKeyError(String.format("%s,%s", VNF_ID, GENERIC_VNF_ID));
65                 }
66                 
67                 String encoded_vnf = encodeQuery(requestProperties.getProperty(key));
68                 request_url = request_url.replace("{vnf-id}", encoded_vnf) ;
69                 if(resourceVersion != null) {
70                         request_url = request_url +"?resource-version="+resourceVersion;
71                 }
72                 URL http_req_url =      new URL(request_url);
73
74                 aaiService.LOGwriteFirstTrace(method, http_req_url.toString());
75                 aaiService.LOGwriteDateTrace("vnf-id", requestProperties.getProperty(key));
76                 
77                 return http_req_url;
78         }
79         
80         @Override
81         public URL getRequestQueryUrl(String method) throws UnsupportedEncodingException, MalformedURLException {
82
83                 String request_url = target_uri+generic_vnf_path;
84                 
85                 String key = VNF_ID;
86                 if(requestProperties.containsKey(GENERIC_VNF_ID)) {
87                         key = GENERIC_VNF_ID;
88                 }
89                 
90                 if(!requestProperties.containsKey(key)) {
91                         aaiService.logKeyError(String.format("%s,%s", VNF_ID, GENERIC_VNF_ID));
92                 }
93                 
94                 String encoded_vnf = encodeQuery(requestProperties.getProperty(key));
95                 request_url = request_url.replace("{vnf-id}", encoded_vnf) ;
96                 URL http_req_url =      new URL(request_url);
97
98                 aaiService.LOGwriteFirstTrace(method, http_req_url.toString());
99                 aaiService.LOGwriteDateTrace("vnf-id", requestProperties.getProperty(key));
100                 
101                 return http_req_url;
102         }
103
104
105         @Override
106         public String toJSONString() {
107                 ObjectMapper mapper = getObjectMapper();
108                 GenericVnf tenant = (GenericVnf)requestDatum;
109                 String json_text = null;
110                 try {
111                         json_text = mapper.writeValueAsString(tenant);
112                 } catch (JsonProcessingException exc) {
113                         handleException(this, exc);
114                         return null;
115                 }
116                 return json_text;
117         }
118
119
120         @Override
121         public String[] getArgsList() {
122                 String[] args = {VNF_ID, GENERIC_VNF_ID};
123                 return args;
124         }
125
126
127         @Override
128         public Class<? extends AAIDatum> getModelClass() {
129                 return GenericVnf.class;
130         }
131
132
133         public static String processPathData(String request_url, Properties requestProperties) throws UnsupportedEncodingException {
134
135                 String key = VNF_ID;
136                 if(requestProperties.containsKey(GENERIC_VNF_ID)) {
137                         key = GENERIC_VNF_ID;
138                 }
139                 String encoded_vnf = encodeQuery(requestProperties.getProperty(key));
140                 request_url = request_url.replace("{vnf-id}", encoded_vnf) ;
141
142                 return request_url;
143         }
144 }