2bca17a9cac6c55f34d6fa1ebca362a2c2058994
[so.git] /
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 public class SdncUnderlayVpnOperationClient {
39
40     private static final String DEFAULT_MSB_IP = "127.0.0.1";
41     private static final int DEFAULT_MSB_PORT = 10081;
42     private RequestsDatabase requestsDB = RequestsDatabase.getInstance();
43
44     private String serviceId;
45     private String operationId;
46     private String resourceTemplateUUID;
47
48
49     private static MsoLogger logger = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL);
50
51     public boolean excute(String msbIp,
52                        int msbPort,
53                        Map<String, String> inputs,
54                        String iServiceID,
55                        String iOperationID,
56                        String resourceTemplateUUID_i){
57         serviceId = iServiceID;
58         operationId = iOperationID;
59         resourceTemplateUUID = resourceTemplateUUID_i;
60         GenericResourceApi genericResourceApiClient = getGenericResourceApiClient(msbIp, msbPort);
61         updateProgress(RequestsDbConstant.Status.PROCESSING, null, "10", "execute begin!");
62         return sendRestrequestAndHandleResponse(inputs, genericResourceApiClient);
63     }
64
65     public boolean sendRestrequestAndHandleResponse(Map<String, String> inputs, GenericResourceApi genericResourceApiClient){
66         updateProgress(null, null, "40", "sendRestrequestAndHandleResponse begin!");
67         NetworkRpcInputEntityBuilder builder = new NetworkRpcInputEntityBuilder();
68         RpcNetworkTopologyOperationInputEntity body = builder.build(null, inputs);
69         updateProgress(null, null, "50", "RequestBody build finished!");
70         RpcNetworkTopologyOperationOutputEntity networkRpcOutputEntiy = null;
71         try {
72             genericResourceApiClient.postNetworkTopologyOperation(HeaderUtil.DefaulAuth ,body).execute().body();
73         } catch (Exception e) {
74             logger.debug("Exception: ", e);
75             updateProgress(RequestsDbConstant.Status.ERROR, null, null, "sendRestrequestAndHandleResponse exception:" + e.getMessage());
76             return false;
77         }
78         updateProgress(null, null, "90", "sendRestrequestAndHandleResponse finished!");
79         updateProgress(RequestsDbConstant.Status.FINISHED, null, RequestsDbConstant.Progress.ONE_HUNDRED, "execute finished!");
80         return true;
81     }
82
83     private GenericResourceApi getGenericResourceApiClient(String msbIp, int msbPort) {
84         if (StringUtils.isBlank(msbIp)) {
85             msbIp = DEFAULT_MSB_IP;
86         }
87         if (msbPort <= 0) {
88             msbPort = DEFAULT_MSB_PORT;
89         }
90         MSBServiceClient msbClient = new MSBServiceClient(msbIp, msbPort);
91         RestServiceCreater restServiceCreater = new RestServiceCreater(msbClient);
92         return restServiceCreater.createService(GenericResourceApi.class);
93     }
94
95     public void updateProgress(String status,
96                                String errorCode,
97                                String progress,
98                                String statusDescription) {
99         ResourceOperationStatus resourceOperationStatus = requestsDB.getResourceOperationStatus(serviceId, operationId, resourceTemplateUUID);
100         if (!StringUtils.isBlank(status)) {
101             resourceOperationStatus.setStatus(status);
102         }
103         if (!StringUtils.isBlank(errorCode)) {
104             resourceOperationStatus.setErrorCode(errorCode);
105         }
106         if (!StringUtils.isBlank(progress)) {
107             resourceOperationStatus.setProgress(progress);
108         }
109         if (!StringUtils.isBlank(statusDescription)) {
110             resourceOperationStatus.setStatusDescription(statusDescription);
111         }
112         requestsDB.updateResOperStatus(resourceOperationStatus);
113     }
114
115     private void saveOutput() {
116         // Not implemented.
117     }
118 }