aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOInfrastructureBPMN/src/main/java/org/openecomp/mso/bpmn/infrastructure/workflow/serviceTask/AbstractSdncOperationTask.java
blob: 09dec4389ab7367bdd0d4e61ba1b5a4eff010ff6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.mso.bpmn.infrastructure.workflow.serviceTask;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.json.JSONObject;
import org.onap.msb.sdk.discovery.common.RouteException;
import org.onap.msb.sdk.httpclient.RestServiceCreater;
import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
import org.openecomp.mso.bpmn.core.BaseTask;
import org.openecomp.mso.bpmn.core.PropertyConfiguration;
import org.openecomp.mso.bpmn.infrastructure.workflow.serviceTask.client.GenericResourceApi;
import org.openecomp.mso.logger.MessageEnum;
import org.openecomp.mso.logger.MsoLogger;
import org.openecomp.mso.requestsdb.RequestsDatabase;
import org.openecomp.mso.requestsdb.ResourceOperationStatus;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 10112215 on 2017/9/16.
 */
public abstract class AbstractSdncOperationTask extends BaseTask {

    private static final String DEFAULT_MSB_IP = "127.0.0.1";
    private static final int DEFAULT_MSB_Port = 80;
    private static final String SDCADAPTOR_INPUTS = "resourceParameters";
    public static final String ONAP_IP = "ONAP_IP";
    private RequestsDatabase requestsDB = RequestsDatabase.getInstance();

    private static final String postBodyTemplate = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns=\"http://org.openecomp.mso/requestsdb\"><soapenv:Header/><soapenv:Body>\n"+
            "     <ns:updateResourceOperationStatus>\n"+
            "                <errorCode>$errorCode</errorCode>\n"+
            "                <jobId>$jobId</jobId>\n"+
            "                <operType>$operType</operType>\n"+
            "                <operationId>$operationId</operationId>\n"+
            "                <progress>$progress</progress>\n"+
            "                <resourceTemplateUUID>$resourceTemplateUUID</resourceTemplateUUID>\n"+
            "                <serviceId>$serviceId</serviceId>\n"+
            "                <status>$status</status>\n"+
            "                <statusDescription>$statusDescription</statusDescription>\n"+
            "     </ns:updateResourceOperationStatus></soapenv:Body></soapenv:Envelope>";


    private void updateResOperStatus(ResourceOperationStatus resourceOperationStatus) throws RouteException {
        String url = "http://mso:8080/dbadapters/RequestsDbAdapter";
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Authorization", "Basic QlBFTENsaWVudDpwYXNzd29yZDEk");
        httpPost.addHeader("Content-type", "application/soap+xml");
        String postBody = getStringBody(resourceOperationStatus);
        httpPost.setEntity(new StringEntity(postBody, ContentType.APPLICATION_XML));
        httpPost(url, httpPost);

        //requestsDB.updateResOperStatus(resourceOperationStatus);
    }

    protected String getPostbody(Object inputEntity) {
        ObjectMapper objectMapper = new ObjectMapper();
        String postBody = null;
        try {
            objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
            postBody = objectMapper.writeValueAsString(inputEntity);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return postBody;
    }

    protected void httpPost(String url, HttpPost httpPost) throws RouteException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String result;
        boolean var15 = false;

        String errorMsg;
        label91: {
            try {
                var15 = true;
                CloseableHttpResponse closeableHttpResponse = httpClient.execute(httpPost);
                result = EntityUtils.toString(closeableHttpResponse.getEntity());
                LOGGER.info(MessageEnum.RA_RESPONSE_FROM_SDNC, result.toString(), "SDNC", "");
                if(closeableHttpResponse.getStatusLine().getStatusCode() != 200) {
                    throw new RouteException(result, "SERVICE_GET_ERR");
                }

                closeableHttpResponse.close();
                var15 = false;
                break label91;
            } catch (IOException var19) {
                errorMsg = url + ":httpPostWithJSON connect faild";
                throwsRouteException(errorMsg, var19, "POST_CONNECT_FAILD");
                var15 = false;
            } finally {
                if(var15) {
                    try {
                        httpClient.close();
                    } catch (IOException var16) {
                        String errorMsg1 = url + ":close  httpClient faild";
                        throwsRouteException(errorMsg1, var16, "CLOSE_CONNECT_FAILD");
                    }

                }
            }

            try {
                httpClient.close();
            } catch (IOException var17) {
                errorMsg = url + ":close  httpClient faild";
                throwsRouteException(errorMsg, var17, "CLOSE_CONNECT_FAILD");
            }
        }

        try {
            httpClient.close();
        } catch (IOException var18) {
            errorMsg = url + ":close  httpClient faild";
            throwsRouteException(errorMsg, var18, "CLOSE_CONNECT_FAILD");
        }
    }

    private static void throwsRouteException(String errorMsg, Exception e, String errorCode) throws RouteException {
        String msg = errorMsg + ".errorMsg:" + e.getMessage();
        throw new RouteException(errorMsg, errorCode);
    }

    private String getStringBody(ResourceOperationStatus resourceOperationStatus) {
        String postBody = new String(postBodyTemplate);
        postBody.replace("$errorCode", resourceOperationStatus.getErrorCode());
        postBody.replace("$jobId", resourceOperationStatus.getJobId());
        postBody.replace("$operType", resourceOperationStatus.getOperType());
        postBody.replace("$operationId", resourceOperationStatus.getOperationId());
        postBody.replace("$progress", resourceOperationStatus.getProgress());
        postBody.replace("$resourceTemplateUUID", resourceOperationStatus.getResourceTemplateUUID());
        postBody.replace("$serviceId", resourceOperationStatus.getServiceId());
        postBody.replace("$status", resourceOperationStatus.getStatus());
        postBody.replace("$statusDescription", resourceOperationStatus.getStatusDescription());
        return postBody;
    }

    private ResourceOperationStatus getResourceOperationStatus(String serviceId, String operationId, String resourceTemplateUUID) throws RouteException {
        String url = "http://mso:8080/dbadapters/RequestsDbAdapter";
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("Authorization", "Basic QlBFTENsaWVudDpwYXNzd29yZDEk");
        httpGet.setHeader("Content-type", "application/soap+xml");
        String result = httpGet(url, httpGet);
        ResourceOperationStatus resourceOperationStatus = getResourceOperationStatusFromXmlString(result);

        return resourceOperationStatus;

        //return requestsDB.getResourceOperationStatus(serviceId, operationId, resourceTemplateUUID);
    }

    private String httpGet(String url, HttpGet httpGet) throws RouteException {
        boolean var16 = false;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String result="";
        String errorMsg;
        label109: {
            label110: {
                try {
                    var16 = true;
                    CloseableHttpResponse e = httpClient.execute(httpGet);
                    result = EntityUtils.toString(e.getEntity());
                    if(e.getStatusLine().getStatusCode() != 200) {
                        throw new RouteException(result, "SERVICE_GET_ERR");
                    }

                    e.close();
                    var16 = false;
                    break label110;
                } catch (ClientProtocolException var21) {
                    errorMsg = url + ":httpGetWithJSON connect faild";
                    throwsRouteException(errorMsg, var21, "GET_CONNECT_FAILD");
                    var16 = false;
                } catch (IOException var22) {
                    errorMsg = url + ":httpGetWithJSON connect faild";
                    throwsRouteException(errorMsg, var22, "GET_CONNECT_FAILD");
                    var16 = false;
                    break label109;
                } finally {
                    if(var16) {
                        try {
                            httpClient.close();
                        } catch (IOException var17) {
                            String errorMsg1 = url + ":close  httpClient faild";
                            throwsRouteException(errorMsg1, var17, "CLOSE_CONNECT_FAILD");
                        }

                    }
                }

                try {
                    httpClient.close();
                } catch (IOException var19) {
                    errorMsg = url + ":close  httpClient faild";
                    throwsRouteException(errorMsg, var19, "CLOSE_CONNECT_FAILD");
                }

            }

            try {
                httpClient.close();
            } catch (IOException var20) {
                errorMsg = url + ":close  httpClient faild";
                throwsRouteException(errorMsg, var20, "CLOSE_CONNECT_FAILD");
            }

        }

        try {
            httpClient.close();
        } catch (IOException var18) {
            errorMsg = url + ":close  httpClient faild";
            throwsRouteException(errorMsg, var18, "CLOSE_CONNECT_FAILD");
        }
        return result;
    }

    private ResourceOperationStatus getResourceOperationStatusFromXmlString(String result) {
        ResourceOperationStatus resourceOperationStatus = new ResourceOperationStatus();
        resourceOperationStatus.setErrorCode(getValueByName("errorCode", result));
        resourceOperationStatus.setJobId(getValueByName("jobId", result));
        resourceOperationStatus.setOperType(getValueByName("operType", result));
        resourceOperationStatus.setOperationId(getValueByName("operationId", result));
        resourceOperationStatus.setProgress(getValueByName("progress", result));
        resourceOperationStatus.setResourceTemplateUUID(getValueByName("resourceTemplateUUID", result));
        resourceOperationStatus.setServiceId(getValueByName("serviceId", result));
        resourceOperationStatus.setStatus(getValueByName("status", result));
        resourceOperationStatus.setStatusDescription(getValueByName("statusDescription", result));
        return resourceOperationStatus;
    }

    private String getValueByName(String Name, String xml) {
        String start = "<" + Name + ">";
        String end = "</" + Name + ">";
        return xml.substring(xml.indexOf(start), xml.indexOf(end)).replace(start, "");
    }

    protected static MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);

    @Override
    public void execute(DelegateExecution execution) {
        GenericResourceApi genericResourceApiClient = getGenericResourceApiClient(execution);
//        updateProgress(execution, RequestsDbConstant.Status.PROCESSING, null, "10", "execute begin!");
        Map<String, String> inputs = getInputs(execution);
//        updateProgress(execution, null, null, "30", "getGenericResourceApiClient finished!");
        try {
            sendRestrequestAndHandleResponse(execution, inputs, genericResourceApiClient);
            execution.setVariable("SDNCA_SuccessIndicator", true);
//            updateProgress(execution, RequestsDbConstant.Status.FINISHED, null, RequestsDbConstant.Progress.ONE_HUNDRED, "execute finished!");
        } catch (Exception e) {
            e.printStackTrace();
            execution.setVariable("SDNCA_SuccessIndicator", false);
        }
    }

    protected Map<String, String> getInputs(DelegateExecution execution) {
        Map<String, String> inputs = new HashMap<>();
        String json = (String) execution.getVariable(SDCADAPTOR_INPUTS);
        JSONObject jsonObject = new JSONObject(json);
        JSONObject paras = jsonObject.getJSONObject("additionalParamForNs");

        while (paras.keys().hasNext()) {
            String key = paras.keys().next();
            inputs.put(key, paras.getString(key));
        }
/*        if (paras.keys().hasNext()) {
            paras.keySet().stream().forEach(key -> inputs.put(key, paras.getString((String) key)));
        }*/
        return inputs;
    }

    public abstract void sendRestrequestAndHandleResponse(DelegateExecution execution,
                                                          Map<String, String> inputs,
                                                          GenericResourceApi genericResourceApiClient) throws Exception;

    public void updateProgress(DelegateExecution execution,
                               String status,
                               String errorCode,
                               String progress,
                               String statusDescription) {
        String serviceId = (String) execution.getVariable("serviceId");
        String operationId = (String) execution.getVariable("operationId");
        String resourceTemplateUUID = (String) execution.getVariable("resourceUUID");
        try {
            ResourceOperationStatus resourceOperationStatus = getResourceOperationStatus(serviceId, operationId, resourceTemplateUUID);
            if (!StringUtils.isBlank(status)) {
                resourceOperationStatus.setStatus(status);
            }
            if (!StringUtils.isBlank(errorCode)) {
                resourceOperationStatus.setErrorCode(errorCode);
            }
            if (!StringUtils.isBlank(progress)) {
                resourceOperationStatus.setProgress(progress);
            }
            if (!StringUtils.isBlank(statusDescription)) {
                resourceOperationStatus.setStatusDescription(statusDescription);
            }
            updateResOperStatus(resourceOperationStatus);
        } catch (Exception exception) {
            System.out.println(exception);
            LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " updateProgress catch exception: ", "", this.getTaskName(), MsoLogger.ErrorCode.UnknownError, exception.getClass().toString());
        }
    }


    protected boolean isSend2SdncDirectly() {
        Map<String, String> properties = PropertyConfiguration.getInstance().getProperties("topology.properties");
        if (properties != null) {
            String sdncIp = properties.get("sdnc-ip");
            String sdncPort = properties.get("sdnc-port");
            if (!StringUtils.isBlank(sdncIp) && isIp(sdncIp) && !StringUtils.isBlank(sdncPort)) {
                return true;
            }
        }
        return false;
    }

    protected String getSdncIp() {
        String sdncIp = null;
        Map<String, String> properties = PropertyConfiguration.getInstance().getProperties("topology.properties");
        if (properties != null) {
            sdncIp = properties.get("sdnc-ip");
        }
        return StringUtils.isBlank(sdncIp) || !isIp(sdncIp) ? null : sdncIp;
    }

    protected String getSdncPort() {
        String sdncIp = null;
        Map<String, String> properties = PropertyConfiguration.getInstance().getProperties("topology.properties");
        if (properties != null) {
            sdncIp = properties.get("sdnc-port");
        }
        return StringUtils.isBlank(sdncIp) ? null : sdncIp;
    }

    private GenericResourceApi getGenericResourceApiClient(DelegateExecution execution) {
//        updateProgress(execution, null, null, "20", "getGenericResourceApiClient begin!");
        String msbIp = System.getenv().get(ONAP_IP);
        int msbPort = DEFAULT_MSB_Port;
        Map<String, String> properties = PropertyConfiguration.getInstance().getProperties("topology.properties");
        if (properties != null) {
            if (StringUtils.isBlank(msbIp) || !isIp(msbIp)) {
                msbIp = properties.get("msb-ip");
                if (StringUtils.isBlank(msbIp)) {
                    msbIp = getString(properties, "msb.address", DEFAULT_MSB_IP);
                }
            }
            String strMsbPort = properties.get("msb-port");
            if (StringUtils.isBlank(strMsbPort)) {
                strMsbPort = getString(properties, "msb.port", String.valueOf(DEFAULT_MSB_Port));
            }
            msbPort = Integer.valueOf(strMsbPort);
        }
        MSBServiceClient msbClient = new MSBServiceClient(msbIp, msbPort);
        RestServiceCreater restServiceCreater = new RestServiceCreater(msbClient);
        return restServiceCreater.createService(GenericResourceApi.class);
    }

    protected boolean isIp(String msbIp) {
        return !StringUtils.isBlank(msbIp) && msbIp.split("\\.").length == 4;
    }

    private String getString(Map<String, String> properties, String name, String defaultValue) {
        String vlaue = properties.get(name);
        try {
            if (!StringUtils.isBlank(vlaue)) {
                return vlaue;
            }
        } catch (Exception e) {
            System.out.println(e);
            LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " getMsbIp catch exception: ", "", this.getTaskName(), MsoLogger.ErrorCode.UnknownError, e.getClass().toString());
        }
        return defaultValue;
    }

    private Integer getInteger(DelegateExecution execution, String name, Integer defaultValue) {
        Integer vlaue = (Integer) execution.getVariable(name);
        try {
            if (vlaue != null) {
                return vlaue;
            }
        } catch (Exception e) {
            System.out.println(e);
            LOGGER.error(MessageEnum.GENERAL_EXCEPTION, " getMsbIp catch exception: ", "", this.getTaskName(), MsoLogger.ErrorCode.UnknownError, e.getClass().toString());
        }
        return defaultValue;
    }

    public String getProcessKey(DelegateExecution execution) {
        return execution.getProcessEngineServices().getRepositoryService().getProcessDefinition(execution.getProcessDefinitionId()).getKey();
    }
}