4703dddf542c1964d9c56b393494915d1ff5e04d
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.infrastructure.workflow.serviceTask;
24
25 import java.util.Map;
26 import com.google.common.base.Strings;
27 import org.apache.http.client.methods.HttpPost;
28 import org.apache.http.entity.ContentType;
29 import org.apache.http.entity.StringEntity;
30 import org.camunda.bpm.engine.delegate.DelegateExecution;
31 import org.onap.msb.sdk.discovery.common.RouteException;
32 import org.onap.so.bpmn.core.WorkflowException;
33 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.GenericResourceApi;
34 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.HeaderUtil;
35 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.builder.NetworkRpcInputEntityBuilder;
36 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.entity.RpcNetworkTopologyOperationInputEntity;
37 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.entity.RpcNetworkTopologyOperationOutputEntity;
38 import org.onap.so.logger.MessageEnum;
39 import org.onap.so.requestsdb.RequestsDbConstant;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.stereotype.Component;
43
44 @Component
45 public class SdncNetworkTopologyOperationTask extends AbstractSdncOperationTask {
46     private static final Logger logger = LoggerFactory.getLogger(SdncNetworkTopologyOperationTask.class);
47
48
49     private static final String URL = "/restconf/operations/GENERIC-RESOURCE-API:network-topology-operation";
50
51     @Override
52     public void sendRestrequestAndHandleResponse(DelegateExecution execution, Map<String, String> inputs,
53             GenericResourceApi genericResourceApiClient) throws Exception {
54         logger.info("SdncNetworkTopologyOperationTask.sendRestrequestAndHandleResponse begin!");
55         updateProgress(execution, RequestsDbConstant.Status.PROCESSING, null, "40",
56                 "sendRestrequestAndHandleResponse begin!");
57         NetworkRpcInputEntityBuilder builder = new NetworkRpcInputEntityBuilder();
58         RpcNetworkTopologyOperationInputEntity inputEntity = builder.build(execution, inputs);
59         updateProgress(execution, RequestsDbConstant.Status.PROCESSING, null, "50", "RequestBody build finished!");
60         RpcNetworkTopologyOperationOutputEntity outputEntity;
61         if (!isSend2SdncDirectly()) {
62             outputEntity = genericResourceApiClient.postNetworkTopologyOperation(HeaderUtil.DefaulAuth, inputEntity)
63                     .execute().body();
64             updateProgress(execution, null, null, "90", "sendRestrequestAndHandleResponse finished!");
65             saveOutput(execution, outputEntity);
66         } else {
67             send2SdncDirectly(HeaderUtil.DefaulAuth, inputEntity);
68         }
69         updateProgress(execution, RequestsDbConstant.Status.FINISHED, null, RequestsDbConstant.Progress.ONE_HUNDRED,
70                 "execute finished!");
71         logger.info("SdncNetworkTopologyOperationTask.sendRestrequestAndHandleResponse end!");
72     }
73
74     private void send2SdncDirectly(String defaulAuth, RpcNetworkTopologyOperationInputEntity inputEntity)
75             throws RouteException {
76         logger.info("SdncNetworkTopologyOperationTask.send2SdncDirectly begin!");
77         String url = "http://" + getSdncIp() + ":" + getSdncPort() + URL;
78         HttpPost httpPost = new HttpPost(url);
79         httpPost.addHeader("Authorization", defaulAuth);
80         httpPost.addHeader("Content-type", "application/json");
81         String postBody = getPostbody(inputEntity);
82         logger.info(Strings.repeat("{} ", 3), MessageEnum.RA_SEND_REQUEST_SDNC, postBody, "SDNC");
83         httpPost.setEntity(new StringEntity(postBody, ContentType.APPLICATION_XML));
84         httpPost(url, httpPost);
85         logger.info("SdncNetworkTopologyOperationTask.send2SdncDirectly end!");
86     }
87
88     private void saveOutput(DelegateExecution execution, RpcNetworkTopologyOperationOutputEntity output)
89             throws RouteException {
90         logger.info("SdncNetworkTopologyOperationTask.saveOutput begin!");
91         String responseCode = output.getOutput().getResponseCode();
92         if (!"200".equals(responseCode)) {
93             String processKey = getProcessKey(execution);
94             int errorCode = Integer.parseInt(responseCode);
95             String errorMessage = output.getOutput().getResponseMessage();
96             WorkflowException workflowException = new WorkflowException(processKey, errorCode, errorMessage);
97             execution.setVariable("SDNCA_SuccessIndicator", workflowException);
98             updateProgress(execution, RequestsDbConstant.Status.ERROR, String.valueOf(errorCode), "100", errorMessage);
99             logger.info("exception: SdncNetworkTopologyOperationTask.saveOutput fail!");
100             throw new RouteException();
101         }
102
103         logger.info("SdncNetworkTopologyOperationTask.saveOutput end!");
104     }
105
106 }