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.onap.msb.sdk.httpclient.RestServiceCreater;
26 import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
27 import org.openecomp.mso.bpmn.infrastructure.workflow.serviceTask.client.GenericResourceApi;
28 import org.openecomp.mso.bpmn.core.BaseTask;
29 import org.openecomp.mso.logger.MessageEnum;
30 import org.openecomp.mso.logger.MsoLogger;
31 import org.openecomp.mso.requestsdb.RequestsDatabase;
32 import org.openecomp.mso.requestsdb.RequestsDbConstant;
33 import org.openecomp.mso.requestsdb.ResourceOperationStatus;
38 * Created by 10112215 on 2017/9/16.
40 public abstract class AbstractSdncOperationTask extends BaseTask {
42 private static final String DEFAULT_MSB_IP = "127.0.0.1";
43 private static final int DEFAULT_MSB_Port = 10081;
44 private static final String SDCADAPTOR_INPUTS = "SDCADAPTOR_INPUTS";
45 private RequestsDatabase requestsDB = RequestsDatabase.getInstance();
48 private static MsoLogger logger = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL);
51 public void execute(DelegateExecution execution) {
52 GenericResourceApi genericResourceApiClient = getGenericResourceApiClient(execution);
53 updateProgress(execution, RequestsDbConstant.Status.PROCESSING, null, "10", "execute begin!");
54 Map<String, String> inputs = (Map<String, String>) execution.getVariable(SDCADAPTOR_INPUTS);
55 updateProgress(execution, null, null, "30", "getGenericResourceApiClient finished!");
57 sendRestrequestAndHandleResponse(execution, inputs, genericResourceApiClient);
58 execution.setVariable("SDNCA_SuccessIndicator", true);
59 updateProgress(execution, RequestsDbConstant.Status.FINISHED, null, RequestsDbConstant.Progress.ONE_HUNDRED, "execute finished!");
60 } catch (Exception e) {
62 execution.setVariable("SDNCA_SuccessIndicator", false);
66 public abstract void sendRestrequestAndHandleResponse(DelegateExecution execution,
67 Map<String, String> inputs,
68 GenericResourceApi genericResourceApiClient) throws Exception;
70 public void updateProgress(DelegateExecution execution,
74 String statusDescription) {
75 String serviceId = (String) execution.getVariable("serviceId");
76 String operationId = (String) execution.getVariable("operationId");
77 String resourceTemplateUUID = (String) execution.getVariable("resourceTemplateUUID");
78 ResourceOperationStatus resourceOperationStatus = requestsDB.getResourceOperationStatus(serviceId, operationId, resourceTemplateUUID);
79 if (!StringUtils.isBlank(status)) {
80 resourceOperationStatus.setStatus(status);
82 if (!StringUtils.isBlank(errorCode)) {
83 resourceOperationStatus.setErrorCode(errorCode);
85 if (!StringUtils.isBlank(progress)) {
86 resourceOperationStatus.setProgress(progress);
88 if (!StringUtils.isBlank(statusDescription)) {
89 resourceOperationStatus.setStatusDescription(statusDescription);
91 requestsDB.updateResOperStatus(resourceOperationStatus);
94 private GenericResourceApi getGenericResourceApiClient(DelegateExecution execution) {
95 updateProgress(execution, null, null, "20", "getGenericResourceApiClient begin!");
96 String msbIp = getString(execution, "MSB_IP", DEFAULT_MSB_IP);
97 int msbPort = getInteger(execution, "MSB_Port", DEFAULT_MSB_Port);
98 MSBServiceClient msbClient = new MSBServiceClient(msbIp, msbPort);
99 RestServiceCreater restServiceCreater = new RestServiceCreater(msbClient);
100 return restServiceCreater.createService(GenericResourceApi.class);
103 private String getString(DelegateExecution execution, String name, String defaultValue) {
104 String vlaue = (String) execution.getVariable(name);
106 if (!StringUtils.isBlank(vlaue)) {
109 } catch (Exception e) {
110 System.out.println(e);
111 logger.error(MessageEnum.GENERAL_EXCEPTION, " getMsbIp catch exception: ", "", this.getTaskName(), MsoLogger.ErrorCode.UnknownError, e.getClass().toString());
116 private Integer getInteger(DelegateExecution execution, String name, Integer defaultValue) {
117 Integer vlaue = (Integer) execution.getVariable(name);
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 public String getProcessKey(DelegateExecution execution) {
130 return execution.getProcessEngineServices().getRepositoryService().getProcessDefinition(execution.getProcessDefinitionId()).getKey();