Replaced all tabs with spaces in java and pom.xml
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / java / org / onap / so / 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  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.infrastructure.workflow.serviceTask;
24
25 import java.util.Map;
26 import org.apache.commons.lang3.StringUtils;
27 import org.onap.msb.sdk.httpclient.RestServiceCreater;
28 import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
29 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.GenericResourceApi;
30 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.HeaderUtil;
31 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.builder.NetworkRpcInputEntityBuilder;
32 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.entity.RpcNetworkTopologyOperationInputEntity;
33 import org.onap.so.db.request.beans.ResourceOperationStatus;
34 import org.onap.so.db.request.beans.ResourceOperationStatusId;
35 import org.onap.so.requestsdb.RequestsDbConstant;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.stereotype.Component;
39
40 @Component
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
46     private static Logger logger = LoggerFactory.getLogger(SdncUnderlayVpnOperationClient.class);
47
48     public boolean excute(String msbIp, int msbPort, Map<String, String> inputs, String iServiceID, String iOperationID,
49             String resourceTemplateUUID_i) {
50         ResourceOperationStatusId id = new ResourceOperationStatusId(iServiceID, iOperationID, resourceTemplateUUID_i);
51         GenericResourceApi genericResourceApiClient = getGenericResourceApiClient(msbIp, msbPort);
52         updateProgress(id, RequestsDbConstant.Status.PROCESSING, null, "10", "execute begin!");
53         return sendRestrequestAndHandleResponse(id, inputs, genericResourceApiClient);
54     }
55
56     public boolean sendRestrequestAndHandleResponse(ResourceOperationStatusId id, Map<String, String> inputs,
57             GenericResourceApi genericResourceApiClient) {
58         updateProgress(id, null, null, "40", "sendRestrequestAndHandleResponse begin!");
59         NetworkRpcInputEntityBuilder builder = new NetworkRpcInputEntityBuilder();
60         RpcNetworkTopologyOperationInputEntity body = builder.build(null, inputs);
61         updateProgress(id, null, null, "50", "RequestBody build finished!");
62         // RpcNetworkTopologyOperationOutputEntity networkRpcOutputEntiy = null;
63         try {
64             genericResourceApiClient.postNetworkTopologyOperation(HeaderUtil.DefaulAuth, body).execute().body();
65         } catch (Exception e) {
66             logger.debug("Exception: ", e);
67             updateProgress(id, RequestsDbConstant.Status.ERROR, null, null,
68                     "sendRestrequestAndHandleResponse exception:" + e.getMessage());
69             return false;
70         }
71         updateProgress(id, null, null, "90", "sendRestrequestAndHandleResponse finished!");
72         updateProgress(id, RequestsDbConstant.Status.FINISHED, null, RequestsDbConstant.Progress.ONE_HUNDRED,
73                 "execute finished!");
74         return true;
75     }
76
77     private GenericResourceApi getGenericResourceApiClient(String msbIp, int msbPort) {
78         if (StringUtils.isBlank(msbIp)) {
79             msbIp = DEFAULT_MSB_IP;
80         }
81         if (msbPort <= 0) {
82             msbPort = DEFAULT_MSB_PORT;
83         }
84         MSBServiceClient msbClient = new MSBServiceClient(msbIp, msbPort);
85         RestServiceCreater restServiceCreater = new RestServiceCreater(msbClient);
86         return restServiceCreater.createService(GenericResourceApi.class);
87     }
88
89     public void updateProgress(ResourceOperationStatusId id, String status, String errorCode, String progress,
90             String statusDescription) {
91
92
93         ResourceOperationStatus resourceOperationStatus = new ResourceOperationStatus();// rosRepo.getOne(id);
94         if (!StringUtils.isBlank(status)) {
95             resourceOperationStatus.setStatus(status);
96         }
97         if (!StringUtils.isBlank(errorCode)) {
98             resourceOperationStatus.setErrorCode(errorCode);
99         }
100         if (!StringUtils.isBlank(progress)) {
101             resourceOperationStatus.setProgress(progress);
102         }
103         if (!StringUtils.isBlank(statusDescription)) {
104             resourceOperationStatus.setStatusDescription(statusDescription);
105         }
106         // rosRepo.save(resourceOperationStatus);
107     }
108
109     private void saveOutput() {
110         // Not implemented.
111     }
112 }