f5bbde7e31f03c675c7bbf9ebf7a6844603946ee
[ccsdk/sli/adaptors.git] / aai-service / provider / src / main / java / org / onap / ccsdk / sli / adaptors / aai / LagInterfacePnfRequest.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.Properties;
28
29 import org.onap.ccsdk.sli.adaptors.aai.data.AAIDatum;
30 import org.openecomp.aai.inventory.v11.LagInterface;
31 import org.slf4j.LoggerFactory;
32
33 import com.fasterxml.jackson.core.JsonProcessingException;
34 import com.fasterxml.jackson.databind.ObjectMapper;
35
36 public class LagInterfacePnfRequest extends AAIRequest {
37
38         // tenant (1602)
39         public static final String LAG_INTERFACE_PATH                   = "org.onap.ccsdk.sli.adaptors.aai.path.pserver.pinterface";
40         public static final String LAG_INTERFACE_QUERY_PATH     = "org.onap.ccsdk.sli.adaptors.aai.path.pserver.pinterface.query";
41         public static final String PNF_PATH               = "org.onap.ccsdk.sli.adaptors.aai.path.pnf";
42
43         private final String lag_interface_path;
44         private final String lag_interface_query_path;
45
46         public static final String INTERFACE_NAME = "interface-name";
47         public static final String LAG_INTERFACE_INTERFACE_NAME = "lag-interface.interface-name";
48         public static final String PNF_NAME     = "pnf-name";
49         public static final String PNF_PNF_NAME = "pnf.pnf-name";
50
51
52         public LagInterfacePnfRequest() {
53                 lag_interface_path = configProperties.getProperty(PNF_PATH) + "/lag-interfaces/lag-interface/{interface-name}";
54                 lag_interface_query_path = configProperties.getProperty(LAG_INTERFACE_QUERY_PATH);
55                 LoggerFactory.getLogger(LagInterfacePnfRequest.class).debug("org.onap.ccsdk.sli.adaptors.aai.path.pserver.pinterface=\t" + lag_interface_path);
56                 LoggerFactory.getLogger(LagInterfacePnfRequest.class).debug("org.onap.ccsdk.sli.adaptors.aai.path.pserver.pinterface.query=\t" + lag_interface_query_path);
57                 if(lag_interface_path == null) {
58                         LoggerFactory.getLogger(LagInterfacePnfRequest.class).warn("org.onap.ccsdk.sli.adaptors.aai.path.pserver.pinterface PATH not found in aaiclient.properties");
59                 }
60         }
61
62
63         @Override
64         public URL getRequestUrl(String method, String resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
65
66                 String request_url = target_uri + lag_interface_path;
67                 String encoded_vnf = null;
68
69                 String interfaceName = null;
70
71                 if(requestProperties.containsKey(INTERFACE_NAME)) {
72                         interfaceName = requestProperties.getProperty(INTERFACE_NAME);
73                 } else
74                 if(requestProperties.containsKey(LAG_INTERFACE_INTERFACE_NAME)) {
75                         interfaceName = requestProperties.getProperty(LAG_INTERFACE_INTERFACE_NAME);
76                 }
77
78                 encoded_vnf = encodeQuery(interfaceName);
79                 request_url = request_url.replace("{interface-name}", encoded_vnf) ;
80
81                 request_url = processPnfRequestPathData(request_url,requestProperties);
82
83                 if(resourceVersion != null) {
84                         request_url = request_url +"?resource-version="+resourceVersion;
85                 }
86                 URL http_req_url =      new URL(request_url);
87
88                 aaiService.LOGwriteFirstTrace(method, http_req_url.toString());
89                 aaiService.LOGwriteDateTrace("interface-name", interfaceName);
90
91                 return http_req_url;
92         }
93
94         public static String processPnfRequestPathData(String request_url, Properties requestProperties) throws UnsupportedEncodingException {
95
96                 String key = PNF_NAME;
97                 if(requestProperties.containsKey(PNF_PNF_NAME)) {
98                         key = PNF_PNF_NAME;
99                 }
100
101                 if(!requestProperties.containsKey(key)) {
102                         aaiService.logKeyError(String.format("%s,%s", PNF_NAME, PNF_PNF_NAME));
103                 }
104
105                 String encoded_vnf = encodeQuery(requestProperties.getProperty(key));
106                 request_url = request_url.replace("{pnf-name}", encoded_vnf) ;
107
108                 return request_url;
109         }
110
111         @Override
112         public URL getRequestQueryUrl(String method) throws UnsupportedEncodingException, MalformedURLException {
113                 return this.getRequestUrl(method, null);
114         }
115
116
117         @Override
118         public String toJSONString() {
119                 ObjectMapper mapper = getObjectMapper();
120                 LagInterface vnfc = (LagInterface)requestDatum;
121                 String json_text = null;
122                 try {
123                         json_text = mapper.writeValueAsString(vnfc);
124                 } catch (JsonProcessingException exc) {
125                         handleException(this, exc);
126                         return null;
127                 }
128                 return json_text;
129         }
130
131         @Override
132         public String[] getArgsList() {
133                 String[] args = {INTERFACE_NAME, LAG_INTERFACE_INTERFACE_NAME, PNF_PNF_NAME};
134
135                 return args;
136         }
137
138         @Override
139         public Class<? extends AAIDatum> getModelClass() {
140                 return LagInterface.class;
141         }
142 }