db54b219a901b442893e7d62d01fbd63ad5f444e
[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                         
66                         execution.setVariable("isDebugLogEnabled", "true");
67                         execution.setVariable("mso-request-id", gBBInput.getRequestContext().getMsoRequestId());
68                         execution.setVariable("mso-service-instance-id", serviceInstance.getServiceInstanceId());
69                         execution.setVariable("heatStackId", null);
70                 } catch (Exception ex) {
71                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
72                 }
73         }
74         
75         public void postProcessVnfAdapter(BuildingBlockExecution execution) {
76                 try {                   
77             String vnfAdapterResponse = execution.getVariable("vnfAdapterRestV1Response");           
78             if (!StringUtils.isEmpty( vnfAdapterResponse)) {
79                 Object vnfRestResponse = unMarshal(vnfAdapterResponse);
80                 if(vnfRestResponse instanceof CreateVfModuleResponse) {
81                     VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID, execution.getLookupMap().get(ResourceKey.VF_MODULE_ID));
82                     String heatStackId = ((CreateVfModuleResponse) vnfRestResponse).getVfModuleStackId();
83                     if(!StringUtils.isEmpty(heatStackId)) {
84                         vfModule.setHeatStackId(heatStackId);
85                         execution.setVariable("heatStackId", heatStackId);
86                     }
87                 } else if(vnfRestResponse instanceof DeleteVfModuleResponse) {
88                     VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID, execution.getLookupMap().get(ResourceKey.VF_MODULE_ID));
89                     Boolean vfModuleDelete = ((DeleteVfModuleResponse) vnfRestResponse).getVfModuleDeleted();
90                     if(null!= vfModuleDelete && vfModuleDelete) {
91                         vfModule.setHeatStackId(null);
92                         execution.setVariable("heatStackId", null);
93                     }
94                 } else if(vnfRestResponse instanceof CreateVolumeGroupResponse) {
95                     VolumeGroup volumeGroup = extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID, execution.getLookupMap().get(ResourceKey.VOLUME_GROUP_ID));
96                     String heatStackId = ((CreateVolumeGroupResponse) vnfRestResponse).getVolumeGroupStackId();
97                     if(!StringUtils.isEmpty(heatStackId)) {
98                         volumeGroup.setHeatStackId(heatStackId);
99                         execution.setVariable("heatStackId", heatStackId);
100                     }else{
101                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "HeatStackId is missing from create VolumeGroup Vnf Adapter response.");
102                     }                    
103                 } else if(vnfRestResponse instanceof DeleteVolumeGroupResponse) {                       
104                         VolumeGroup volumeGroup = extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID, execution.getLookupMap().get(ResourceKey.VOLUME_GROUP_ID));
105                         Boolean volumeGroupDelete = ((DeleteVolumeGroupResponse) vnfRestResponse).getVolumeGroupDeleted();
106                         if(null!= volumeGroupDelete && volumeGroupDelete) {                             
107                                 volumeGroup.setHeatStackId(null);
108                                 execution.setVariable("heatStackId", null);
109                         }
110                 }
111             }
112                 } catch (Exception ex) {
113                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
114                 }
115         }
116
117     private Object unMarshal(String input) throws MarshallerException {
118         try {
119             SAXParserFactory spf = SAXParserFactory.newInstance();
120             spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
121             spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
122             spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
123             spf.setNamespaceAware(true);
124             XMLReader xmlReader = spf.newSAXParser().getXMLReader();
125
126             JAXBContext jaxbContext = JAXBContext.newInstance(CreateVfModuleResponse.class,
127                     CreateVolumeGroupResponse.class,DeleteVfModuleResponse.class,DeleteVolumeGroupResponse.class);
128             Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
129
130             InputSource inputSource = new InputSource(new StringReader(input));
131             SAXSource source = new SAXSource(xmlReader, inputSource);
132             return jaxbUnmarshaller.unmarshal(source);
133         } catch (Exception e) {
134             msoLogger.error(MessageEnum.GENERAL_EXCEPTION, "", "", "", MsoLogger.ErrorCode.SchemaError, e.getMessage(), e);
135             throw new MarshallerException("Error parsing VNF Adapter response. " + e.getMessage(), MsoLogger.ErrorCode.SchemaError.getValue(), e);
136         }
137     }
138 }