2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2018 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.bpmn.infrastructure.adapter.vnf.tasks;
23 import org.apache.commons.lang3.StringUtils;
24 import org.onap.so.adapters.vnfrest.CreateVfModuleResponse;
25 import org.onap.so.adapters.vnfrest.CreateVolumeGroupResponse;
26 import org.onap.so.adapters.vnfrest.DeleteVfModuleResponse;
27 import org.onap.so.adapters.vnfrest.DeleteVolumeGroupResponse;
28 import org.onap.so.bpmn.common.BuildingBlockExecution;
29 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
30 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
31 import org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup;
32 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
33 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
34 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
35 import org.onap.so.client.exception.ExceptionBuilder;
36 import org.onap.so.exceptions.MarshallerException;
37 import org.onap.so.logger.MessageEnum;
38 import org.onap.so.logger.MsoLogger;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Component;
41 import org.xml.sax.InputSource;
42 import org.xml.sax.XMLReader;
44 import javax.xml.XMLConstants;
45 import javax.xml.bind.JAXBContext;
46 import javax.xml.bind.Unmarshaller;
47 import javax.xml.parsers.SAXParserFactory;
48 import javax.xml.transform.sax.SAXSource;
49 import java.io.StringReader;
52 public class VnfAdapterImpl {
53 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, VnfAdapterCreateTasks.class);
56 private ExtractPojosForBB extractPojosForBB;
59 private ExceptionBuilder exceptionUtil;
61 public void preProcessVnfAdapter(BuildingBlockExecution execution) {
63 GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
64 ServiceInstance serviceInstance = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
65 execution.setVariable("mso-request-id", gBBInput.getRequestContext().getMsoRequestId());
66 execution.setVariable("mso-service-instance-id", serviceInstance.getServiceInstanceId());
67 execution.setVariable("heatStackId", null);
68 } catch (Exception ex) {
69 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
73 public void postProcessVnfAdapter(BuildingBlockExecution execution) {
75 String vnfAdapterResponse = execution.getVariable("vnfAdapterRestV1Response");
76 if (!StringUtils.isEmpty( vnfAdapterResponse)) {
77 Object vnfRestResponse = unMarshal(vnfAdapterResponse);
78 if(vnfRestResponse instanceof CreateVfModuleResponse) {
79 VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID, execution.getLookupMap().get(ResourceKey.VF_MODULE_ID));
80 String heatStackId = ((CreateVfModuleResponse) vnfRestResponse).getVfModuleStackId();
81 if(!StringUtils.isEmpty(heatStackId)) {
82 vfModule.setHeatStackId(heatStackId);
83 execution.setVariable("heatStackId", heatStackId);
85 } else if(vnfRestResponse instanceof DeleteVfModuleResponse) {
86 VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID, execution.getLookupMap().get(ResourceKey.VF_MODULE_ID));
87 Boolean vfModuleDelete = ((DeleteVfModuleResponse) vnfRestResponse).getVfModuleDeleted();
88 if(null!= vfModuleDelete && vfModuleDelete) {
89 vfModule.setHeatStackId(null);
90 execution.setVariable("heatStackId", null);
92 } else if(vnfRestResponse instanceof CreateVolumeGroupResponse) {
93 VolumeGroup volumeGroup = extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID, execution.getLookupMap().get(ResourceKey.VOLUME_GROUP_ID));
94 String heatStackId = ((CreateVolumeGroupResponse) vnfRestResponse).getVolumeGroupStackId();
95 if(!StringUtils.isEmpty(heatStackId)) {
96 volumeGroup.setHeatStackId(heatStackId);
97 execution.setVariable("heatStackId", heatStackId);
99 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "HeatStackId is missing from create VolumeGroup Vnf Adapter response.");
101 } else if(vnfRestResponse instanceof DeleteVolumeGroupResponse) {
102 VolumeGroup volumeGroup = extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID, execution.getLookupMap().get(ResourceKey.VOLUME_GROUP_ID));
103 Boolean volumeGroupDelete = ((DeleteVolumeGroupResponse) vnfRestResponse).getVolumeGroupDeleted();
104 if(null!= volumeGroupDelete && volumeGroupDelete) {
105 volumeGroup.setHeatStackId(null);
106 execution.setVariable("heatStackId", null);
110 } catch (Exception ex) {
111 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
115 private Object unMarshal(String input) throws MarshallerException {
117 SAXParserFactory spf = SAXParserFactory.newInstance();
118 spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
119 spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
120 spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
121 spf.setNamespaceAware(true);
122 XMLReader xmlReader = spf.newSAXParser().getXMLReader();
124 JAXBContext jaxbContext = JAXBContext.newInstance(CreateVfModuleResponse.class,
125 CreateVolumeGroupResponse.class,DeleteVfModuleResponse.class,DeleteVolumeGroupResponse.class);
126 Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
128 InputSource inputSource = new InputSource(new StringReader(input));
129 SAXSource source = new SAXSource(xmlReader, inputSource);
130 return jaxbUnmarshaller.unmarshal(source);
131 } catch (Exception e) {
132 msoLogger.error(MessageEnum.GENERAL_EXCEPTION, "", "", "", MsoLogger.ErrorCode.SchemaError, e.getMessage(), e);
133 throw new MarshallerException("Error parsing VNF Adapter response. " + e.getMessage(), MsoLogger.ErrorCode.SchemaError.getValue(), e);