Merge "Fixed string literal comparision issue"
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / java / org / openecomp / mso / bpmn / infrastructure / workflow / serviceTask / SdncUnderlayVpnOperationClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T 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.openecomp.mso.bpmn.infrastructure.workflow.serviceTask;
22
23 import org.apache.commons.lang3.StringUtils;
24 import org.onap.msb.sdk.httpclient.RestServiceCreater;
25 import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
26 import org.openecomp.mso.bpmn.infrastructure.workflow.serviceTask.client.GenericResourceApi;
27 import org.openecomp.mso.bpmn.infrastructure.workflow.serviceTask.client.builder.NetworkRpcInputEntityBuilder;
28 import org.openecomp.mso.bpmn.infrastructure.workflow.serviceTask.client.entity.RpcNetworkTopologyOperationInputEntity;
29 import org.openecomp.mso.bpmn.infrastructure.workflow.serviceTask.client.entity.RpcNetworkTopologyOperationOutputEntity;
30 import org.openecomp.mso.logger.MsoLogger;
31 import org.openecomp.mso.requestsdb.RequestsDatabase;
32 import org.openecomp.mso.requestsdb.RequestsDbConstant;
33 import org.openecomp.mso.requestsdb.ResourceOperationStatus;
34
35 import java.util.Map;
36
37 /**
38  * Created by 10112215 on 2017/9/21.
39  */
40 public class SdncUnderlayVpnOperationClient {
41
42     private static final String DEFAULT_MSB_IP = "127.0.0.1";
43     private static final int DEFAULT_MSB_Port = 10081;
44     private RequestsDatabase requestsDB = RequestsDatabase.getInstance();
45
46     private String serviceId;
47     private String operationId;
48     private String resourceTemplateUUID;
49
50
51     private static MsoLogger logger = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL);
52
53     public boolean excute(String msbIp,
54                        int msbPort,
55                        Map<String, String> inputs,
56                        String serviceId_i,
57                        String operationId_i,
58                        String resourceTemplateUUID_i){
59         serviceId = serviceId_i;
60         operationId = operationId_i;
61         resourceTemplateUUID = resourceTemplateUUID_i;
62         GenericResourceApi genericResourceApiClient = getGenericResourceApiClient(msbIp, msbPort);
63         updateProgress(RequestsDbConstant.Status.PROCESSING, null, "10", "execute begin!");
64         return sendRestrequestAndHandleResponse(inputs, genericResourceApiClient);
65     }
66
67     public boolean sendRestrequestAndHandleResponse(Map<String, String> inputs, GenericResourceApi genericResourceApiClient){
68         updateProgress(null, null, "40", "sendRestrequestAndHandleResponse begin!");
69         NetworkRpcInputEntityBuilder builder = new NetworkRpcInputEntityBuilder();
70         RpcNetworkTopologyOperationInputEntity body = builder.build(null, inputs);
71         updateProgress(null, null, "50", "RequestBody build finished!");
72         RpcNetworkTopologyOperationOutputEntity networkRpcOutputEntiy = null;
73         try {
74             networkRpcOutputEntiy = genericResourceApiClient.postNetworkTopologyOperation(body).execute().body();
75         } catch (Exception e) {
76             e.printStackTrace();
77             updateProgress(RequestsDbConstant.Status.ERROR, null, null, "sendRestrequestAndHandleResponse exception:" + e.getMessage());
78             return false;
79         }
80         updateProgress(null, null, "90", "sendRestrequestAndHandleResponse finished!");
81         saveOutput(networkRpcOutputEntiy);
82         updateProgress(RequestsDbConstant.Status.FINISHED, null, RequestsDbConstant.Progress.ONE_HUNDRED, "execute finished!");
83         return true;
84     }
85
86     private GenericResourceApi getGenericResourceApiClient(String msbIp, int msbPort) {
87         if (StringUtils.isBlank(msbIp)) {
88             msbIp = DEFAULT_MSB_IP;
89         }
90         if (msbPort <= 0) {
91             msbPort = DEFAULT_MSB_Port;
92         }
93         MSBServiceClient msbClient = new MSBServiceClient(msbIp, msbPort);
94         RestServiceCreater restServiceCreater = new RestServiceCreater(msbClient);
95         return restServiceCreater.createService(GenericResourceApi.class);
96     }
97
98     public void updateProgress(String status,
99                                String errorCode,
100                                String progress,
101                                String statusDescription) {
102         ResourceOperationStatus resourceOperationStatus = requestsDB.getResourceOperationStatus(serviceId, operationId, resourceTemplateUUID);
103         if (!StringUtils.isBlank(status)) {
104             resourceOperationStatus.setStatus(status);
105         }
106         if (!StringUtils.isBlank(errorCode)) {
107             resourceOperationStatus.setErrorCode(errorCode);
108         }
109         if (!StringUtils.isBlank(progress)) {
110             resourceOperationStatus.setProgress(progress);
111         }
112         if (!StringUtils.isBlank(statusDescription)) {
113             resourceOperationStatus.setStatusDescription(statusDescription);
114         }
115         requestsDB.updateResOperStatus(resourceOperationStatus);
116     }
117
118     private void saveOutput(RpcNetworkTopologyOperationOutputEntity output) {
119
120     }
121 }