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