2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.servicedecomposition.tasks;
23 import java.lang.reflect.Field;
24 import java.lang.reflect.InvocationTargetException;
25 import java.util.List;
26 import java.util.Optional;
28 import javax.persistence.Id;
30 import org.onap.so.bpmn.common.BuildingBlockExecution;
31 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
32 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
33 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
34 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
35 import org.onap.so.client.exception.BBObjectNotFoundException;
36 import org.onap.so.logger.MsoLogger;
37 import org.springframework.stereotype.Component;
39 import com.google.common.base.CaseFormat;
42 public class ExtractPojosForBB {
44 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, ExtractPojosForBB.class);
46 public <T> T extractByKey(BuildingBlockExecution execution, ResourceKey key, String value)
47 throws BBObjectNotFoundException {
49 Optional<T> result = Optional.empty();
50 GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
52 ServiceInstance serviceInstance;
55 case SERVICE_INSTANCE_ID:
56 result = lookupObjectInList(gBBInput.getCustomer().getServiceSubscription().getServiceInstances(), value);
59 serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
60 result = lookupObjectInList(serviceInstance.getVnfs(), value);
63 serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
64 result = lookupObjectInList(serviceInstance.getNetworks(), value);
67 vnf = extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
68 result = lookupObjectInList(vnf.getVolumeGroups(), value);
71 vnf = extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
72 result = lookupObjectInList(vnf.getVfModules(), value);
74 case ALLOTTED_RESOURCE_ID:
75 serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
76 result = lookupObjectInList(serviceInstance.getAllottedResources(), value);
78 case CONFIGURATION_ID:
79 serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
80 result = lookupObjectInList(serviceInstance.getConfigurations(), value);
83 serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
84 result = lookupObjectInList(gBBInput.getCustomer().getVpnBindings(), value);
86 case VPN_BONDING_LINK_ID:
87 serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
88 result = lookupObjectInList(serviceInstance.getVpnBondingLinks(),value);
91 throw new BBObjectNotFoundException(key, value);
93 } catch (BBObjectNotFoundException e) { // re-throw parent object not found
95 } catch (Exception e) { // convert all other exceptions to object not found
96 msoLogger.warnSimple("BBObjectNotFoundException in ExtractPojosForBB", "BBObject " + key + " was not found in gBBInput using reference value: " + value);
97 throw new BBObjectNotFoundException(key, value);
100 if (result.isPresent()) {
103 throw new BBObjectNotFoundException(key, value);
107 protected <T> Optional<T> lookupObject(Object obj, String value) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
108 return findValue(obj, value);
111 protected <T> Optional<T> lookupObjectInList(List<?> list, String value) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
112 Optional<T> result = Optional.empty();
113 for (Object obj : list) {
114 result = findValue(obj, value);
115 if (result.isPresent()) {
123 protected <T> Optional<T> findValue(Object obj, String value) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
124 for (Field field : obj.getClass().getDeclaredFields()) {
125 if (field.isAnnotationPresent(Id.class)) {
126 String fieldName = field.getName();
127 fieldName = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, fieldName);
128 String fieldValue = (String) obj.getClass().getMethod("get" + fieldName).invoke(obj);
129 if (fieldValue.equals(value)) {
130 return Optional.of((T)obj);
135 return Optional.empty();