554af37924b54fa3df532297d90417f178754fd8
[so.git] /
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.bpmn.common.BuildingBlockExecution;
28 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
29 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
30 import org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup;
31 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
32 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
33 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
34 import org.onap.so.client.exception.ExceptionBuilder;
35 import org.onap.so.exceptions.MarshallerException;
36 import org.onap.so.logger.MessageEnum;
37 import org.onap.so.logger.MsoLogger;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Component;
40 import org.xml.sax.InputSource;
41 import org.xml.sax.XMLReader;
42
43 import javax.xml.XMLConstants;
44 import javax.xml.bind.JAXBContext;
45 import javax.xml.bind.Unmarshaller;
46 import javax.xml.parsers.SAXParserFactory;
47 import javax.xml.transform.sax.SAXSource;
48 import java.io.StringReader;
49
50 @Component
51 public class VnfAdapterImpl {
52         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, VnfAdapterCreateTasks.class);
53         
54         @Autowired
55         private ExtractPojosForBB extractPojosForBB;
56         
57         @Autowired
58         private ExceptionBuilder exceptionUtil;
59
60         public void preProcessVnfAdapter(BuildingBlockExecution execution) {
61                 try {
62                         GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
63                         ServiceInstance serviceInstance = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
64                         
65                         execution.setVariable("isDebugLogEnabled", "true");
66                         execution.setVariable("mso-request-id", gBBInput.getRequestContext().getMsoRequestId());
67                         execution.setVariable("mso-service-instance-id", serviceInstance.getServiceInstanceId());
68                         execution.setVariable("heatStackId", null);
69                 } catch (Exception ex) {
70                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
71                 }
72         }
73         
74         public void postProcessVnfAdapter(BuildingBlockExecution execution) {
75                 try {
76             String vnfAdapterResponse = execution.getVariable("vnfAdapterRestV1Response");
77             if (!StringUtils.isEmpty( vnfAdapterResponse)) {
78                 Object vnfRestResponse = unMarshal(vnfAdapterResponse);
79                 if(vnfRestResponse instanceof CreateVfModuleResponse) {
80                     VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID, execution.getLookupMap().get(ResourceKey.VF_MODULE_ID));
81                     String heatStackId = ((CreateVfModuleResponse) vnfRestResponse).getVfModuleStackId();
82                     if(!StringUtils.isEmpty(heatStackId)) {
83                         vfModule.setHeatStackId(heatStackId);
84                         execution.setVariable("heatStackId", heatStackId);
85                     }
86                 } else if(vnfRestResponse instanceof DeleteVfModuleResponse) {
87                     VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID, execution.getLookupMap().get(ResourceKey.VF_MODULE_ID));
88                     Boolean vfModuleDelete = ((DeleteVfModuleResponse) vnfRestResponse).getVfModuleDeleted();
89                     if(null!= vfModuleDelete && vfModuleDelete) {
90                         vfModule.setHeatStackId(null);
91                         execution.setVariable("heatStackId", null);
92                     }
93                 } else if(vnfRestResponse instanceof CreateVolumeGroupResponse) {
94                     VolumeGroup volumeGroup = extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID, execution.getLookupMap().get(ResourceKey.VOLUME_GROUP_ID));
95                     String heatStackId = ((CreateVolumeGroupResponse) vnfRestResponse).getVolumeGroupStackId();
96                     if(!StringUtils.isEmpty(heatStackId)) {
97                         volumeGroup.setHeatStackId(heatStackId);
98                         execution.setVariable("heatStackId", heatStackId);
99                     }else{
100                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "HeatStackId is missing from create VolumeGroup Vnf Adapter response.");
101                     }
102                 }                
103             }
104                 } catch (Exception ex) {
105                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
106                 }
107         }
108
109     private Object unMarshal(String input) throws MarshallerException {
110         try {
111             SAXParserFactory spf = SAXParserFactory.newInstance();
112             spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
113             spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
114             spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
115             spf.setNamespaceAware(true);
116             XMLReader xmlReader = spf.newSAXParser().getXMLReader();
117
118             JAXBContext jaxbContext = JAXBContext.newInstance(CreateVfModuleResponse.class,
119                     CreateVolumeGroupResponse.class,DeleteVfModuleResponse.class);
120             Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
121
122             InputSource inputSource = new InputSource(new StringReader(input));
123             SAXSource source = new SAXSource(xmlReader, inputSource);
124             return jaxbUnmarshaller.unmarshal(source);
125         } catch (Exception e) {
126             msoLogger.error(MessageEnum.GENERAL_EXCEPTION, "", "", "", MsoLogger.ErrorCode.SchemaError, e.getMessage(), e);
127             throw new MarshallerException("Error parsing VNF Adapter response. " + e.getMessage(), MsoLogger.ErrorCode.SchemaError.getValue(), e);
128         }
129     }
130 }