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