4495e8778925e106311359ba1b12152a340721e0
[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.camunda.bpm.engine.delegate.DelegateExecution;
25 import org.json.JSONObject;
26 import org.onap.msb.sdk.httpclient.RestServiceCreater;
27 import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
28 import org.openecomp.mso.bpmn.core.BaseTask;
29 import org.openecomp.mso.bpmn.core.PropertyConfiguration;
30 import org.openecomp.mso.bpmn.infrastructure.workflow.serviceTask.client.GenericResourceApi;
31 import org.openecomp.mso.logger.MessageEnum;
32 import org.openecomp.mso.logger.MsoLogger;
33 import org.openecomp.mso.requestsdb.RequestsDatabase;
34 import org.openecomp.mso.requestsdb.RequestsDbConstant;
35 import org.openecomp.mso.requestsdb.ResourceOperationStatus;
36
37 import java.util.HashMap;
38 import java.util.Map;
39
40 /**
41  * Created by 10112215 on 2017/9/16.
42  */
43 public abstract class AbstractSdncOperationTask extends BaseTask {
44
45     private static final String DEFAULT_MSB_IP = "127.0.0.1";
46     private static final int DEFAULT_MSB_Port = 10081;
47     private static final String SDCADAPTOR_INPUTS = "resourceParameters";
48     private RequestsDatabase requestsDB = RequestsDatabase.getInstance();
49
50
51     private static MsoLogger logger = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL);
52
53     @Override
54     public void execute(DelegateExecution execution) {
55         GenericResourceApi genericResourceApiClient = getGenericResourceApiClient(execution);
56         updateProgress(execution, RequestsDbConstant.Status.PROCESSING, null, "10", "execute begin!");
57         Map<String, String> inputs = getInputs(execution);
58         updateProgress(execution, null, null, "30", "getGenericResourceApiClient finished!");
59         try {
60             sendRestrequestAndHandleResponse(execution, inputs, genericResourceApiClient);
61             execution.setVariable("SDNCA_SuccessIndicator", true);
62             updateProgress(execution, RequestsDbConstant.Status.FINISHED, null, RequestsDbConstant.Progress.ONE_HUNDRED, "execute finished!");
63         } catch (Exception e) {
64             e.printStackTrace();
65             execution.setVariable("SDNCA_SuccessIndicator", false);
66         }
67     }
68
69     protected Map<String, String> getInputs(DelegateExecution execution) {
70         Map<String, String> inputs = new HashMap<>();
71         String json = (String) execution.getVariable(SDCADAPTOR_INPUTS);
72         JSONObject jsonObject = new JSONObject(json);
73         JSONObject paras = jsonObject.getJSONObject("additionalParamForNs");
74         paras.keySet().stream().forEach(key -> inputs.put(key, paras.getString((String) key)));
75         return inputs;
76     }
77
78     public abstract void sendRestrequestAndHandleResponse(DelegateExecution execution,
79                                                           Map<String, String> inputs,
80                                                           GenericResourceApi genericResourceApiClient) throws Exception;
81
82     public void updateProgress(DelegateExecution execution,
83                                String status,
84                                String errorCode,
85                                String progress,
86                                String statusDescription) {
87         String serviceId = (String) execution.getVariable("serviceId");
88         String operationId = (String) execution.getVariable("operationId");
89         String resourceTemplateUUID = (String) execution.getVariable("resourceTemplateUUID");
90         ResourceOperationStatus resourceOperationStatus = requestsDB.getResourceOperationStatus(serviceId, operationId, resourceTemplateUUID);
91         if (!StringUtils.isBlank(status)) {
92             resourceOperationStatus.setStatus(status);
93         }
94         if (!StringUtils.isBlank(errorCode)) {
95             resourceOperationStatus.setErrorCode(errorCode);
96         }
97         if (!StringUtils.isBlank(progress)) {
98             resourceOperationStatus.setProgress(progress);
99         }
100         if (!StringUtils.isBlank(statusDescription)) {
101             resourceOperationStatus.setStatusDescription(statusDescription);
102         }
103         requestsDB.updateResOperStatus(resourceOperationStatus);
104     }
105
106     private GenericResourceApi getGenericResourceApiClient(DelegateExecution execution) {
107         updateProgress(execution, null, null, "20", "getGenericResourceApiClient begin!");
108         Map<String, String> properties = PropertyConfiguration.getInstance().getProperties("mso.bpmn.urn.properties");
109         String msbIp = getString(properties, "msb.address", DEFAULT_MSB_IP);
110         int msbPort = Integer.valueOf(getString(properties, "msb.port", String.valueOf(DEFAULT_MSB_Port)));
111         MSBServiceClient msbClient = new MSBServiceClient(msbIp, msbPort);
112         RestServiceCreater restServiceCreater = new RestServiceCreater(msbClient);
113         return restServiceCreater.createService(GenericResourceApi.class);
114     }
115
116     private String getString(Map<String, String> properties, String name, String defaultValue) {
117         String vlaue = properties.get(name);
118         try {
119             if (!StringUtils.isBlank(vlaue)) {
120                 return vlaue;
121             }
122         } catch (Exception e) {
123             System.out.println(e);
124             logger.error(MessageEnum.GENERAL_EXCEPTION, " getMsbIp catch exception: ", "", this.getTaskName(), MsoLogger.ErrorCode.UnknownError, e.getClass().toString());
125         }
126         return defaultValue;
127     }
128
129     private Integer getInteger(DelegateExecution execution, String name, Integer defaultValue) {
130         Integer vlaue = (Integer) execution.getVariable(name);
131         try {
132             if (vlaue != null) {
133                 return vlaue;
134             }
135         } catch (Exception e) {
136             System.out.println(e);
137             logger.error(MessageEnum.GENERAL_EXCEPTION, " getMsbIp catch exception: ", "", this.getTaskName(), MsoLogger.ErrorCode.UnknownError, e.getClass().toString());
138         }
139         return defaultValue;
140     }
141
142     public String getProcessKey(DelegateExecution execution) {
143         return execution.getProcessEngineServices().getRepositoryService().getProcessDefinition(execution.getProcessDefinitionId()).getKey();
144     }
145 }