Update sli-adaptor/aai-service package names
[ccsdk/sli/adaptors.git] / aai-service / provider / src / main / java / org / onap / sli / adaptors / aai / CloudRegionRequest.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.onap.ccsdk.sli.adaptors.aai;
23
24 import java.io.UnsupportedEncodingException;
25 import java.net.MalformedURLException;
26 import java.net.URL;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.Properties;
30
31 import org.onap.ccsdk.sli.adaptors.aai.data.AAIDatum;
32 import org.openecomp.aai.inventory.v10.CloudRegion;
33
34 import com.fasterxml.jackson.core.JsonProcessingException;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36 import com.google.common.base.Joiner;
37
38 public class CloudRegionRequest extends AAIRequest {
39
40         public static final String CLOUD_REGION_PATH    = "org.onap.ccsdk.sli.adaptors.aai.path.cloud.region";
41         
42         private final String cloud_region_path;
43         
44         public static final String CLOUD_REGION_CLOUD_OWNER     = "cloud-region.cloud-owner";
45         public static final String CLOUD_REGION_CLOUD_REGION_ID = "cloud-region.cloud-region-id";
46
47
48         public CloudRegionRequest() {
49                 cloud_region_path = configProperties.getProperty(CLOUD_REGION_PATH);
50         }
51
52         @Override
53         public URL getRequestQueryUrl(String method) throws UnsupportedEncodingException, MalformedURLException {
54                 return this.getRequestUrl(method, null);
55         }
56         
57         @Override
58         public URL getRequestUrl(String method, String resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
59
60                 String request_url = target_uri+cloud_region_path;
61
62                 request_url = processPathData(request_url, requestProperties);
63
64                 Map<String, String> query = new HashMap<String, String>();
65                 if(requestProperties.containsKey(DEPTH)) {
66                         query.put(DEPTH, requestProperties.getProperty(DEPTH));
67                 }
68
69                 if(resourceVersion != null) {
70 //                      request_url = request_url +"?resource-version="+resourceVersion;
71                         query.put(RESOURCE_VERSION, resourceVersion);
72                 }
73                 
74                 if(!query.isEmpty()) {
75                         Joiner.MapJoiner mapJoiner = Joiner.on("&").withKeyValueSeparator("=");
76                         String queryString = mapJoiner.join(query);
77                         request_url = String.format("%s?%s", request_url, queryString);
78                 }
79                 URL http_req_url =      new URL(request_url);
80
81                 aaiService.LOGwriteFirstTrace(method, http_req_url.toString());
82                 
83                 return http_req_url;
84         }
85
86
87         @Override
88         public String toJSONString() {
89                 ObjectMapper mapper = getObjectMapper();
90                 CloudRegion vnfc = (CloudRegion)requestDatum;
91                 String json_text = null;
92                 try {
93                         json_text = mapper.writeValueAsString(vnfc);
94                 } catch (JsonProcessingException exc) {
95                         handleException(this, exc);
96                         return null;
97                 }
98                 return json_text;
99         }
100
101         @Override
102         public String[] getArgsList() {
103                 String[] args =  
104                         {
105                                 CLOUD_REGION_CLOUD_OWNER,
106                                 CLOUD_REGION_CLOUD_REGION_ID,
107                                 DEPTH
108                         };
109
110                 return args;
111         }
112
113         @Override
114         public Class<? extends AAIDatum> getModelClass() {
115                 return CloudRegion.class;
116         }
117         
118         public static String processPathData(String request_url, Properties requestProperties) throws UnsupportedEncodingException {
119
120                 if(!requestProperties.containsKey(CLOUD_REGION_CLOUD_OWNER) || !requestProperties.containsKey(CLOUD_REGION_CLOUD_REGION_ID)) {
121                         aaiService.logKeyError(String.format("%s,%s", CLOUD_REGION_CLOUD_OWNER, CLOUD_REGION_CLOUD_REGION_ID));
122                 }
123                 
124                 String encoded_vnf = encodeQuery(requestProperties.getProperty(CLOUD_REGION_CLOUD_OWNER));
125                 request_url = request_url.replace("{cloud-owner}", encoded_vnf) ;
126
127                 encoded_vnf = encodeQuery(requestProperties.getProperty(CLOUD_REGION_CLOUD_REGION_ID));
128                 request_url = request_url.replace("{cloud-region-id}", encoded_vnf) ;
129
130                 aaiService.LOGwriteDateTrace("cloud-owner", requestProperties.getProperty(CLOUD_REGION_CLOUD_OWNER));
131                 aaiService.LOGwriteDateTrace("cloud-region-id", requestProperties.getProperty(CLOUD_REGION_CLOUD_REGION_ID));
132                 
133                 return request_url;
134         }
135 }