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