2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.bpmn.infrastructure.workflow.serviceTask;
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;
37 import java.util.HashMap;
41 * Created by 10112215 on 2017/9/16.
43 public abstract class AbstractSdncOperationTask extends BaseTask {
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();
51 private static MsoLogger logger = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL);
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!");
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) {
65 execution.setVariable("SDNCA_SuccessIndicator", false);
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)));
78 public abstract void sendRestrequestAndHandleResponse(DelegateExecution execution,
79 Map<String, String> inputs,
80 GenericResourceApi genericResourceApiClient) throws Exception;
82 public void updateProgress(DelegateExecution execution,
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);
94 if (!StringUtils.isBlank(errorCode)) {
95 resourceOperationStatus.setErrorCode(errorCode);
97 if (!StringUtils.isBlank(progress)) {
98 resourceOperationStatus.setProgress(progress);
100 if (!StringUtils.isBlank(statusDescription)) {
101 resourceOperationStatus.setStatusDescription(statusDescription);
103 requestsDB.updateResOperStatus(resourceOperationStatus);
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);
116 private String getString(Map<String, String> properties, String name, String defaultValue) {
117 String vlaue = properties.get(name);
119 if (!StringUtils.isBlank(vlaue)) {
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());
129 private Integer getInteger(DelegateExecution execution, String name, Integer defaultValue) {
130 Integer vlaue = (Integer) execution.getVariable(name);
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());
142 public String getProcessKey(DelegateExecution execution) {
143 return execution.getProcessEngineServices().getRepositoryService().getProcessDefinition(execution.getProcessDefinitionId()).getKey();