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