c52c3b2f1473ac6d4afb823c40be50618ca0f25c
[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 = 80;
47     private static final String SDCADAPTOR_INPUTS = "resourceParameters";
48     public static final String ONAP_IP = "ONAP_IP";
49     private RequestsDatabase requestsDB = RequestsDatabase.getInstance();
50
51
52     private static MsoLogger logger = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL);
53
54     @Override
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!");
60         try {
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) {
65             e.printStackTrace();
66             execution.setVariable("SDNCA_SuccessIndicator", false);
67         }
68     }
69
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)));
76         return inputs;
77     }
78
79     public abstract void sendRestrequestAndHandleResponse(DelegateExecution execution,
80                                                           Map<String, String> inputs,
81                                                           GenericResourceApi genericResourceApiClient) throws Exception;
82
83     public void updateProgress(DelegateExecution execution,
84                                String status,
85                                String errorCode,
86                                String progress,
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);
94         }
95         if (!StringUtils.isBlank(errorCode)) {
96             resourceOperationStatus.setErrorCode(errorCode);
97         }
98         if (!StringUtils.isBlank(progress)) {
99             resourceOperationStatus.setProgress(progress);
100         }
101         if (!StringUtils.isBlank(statusDescription)) {
102             resourceOperationStatus.setStatusDescription(statusDescription);
103         }
104         requestsDB.updateResOperStatus(resourceOperationStatus);
105     }
106
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);
115             }
116             msbPort = Integer.valueOf(getString(properties, "msb.port", String.valueOf(DEFAULT_MSB_Port)));
117         }
118         MSBServiceClient msbClient = new MSBServiceClient(msbIp, msbPort);
119         RestServiceCreater restServiceCreater = new RestServiceCreater(msbClient);
120         return restServiceCreater.createService(GenericResourceApi.class);
121     }
122
123     private String getString(Map<String, String> properties, String name, String defaultValue) {
124         String vlaue = properties.get(name);
125         try {
126             if (!StringUtils.isBlank(vlaue)) {
127                 return vlaue;
128             }
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());
132         }
133         return defaultValue;
134     }
135
136     private Integer getInteger(DelegateExecution execution, String name, Integer defaultValue) {
137         Integer vlaue = (Integer) execution.getVariable(name);
138         try {
139             if (vlaue != null) {
140                 return vlaue;
141             }
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());
145         }
146         return defaultValue;
147     }
148
149     public String getProcessKey(DelegateExecution execution) {
150         return execution.getProcessEngineServices().getRepositoryService().getProcessDefinition(execution.getProcessDefinitionId()).getKey();
151     }
152 }