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