Springboot 2.0 upgrade
[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  * 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.vnf.tasks;
22
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;
43
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;
50
51 @Component
52 public class VnfAdapterImpl {
53         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, VnfAdapterCreateTasks.class);
54         
55         @Autowired
56         private ExtractPojosForBB extractPojosForBB;
57         
58         @Autowired
59         private ExceptionBuilder exceptionUtil;
60
61         public void preProcessVnfAdapter(BuildingBlockExecution execution) {
62                 try {
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);
70                 }
71         }
72         
73         public void postProcessVnfAdapter(BuildingBlockExecution execution) {
74                 try {                   
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);
84                     }
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);
91                     }
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);
98                     }else{
99                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "HeatStackId is missing from create VolumeGroup Vnf Adapter response.");
100                     }                    
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);
107                         }
108                 }
109             }
110                 } catch (Exception ex) {
111                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
112                 }
113         }
114
115     private Object unMarshal(String input) throws MarshallerException {
116         try {
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();
123
124             JAXBContext jaxbContext = JAXBContext.newInstance(CreateVfModuleResponse.class,
125                     CreateVolumeGroupResponse.class,DeleteVfModuleResponse.class,DeleteVolumeGroupResponse.class);
126             Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
127
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);
134         }
135     }
136 }