Merge "Add subscription for notifications"
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / adapter / vnf / tasks / VnfAdapterImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 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.adapter.vnf.tasks;
24
25 import org.apache.commons.lang3.StringUtils;
26 import org.onap.so.adapters.vnfrest.CreateVfModuleResponse;
27 import org.onap.so.adapters.vnfrest.CreateVolumeGroupResponse;
28 import org.onap.so.adapters.vnfrest.DeleteVfModuleResponse;
29 import org.onap.so.adapters.vnfrest.DeleteVolumeGroupResponse;
30 import org.onap.so.bpmn.common.BuildingBlockExecution;
31 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
32 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
33 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
34 import org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup;
35 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
36 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
37 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
38 import org.onap.so.client.exception.ExceptionBuilder;
39 import org.onap.so.exceptions.MarshallerException;
40 import org.onap.so.logger.ErrorCode;
41 import org.onap.so.logger.MessageEnum;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.stereotype.Component;
46 import org.xml.sax.InputSource;
47 import org.xml.sax.XMLReader;
48 import javax.xml.XMLConstants;
49 import javax.xml.bind.JAXBContext;
50 import javax.xml.bind.Unmarshaller;
51 import javax.xml.parsers.SAXParserFactory;
52 import javax.xml.transform.sax.SAXSource;
53 import java.io.StringReader;
54 import java.util.ArrayList;
55 import java.util.Iterator;
56 import java.util.List;
57 import java.util.Map;
58
59 @Component
60 public class VnfAdapterImpl {
61     private static final Logger logger = LoggerFactory.getLogger(VnfAdapterImpl.class);
62     private static final String CONTRAIL_SERVICE_INSTANCE_FQDN = "contrailServiceInstanceFqdn";
63     private static final String OAM_MANAGEMENT_V4_ADDRESS = "oamManagementV4Address";
64     private static final String OAM_MANAGEMENT_V6_ADDRESS = "oamManagementV6Address";
65     private static final String CONTRAIL_NETWORK_POLICY_FQDN_LIST = "contrailNetworkPolicyFqdnList";
66
67     @Autowired
68     private ExtractPojosForBB extractPojosForBB;
69
70     @Autowired
71     private ExceptionBuilder exceptionUtil;
72
73     public void preProcessVnfAdapter(BuildingBlockExecution execution) {
74         try {
75             GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
76             ServiceInstance serviceInstance =
77                     extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
78             execution.setVariable("mso-request-id", gBBInput.getRequestContext().getMsoRequestId());
79             execution.setVariable("mso-service-instance-id", serviceInstance.getServiceInstanceId());
80             execution.setVariable("heatStackId", null);
81             execution.setVariable(CONTRAIL_SERVICE_INSTANCE_FQDN, null);
82             execution.setVariable(OAM_MANAGEMENT_V4_ADDRESS, null);
83             execution.setVariable(OAM_MANAGEMENT_V6_ADDRESS, null);
84             execution.setVariable(CONTRAIL_NETWORK_POLICY_FQDN_LIST, null);
85         } catch (Exception ex) {
86             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
87         }
88     }
89
90     public void postProcessVnfAdapter(BuildingBlockExecution execution) {
91         try {
92             String vnfAdapterResponse = execution.getVariable("vnfAdapterRestV1Response");
93             if (!StringUtils.isEmpty(vnfAdapterResponse)) {
94                 Object vnfRestResponse = unMarshal(vnfAdapterResponse);
95                 if (vnfRestResponse instanceof CreateVfModuleResponse) {
96                     VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
97                     String heatStackId = ((CreateVfModuleResponse) vnfRestResponse).getVfModuleStackId();
98                     if (!StringUtils.isEmpty(heatStackId)) {
99                         vfModule.setHeatStackId(heatStackId);
100                         execution.setVariable("heatStackId", heatStackId);
101                     }
102                     Map<String, String> vfModuleOutputs =
103                             ((CreateVfModuleResponse) vnfRestResponse).getVfModuleOutputs();
104                     if (vfModuleOutputs != null) {
105                         processVfModuleOutputs(execution, vfModuleOutputs);
106                     }
107                 } else if (vnfRestResponse instanceof DeleteVfModuleResponse) {
108                     VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
109                     GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
110                     Boolean vfModuleDelete = ((DeleteVfModuleResponse) vnfRestResponse).getVfModuleDeleted();
111                     if (null != vfModuleDelete && vfModuleDelete) {
112                         vfModule.setHeatStackId(null);
113                         execution.setVariable("heatStackId", null);
114                         Map<String, String> vfModuleOutputs =
115                                 ((DeleteVfModuleResponse) vnfRestResponse).getVfModuleOutputs();
116                         if (vfModuleOutputs != null) {
117                             processVfModuleOutputs(execution, vfModuleOutputs);
118                             if (execution.getVariable(OAM_MANAGEMENT_V4_ADDRESS) != null) {
119                                 genericVnf.setIpv4OamAddress("");
120                                 execution.setVariable(OAM_MANAGEMENT_V4_ADDRESS, "");
121                             }
122                             if (execution.getVariable(OAM_MANAGEMENT_V6_ADDRESS) != null) {
123                                 genericVnf.setManagementV6Address("");
124                                 execution.setVariable(OAM_MANAGEMENT_V6_ADDRESS, "");
125                             }
126                             if (execution.getVariable(CONTRAIL_SERVICE_INSTANCE_FQDN) != null) {
127                                 vfModule.setContrailServiceInstanceFqdn("");
128                                 execution.setVariable(CONTRAIL_SERVICE_INSTANCE_FQDN, "");
129                             }
130                         }
131                     }
132                 } else if (vnfRestResponse instanceof CreateVolumeGroupResponse) {
133                     VolumeGroup volumeGroup = extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID);
134                     String heatStackId = ((CreateVolumeGroupResponse) vnfRestResponse).getVolumeGroupStackId();
135                     if (!StringUtils.isEmpty(heatStackId)) {
136                         volumeGroup.setHeatStackId(heatStackId);
137                         execution.setVariable("heatStackId", heatStackId);
138                     } else {
139                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000,
140                                 "HeatStackId is missing from create VolumeGroup Vnf Adapter response.");
141                     }
142                 } else if (vnfRestResponse instanceof DeleteVolumeGroupResponse) {
143                     VolumeGroup volumeGroup = extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID);
144                     Boolean volumeGroupDelete = ((DeleteVolumeGroupResponse) vnfRestResponse).getVolumeGroupDeleted();
145                     if (null != volumeGroupDelete && volumeGroupDelete) {
146                         volumeGroup.setHeatStackId(null);
147                         execution.setVariable("heatStackId", null);
148                     }
149                 }
150             }
151         } catch (Exception ex) {
152             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
153         }
154     }
155
156     private Object unMarshal(String input) throws MarshallerException {
157         try {
158             SAXParserFactory spf = SAXParserFactory.newInstance();
159             spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
160             spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
161             spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
162             spf.setNamespaceAware(true);
163             XMLReader xmlReader = spf.newSAXParser().getXMLReader();
164
165             JAXBContext jaxbContext = JAXBContext.newInstance(CreateVfModuleResponse.class,
166                     CreateVolumeGroupResponse.class, DeleteVfModuleResponse.class, DeleteVolumeGroupResponse.class);
167             Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
168
169             InputSource inputSource = new InputSource(new StringReader(input));
170             SAXSource source = new SAXSource(xmlReader, inputSource);
171             return jaxbUnmarshaller.unmarshal(source);
172         } catch (Exception e) {
173             logger.error("{} {} {}", MessageEnum.GENERAL_EXCEPTION.toString(), ErrorCode.SchemaError.getValue(),
174                     e.getMessage(), e);
175             throw new MarshallerException("Error parsing VNF Adapter response. " + e.getMessage(),
176                     ErrorCode.SchemaError.getValue(), e);
177         }
178     }
179
180     private void processVfModuleOutputs(BuildingBlockExecution execution, Map<String, String> vfModuleOutputs) {
181         if (vfModuleOutputs == null) {
182             return;
183         }
184         try {
185             VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
186             GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
187             List<String> contrailNetworkPolicyFqdnList = new ArrayList<String>();
188             Iterator<String> keys = vfModuleOutputs.keySet().iterator();
189             while (keys.hasNext()) {
190                 String key = keys.next();
191                 if (key.equals("contrail-service-instance-fqdn")) {
192                     String contrailServiceInstanceFqdn = vfModuleOutputs.get(key);
193                     logger.debug("Obtained contrailServiceInstanceFqdn: {}", contrailServiceInstanceFqdn);
194                     vfModule.setContrailServiceInstanceFqdn(contrailServiceInstanceFqdn);
195                     execution.setVariable(CONTRAIL_SERVICE_INSTANCE_FQDN, contrailServiceInstanceFqdn);
196                 } else if (key.endsWith("contrail_network_policy_fqdn")) {
197                     String contrailNetworkPolicyFqdn = vfModuleOutputs.get(key);
198                     logger.debug("Obtained contrailNetworkPolicyFqdn: {}", contrailNetworkPolicyFqdn);
199                     contrailNetworkPolicyFqdnList.add(contrailNetworkPolicyFqdn);
200                 } else if (key.equals("oam_management_v4_address")) {
201                     String oamManagementV4Address = vfModuleOutputs.get(key);
202                     logger.debug("Obtained oamManagementV4Address: {}", oamManagementV4Address);
203                     genericVnf.setIpv4OamAddress(oamManagementV4Address);
204                     execution.setVariable(OAM_MANAGEMENT_V4_ADDRESS, oamManagementV4Address);
205                 } else if (key.equals("oam_management_v6_address")) {
206                     String oamManagementV6Address = vfModuleOutputs.get(key);
207                     logger.debug("Obtained oamManagementV6Address: {}", oamManagementV6Address);
208                     genericVnf.setManagementV6Address(oamManagementV6Address);
209                     execution.setVariable(OAM_MANAGEMENT_V6_ADDRESS, oamManagementV6Address);
210                 }
211
212                 if (!contrailNetworkPolicyFqdnList.isEmpty()) {
213                     execution.setVariable(CONTRAIL_NETWORK_POLICY_FQDN_LIST,
214                             String.join(",", contrailNetworkPolicyFqdnList));
215                 }
216             }
217         } catch (Exception ex) {
218             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
219         }
220
221     }
222 }