f54d6692d630a82ed543192325e12621aa25e11f
[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
27 import org.apache.commons.lang3.StringUtils;
28 import org.onap.msb.sdk.httpclient.RestServiceCreater;
29 import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
30 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.GenericResourceApi;
31 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.HeaderUtil;
32 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.builder.NetworkRpcInputEntityBuilder;
33 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.entity.RpcNetworkTopologyOperationInputEntity;
34 import org.onap.so.db.request.beans.ResourceOperationStatus;
35 import org.onap.so.db.request.beans.ResourceOperationStatusId;
36 import org.onap.so.requestsdb.RequestsDbConstant;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.stereotype.Component;
40
41 @Component
42 public class SdncUnderlayVpnOperationClient {
43
44     private static final String DEFAULT_MSB_IP = "127.0.0.1";
45     private static final int DEFAULT_MSB_PORT = 10081;
46
47     private static Logger logger = LoggerFactory.getLogger(SdncUnderlayVpnOperationClient.class);
48
49     public boolean excute(String msbIp,
50                        int msbPort,
51                        Map<String, String> inputs,
52                        String iServiceID,
53                        String iOperationID,
54                        String resourceTemplateUUID_i){
55         ResourceOperationStatusId id = new ResourceOperationStatusId(iServiceID, iOperationID, resourceTemplateUUID_i);
56         GenericResourceApi genericResourceApiClient = getGenericResourceApiClient(msbIp, msbPort);
57         updateProgress(id, RequestsDbConstant.Status.PROCESSING, null, "10", "execute begin!");
58         return sendRestrequestAndHandleResponse(id, inputs, genericResourceApiClient);
59     }
60
61     public boolean sendRestrequestAndHandleResponse(ResourceOperationStatusId id, Map<String, String> inputs, GenericResourceApi genericResourceApiClient){
62         updateProgress(id, null, null, "40", "sendRestrequestAndHandleResponse begin!");
63         NetworkRpcInputEntityBuilder builder = new NetworkRpcInputEntityBuilder();
64         RpcNetworkTopologyOperationInputEntity body = builder.build(null, inputs);
65         updateProgress(id, null, null, "50", "RequestBody build finished!");
66         //RpcNetworkTopologyOperationOutputEntity networkRpcOutputEntiy = null;
67         try {
68             genericResourceApiClient.postNetworkTopologyOperation(HeaderUtil.DefaulAuth ,body).execute().body();
69         } catch (Exception e) {
70             logger.debug("Exception: ", e);
71             updateProgress(id, RequestsDbConstant.Status.ERROR, null, null, "sendRestrequestAndHandleResponse exception:" + e.getMessage());
72             return false;
73         }
74         updateProgress(id, null, null, "90", "sendRestrequestAndHandleResponse finished!");
75         updateProgress(id, RequestsDbConstant.Status.FINISHED, null, RequestsDbConstant.Progress.ONE_HUNDRED, "execute finished!");
76         return true;
77     }
78
79     private GenericResourceApi getGenericResourceApiClient(String msbIp, int msbPort) {
80         if (StringUtils.isBlank(msbIp)) {
81             msbIp = DEFAULT_MSB_IP;
82         }
83         if (msbPort <= 0) {
84             msbPort = DEFAULT_MSB_PORT;
85         }
86         MSBServiceClient msbClient = new MSBServiceClient(msbIp, msbPort);
87         RestServiceCreater restServiceCreater = new RestServiceCreater(msbClient);
88         return restServiceCreater.createService(GenericResourceApi.class);
89     }
90
91     public void updateProgress(ResourceOperationStatusId id, String status,
92                                String errorCode,
93                                String progress,
94                                String statusDescription) {
95         
96         
97         ResourceOperationStatus resourceOperationStatus = new ResourceOperationStatus();//rosRepo.getOne(id);
98         if (!StringUtils.isBlank(status)) {
99             resourceOperationStatus.setStatus(status);
100         }
101         if (!StringUtils.isBlank(errorCode)) {
102             resourceOperationStatus.setErrorCode(errorCode);
103         }
104         if (!StringUtils.isBlank(progress)) {
105             resourceOperationStatus.setProgress(progress);
106         }
107         if (!StringUtils.isBlank(statusDescription)) {
108             resourceOperationStatus.setStatusDescription(statusDescription);
109         }
110         //rosRepo.save(resourceOperationStatus);
111     }
112
113     private void saveOutput() {
114         // Not implemented.
115     }
116 }