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 = 80;
47 private static final String SDCADAPTOR_INPUTS = "resourceParameters";
48 public static final String ONAP_IP = "ONAP_IP";
49 private RequestsDatabase requestsDB = RequestsDatabase.getInstance();
52 private static MsoLogger logger = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL);
55 public void execute(DelegateExecution execution) {
56 GenericResourceApi genericResourceApiClient = getGenericResourceApiClient(execution);
57 updateProgress(execution, RequestsDbConstant.Status.PROCESSING, null, "10", "execute begin!");
58 Map<String, String> inputs = getInputs(execution);
59 updateProgress(execution, null, null, "30", "getGenericResourceApiClient finished!");
61 sendRestrequestAndHandleResponse(execution, inputs, genericResourceApiClient);
62 execution.setVariable("SDNCA_SuccessIndicator", true);
63 updateProgress(execution, RequestsDbConstant.Status.FINISHED, null, RequestsDbConstant.Progress.ONE_HUNDRED, "execute finished!");
64 } catch (Exception e) {
66 execution.setVariable("SDNCA_SuccessIndicator", false);
70 protected Map<String, String> getInputs(DelegateExecution execution) {
71 Map<String, String> inputs = new HashMap<>();
72 String json = (String) execution.getVariable(SDCADAPTOR_INPUTS);
73 JSONObject jsonObject = new JSONObject(json);
74 JSONObject paras = jsonObject.getJSONObject("additionalParamForNs");
75 paras.keySet().stream().forEach(key -> inputs.put(key, paras.getString((String) key)));
79 public abstract void sendRestrequestAndHandleResponse(DelegateExecution execution,
80 Map<String, String> inputs,
81 GenericResourceApi genericResourceApiClient) throws Exception;
83 public void updateProgress(DelegateExecution execution,
87 String statusDescription) {
88 String serviceId = (String) execution.getVariable("serviceId");
89 String operationId = (String) execution.getVariable("operationId");
90 String resourceTemplateUUID = (String) execution.getVariable("resourceTemplateUUID");
91 ResourceOperationStatus resourceOperationStatus = requestsDB.getResourceOperationStatus(serviceId, operationId, resourceTemplateUUID);
92 if (!StringUtils.isBlank(status)) {
93 resourceOperationStatus.setStatus(status);
95 if (!StringUtils.isBlank(errorCode)) {
96 resourceOperationStatus.setErrorCode(errorCode);
98 if (!StringUtils.isBlank(progress)) {
99 resourceOperationStatus.setProgress(progress);
101 if (!StringUtils.isBlank(statusDescription)) {
102 resourceOperationStatus.setStatusDescription(statusDescription);
104 requestsDB.updateResOperStatus(resourceOperationStatus);
107 private GenericResourceApi getGenericResourceApiClient(DelegateExecution execution) {
108 updateProgress(execution, null, null, "20", "getGenericResourceApiClient begin!");
109 String msbIp = System.getenv().get(ONAP_IP);
110 int msbPort = DEFAULT_MSB_Port;
111 Map<String, String> properties = PropertyConfiguration.getInstance().getProperties("mso.bpmn.urn.properties");
112 if (properties != null) {
113 if (StringUtils.isBlank(msbIp)) {
114 msbIp = getString(properties, "msb.address", DEFAULT_MSB_IP);
116 msbPort = Integer.valueOf(getString(properties, "msb.port", String.valueOf(DEFAULT_MSB_Port)));
118 MSBServiceClient msbClient = new MSBServiceClient(msbIp, msbPort);
119 RestServiceCreater restServiceCreater = new RestServiceCreater(msbClient);
120 return restServiceCreater.createService(GenericResourceApi.class);
123 private String getString(Map<String, String> properties, String name, String defaultValue) {
124 String vlaue = properties.get(name);
126 if (!StringUtils.isBlank(vlaue)) {
129 } catch (Exception e) {
130 System.out.println(e);
131 logger.error(MessageEnum.GENERAL_EXCEPTION, " getMsbIp catch exception: ", "", this.getTaskName(), MsoLogger.ErrorCode.UnknownError, e.getClass().toString());
136 private Integer getInteger(DelegateExecution execution, String name, Integer defaultValue) {
137 Integer vlaue = (Integer) execution.getVariable(name);
142 } catch (Exception e) {
143 System.out.println(e);
144 logger.error(MessageEnum.GENERAL_EXCEPTION, " getMsbIp catch exception: ", "", this.getTaskName(), MsoLogger.ErrorCode.UnknownError, e.getClass().toString());
149 public String getProcessKey(DelegateExecution execution) {
150 return execution.getProcessEngineServices().getRepositoryService().getProcessDefinition(execution.getProcessDefinitionId()).getKey();