18230c4d07e4441e7358f947ae097f1e21b6f5e9
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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.adapter.network.tasks;
22
23 import java.io.StringReader;
24 import java.util.Optional;
25
26 import javax.ws.rs.core.Response;
27 import javax.xml.bind.JAXBContext;
28 import javax.xml.bind.JAXBException;
29 import javax.xml.bind.Unmarshaller;
30
31 import org.camunda.bpm.engine.delegate.DelegateExecution;
32 import org.onap.so.adapters.nwrest.CreateNetworkError;
33 import org.onap.so.adapters.nwrest.CreateNetworkRequest;
34 import org.onap.so.adapters.nwrest.CreateNetworkResponse;
35 import org.onap.so.adapters.nwrest.DeleteNetworkError;
36 import org.onap.so.adapters.nwrest.DeleteNetworkRequest;
37 import org.onap.so.adapters.nwrest.DeleteNetworkResponse;
38 import org.onap.so.client.exception.ExceptionBuilder;
39 import org.onap.so.client.orchestration.NetworkAdapterResources;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.stereotype.Component;
44
45 @Component
46 public class NetworkAdapterRestV1 {
47
48         private static final Logger logger = LoggerFactory.getLogger(NetworkAdapterRestV1.class);
49         
50         private static final String NETWORK_REQUEST = "networkAdapterRequest";
51         private static final String NETWORK_MESSAGE = "NetworkAResponse_MESSAGE";
52         private static final String NETWORK_SYNC_CODE = "NETWORKREST_networkAdapterStatusCode";
53         private static final String NETWORK_SYNC_RESPONSE = "NETWORKREST_networkAdapterResponse";
54         private static final String NETWORK_CORRELATOR = "NetworkAResponse_CORRELATOR";
55         
56         @Autowired
57         private ExceptionBuilder exceptionBuilder;
58
59         @Autowired
60         private NetworkAdapterResources networkAdapterResources;
61         
62         public void callNetworkAdapter (DelegateExecution execution) {
63                 try {
64                         Object networkAdapterRequest = execution.getVariable(NETWORK_REQUEST);
65                         if (networkAdapterRequest != null) {
66                                 Optional<Response> response = Optional.empty();
67                                 if (networkAdapterRequest instanceof CreateNetworkRequest) {
68                                         CreateNetworkRequest createNetworkRequest = (CreateNetworkRequest) networkAdapterRequest;
69                                         execution.setVariable(NETWORK_CORRELATOR, createNetworkRequest.getMessageId());
70                                         response = networkAdapterResources.createNetworkAsync(createNetworkRequest);
71                                 } else if (networkAdapterRequest instanceof DeleteNetworkRequest) {
72                                         DeleteNetworkRequest deleteNetworkRequest = (DeleteNetworkRequest) networkAdapterRequest;
73                                         execution.setVariable(NETWORK_CORRELATOR, deleteNetworkRequest.getMessageId());
74                                         response = networkAdapterResources.deleteNetworkAsync(deleteNetworkRequest);
75                                 }
76                                 if(response.isPresent()) {
77                                         String statusCode = Integer.toString(response.get().getStatus());
78                                         String responseString = "";
79                                         if(response.get().getEntity() != null) {
80                                                 responseString = (String) response.get().getEntity();
81                                         }
82                                         execution.setVariable(NETWORK_SYNC_CODE, statusCode);
83                                         execution.setVariable(NETWORK_SYNC_RESPONSE, responseString);
84                                 } else {
85                                         throw new Exception("No Ack response from Openstack Adapter");
86                                 }
87                         } else {
88                                 throw new Exception("No Network Request was created. networkAdapterRequest was null.");
89                         }
90                 } catch (Exception ex) {
91                         exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, ex);
92                 }
93         }
94         
95         public void processCallback (DelegateExecution execution) {
96                 try {
97                         Object networkAdapterRequest = execution.getVariable(NETWORK_REQUEST);
98                         String callback = (String) execution.getVariable(NETWORK_MESSAGE);
99                         String logCallbackMessage = "Callback from OpenstackAdapter: " + callback;
100                         logger.debug(logCallbackMessage);
101                         if (networkAdapterRequest != null) {
102                                 if (networkAdapterRequest instanceof CreateNetworkRequest) {
103                                         if(callback.contains("createNetworkError")) {
104                                                 CreateNetworkError createNetworkError = (CreateNetworkError) unmarshalXml(callback, CreateNetworkError.class);
105                                                 throw new Exception(createNetworkError.getMessage());
106                                         } else {
107                                                 CreateNetworkResponse createNetworkResponse = (CreateNetworkResponse) unmarshalXml(callback, CreateNetworkResponse.class);
108                                                 execution.setVariable("createNetworkResponse", createNetworkResponse);
109                                         }
110                                 } else if (networkAdapterRequest instanceof DeleteNetworkRequest) {
111                                         if(callback.contains("deleteNetworkError")) {
112                                                 DeleteNetworkError deleteNetworkError = (DeleteNetworkError) unmarshalXml(callback, DeleteNetworkError.class);
113                                                 throw new Exception(deleteNetworkError.getMessage());
114                                         } else {
115                                                 DeleteNetworkResponse deleteNetworkResponse = (DeleteNetworkResponse) unmarshalXml(callback, DeleteNetworkResponse.class);
116                                                 execution.setVariable("deleteNetworkResponse", deleteNetworkResponse);
117                                         }
118                                 }
119                         }
120                 } catch (Exception e) {
121                         logger.error("Error in Openstack Adapter callback", e);
122                         exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, e.getMessage());
123                 }
124         }
125         
126         protected <T> Object unmarshalXml(String xmlString, Class<T> resultClass) throws JAXBException {
127                 StringReader reader = new StringReader(xmlString);
128                 JAXBContext context = JAXBContext.newInstance(resultClass);
129                 Unmarshaller unmarshaller = context.createUnmarshaller();
130                 return unmarshaller.unmarshal(reader);
131         }
132         
133         public void handleTimeOutException (DelegateExecution execution) {              
134                 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, "Error timed out waiting on Openstack Async-Response");
135         }
136         
137         public void handleSyncError (DelegateExecution execution) {
138                 String statusCode = (String) execution.getVariable(NETWORK_SYNC_CODE);
139                 String responseString = (String) execution.getVariable(NETWORK_SYNC_RESPONSE);
140                 String errorMessage = "Error with Openstack Adapter Sync Request: StatusCode = " + statusCode + " Response = " + responseString;
141                 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, errorMessage);
142         }
143 }