2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Huawei Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.bpmn.infrastructure.workflow.service;
23 import org.json.JSONObject;
24 import java.io.IOException;
25 import java.net.SocketTimeoutException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
30 import java.util.Map.Entry;
31 import org.apache.commons.lang3.StringUtils;
32 import org.apache.http.HttpResponse;
33 import org.apache.http.ParseException;
34 import org.apache.http.client.HttpClient;
35 import org.apache.http.client.config.RequestConfig;
36 import org.apache.http.client.methods.HttpDelete;
37 import org.apache.http.client.methods.HttpGet;
38 import org.apache.http.client.methods.HttpPost;
39 import org.apache.http.client.methods.HttpPut;
40 import org.apache.http.client.methods.HttpRequestBase;
41 import org.apache.http.conn.ConnectTimeoutException;
42 import org.apache.http.entity.ContentType;
43 import org.apache.http.entity.StringEntity;
44 import org.apache.http.impl.client.HttpClientBuilder;
45 import org.apache.http.util.EntityUtils;
46 import org.camunda.bpm.engine.runtime.Execution;
47 import org.onap.so.bpmn.core.UrnPropertiesReader;
48 import org.onap.so.bpmn.core.domain.ServiceDecomposition;
49 import org.onap.so.bpmn.core.domain.Resource;
50 import org.onap.so.bpmn.core.json.JsonUtils;
51 import org.onap.so.logger.MessageEnum;
52 import org.onap.so.logger.MsoLogger;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.core.env.Environment;
56 import com.fasterxml.jackson.core.JsonProcessingException;
57 import com.fasterxml.jackson.databind.ObjectMapper;
58 import com.fasterxml.jackson.databind.SerializationFeature;
60 public class ServicePluginFactory {
62 // SOTN calculate route
63 public static final String OOF_Default_EndPoint = "http://192.168.1.223:8443/oof/sotncalc";
65 public static final String Third_SP_Default_EndPoint = "http://192.168.1.223:8443/sp/resourcemgr/querytps";
67 public static final String Inventory_OSS_Default_EndPoint = "http://192.168.1.199:8443/oss/inventory";
69 private static final int DEFAULT_TIME_OUT = 60000;
71 static JsonUtils jsonUtil = new JsonUtils();
73 private static MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA, ServicePluginFactory.class);
75 private static ServicePluginFactory instance;
78 public static synchronized ServicePluginFactory getInstance() {
79 if (null == instance) {
80 instance = new ServicePluginFactory();
85 private ServicePluginFactory() {
90 private String getInventoryOSSEndPoint(){
91 return UrnPropertiesReader.getVariable("mso.service-plugin.inventory-oss-endpoint", Inventory_OSS_Default_EndPoint);
93 private String getThirdSPEndPoint(){
94 return UrnPropertiesReader.getVariable("mso.service-plugin.third-sp-endpoint", Third_SP_Default_EndPoint);
97 private String getOOFCalcEndPoint(){
98 return UrnPropertiesReader.getVariable("mso.service-plugin.oof-calc-endpoint", OOF_Default_EndPoint);
101 public ServiceDecomposition doProcessSiteLocation(ServiceDecomposition serviceDecomposition, String uuiRequest) {
102 ServiceDecomposition serviceDecompositionforLocal = serviceDecomposition;
104 if (isSiteLocationLocal(serviceDecomposition, uuiRequest)) {
105 return serviceDecomposition;
108 List<Resource> addResourceList = serviceDecomposition.getServiceResources();
109 for (Resource resource : addResourceList) {
110 String resourcemodelName = resource.getModelInfo().getModelName();
111 if (!StringUtils.containsIgnoreCase(resourcemodelName, "sp-partner")) {
112 serviceDecompositionforLocal.deleteResource(resource);
115 if (!StringUtils.containsIgnoreCase(resourcemodelName, "sppartner")) {
116 serviceDecompositionforLocal.deleteResource(resource);
121 return serviceDecompositionforLocal;
124 public boolean isSiteLocationLocal(ServiceDecomposition serviceDecomposition, String uuiRequest) {
125 boolean isSiteLocationLocal = true;
127 String serviceModelName = serviceDecomposition.getModelInfo().getModelName();
128 String serviceParameters = JsonUtils.getJsonValue(uuiRequest, "service.parameters");
129 String requestInputs = JsonUtils.getJsonValue(serviceParameters, "requestInputs");
130 JSONObject inputParameters = new JSONObject(requestInputs);
132 if(StringUtils.containsIgnoreCase(serviceModelName, "site") && inputParameters.has("location"))
134 Object location = inputParameters.get("location");
135 JSONObject locationObj = new JSONObject(location);
136 String locationONAP = queryLocationFromInventoryOSS(locationObj);
137 if(StringUtils.containsIgnoreCase(locationONAP, "remote")) {
138 isSiteLocationLocal = false;
142 return isSiteLocationLocal;
145 private String queryLocationFromInventoryOSS(JSONObject locationObj) {
146 String reqContent = getJsonString(locationObj);
147 String url = getInventoryOSSEndPoint();
148 String responseContent = sendRequest(url, "POST", reqContent);
149 String locationONAP = "";
150 if (null != responseContent) {
151 locationONAP = getJsonObject(responseContent, String.class);
156 public String preProcessService(ServiceDecomposition serviceDecomposition, String uuiRequest) {
159 if (isSOTN(serviceDecomposition, uuiRequest)) {
160 // We Need to query the terminalpoint of the VPN by site location
162 return preProcessSOTNService(serviceDecomposition, uuiRequest);
167 public String doServiceHoming(ServiceDecomposition serviceDecomposition, String uuiRequest) {
169 if (isSOTN(serviceDecomposition, uuiRequest)) {
170 return doSOTNServiceHoming(serviceDecomposition, uuiRequest);
175 private boolean isSOTN(ServiceDecomposition serviceDecomposition, String uuiRequest) {
176 // there should be a register platform , we check it very simple here.
177 return uuiRequest.contains("clientSignal") && uuiRequest.contains("vpnType");
180 private String preProcessSOTNService(ServiceDecomposition serviceDecomposition, String uuiRequest) {
181 Map<String, Object> uuiObject = getJsonObject(uuiRequest, Map.class);
182 Map<String, Object> serviceObject = (Map<String, Object>) uuiObject.get("service");
183 Map<String, Object> serviceParametersObject = (Map<String, Object>) serviceObject.get("parameters");
184 Map<String, Object> serviceRequestInputs = (Map<String, Object>) serviceParametersObject.get("requestInputs");
185 List<Object> resources = (List<Object>) serviceParametersObject.get("resources");
186 // This is a logic for demo , it could not be finalized to community.
187 String srcLocation = "";
188 String dstLocation = "";
189 String srcClientSignal = "";
190 String dstClientSignal = "";
191 // support R2 uuiReq and R1 uuiReq
192 // logic for R2 uuiRequest params in service level
193 for (Entry<String, Object> entry : serviceRequestInputs.entrySet()) {
194 if (entry.getKey().toLowerCase().contains("location")) {
195 if ("".equals(srcLocation)) {
196 srcLocation = (String) entry.getValue();
197 } else if ("".equals(dstLocation)) {
198 dstLocation = (String) entry.getValue();
201 if (entry.getKey().toLowerCase().contains("clientsignal")) {
202 if ("".equals(srcClientSignal)) {
203 srcClientSignal = (String) entry.getValue();
204 } else if ("".equals(dstClientSignal)) {
205 dstClientSignal = (String) entry.getValue();
210 // logic for R1 uuiRequest, params in resource level
211 for (Object resource : resources) {
212 Map<String, Object> resourceObject = (Map<String, Object>) resource;
213 Map<String, Object> resourceParametersObject = (Map<String, Object>) resourceObject.get("parameters");
214 Map<String, Object> resourceRequestInputs = (Map<String, Object>) resourceParametersObject
215 .get("requestInputs");
216 for (Entry<String, Object> entry : resourceRequestInputs.entrySet()) {
217 if (entry.getKey().toLowerCase().contains("location")) {
218 if ("".equals(srcLocation)) {
219 srcLocation = (String) entry.getValue();
220 } else if ("".equals(dstLocation)) {
221 dstLocation = (String) entry.getValue();
224 if (entry.getKey().toLowerCase().contains("clientsignal")) {
225 if ("".equals(srcClientSignal)) {
226 srcClientSignal = (String) entry.getValue();
227 } else if ("".equals(dstClientSignal)) {
228 dstClientSignal = (String) entry.getValue();
234 Map<String, Object> vpnRequestInputs = getVPNResourceRequestInputs(resources);
235 // here we put client signal to vpn resource inputs
236 vpnRequestInputs.put("src-client-signal", srcClientSignal);
237 vpnRequestInputs.put("dst-client-signal", dstClientSignal);
239 // Now we need to query terminal points from SP resourcemgr system.
240 List<Object> locationTerminalPointList = queryTerminalPointsFromServiceProviderSystem(srcLocation, dstLocation);
241 Map<String, Object> tpInfoMap = (Map<String, Object>) locationTerminalPointList.get(0);
243 serviceRequestInputs.put("inner-src-access-provider-id", tpInfoMap.get("access-provider-id"));
244 serviceRequestInputs.put("inner-src-access-client-id", tpInfoMap.get("access-client-id"));
245 serviceRequestInputs.put("inner-src-access-topology-id", tpInfoMap.get("access-topology-id"));
246 serviceRequestInputs.put("inner-src-access-node-id", tpInfoMap.get("access-node-id"));
247 serviceRequestInputs.put("inner-src-access-ltp-id", tpInfoMap.get("access-ltp-id"));
248 tpInfoMap = (Map<String, Object>) locationTerminalPointList.get(1);
250 serviceRequestInputs.put("inner-dst-access-provider-id", tpInfoMap.get("access-provider-id"));
251 serviceRequestInputs.put("inner-dst-access-client-id", tpInfoMap.get("access-client-id"));
252 serviceRequestInputs.put("inner-dst-access-topology-id", tpInfoMap.get("access-topology-id"));
253 serviceRequestInputs.put("inner-dst-access-node-id", tpInfoMap.get("access-node-id"));
254 serviceRequestInputs.put("inner-dst-access-ltp-id", tpInfoMap.get("access-ltp-id"));
256 String newRequest = getJsonString(uuiObject);
260 private List<Object> queryTerminalPointsFromServiceProviderSystem(String srcLocation, String dstLocation) {
261 Map<String, String> locationSrc = new HashMap<>();
262 locationSrc.put("location", srcLocation);
263 Map<String, String> locationDst = new HashMap<>();
264 locationDst.put("location", dstLocation);
265 List<Map<String, String>> locations = new ArrayList<>();
266 locations.add(locationSrc);
267 locations.add(locationDst);
268 List<Object> returnList = new ArrayList<>();
269 String reqContent = getJsonString(locations);
270 String url = getThirdSPEndPoint();
271 String responseContent = sendRequest(url, "POST", reqContent);
272 if (null != responseContent) {
273 returnList = getJsonObject(responseContent, List.class);
278 private Map<String, Object> getVPNResourceRequestInputs(List<Object> resources) {
279 for (Object resource : resources) {
280 Map<String, Object> resourceObject = (Map<String, Object>) resource;
281 Map<String, Object> resourceParametersObject = (Map<String, Object>) resourceObject.get("parameters");
282 Map<String, Object> resourceRequestInputs = (Map<String, Object>) resourceParametersObject
283 .get("requestInputs");
284 for (Entry<String, Object> entry : resourceRequestInputs.entrySet()) {
285 if (entry.getKey().toLowerCase().contains("vpntype")) {
286 return resourceRequestInputs;
293 public static void main(String args[]){
294 String str = "restconf/config/GENERIC-RESOURCE-API:services/service/eca7e542-12ba-48de-8544-fac59303b14e/service-data/networks/network/aec07806-1671-4af2-b722-53c8e320a633/network-data/";
296 int index1 = str.indexOf("/network/");
297 int index2 = str.indexOf("/network-data");
299 String str1 = str.substring(index1 + "/network/".length(), index2);
300 System.out.println(str1);
304 private String doSOTNServiceHoming(ServiceDecomposition serviceDecomposition, String uuiRequest) {
305 // query the route for the service.
306 Map<String, Object> uuiObject = getJsonObject(uuiRequest, Map.class);
307 Map<String, Object> serviceObject = (Map<String, Object>) uuiObject.get("service");
308 Map<String, Object> serviceParametersObject = (Map<String, Object>) serviceObject.get("parameters");
309 Map<String, Object> serviceRequestInputs = (Map<String, Object>) serviceParametersObject.get("requestInputs");
310 Map<String, Object> oofQueryObject = new HashMap<>();
311 List<Object> resources = (List<Object>) serviceParametersObject.get("resources");
312 oofQueryObject.put("src-access-provider-id", serviceRequestInputs.get("inner-src-access-provider-id"));
313 oofQueryObject.put("src-access-client-id", serviceRequestInputs.get("inner-src-access-client-id"));
314 oofQueryObject.put("src-access-topology-id", serviceRequestInputs.get("inner-src-access-topology-id"));
315 oofQueryObject.put("src-access-node-id", serviceRequestInputs.get("inner-src-access-node-id"));
316 oofQueryObject.put("src-access-ltp-id", serviceRequestInputs.get("inner-src-access-ltp-id"));
317 oofQueryObject.put("dst-access-provider-id", serviceRequestInputs.get("inner-dst-access-provider-id"));
318 oofQueryObject.put("dst-access-client-id", serviceRequestInputs.get("inner-dst-access-client-id"));
319 oofQueryObject.put("dst-access-topology-id", serviceRequestInputs.get("inner-dst-access-topology-id"));
320 oofQueryObject.put("dst-access-node-id", serviceRequestInputs.get("inner-dst-access-node-id"));
321 oofQueryObject.put("dst-access-ltp-id", serviceRequestInputs.get("inner-dst-access-ltp-id"));
322 String oofRequestReq = getJsonString(oofQueryObject);
323 String url = getOOFCalcEndPoint();
324 String responseContent = sendRequest(url, "POST", oofRequestReq);
326 List<Object> returnList = new ArrayList<>();
327 if (null != responseContent) {
328 returnList = getJsonObject(responseContent, List.class);
330 // in demo we have only one VPN. no cross VPNs, so get first item.
331 Map<String, Object> returnRoute = getReturnRoute(returnList);
332 Map<String, Object> vpnRequestInputs = getVPNResourceRequestInputs(resources);
333 vpnRequestInputs.putAll(returnRoute);
334 String newRequest = getJsonString(uuiObject);
338 private Map<String, Object> getReturnRoute(List<Object> returnList){
339 Map<String, Object> returnRoute = new HashMap<>();
340 for(Object returnVpn :returnList){
341 Map<String, Object> returnVpnInfo = (Map<String, Object>) returnVpn;
342 String accessTopoId = (String)returnVpnInfo.get("access-topology-id");
343 if("100".equals(accessTopoId)){
344 returnRoute.putAll(returnVpnInfo);
346 else if("101".equals(accessTopoId)){
347 for(String key : returnVpnInfo.keySet()){
348 returnRoute.put("domain1-" + key, returnVpnInfo.get(key));
351 else if("102".equals(accessTopoId)){
352 for(String key : returnVpnInfo.keySet()){
353 returnRoute.put("domain2-" + key, returnVpnInfo.get(key));
357 for(String key : returnVpnInfo.keySet()){
358 returnRoute.put("domain" + accessTopoId +"-" + key, returnVpnInfo.get(key));
365 private Map<String, Object> getResourceParams(Execution execution, String resourceCustomizationUuid,
366 String serviceParameters) {
367 List<String> resourceList = jsonUtil.StringArrayToList(execution,
368 (String) JsonUtils.getJsonValue(serviceParameters, "resources"));
369 // Get the right location str for resource. default is an empty array.
370 String resourceInputsFromUui = "";
371 for (String resource : resourceList) {
372 String resCusUuid = (String) JsonUtils.getJsonValue(resource, "resourceCustomizationUuid");
373 if (resourceCustomizationUuid.equals(resCusUuid)) {
374 String resourceParameters = JsonUtils.getJsonValue(resource, "parameters");
375 resourceInputsFromUui = JsonUtils.getJsonValue(resourceParameters, "requestInputs");
378 Map<String, Object> resourceInputsFromUuiMap = getJsonObject(resourceInputsFromUui, Map.class);
379 return resourceInputsFromUuiMap;
382 public static <T> T getJsonObject(String jsonstr, Class<T> type) {
383 ObjectMapper mapper = new ObjectMapper();
384 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
386 return mapper.readValue(jsonstr, type);
387 } catch (IOException e) {
388 LOGGER.error(MessageEnum.RA_NS_EXC, "", "", MsoLogger.ErrorCode.BusinessProcesssError,
389 "fail to unMarshal json", e);
394 public static String getJsonString(Object srcObj) {
395 ObjectMapper mapper = new ObjectMapper();
396 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
397 String jsonStr = null;
399 jsonStr = mapper.writeValueAsString(srcObj);
400 } catch (JsonProcessingException e) {
401 LOGGER.debug("SdcToscaParserException", e);
407 private static String sendRequest(String url, String methodType, String content) {
410 HttpRequestBase method = null;
411 HttpResponse httpResponse = null;
414 int timeout = DEFAULT_TIME_OUT;
416 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout)
417 .setConnectionRequestTimeout(timeout).build();
419 HttpClient client = HttpClientBuilder.create().build();
421 if ("POST".equals(methodType.toUpperCase())) {
422 HttpPost httpPost = new HttpPost(msbUrl);
423 httpPost.setConfig(requestConfig);
424 httpPost.setEntity(new StringEntity(content, ContentType.APPLICATION_JSON));
426 } else if ("PUT".equals(methodType.toUpperCase())) {
427 HttpPut httpPut = new HttpPut(msbUrl);
428 httpPut.setConfig(requestConfig);
429 httpPut.setEntity(new StringEntity(content, ContentType.APPLICATION_JSON));
431 } else if ("GET".equals(methodType.toUpperCase())) {
432 HttpGet httpGet = new HttpGet(msbUrl);
433 httpGet.setConfig(requestConfig);
435 } else if ("DELETE".equals(methodType.toUpperCase())) {
436 HttpDelete httpDelete = new HttpDelete(msbUrl);
437 httpDelete.setConfig(requestConfig);
442 // String userCredentials =
443 // SDNCAdapterProperties.getEncryptedProperty(Constants.SDNC_AUTH_PROP,
444 // Constants.DEFAULT_SDNC_AUTH, Constants.ENCRYPTION_KEY);
445 // String authorization = "Basic " +
446 // DatatypeConverter.printBase64Binary(userCredentials.getBytes());
447 // method.setHeader("Authorization", authorization);
449 httpResponse = client.execute(method);
450 String responseContent = null;
451 if (null != httpResponse && httpResponse.getEntity() != null) {
453 responseContent = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
454 } catch (ParseException e) {
456 } catch (IOException e) {
460 if (null != method) {
464 return responseContent;
466 } catch (SocketTimeoutException | ConnectTimeoutException e) {
469 } catch (Exception e) {
473 if (httpResponse != null) {
475 EntityUtils.consume(httpResponse.getEntity());
476 } catch (Exception e) {
479 if (method != null) {
482 } catch (Exception e) {