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