Containerization feature of SO
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / java / org / onap / so / 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.onap.so.bpmn.infrastructure.workflow.serviceTask;
22
23 import java.util.Map;
24
25 import org.apache.http.client.methods.HttpPost;
26 import org.apache.http.entity.ContentType;
27 import org.apache.http.entity.StringEntity;
28 import org.camunda.bpm.engine.delegate.DelegateExecution;
29 import org.onap.msb.sdk.discovery.common.RouteException;
30 import org.onap.so.bpmn.core.WorkflowException;
31 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.GenericResourceApi;
32 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.HeaderUtil;
33 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.builder.NetworkRpcInputEntityBuilder;
34 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.entity.RpcNetworkTopologyOperationInputEntity;
35 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.entity.RpcNetworkTopologyOperationOutputEntity;
36 import org.onap.so.logger.MessageEnum;
37 import org.onap.so.requestsdb.RequestsDbConstant;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.stereotype.Component;
41
42 @Component
43 public class SdncNetworkTopologyOperationTask extends AbstractSdncOperationTask {
44     private static final Logger sdncLogger = LoggerFactory.getLogger(SdncNetworkTopologyOperationTask.class);
45
46
47     private static final String URL = "/restconf/operations/GENERIC-RESOURCE-API:network-topology-operation";
48
49     @Override
50     public void sendRestrequestAndHandleResponse(DelegateExecution execution,
51                                                  Map<String, String> inputs,
52                                                  GenericResourceApi genericResourceApiClient) throws Exception {
53         sdncLogger.info("SdncNetworkTopologyOperationTask.sendRestrequestAndHandleResponse begin!");
54         updateProgress(execution, RequestsDbConstant.Status.PROCESSING, null, "40", "sendRestrequestAndHandleResponse begin!");
55         NetworkRpcInputEntityBuilder builder = new NetworkRpcInputEntityBuilder();
56         RpcNetworkTopologyOperationInputEntity inputEntity = builder.build(execution, inputs);
57         updateProgress(execution, RequestsDbConstant.Status.PROCESSING, null, "50", "RequestBody build finished!");
58         RpcNetworkTopologyOperationOutputEntity outputEntity;
59         if (!isSend2SdncDirectly()) {
60             outputEntity = genericResourceApiClient.postNetworkTopologyOperation
61                     (HeaderUtil.DefaulAuth, inputEntity).execute().body();
62         updateProgress(execution, null, null, "90", "sendRestrequestAndHandleResponse finished!");
63         saveOutput(execution, outputEntity);
64         } else {
65             send2SdncDirectly(HeaderUtil.DefaulAuth, inputEntity);
66         }
67         updateProgress(execution, RequestsDbConstant.Status.FINISHED, null, RequestsDbConstant.Progress.ONE_HUNDRED, "execute finished!");
68         sdncLogger.info("SdncNetworkTopologyOperationTask.sendRestrequestAndHandleResponse end!");
69     }
70
71     private void send2SdncDirectly(String defaulAuth,
72                                    RpcNetworkTopologyOperationInputEntity inputEntity) throws RouteException {
73         sdncLogger.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         msoLogger.info(MessageEnum.RA_SEND_REQUEST_SDNC, postBody, "SDNC", "");
80         httpPost.setEntity(new StringEntity(postBody, ContentType.APPLICATION_XML));
81         httpPost(url, httpPost);
82         sdncLogger.info("SdncNetworkTopologyOperationTask.send2SdncDirectly end!");
83     }
84
85     private void saveOutput(DelegateExecution execution, RpcNetworkTopologyOperationOutputEntity output) throws RouteException {
86         sdncLogger.info("SdncNetworkTopologyOperationTask.saveOutput begin!");
87         String responseCode = output.getOutput().getResponseCode();
88         if (!"200".equals(responseCode)) {
89             String processKey = getProcessKey(execution);
90             int errorCode = Integer.parseInt(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), "100", errorMessage);
95             sdncLogger.info("exception: SdncNetworkTopologyOperationTask.saveOutput fail!");
96             throw new RouteException();
97         }
98
99         sdncLogger.info("SdncNetworkTopologyOperationTask.saveOutput end!");
100     }
101
102 }