ed2411a30eeb847c7a6227a6e2c848a41a803556
[so.git] /
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
37 import java.util.Map;
38
39 /**
40  * Created by 10112215 on 2017/9/26.
41  */
42 public class SdncServiceTopologyOperationTask extends AbstractSdncOperationTask {
43
44     private static final String URL = "/restconf/operations/GENERIC-RESOURCE-API:service-topology-operation";
45
46     @Override
47     public void sendRestrequestAndHandleResponse(DelegateExecution execution,
48                                                  Map<String, String> inputs,
49                                                  GenericResourceApi genericResourceApiClient) throws Exception {
50 //        updateProgress(execution, null, null, "40", "sendRestrequestAndHandleResponse begin!");
51         ServiceRpcInputEntityBuilder builder = new ServiceRpcInputEntityBuilder();
52         RpcServiceTopologyOperationInputEntity inputEntity = builder.build(execution, inputs);
53 //        updateProgress(execution, null, null, "50", "RequestBody build finished!");
54         RpcServiceTopologyOperationOutputEntity outputEntity;
55         if (!isSend2SdncDirectly()) {
56             outputEntity = genericResourceApiClient.postServiceTopologyOperation
57                     (HeaderUtil.DefaulAuth, inputEntity).execute().body();
58 //        updateProgress(execution, null, null, "90", "sendRestrequestAndHandleResponse finished!");
59             saveOutput(execution, outputEntity);
60         } else {
61             Send2SdncDirectly(HeaderUtil.DefaulAuth, inputEntity);
62         }
63     }
64
65     private void Send2SdncDirectly(String defaulAuth,
66                                    RpcServiceTopologyOperationInputEntity inputEntity) throws RouteException {
67         String url = "http://" + getSdncIp() + ":" + getSdncPort() + URL;
68         HttpPost httpPost = new HttpPost(url);
69         httpPost.addHeader("Authorization", defaulAuth);
70         httpPost.addHeader("Content-type", "application/json");
71         String postBody = getPostbody(inputEntity);
72         LOGGER.info(MessageEnum.RA_SEND_REQUEST_SDNC, postBody.toString(), "SDNC", "");
73         httpPost.setEntity(new StringEntity(postBody, ContentType.APPLICATION_XML));
74         httpPost(url, httpPost);
75     }
76
77     private void saveOutput(DelegateExecution execution, RpcServiceTopologyOperationOutputEntity output) throws Exception {
78         String responseCode = output.getOutput().getResponseCode();
79         if (!"200".equals(responseCode)) {
80             String processKey = getProcessKey(execution);
81             int errorCode = Integer.valueOf(responseCode);
82             String errorMessage = output.getOutput().getResponseMessage();
83             WorkflowException workflowException = new WorkflowException(processKey, errorCode, errorMessage);
84             execution.setVariable("SDNCA_SuccessIndicator", workflowException);
85 //            updateProgress(execution, RequestsDbConstant.Status.ERROR, String.valueOf(errorCode), null, errorMessage);
86             throw new Exception("");
87         }
88     }
89 }