AT&T 1712 and 1802 release code
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / java / org / openecomp / mso / bpmn / infrastructure / workflow / serviceTask / SdncServiceTopologyOperationTask.java
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
24 import org.apache.http.client.methods.HttpPost;
25 import org.apache.http.entity.ContentType;
26 import org.apache.http.entity.StringEntity;
27 import org.camunda.bpm.engine.delegate.DelegateExecution;
28 import org.onap.msb.sdk.discovery.common.RouteException;
29 import org.openecomp.mso.bpmn.core.WorkflowException;
30 import org.openecomp.mso.bpmn.infrastructure.workflow.serviceTask.client.GenericResourceApi;
31 import org.openecomp.mso.bpmn.infrastructure.workflow.serviceTask.client.HeaderUtil;
32 import org.openecomp.mso.bpmn.infrastructure.workflow.serviceTask.client.builder.ServiceRpcInputEntityBuilder;
33 import org.openecomp.mso.bpmn.infrastructure.workflow.serviceTask.client.entity.RpcServiceTopologyOperationInputEntity;
34 import org.openecomp.mso.bpmn.infrastructure.workflow.serviceTask.client.entity.RpcServiceTopologyOperationOutputEntity;
35 import org.openecomp.mso.logger.MessageEnum;
36 import org.openecomp.mso.requestsdb.RequestsDbConstant;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 import java.util.Map;
41
42 /**
43  * Created by 10112215 on 2017/9/26.
44  */
45 public class SdncServiceTopologyOperationTask extends AbstractSdncOperationTask {
46     private static final Logger sdncLogger = LoggerFactory.getLogger(SdncServiceTopologyOperationTask.class);
47
48
49     private static final String URL = "/restconf/operations/GENERIC-RESOURCE-API:service-topology-operation";
50
51     @Override
52     public void sendRestrequestAndHandleResponse(DelegateExecution execution,
53                                                  Map<String, String> inputs,
54                                                  GenericResourceApi genericResourceApiClient) throws Exception {
55         sdncLogger.info("SdncServiceTopologyOperationTask.sendRestrequestAndHandleResponse begin!");
56         updateProgress(execution, null, null, "40", "sendRestrequestAndHandleResponse begin!");
57         ServiceRpcInputEntityBuilder builder = new ServiceRpcInputEntityBuilder();
58         RpcServiceTopologyOperationInputEntity inputEntity = builder.build(execution, inputs);
59         updateProgress(execution, null, null, "50", "RequestBody build finished!");
60         RpcServiceTopologyOperationOutputEntity outputEntity;
61         if (!isSend2SdncDirectly()) {
62             outputEntity = genericResourceApiClient.postServiceTopologyOperation
63                     (HeaderUtil.DefaulAuth, inputEntity).execute().body();
64         updateProgress(execution, null, null, "90", "sendRestrequestAndHandleResponse finished!");
65         saveOutput(execution, outputEntity);
66         } else {
67             send2SdncDirectly(HeaderUtil.DefaulAuth, inputEntity);
68         }
69         sdncLogger.info("SdncServiceTopologyOperationTask.sendRestrequestAndHandleResponse end!");
70
71     }
72
73     private void send2SdncDirectly(String defaulAuth,
74                                    RpcServiceTopologyOperationInputEntity inputEntity) throws RouteException {
75         sdncLogger.info("SdncServiceTopologyOperationTask.send2SdncDirectly begin!");
76         String url = "http://" + getSdncIp() + ":" + getSdncPort() + URL;
77         HttpPost httpPost = new HttpPost(url);
78         httpPost.addHeader("Authorization", defaulAuth);
79         httpPost.addHeader("Content-type", "application/json");
80         String postBody = getPostbody(inputEntity);
81         msoLogger.info(MessageEnum.RA_SEND_REQUEST_SDNC, postBody, "SDNC", "");
82         httpPost.setEntity(new StringEntity(postBody, ContentType.APPLICATION_XML));
83         httpPost(url, httpPost);
84         sdncLogger.info("SdncServiceTopologyOperationTask.send2SdncDirectly end!");
85     }
86
87     private void saveOutput(DelegateExecution execution, RpcServiceTopologyOperationOutputEntity output) throws Exception {
88         sdncLogger.info("SdncServiceTopologyOperationTask.saveOutput begin!");
89         String responseCode = output.getOutput().getResponseCode();
90         if (!"200".equals(responseCode)) {
91             String processKey = getProcessKey(execution);
92             int errorCode = Integer.parseInt(responseCode);
93             String errorMessage = output.getOutput().getResponseMessage();
94             WorkflowException workflowException = new WorkflowException(processKey, errorCode, errorMessage);
95             execution.setVariable("SDNCA_SuccessIndicator", workflowException);
96             updateProgress(execution, RequestsDbConstant.Status.ERROR, String.valueOf(errorCode), null, errorMessage);
97             sdncLogger.info("exception: SdncServiceTopologyOperationTask.saveOutput fail!");
98             throw new RouteException();
99         }
100         sdncLogger.info("SdncServiceTopologyOperationTask.saveOutput end!");
101     }
102 }