create custom spring aop annotation for logging
[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  * 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.onap.so.logger.LoggingAnchor;
26 import org.apache.commons.lang3.StringUtils;
27 import org.onap.so.adapters.vnfrest.CreateVfModuleResponse;
28 import org.onap.so.adapters.vnfrest.CreateVolumeGroupResponse;
29 import org.onap.so.adapters.vnfrest.DeleteVfModuleResponse;
30 import org.onap.so.adapters.vnfrest.DeleteVolumeGroupResponse;
31 import org.onap.so.bpmn.common.BuildingBlockExecution;
32 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
33 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
34 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
35 import org.onap.so.bpmn.servicedecomposition.bbobjects.VolumeGroup;
36 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
37 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
38 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
39 import org.onap.so.client.exception.ExceptionBuilder;
40 import org.onap.so.exceptions.MarshallerException;
41 import org.onap.logging.filter.base.ErrorCode;
42 import org.onap.so.logger.MessageEnum;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.stereotype.Component;
47 import org.xml.sax.InputSource;
48 import org.xml.sax.XMLReader;
49 import javax.xml.XMLConstants;
50 import javax.xml.bind.JAXBContext;
51 import javax.xml.bind.Unmarshaller;
52 import javax.xml.parsers.SAXParserFactory;
53 import javax.xml.transform.sax.SAXSource;
54 import java.io.StringReader;
55 import java.util.ArrayList;
56 import java.util.Iterator;
57 import java.util.List;
58 import java.util.Map;
59
60 @Component
61 public class VnfAdapterImpl {
62     private static final Logger logger = LoggerFactory.getLogger(VnfAdapterImpl.class);
63     private static final String CONTRAIL_SERVICE_INSTANCE_FQDN = "contrailServiceInstanceFqdn";
64     private static final String OAM_MANAGEMENT_V4_ADDRESS = "oamManagementV4Address";
65     private static final String OAM_MANAGEMENT_V6_ADDRESS = "oamManagementV6Address";
66     private static final String CONTRAIL_NETWORK_POLICY_FQDN_LIST = "contrailNetworkPolicyFqdnList";
67     public static final String HEAT_STACK_ID = "heatStackId";
68
69     @Autowired
70     private ExtractPojosForBB extractPojosForBB;
71
72     @Autowired
73     private ExceptionBuilder exceptionUtil;
74
75     public void preProcessVnfAdapter(BuildingBlockExecution execution) {
76         try {
77             GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
78             ServiceInstance serviceInstance =
79                     extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
80             execution.setVariable("mso-request-id", gBBInput.getRequestContext().getMsoRequestId());
81             execution.setVariable("mso-service-instance-id", serviceInstance.getServiceInstanceId());
82             execution.setVariable(HEAT_STACK_ID, null);
83             execution.setVariable(CONTRAIL_SERVICE_INSTANCE_FQDN, null);
84             execution.setVariable(OAM_MANAGEMENT_V4_ADDRESS, null);
85             execution.setVariable(OAM_MANAGEMENT_V6_ADDRESS, null);
86             execution.setVariable(CONTRAIL_NETWORK_POLICY_FQDN_LIST, null);
87         } catch (Exception ex) {
88             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
89         }
90     }
91
92     public void postProcessVnfAdapter(BuildingBlockExecution execution) {
93         try {
94             String vnfAdapterResponse = execution.getVariable("vnfAdapterRestV1Response");
95             if (!StringUtils.isEmpty(vnfAdapterResponse)) {
96                 Object vnfRestResponse = unMarshal(vnfAdapterResponse);
97                 if (vnfRestResponse instanceof CreateVfModuleResponse) {
98                     VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
99                     String heatStackId = ((CreateVfModuleResponse) vnfRestResponse).getVfModuleStackId();
100                     if (!StringUtils.isEmpty(heatStackId)) {
101                         vfModule.setHeatStackId(heatStackId);
102                         execution.setVariable(HEAT_STACK_ID, heatStackId);
103                     }
104                     Map<String, String> vfModuleOutputs =
105                             ((CreateVfModuleResponse) vnfRestResponse).getVfModuleOutputs();
106                     if (vfModuleOutputs != null) {
107                         processVfModuleOutputs(execution, vfModuleOutputs);
108                     }
109                 } else if (vnfRestResponse instanceof DeleteVfModuleResponse) {
110                     VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
111                     GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
112                     Boolean vfModuleDelete = ((DeleteVfModuleResponse) vnfRestResponse).getVfModuleDeleted();
113                     if (null != vfModuleDelete && vfModuleDelete) {
114                         vfModule.setHeatStackId(null);
115                         execution.setVariable(HEAT_STACK_ID, null);
116                         Map<String, String> vfModuleOutputs =
117                                 ((DeleteVfModuleResponse) vnfRestResponse).getVfModuleOutputs();
118                         if (vfModuleOutputs != null) {
119                             processVfModuleOutputs(execution, vfModuleOutputs);
120                             if (execution.getVariable(OAM_MANAGEMENT_V4_ADDRESS) != null) {
121                                 genericVnf.setIpv4OamAddress("");
122                                 execution.setVariable(OAM_MANAGEMENT_V4_ADDRESS, "");
123                             }
124                             if (execution.getVariable(OAM_MANAGEMENT_V6_ADDRESS) != null) {
125                                 genericVnf.setManagementV6Address("");
126                                 execution.setVariable(OAM_MANAGEMENT_V6_ADDRESS, "");
127                             }
128                             if (execution.getVariable(CONTRAIL_SERVICE_INSTANCE_FQDN) != null) {
129                                 vfModule.setContrailServiceInstanceFqdn("");
130                                 execution.setVariable(CONTRAIL_SERVICE_INSTANCE_FQDN, "");
131                             }
132                         }
133                     }
134                 } else if (vnfRestResponse instanceof CreateVolumeGroupResponse) {
135                     VolumeGroup volumeGroup = extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID);
136                     String heatStackId = ((CreateVolumeGroupResponse) vnfRestResponse).getVolumeGroupStackId();
137                     if (!StringUtils.isEmpty(heatStackId)) {
138                         volumeGroup.setHeatStackId(heatStackId);
139                         execution.setVariable(HEAT_STACK_ID, heatStackId);
140                     } else {
141                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000,
142                                 "HeatStackId is missing from create VolumeGroup Vnf Adapter response.");
143                     }
144                 } else if (vnfRestResponse instanceof DeleteVolumeGroupResponse) {
145                     VolumeGroup volumeGroup = extractPojosForBB.extractByKey(execution, ResourceKey.VOLUME_GROUP_ID);
146                     Boolean volumeGroupDelete = ((DeleteVolumeGroupResponse) vnfRestResponse).getVolumeGroupDeleted();
147                     if (null != volumeGroupDelete && volumeGroupDelete) {
148                         volumeGroup.setHeatStackId(null);
149                         execution.setVariable(HEAT_STACK_ID, null);
150                     }
151                 }
152             }
153         } catch (Exception ex) {
154             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
155         }
156     }
157
158     private Object unMarshal(String input) throws MarshallerException {
159         try {
160             SAXParserFactory spf = SAXParserFactory.newInstance();
161             spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
162             spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
163             spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
164             spf.setNamespaceAware(true);
165             XMLReader xmlReader = spf.newSAXParser().getXMLReader();
166
167             JAXBContext jaxbContext = JAXBContext.newInstance(CreateVfModuleResponse.class,
168                     CreateVolumeGroupResponse.class, DeleteVfModuleResponse.class, DeleteVolumeGroupResponse.class);
169             Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
170
171             InputSource inputSource = new InputSource(new StringReader(input));
172             SAXSource source = new SAXSource(xmlReader, inputSource);
173             return jaxbUnmarshaller.unmarshal(source);
174         } catch (Exception e) {
175             logger.error(LoggingAnchor.THREE, MessageEnum.GENERAL_EXCEPTION.toString(),
176                     ErrorCode.SchemaError.getValue(), e.getMessage(), e);
177             throw new MarshallerException("Error parsing VNF Adapter response. " + e.getMessage(),
178                     ErrorCode.SchemaError.getValue(), e);
179         }
180     }
181
182     private void processVfModuleOutputs(BuildingBlockExecution execution, Map<String, String> vfModuleOutputs) {
183         if (vfModuleOutputs == null) {
184             return;
185         }
186         try {
187             VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
188             GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
189             List<String> contrailNetworkPolicyFqdnList = new ArrayList<>();
190             Iterator<String> keys = vfModuleOutputs.keySet().iterator();
191             while (keys.hasNext()) {
192                 String key = keys.next();
193                 if (key.equals("contrail-service-instance-fqdn")) {
194                     String contrailServiceInstanceFqdn = vfModuleOutputs.get(key);
195                     logger.debug("Obtained contrailServiceInstanceFqdn: {}", contrailServiceInstanceFqdn);
196                     vfModule.setContrailServiceInstanceFqdn(contrailServiceInstanceFqdn);
197                     execution.setVariable(CONTRAIL_SERVICE_INSTANCE_FQDN, contrailServiceInstanceFqdn);
198                 } else if (key.endsWith("contrail_network_policy_fqdn")) {
199                     String contrailNetworkPolicyFqdn = vfModuleOutputs.get(key);
200                     logger.debug("Obtained contrailNetworkPolicyFqdn: {}", contrailNetworkPolicyFqdn);
201                     contrailNetworkPolicyFqdnList.add(contrailNetworkPolicyFqdn);
202                 } else if (key.equals("oam_management_v4_address")) {
203                     String oamManagementV4Address = vfModuleOutputs.get(key);
204                     logger.debug("Obtained oamManagementV4Address: {}", oamManagementV4Address);
205                     genericVnf.setIpv4OamAddress(oamManagementV4Address);
206                     execution.setVariable(OAM_MANAGEMENT_V4_ADDRESS, oamManagementV4Address);
207                 } else if (key.equals("oam_management_v6_address")) {
208                     String oamManagementV6Address = vfModuleOutputs.get(key);
209                     logger.debug("Obtained oamManagementV6Address: {}", oamManagementV6Address);
210                     genericVnf.setManagementV6Address(oamManagementV6Address);
211                     execution.setVariable(OAM_MANAGEMENT_V6_ADDRESS, oamManagementV6Address);
212                 }
213
214                 if (!contrailNetworkPolicyFqdnList.isEmpty()) {
215                     execution.setVariable(CONTRAIL_NETWORK_POLICY_FQDN_LIST,
216                             String.join(",", contrailNetworkPolicyFqdnList));
217                 }
218             }
219         } catch (Exception ex) {
220             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
221         }
222
223     }
224 }