48426fa725ed33aa855a1ef15913e3111916881f
[so.git] /
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     public static final String HEAT_STACK_ID = "heatStackId";
67
68     @Autowired
69     private ExtractPojosForBB extractPojosForBB;
70
71     @Autowired
72     private ExceptionBuilder exceptionUtil;
73
74     public void preProcessVnfAdapter(BuildingBlockExecution execution) {
75         try {
76             GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
77             ServiceInstance serviceInstance =
78                     extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
79             execution.setVariable("mso-request-id", gBBInput.getRequestContext().getMsoRequestId());
80             execution.setVariable("mso-service-instance-id", serviceInstance.getServiceInstanceId());
81             execution.setVariable(HEAT_STACK_ID, null);
82             execution.setVariable(CONTRAIL_SERVICE_INSTANCE_FQDN, null);
83             execution.setVariable(OAM_MANAGEMENT_V4_ADDRESS, null);
84             execution.setVariable(OAM_MANAGEMENT_V6_ADDRESS, null);
85             execution.setVariable(CONTRAIL_NETWORK_POLICY_FQDN_LIST, null);
86         } catch (Exception ex) {
87             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
88         }
89     }
90
91     public void postProcessVnfAdapter(BuildingBlockExecution execution) {
92         try {
93             String vnfAdapterResponse = execution.getVariable("vnfAdapterRestV1Response");
94             if (!StringUtils.isEmpty(vnfAdapterResponse)) {
95                 Object vnfRestResponse = unMarshal(vnfAdapterResponse);
96                 if (vnfRestResponse instanceof CreateVfModuleResponse) {
97                     VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
98                     String heatStackId = ((CreateVfModuleResponse) vnfRestResponse).getVfModuleStackId();
99                     if (!StringUtils.isEmpty(heatStackId)) {
100                         vfModule.setHeatStackId(heatStackId);
101                         execution.setVariable(HEAT_STACK_ID, heatStackId);
102                     }
103                     Map<String, String> vfModuleOutputs =
104                             ((CreateVfModuleResponse) vnfRestResponse).getVfModuleOutputs();
105                     if (vfModuleOutputs != null) {
106                         processVfModuleOutputs(execution, vfModuleOutputs);
107                     }
108                 } else if (vnfRestResponse instanceof DeleteVfModuleResponse) {
109                     VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
110                     GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
111                     Boolean vfModuleDelete = ((DeleteVfModuleResponse) vnfRestResponse).getVfModuleDeleted();
112                     if (null != vfModuleDelete && vfModuleDelete) {
113                         vfModule.setHeatStackId(null);
114                         execution.setVariable(HEAT_STACK_ID, null);
115                         Map<String, String> vfModuleOutputs =
116                                 ((DeleteVfModuleResponse) vnfRestResponse).getVfModuleOutputs();
117                         if (vfModuleOutputs != null) {
118                             processVfModuleOutputs(execution, vfModuleOutputs);
119                             if (execution.getVariable(OAM_MANAGEMENT_V4_ADDRESS) != null) {
120                                 genericVnf.setIpv4OamAddress("");
121                                 execution.setVariable(OAM_MANAGEMENT_V4_ADDRESS, "");
122                             }
123                             if (execution.getVariable(OAM_MANAGEMENT_V6_ADDRESS) != null) {
124                                 genericVnf.setManagementV6Address("");
125                                 execution.setVariable(OAM_MANAGEMENT_V6_ADDRESS, "");
126                             }
127                             if (execution.getVariable(CONTRAIL_SERVICE_INSTANCE_FQDN) != null) {
128                                 vfModule.setContrailServiceInstanceFqdn("");
129                                 execution.setVariable(CONTRAIL_SERVICE_INSTANCE_FQDN, "");
130                             }
131                         }
132                     }
133                 } else if (vnfRestResponse instanceof CreateVolumeGroupResponse) {
134                     VolumeGroup volumeGroup = extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID);
135                     String heatStackId = ((CreateVolumeGroupResponse) vnfRestResponse).getVolumeGroupStackId();
136                     if (!StringUtils.isEmpty(heatStackId)) {
137                         volumeGroup.setHeatStackId(heatStackId);
138                         execution.setVariable(HEAT_STACK_ID, heatStackId);
139                     } else {
140                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000,
141                                 "HeatStackId is missing from create VolumeGroup Vnf Adapter response.");
142                     }
143                 } else if (vnfRestResponse instanceof DeleteVolumeGroupResponse) {
144                     VolumeGroup volumeGroup = extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID);
145                     Boolean volumeGroupDelete = ((DeleteVolumeGroupResponse) vnfRestResponse).getVolumeGroupDeleted();
146                     if (null != volumeGroupDelete && volumeGroupDelete) {
147                         volumeGroup.setHeatStackId(null);
148                         execution.setVariable(HEAT_STACK_ID, null);
149                     }
150                 }
151             }
152         } catch (Exception ex) {
153             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
154         }
155     }
156
157     private Object unMarshal(String input) throws MarshallerException {
158         try {
159             SAXParserFactory spf = SAXParserFactory.newInstance();
160             spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
161             spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
162             spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
163             spf.setNamespaceAware(true);
164             XMLReader xmlReader = spf.newSAXParser().getXMLReader();
165
166             JAXBContext jaxbContext = JAXBContext.newInstance(CreateVfModuleResponse.class,
167                     CreateVolumeGroupResponse.class, DeleteVfModuleResponse.class, DeleteVolumeGroupResponse.class);
168             Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
169
170             InputSource inputSource = new InputSource(new StringReader(input));
171             SAXSource source = new SAXSource(xmlReader, inputSource);
172             return jaxbUnmarshaller.unmarshal(source);
173         } catch (Exception e) {
174             logger.error("{} {} {}", MessageEnum.GENERAL_EXCEPTION.toString(), ErrorCode.SchemaError.getValue(),
175                     e.getMessage(), e);
176             throw new MarshallerException("Error parsing VNF Adapter response. " + e.getMessage(),
177                     ErrorCode.SchemaError.getValue(), e);
178         }
179     }
180
181     private void processVfModuleOutputs(BuildingBlockExecution execution, Map<String, String> vfModuleOutputs) {
182         if (vfModuleOutputs == null) {
183             return;
184         }
185         try {
186             VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
187             GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
188             List<String> contrailNetworkPolicyFqdnList = new ArrayList<>();
189             Iterator<String> keys = vfModuleOutputs.keySet().iterator();
190             while (keys.hasNext()) {
191                 String key = keys.next();
192                 if (key.equals("contrail-service-instance-fqdn")) {
193                     String contrailServiceInstanceFqdn = vfModuleOutputs.get(key);
194                     logger.debug("Obtained contrailServiceInstanceFqdn: {}", contrailServiceInstanceFqdn);
195                     vfModule.setContrailServiceInstanceFqdn(contrailServiceInstanceFqdn);
196                     execution.setVariable(CONTRAIL_SERVICE_INSTANCE_FQDN, contrailServiceInstanceFqdn);
197                 } else if (key.endsWith("contrail_network_policy_fqdn")) {
198                     String contrailNetworkPolicyFqdn = vfModuleOutputs.get(key);
199                     logger.debug("Obtained contrailNetworkPolicyFqdn: {}", contrailNetworkPolicyFqdn);
200                     contrailNetworkPolicyFqdnList.add(contrailNetworkPolicyFqdn);
201                 } else if (key.equals("oam_management_v4_address")) {
202                     String oamManagementV4Address = vfModuleOutputs.get(key);
203                     logger.debug("Obtained oamManagementV4Address: {}", oamManagementV4Address);
204                     genericVnf.setIpv4OamAddress(oamManagementV4Address);
205                     execution.setVariable(OAM_MANAGEMENT_V4_ADDRESS, oamManagementV4Address);
206                 } else if (key.equals("oam_management_v6_address")) {
207                     String oamManagementV6Address = vfModuleOutputs.get(key);
208                     logger.debug("Obtained oamManagementV6Address: {}", oamManagementV6Address);
209                     genericVnf.setManagementV6Address(oamManagementV6Address);
210                     execution.setVariable(OAM_MANAGEMENT_V6_ADDRESS, oamManagementV6Address);
211                 }
212
213                 if (!contrailNetworkPolicyFqdnList.isEmpty()) {
214                     execution.setVariable(CONTRAIL_NETWORK_POLICY_FQDN_LIST,
215                             String.join(",", contrailNetworkPolicyFqdnList));
216                 }
217             }
218         } catch (Exception ex) {
219             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
220         }
221
222     }
223 }