Merge "Added fix for potential nullpointerexception"
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / java / org / onap / so / bpmn / infrastructure / workflow / service / ServicePluginFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.workflow.service;
22
23 import java.io.IOException;
24 import java.net.SocketTimeoutException;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Map.Entry;
30 import org.apache.commons.lang3.StringUtils;
31 import org.apache.http.HttpResponse;
32 import org.apache.http.ParseException;
33 import org.apache.http.client.HttpClient;
34 import org.apache.http.client.config.RequestConfig;
35 import org.apache.http.client.methods.HttpDelete;
36 import org.apache.http.client.methods.HttpGet;
37 import org.apache.http.client.methods.HttpPost;
38 import org.apache.http.client.methods.HttpPut;
39 import org.apache.http.client.methods.HttpRequestBase;
40 import org.apache.http.conn.ConnectTimeoutException;
41 import org.apache.http.entity.ContentType;
42 import org.apache.http.entity.StringEntity;
43 import org.apache.http.impl.client.HttpClientBuilder;
44 import org.apache.http.util.EntityUtils;
45 import org.camunda.bpm.engine.delegate.DelegateExecution;
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.onap.so.bpmn.common.scripts.AaiUtil;
54 import com.fasterxml.jackson.core.JsonProcessingException;
55 import com.fasterxml.jackson.databind.ObjectMapper;
56 import com.fasterxml.jackson.databind.SerializationFeature;
57
58 public class ServicePluginFactory {
59
60         // SOTN calculate route
61         public static final String OOF_DEFAULT_ENDPOINT = "http://192.168.1.223:8443/oof/sotncalc";
62
63         public static final String THIRD_SP_DEFAULT_ENDPOINT = "http://192.168.1.223:8443/sp/resourcemgr/querytps";
64         
65         public static final String INVENTORY_OSS_DEFAULT_ENDPOINT = "http://192.168.1.199:8443/oss/inventory";
66
67         private static final int DEFAULT_TIME_OUT = 60000;
68
69         static JsonUtils jsonUtil = new JsonUtils();
70
71         private static MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, ServicePluginFactory.class);
72
73         private static ServicePluginFactory instance;
74         
75
76         public static synchronized ServicePluginFactory getInstance() {
77                 if (null == instance) {
78                         instance = new ServicePluginFactory();
79                 }
80                 return instance;
81         }
82         
83         private String getInventoryOSSEndPoint(){
84                 return UrnPropertiesReader.getVariable("mso.service-plugin.inventory-oss-endpoint", INVENTORY_OSS_DEFAULT_ENDPOINT);
85         }
86         
87         private String getThirdSPEndPoint(){
88                 return UrnPropertiesReader.getVariable("mso.service-plugin.third-sp-endpoint", THIRD_SP_DEFAULT_ENDPOINT);
89         }
90
91         private String getOOFCalcEndPoint(){
92                 return UrnPropertiesReader.getVariable("mso.service-plugin.oof-calc-endpoint", OOF_DEFAULT_ENDPOINT);
93         }
94
95         @SuppressWarnings("unchecked")
96         public String doProcessSiteLocation(ServiceDecomposition serviceDecomposition, String uuiRequest) {
97                 if(!isNeedProcessSite(uuiRequest)) {
98                         return uuiRequest;
99                 }
100         
101                 Map<String, Object> uuiObject = getJsonObject(uuiRequest, Map.class);
102                 Map<String, Object> serviceObject = (Map<String, Object>) uuiObject.get("service");
103                 Map<String, Object> serviceParametersObject = (Map<String, Object>) serviceObject.get("parameters");
104                 Map<String, Object> serviceRequestInputs = (Map<String, Object>) serviceParametersObject.get("requestInputs");
105                 List<Object> resources = (List<Object>) serviceParametersObject.get("resources");       
106
107                 if (isSiteLocationLocal(serviceRequestInputs, resources)) {
108                         // resources changed : added TP info 
109                         String newRequest = getJsonString(uuiObject);
110                         return newRequest;
111                 }
112
113                 List<Resource> addResourceList = serviceDecomposition.getServiceResources();
114                 for (Resource resource : addResourceList) {
115                         String resourcemodelName = resource.getModelInfo().getModelName();
116                         if (!StringUtils.containsIgnoreCase(resourcemodelName, "sp-partner") 
117                                         || !StringUtils.containsIgnoreCase(resourcemodelName, "sppartner")) {
118                                 // change serviceDecomposition
119                                 serviceDecomposition.deleteResource(resource);
120                                 break;
121                         }
122                 }
123
124                 return uuiRequest;
125         }
126
127         private boolean isNeedProcessSite(String uuiRequest) {
128                 return uuiRequest.toLowerCase().contains("site_address") && uuiRequest.toLowerCase().contains("sotncondition_clientsignal");
129         }
130
131         @SuppressWarnings("unchecked")
132         private boolean isSiteLocationLocal(Map<String, Object> serviceRequestInputs, List<Object> resources) {         
133         Map<String, Object> tpInfoMap = getTPforVPNAttachment(serviceRequestInputs);    
134                         
135                 if(tpInfoMap.isEmpty()) {
136                         return true;
137                 }
138                 String host = (String) tpInfoMap.get("host");
139                 // host is empty means TP is in local, not empty means TP is in remote ONAP
140                 if (!host.isEmpty()) {
141                         return false;
142                 }
143                 
144                 Map<String, Object> accessTPInfo = new HashMap<String, Object>();
145                 accessTPInfo.put("access-provider-id", tpInfoMap.get("access-provider-id"));
146                 accessTPInfo.put("access-client-id", tpInfoMap.get("access-client-id"));
147                 accessTPInfo.put("access-topology-id", tpInfoMap.get("access-topology-id"));
148                 accessTPInfo.put("access-node-id", tpInfoMap.get("access-node-id"));
149                 accessTPInfo.put("access-ltp-id", tpInfoMap.get("access-ltp-id"));
150
151                 // change resources
152                 String resourceName = (String) accessTPInfo.get("resourceName");
153                 for(Object curResource : resources) {
154                         Map<String, Object> resource = (Map<String, Object>)curResource;
155                         String curResourceName = (String) resource.get("resourceName");
156                         curResourceName = curResourceName.replaceAll(" ", "");
157                         if(resourceName.equalsIgnoreCase(curResourceName)) { 
158                                 putResourceRequestInputs(resource, accessTPInfo);
159                                 break;
160                         }
161                 }
162
163                 return true;
164         }
165         
166         @SuppressWarnings("unchecked")
167         private Map<String, Object> getTPforVPNAttachment(Map<String, Object> serviceRequestInputs) {
168                 Object location = "";
169                 Object clientSignal = "";
170                 String vpnAttachmentResourceName = "";
171
172                 // support R2 uuiReq and R1 uuiReq
173                 // logic for R2 uuiRequest params in service level
174                 for (Entry<String, Object> entry : serviceRequestInputs.entrySet()) {
175                         String key = entry.getKey();
176                         if (key.toLowerCase().contains("site_address")) {                               
177                                 location = entry.getValue();
178                         } 
179                         if (key.toLowerCase().contains("sotncondition_clientsignal")) {                         
180                                 clientSignal = entry.getValue();
181                                 vpnAttachmentResourceName = key.substring(0, key.indexOf("_"));
182                         }
183                 }
184
185                 Map<String, Object> tpInfoMap =  new HashMap<String, Object>();
186                 
187                 // Site resource has location param and SOTNAttachment resource has clientSignal param
188                 if("".equals(location) || "".equals(clientSignal) ) {
189                         return tpInfoMap;
190                 }
191                 
192                 // Query terminal points from InventoryOSS system by location.          
193                 String locationAddress = (String) location;             
194                 List<Object> locationTPList = queryAccessTPbyLocationFromInventoryOSS(locationAddress);
195                 if(locationTPList != null && !locationTPList.isEmpty()) {
196                         tpInfoMap = (Map<String, Object>) locationTPList.get(0);
197                         // add resourceName
198                         tpInfoMap.put("resourceName", vpnAttachmentResourceName);
199                         LOGGER.debug("Get Terminal TP from InventoryOSS");
200                         return tpInfoMap;
201                 }
202                 
203                 return tpInfoMap;
204         }
205         
206         @SuppressWarnings("unchecked")
207         private List<Object> queryAccessTPbyLocationFromInventoryOSS(String locationAddress) {
208                 Map<String, String> locationSrc = new HashMap<String, String>();
209                 locationSrc.put("location", locationAddress);
210                 String reqContent = getJsonString(locationSrc);
211                 String url = getInventoryOSSEndPoint();
212                 String responseContent = sendRequest(url, "POST", reqContent);
213                 List<Object> accessTPs = new ArrayList<Object>();
214                 if (null != responseContent) {
215                         accessTPs = getJsonObject(responseContent, List.class);
216                 }
217                 return accessTPs;
218         }
219         
220         @SuppressWarnings("unchecked")
221         private void putResourceRequestInputs(Map<String, Object> resource, Map<String, Object> resourceInputs) {
222                 Map<String, Object> resourceParametersObject = new HashMap<String, Object>();
223                 Map<String, Object> resourceRequestInputs = new HashMap<String, Object>();
224                 resourceRequestInputs.put("requestInputs", resourceInputs);
225                 resourceParametersObject.put("parameters", resourceRequestInputs);
226
227                 if(resource.containsKey("parameters")) {
228                         Map<String, Object> resParametersObject = (Map<String, Object>) resource.get("parameters");
229                         if(resParametersObject.containsKey("requestInputs")) {
230                                 Map<String, Object> resRequestInputs = (Map<String, Object>) resourceParametersObject.get("requestInputs");
231                                 resRequestInputs.putAll(resourceInputs);                                
232                         }
233                         else {
234                                 resParametersObject.putAll(resourceRequestInputs);                              
235                         }
236                 }
237                 else {
238                         resource.putAll(resourceParametersObject);
239                 }
240
241                 return;
242         }
243         
244
245         
246         @SuppressWarnings("unchecked")
247         public String doTPResourcesAllocation(DelegateExecution execution, String uuiRequest) {         
248                 Map<String, Object> uuiObject = getJsonObject(uuiRequest, Map.class);
249                 Map<String, Object> serviceObject = (Map<String, Object>) uuiObject.get("service");
250                 Map<String, Object> serviceParametersObject = (Map<String, Object>) serviceObject.get("parameters");
251                 Map<String, Object> serviceRequestInputs = (Map<String, Object>) serviceParametersObject.get("requestInputs");
252                 
253                 if(!isNeedAllocateCrossTPResources(serviceRequestInputs)) {
254                         return uuiRequest;
255                 }
256                 
257                 allocateCrossTPResources(execution, serviceRequestInputs);
258                 String newRequest = getJsonString(uuiObject);
259                 return newRequest;
260         }
261
262         @SuppressWarnings("unchecked")
263         private boolean isNeedAllocateCrossTPResources(Map<String, Object> serviceRequestInputs) {
264                 if(serviceRequestInputs.containsKey("CallSource"))
265                 {
266                         String callSource = (String) serviceRequestInputs.get("CallSource");
267                         if("ExternalAPI".equalsIgnoreCase(callSource)) {
268                                 return false;
269                         }                                                       
270                 }                               
271                 return true;
272         }
273         
274         @SuppressWarnings("unchecked")
275         private void allocateCrossTPResources(DelegateExecution execution, Map<String, Object> serviceRequestInputs) {
276
277                 AaiUtil aai = new AaiUtil(null);
278                 Map<String, Object> crossTPs = aai.getTPsfromAAI(execution);
279                 
280                 if(crossTPs == null || crossTPs.isEmpty()) {
281                         serviceRequestInputs.put("local-access-provider-id", "");
282                         serviceRequestInputs.put("local-access-client-id", "");
283                         serviceRequestInputs.put("local-access-topology-id", "");
284                         serviceRequestInputs.put("local-access-node-id", "");
285                         serviceRequestInputs.put("local-access-ltp-id", "");
286                         serviceRequestInputs.put("remote-access-provider-id", "");
287                         serviceRequestInputs.put("remote-access-client-id", "");
288                         serviceRequestInputs.put("remote-access-topology-id", "");
289                         serviceRequestInputs.put("remote-access-node-id", "");
290                         serviceRequestInputs.put("remote-access-ltp-id", "");                   
291                 }
292                 else {
293                         serviceRequestInputs.put("local-access-provider-id", crossTPs.get("local-access-provider-id"));
294                         serviceRequestInputs.put("local-access-client-id", crossTPs.get("local-access-client-id"));
295                         serviceRequestInputs.put("local-access-topology-id", crossTPs.get("local-access-topology-id"));
296                         serviceRequestInputs.put("local-access-node-id", crossTPs.get("local-access-node-id"));
297                         serviceRequestInputs.put("local-access-ltp-id", crossTPs.get("local-access-ltp-id"));
298                         serviceRequestInputs.put("remote-access-provider-id", crossTPs.get("remote-access-provider-id"));
299                         serviceRequestInputs.put("remote-access-client-id", crossTPs.get("remote-client-id"));
300                         serviceRequestInputs.put("remote-access-topology-id", crossTPs.get("remote-topology-id"));
301                         serviceRequestInputs.put("remote-access-node-id", crossTPs.get("remote-node-id"));
302                         serviceRequestInputs.put("remote-access-ltp-id", crossTPs.get("remote-ltp-id"));
303                 }
304                 
305                 return;
306         }
307
308         public String preProcessService(ServiceDecomposition serviceDecomposition, String uuiRequest) {
309
310                 // now only for sotn
311                 if (isSOTN(serviceDecomposition, uuiRequest)) {
312                         // We Need to query the terminalpoint of the VPN by site location
313                         // info
314                         return preProcessSOTNService(serviceDecomposition, uuiRequest);
315                 }
316                 return uuiRequest;
317         }
318
319         public String doServiceHoming(ServiceDecomposition serviceDecomposition, String uuiRequest) {
320                 // now only for sotn
321                 if (isSOTN(serviceDecomposition, uuiRequest)) {
322                         return doSOTNServiceHoming(serviceDecomposition, uuiRequest);
323                 }
324                 return uuiRequest;
325         }
326
327         private boolean isSOTN(ServiceDecomposition serviceDecomposition, String uuiRequest) {
328                 // there should be a register platform , we check it very simple here.
329                 return uuiRequest.contains("clientSignal") && uuiRequest.contains("vpnType");
330         }
331
332         private String preProcessSOTNService(ServiceDecomposition serviceDecomposition, String uuiRequest) {
333                 Map<String, Object> uuiObject = getJsonObject(uuiRequest, Map.class);
334                 Map<String, Object> serviceObject = (Map<String, Object>) uuiObject.get("service");
335                 Map<String, Object> serviceParametersObject = (Map<String, Object>) serviceObject.get("parameters");
336                 Map<String, Object> serviceRequestInputs = (Map<String, Object>) serviceParametersObject.get("requestInputs");
337                 List<Object> resources = (List<Object>) serviceParametersObject.get("resources");
338                 // This is a logic for demo , it could not be finalized to community.
339                 String srcLocation = "";
340                 String dstLocation = "";
341                 String srcClientSignal = "";
342                 String dstClientSignal = "";
343                 // support R2 uuiReq and R1 uuiReq
344                 // logic for R2 uuiRequest params in service level
345                 for (Entry<String, Object> entry : serviceRequestInputs.entrySet()) {
346                         if (entry.getKey().toLowerCase().contains("location")) {
347                                 if ("".equals(srcLocation)) {
348                                         srcLocation = (String) entry.getValue();
349                                 } else if ("".equals(dstLocation)) {
350                                         dstLocation = (String) entry.getValue();
351                                 }
352                         }
353                         if (entry.getKey().toLowerCase().contains("clientsignal")) {
354                                 if ("".equals(srcClientSignal)) {
355                                         srcClientSignal = (String) entry.getValue();
356                                 } else if ("".equals(dstClientSignal)) {
357                                         dstClientSignal = (String) entry.getValue();
358                                 }
359                         }
360                 }
361
362                 // logic for R1 uuiRequest, params in resource level
363                 for (Object resource : resources) {
364                         Map<String, Object> resourceObject = (Map<String, Object>) resource;
365                         Map<String, Object> resourceParametersObject = (Map<String, Object>) resourceObject.get("parameters");
366                         Map<String, Object> resourceRequestInputs = (Map<String, Object>) resourceParametersObject.get("requestInputs");
367                         for (Entry<String, Object> entry : resourceRequestInputs.entrySet()) {
368                                 if (entry.getKey().toLowerCase().contains("location")) {
369                                         if ("".equals(srcLocation)) {
370                                                 srcLocation = (String) entry.getValue();
371                                         } else if ("".equals(dstLocation)) {
372                                                 dstLocation = (String) entry.getValue();
373                                         }
374                                 }
375                                 if (entry.getKey().toLowerCase().contains("clientsignal")) {
376                                         if ("".equals(srcClientSignal)) {
377                                                 srcClientSignal = (String) entry.getValue();
378                                         } else if ("".equals(dstClientSignal)) {
379                                                 dstClientSignal = (String) entry.getValue();
380                                         }
381                                 }
382                         }
383                 }
384
385                 Map<String, Object> vpnRequestInputs = getVPNResourceRequestInputs(resources);
386                 // here we put client signal to vpn resource inputs
387                 if(null!=vpnRequestInputs) {
388                         vpnRequestInputs.put("src-client-signal", srcClientSignal);
389                         vpnRequestInputs.put("dst-client-signal", dstClientSignal);
390                 }
391                 
392
393                 // Now we need to query terminal points from SP resourcemgr system.
394                 List<Object> locationTerminalPointList = queryTerminalPointsFromServiceProviderSystem(srcLocation, dstLocation);
395                 Map<String, Object> tpInfoMap = (Map<String, Object>) locationTerminalPointList.get(0);
396
397                 serviceRequestInputs.put("inner-src-access-provider-id", tpInfoMap.get("access-provider-id"));
398                 serviceRequestInputs.put("inner-src-access-client-id", tpInfoMap.get("access-client-id"));
399                 serviceRequestInputs.put("inner-src-access-topology-id", tpInfoMap.get("access-topology-id"));
400                 serviceRequestInputs.put("inner-src-access-node-id", tpInfoMap.get("access-node-id"));
401                 serviceRequestInputs.put("inner-src-access-ltp-id", tpInfoMap.get("access-ltp-id"));
402                 tpInfoMap = (Map<String, Object>) locationTerminalPointList.get(1);
403
404                 serviceRequestInputs.put("inner-dst-access-provider-id", tpInfoMap.get("access-provider-id"));
405                 serviceRequestInputs.put("inner-dst-access-client-id", tpInfoMap.get("access-client-id"));
406                 serviceRequestInputs.put("inner-dst-access-topology-id", tpInfoMap.get("access-topology-id"));
407                 serviceRequestInputs.put("inner-dst-access-node-id", tpInfoMap.get("access-node-id"));
408                 serviceRequestInputs.put("inner-dst-access-ltp-id", tpInfoMap.get("access-ltp-id"));
409
410                 String newRequest = getJsonString(uuiObject);
411                 return newRequest;
412         }
413
414         private List<Object> queryTerminalPointsFromServiceProviderSystem(String srcLocation, String dstLocation) {
415                 Map<String, String> locationSrc = new HashMap<String, String>();
416                 locationSrc.put("location", srcLocation);
417                 Map<String, String> locationDst = new HashMap<String, String>();
418                 locationDst.put("location", dstLocation);
419                 List<Map<String, String>> locations = new ArrayList<Map<String, String>>();
420                 locations.add(locationSrc);
421                 locations.add(locationDst);
422                 List<Object> returnList = new ArrayList<Object>();
423                 String reqContent = getJsonString(locations);
424                 String url = getThirdSPEndPoint();
425                 String responseContent = sendRequest(url, "POST", reqContent);
426                 if (null != responseContent) {
427                         returnList = getJsonObject(responseContent, List.class);
428                 }
429                 return returnList;
430         }
431
432         @SuppressWarnings("unchecked")
433         private Map<String, Object> getVPNResourceRequestInputs(List<Object> resources) {
434                 for (Object resource : resources) {
435                         Map<String, Object> resourceObject = (Map<String, Object>) resource;
436                         Map<String, Object> resourceParametersObject = (Map<String, Object>) resourceObject.get("parameters");
437                         Map<String, Object> resourceRequestInputs = (Map<String, Object>) resourceParametersObject.get("requestInputs");
438                         for (Entry<String, Object> entry : resourceRequestInputs.entrySet()) {
439                                 if (entry.getKey().toLowerCase().contains("vpntype")) {
440                                         return resourceRequestInputs;
441                                 }
442                         }
443                 }
444                 return null;
445         }
446         
447         public static void main(String args[]){
448                 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/";
449                 
450                 int index1 = str.indexOf("/network/");
451                 int index2 = str.indexOf("/network-data");
452                 
453                 String str1 = str.substring(index1 + "/network/".length(), index2);
454                 System.out.println(str1);
455                 
456         }
457
458         private String doSOTNServiceHoming(ServiceDecomposition serviceDecomposition, String uuiRequest) {
459                 // query the route for the service.
460                 Map<String, Object> uuiObject = getJsonObject(uuiRequest, Map.class);
461                 Map<String, Object> serviceObject = (Map<String, Object>) uuiObject.get("service");
462                 Map<String, Object> serviceParametersObject = (Map<String, Object>) serviceObject.get("parameters");
463                 Map<String, Object> serviceRequestInputs = (Map<String, Object>) serviceParametersObject.get("requestInputs");
464                 Map<String, Object> oofQueryObject = new HashMap<String, Object>();
465                 List<Object> resources = (List<Object>) serviceParametersObject.get("resources");
466                 oofQueryObject.put("src-access-provider-id", serviceRequestInputs.get("inner-src-access-provider-id"));
467                 oofQueryObject.put("src-access-client-id", serviceRequestInputs.get("inner-src-access-client-id"));
468                 oofQueryObject.put("src-access-topology-id", serviceRequestInputs.get("inner-src-access-topology-id"));
469                 oofQueryObject.put("src-access-node-id", serviceRequestInputs.get("inner-src-access-node-id"));
470                 oofQueryObject.put("src-access-ltp-id", serviceRequestInputs.get("inner-src-access-ltp-id"));
471                 oofQueryObject.put("dst-access-provider-id", serviceRequestInputs.get("inner-dst-access-provider-id"));
472                 oofQueryObject.put("dst-access-client-id", serviceRequestInputs.get("inner-dst-access-client-id"));
473                 oofQueryObject.put("dst-access-topology-id", serviceRequestInputs.get("inner-dst-access-topology-id"));
474                 oofQueryObject.put("dst-access-node-id", serviceRequestInputs.get("inner-dst-access-node-id"));
475                 oofQueryObject.put("dst-access-ltp-id", serviceRequestInputs.get("inner-dst-access-ltp-id"));
476                 String oofRequestReq = getJsonString(oofQueryObject);
477                 String url = getOOFCalcEndPoint();
478                 String responseContent = sendRequest(url, "POST", oofRequestReq);
479
480                 List<Object> returnList = new ArrayList<Object>();
481                 if (null != responseContent) {
482                         returnList = getJsonObject(responseContent, List.class);
483                 }
484                 // in demo we have only one VPN. no cross VPNs, so get first item.
485                 Map<String, Object> returnRoute = getReturnRoute(returnList);
486                 Map<String, Object> vpnRequestInputs = getVPNResourceRequestInputs(resources);
487                 vpnRequestInputs.putAll(returnRoute);
488                 String newRequest = getJsonString(uuiObject);
489                 return newRequest;
490         }
491         
492         private Map<String, Object> getReturnRoute(List<Object> returnList){
493                 Map<String, Object> returnRoute = new HashMap<String,Object>();
494                 for(Object returnVpn :returnList){
495                         Map<String, Object> returnVpnInfo = (Map<String, Object>) returnVpn;
496                     String accessTopoId = (String)returnVpnInfo.get("access-topology-id");
497                         if("100".equals(accessTopoId)){
498                                 returnRoute.putAll(returnVpnInfo);
499                         }
500                         else if("101".equals(accessTopoId)){
501                                 for(String key : returnVpnInfo.keySet()){
502                                         returnRoute.put("domain1-" + key, returnVpnInfo.get(key));
503                                 }
504                         }
505                         else if("102".equals(accessTopoId)){
506                                 for(String key : returnVpnInfo.keySet()){
507                                         returnRoute.put("domain2-" + key, returnVpnInfo.get(key));
508                                 }
509                         }
510                         else{
511                                 for(String key : returnVpnInfo.keySet()){
512                                         returnRoute.put("domain" + accessTopoId +"-" + key, returnVpnInfo.get(key));
513                                 }
514                         }
515                 }
516                 return returnRoute;
517         }
518
519         private Map<String, Object> getResourceParams(Execution execution, String resourceCustomizationUuid,
520                         String serviceParameters) {
521                 List<String> resourceList = jsonUtil.StringArrayToList(execution,
522                                 (String) JsonUtils.getJsonValue(serviceParameters, "resources"));
523                 // Get the right location str for resource. default is an empty array.
524                 String resourceInputsFromUui = "";
525                 for (String resource : resourceList) {
526                         String resCusUuid = (String) JsonUtils.getJsonValue(resource, "resourceCustomizationUuid");
527                         if (resourceCustomizationUuid.equals(resCusUuid)) {
528                                 String resourceParameters = JsonUtils.getJsonValue(resource, "parameters");
529                                 resourceInputsFromUui = JsonUtils.getJsonValue(resourceParameters, "requestInputs");
530                         }
531                 }
532                 Map<String, Object> resourceInputsFromUuiMap = getJsonObject(resourceInputsFromUui, Map.class);
533                 return resourceInputsFromUuiMap;
534         }
535
536         public static <T> T getJsonObject(String jsonstr, Class<T> type) {
537                 ObjectMapper mapper = new ObjectMapper();
538                 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
539                 try {
540                         return mapper.readValue(jsonstr, type);
541                 } catch (IOException e) {
542                         LOGGER.error(MessageEnum.RA_NS_EXC, "", "", MsoLogger.ErrorCode.BusinessProcesssError,
543                                         "fail to unMarshal json", e);
544                 }
545                 return null;
546         }
547
548         public static String getJsonString(Object srcObj) {
549                 ObjectMapper mapper = new ObjectMapper();
550                 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
551                 String jsonStr = null;
552                 try {
553                         jsonStr = mapper.writeValueAsString(srcObj);
554                 } catch (JsonProcessingException e) {
555                         LOGGER.debug("SdcToscaParserException", e);
556                 }
557                 return jsonStr;
558         }
559
560         private static String sendRequest(String url, String methodType, String content) {
561                 
562                 String msbUrl = url;
563                 HttpRequestBase method = null;
564                 HttpResponse httpResponse = null;
565
566                 try {
567                         int timeout = DEFAULT_TIME_OUT;
568
569                         RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout)
570                                         .setConnectionRequestTimeout(timeout).build();
571
572                         HttpClient client = HttpClientBuilder.create().build();
573
574                         if ("POST".equals(methodType.toUpperCase())) {
575                                 HttpPost httpPost = new HttpPost(msbUrl);
576                                 httpPost.setConfig(requestConfig);
577                                 httpPost.setEntity(new StringEntity(content, ContentType.APPLICATION_JSON));
578                                 method = httpPost;
579                         } else if ("PUT".equals(methodType.toUpperCase())) {
580                                 HttpPut httpPut = new HttpPut(msbUrl);
581                                 httpPut.setConfig(requestConfig);
582                                 httpPut.setEntity(new StringEntity(content, ContentType.APPLICATION_JSON));
583                                 method = httpPut;
584                         } else if ("GET".equals(methodType.toUpperCase())) {
585                                 HttpGet httpGet = new HttpGet(msbUrl);
586                                 httpGet.setConfig(requestConfig);
587                                 method = httpGet;
588                         } else if ("DELETE".equals(methodType.toUpperCase())) {
589                                 HttpDelete httpDelete = new HttpDelete(msbUrl);
590                                 httpDelete.setConfig(requestConfig);
591                                 method = httpDelete;
592                         }
593
594                         // now have no auth
595                         // String userCredentials =
596                         // SDNCAdapterProperties.getEncryptedProperty(Constants.SDNC_AUTH_PROP,
597                         // Constants.DEFAULT_SDNC_AUTH, Constants.ENCRYPTION_KEY);
598                         // String authorization = "Basic " +
599                         // DatatypeConverter.printBase64Binary(userCredentials.getBytes());
600                         // method.setHeader("Authorization", authorization);
601
602                         httpResponse = client.execute(method);
603                         String responseContent = null;
604                         if (null != httpResponse && httpResponse.getEntity() != null) {
605                                 try {
606                                         responseContent = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
607                                 } catch (ParseException e) {
608                                         LOGGER.debug("ParseException in sendrequest", e);
609                                 } catch (IOException e) {
610                                         LOGGER.debug("IOException in sendrequest", e);
611                                 }
612                         }
613                         if (null != method) {
614                                 method.reset();
615                         }
616                         method = null;
617                         return responseContent;
618
619                 } catch (SocketTimeoutException | ConnectTimeoutException e) {
620                         return null;
621
622                 } catch (Exception e) {
623                         return null;
624
625                 } finally {
626                         if (httpResponse != null) {
627                                 try {
628                                         EntityUtils.consume(httpResponse.getEntity());
629                                 } catch (Exception e) {
630                                 }
631                         }
632                         if (method != null) {
633                                 try {
634                                         method.reset();
635                                 } catch (Exception e) {
636
637                                 }
638                         }
639                 }
640         }
641 }