Convert tabs to spaces
[ccsdk/sli/adaptors.git] / aai-service / provider / src / main / java / org / openecomp / sdnc / sli / aai / LInterfaceRequest.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.LInterface;
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 LInterfaceRequest extends AAIRequest {
36
37         // tenant (1602)
38         public static final String LAGINTERFACE_LINTERFACE_PATH                 = "org.openecomp.sdnc.sli.aai.path.lag.interface.l.interface";
39         public static final String LAGINTERFACE_LINTERFACE_QUERY_PATH   = "org.openecomp.sdnc.sli.aai.path.lag.interface.l.interface.query";
40
41         public static final String P_INTERFACE_LINTERFACE_PATH                  = "org.openecomp.sdnc.sli.aai.path.p.interface.l.interface";
42         public static final String P_INTERFACE_LINTERFACE_QUERY_PATH    = "org.openecomp.sdnc.sli.aai.path.p.interface.l.interface.query";
43
44         public static final String LAGINTERFACE_LINTERFACE_PNF_PATH             = "org.openecomp.sdnc.sli.aai.path.lag.interface.l.interface.pnf";
45         public static final String P_INTERFACE_LINTERFACE_PNF_PATH              = "org.openecomp.sdnc.sli.aai.path.p.interface.l.interface.pnf";
46
47         private final String laginterface_linterface_path;
48         private final String laginterface_linterface_query_path;
49         private final String p_interface_linterface_path;
50         private final String p_interface_linterface_query_path;
51
52         private final String laginterface_linterface_pnf_path;
53         private final String p_interface_linterface_pnf_path;
54
55         public static final String INTERFACE_NAME = "interface-name";
56         public static final String LINTERFACE_INTERFACE_NAME    = "l-interface.interface-name";
57         public static final String LAG_INTERFACE_INTERFACE_NAME = "lag-interface.interface-name";
58         public static final String P_INTERFACE_INTERFACE_NAME   = "p-interface.interface-name";
59         public static final String PNF_PNF_NAME = "pnf.pnf-name";
60
61         public static final String ROUTER_NAME = "router-name";
62         public static final String HOSTNAME = "hostname";
63
64
65         public static enum TYPE { L2_BRIDGE_BGF, L2_BRIDGE_SBG};
66
67         private final TYPE type;
68
69         public LInterfaceRequest(TYPE type) {
70                 this.type = type;
71
72                 laginterface_linterface_path = configProperties.getProperty(LAGINTERFACE_LINTERFACE_PATH);
73                 laginterface_linterface_query_path = configProperties.getProperty(LAGINTERFACE_LINTERFACE_QUERY_PATH);
74
75                 p_interface_linterface_path = configProperties.getProperty(P_INTERFACE_LINTERFACE_PATH);
76                 p_interface_linterface_query_path = configProperties.getProperty(P_INTERFACE_LINTERFACE_QUERY_PATH);
77
78                 laginterface_linterface_pnf_path = configProperties.getProperty(LAGINTERFACE_LINTERFACE_PNF_PATH);
79                 p_interface_linterface_pnf_path = configProperties.getProperty(P_INTERFACE_LINTERFACE_PNF_PATH);
80         }
81
82
83         @Override
84         public URL getRequestUrl(String method, String resourceVersion) throws UnsupportedEncodingException, MalformedURLException {
85
86                 String request_url = null;
87                 String encoded_vnf = null;
88                 String hostname = null;
89                 String pnfname = null;
90                 String interfaceName = null;
91
92                 if(type == TYPE.L2_BRIDGE_SBG) {
93                         if(requestProperties.containsKey(PNF_PNF_NAME)) {
94                                 request_url = target_uri + laginterface_linterface_pnf_path;
95                         } else {
96                                 request_url = target_uri + laginterface_linterface_path;
97                         }
98
99                         if(requestProperties.containsKey(ROUTER_NAME)) {
100                                 hostname = requestProperties.getProperty(ROUTER_NAME);
101                                 encoded_vnf = encodeQuery(hostname);
102                                 request_url = request_url.replace("{hostname}", encoded_vnf);
103                         }
104
105                         if(requestProperties.containsKey(HOSTNAME)) {
106                                 hostname = requestProperties.getProperty(HOSTNAME);
107                                 encoded_vnf = encodeQuery(hostname);
108                                 request_url = request_url.replace("{hostname}", encoded_vnf);
109                         }
110
111                         if(requestProperties.containsKey(PNF_PNF_NAME)) {
112                                 pnfname = requestProperties.getProperty(PNF_PNF_NAME);
113                                 encoded_vnf = encodeQuery(pnfname);
114                                 request_url = request_url.replace("{pnf-name}", encoded_vnf);
115                         }
116
117                         encoded_vnf = encodeQuery(requestProperties.getProperty(LAG_INTERFACE_INTERFACE_NAME));
118                         request_url = request_url.replace("{lag-interface.interface-name}", encoded_vnf) ;
119
120
121                         interfaceName = requestProperties.getProperty(INTERFACE_NAME);
122                         if(interfaceName == null || interfaceName.isEmpty()) {
123                                 interfaceName = requestProperties.getProperty(LINTERFACE_INTERFACE_NAME);
124                         }
125                         encoded_vnf = encodeQuery(interfaceName);
126                         request_url = request_url.replace("{interface-name}", encoded_vnf) ;
127
128                 }
129                 if(type == TYPE.L2_BRIDGE_BGF) {
130                         if(requestProperties.containsKey(PNF_PNF_NAME)) {
131                                 request_url = target_uri + p_interface_linterface_pnf_path;
132                         } else {
133                                 request_url = target_uri + p_interface_linterface_path;
134                         }
135
136
137                         if(requestProperties.containsKey(ROUTER_NAME)) {
138                                 hostname = requestProperties.getProperty(ROUTER_NAME);
139                                 encoded_vnf = encodeQuery(hostname);
140                                 request_url = request_url.replace("{hostname}", encoded_vnf);
141                         }
142
143                         if(requestProperties.containsKey(HOSTNAME)) {
144                                 hostname = requestProperties.getProperty(HOSTNAME);
145                                 encoded_vnf = encodeQuery(hostname);
146                                 request_url = request_url.replace("{hostname}", encoded_vnf);
147                         }
148
149                         if(requestProperties.containsKey(PNF_PNF_NAME)) {
150                                 pnfname = requestProperties.getProperty(PNF_PNF_NAME);
151                                 encoded_vnf = encodeQuery(pnfname);
152                                 request_url = request_url.replace("{pnf-name}", encoded_vnf);
153                         }
154
155                         encoded_vnf = encodeQuery(requestProperties.getProperty(P_INTERFACE_INTERFACE_NAME));
156                         request_url = request_url.replace("{p-interface.interface-name}", encoded_vnf) ;
157
158
159                         interfaceName = requestProperties.getProperty(INTERFACE_NAME);
160                         if(interfaceName == null || interfaceName.isEmpty()) {
161                                 interfaceName = requestProperties.getProperty(LINTERFACE_INTERFACE_NAME);
162                         }
163                         encoded_vnf = encodeQuery(interfaceName);
164                         request_url = request_url.replace("{interface-name}", encoded_vnf) ;
165                 }
166
167
168                 if(resourceVersion != null) {
169                         request_url = request_url +"?resource-version="+resourceVersion;
170                 }
171                 URL http_req_url =      new URL(request_url);
172
173                 aaiService.LOGwriteFirstTrace(method, http_req_url.toString());
174                 if(hostname != null)
175                         aaiService.LOGwriteDateTrace("hostname", hostname);
176                 if(pnfname != null)
177                         aaiService.LOGwriteDateTrace("pnf-name", pnfname);
178                 if(requestProperties.containsKey(LAG_INTERFACE_INTERFACE_NAME)) {
179                         aaiService.LOGwriteDateTrace("lag-interface.interface-name", requestProperties.getProperty(LAG_INTERFACE_INTERFACE_NAME));
180                 }
181                 if(requestProperties.containsKey(P_INTERFACE_INTERFACE_NAME)) {
182                         aaiService.LOGwriteDateTrace("p-interface.interface-name", requestProperties.getProperty(P_INTERFACE_INTERFACE_NAME));
183                 }
184                 aaiService.LOGwriteDateTrace("interface-name", interfaceName);
185
186                 return http_req_url;
187         }
188
189         @Override
190         public URL getRequestQueryUrl(String method) throws UnsupportedEncodingException, MalformedURLException {
191                 return this.getRequestUrl(method, null);
192         }
193
194
195         @Override
196         public String toJSONString() {
197                 ObjectMapper mapper = getObjectMapper();
198                 LInterface vnfc = (LInterface)requestDatum;
199                 String json_text = null;
200                 try {
201                         json_text = mapper.writeValueAsString(vnfc);
202                 } catch (JsonProcessingException exc) {
203                         handleException(this, exc);
204                         return null;
205                 }
206                 return json_text;
207         }
208
209
210         @Override
211         public String[] getArgsList() {
212                 String[] args = {};
213                 if(type == TYPE.L2_BRIDGE_SBG) {
214                         String[] tmpArray = {INTERFACE_NAME, LINTERFACE_INTERFACE_NAME, LAG_INTERFACE_INTERFACE_NAME, HOSTNAME, ROUTER_NAME, PNF_PNF_NAME};
215                         args = tmpArray;
216                 }
217                 if(type == TYPE.L2_BRIDGE_BGF) {
218                         String[] tmpArray = {INTERFACE_NAME, LINTERFACE_INTERFACE_NAME, P_INTERFACE_INTERFACE_NAME, HOSTNAME, ROUTER_NAME, PNF_PNF_NAME};
219                         args = tmpArray;
220                 }
221
222                 return args;
223         }
224
225         @Override
226         public Class<? extends AAIDatum> getModelClass() {
227                 return LInterface.class;
228         }
229
230         @Override
231         public String getPrimaryResourceName(String resource) {
232                 return "l-interface";
233         }
234
235         public static final String processPathData(String request_url, Properties requestProperties) throws UnsupportedEncodingException {
236                 String interfaceName = requestProperties.getProperty(INTERFACE_NAME);
237                 if(interfaceName == null || interfaceName.isEmpty()) {
238                         interfaceName = requestProperties.getProperty(LINTERFACE_INTERFACE_NAME);
239                 }
240
241                 request_url = request_url.replace("{interface-name}", encodeQuery(interfaceName)) ;
242                 return request_url;
243         }
244 }