2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.so.bpmn.servicedecomposition.tasks;
25 import java.lang.reflect.Field;
26 import java.lang.reflect.InvocationTargetException;
27 import java.util.List;
28 import java.util.Optional;
30 import javax.persistence.Id;
32 import org.onap.so.bpmn.common.BuildingBlockExecution;
33 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
34 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
35 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
36 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
37 import org.onap.so.client.exception.BBObjectNotFoundException;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.stereotype.Component;
42 import com.google.common.base.CaseFormat;
45 public class ExtractPojosForBB {
47 private static final Logger logger = LoggerFactory.getLogger(ExtractPojosForBB.class);
49 public <T> T extractByKey(BuildingBlockExecution execution, ResourceKey key) throws BBObjectNotFoundException {
50 return extractByKey(execution, key, execution.getLookupMap().get(key));
52 public <T> T extractByKey(BuildingBlockExecution execution, ResourceKey key, String value)
53 throws BBObjectNotFoundException {
55 Optional<T> result = Optional.empty();
56 GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
58 ServiceInstance serviceInstance;
61 case SERVICE_INSTANCE_ID:
62 result = lookupObjectInList(gBBInput.getCustomer().getServiceSubscription().getServiceInstances(), value);
65 serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
66 result = lookupObjectInList(serviceInstance.getVnfs(), value);
69 serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
70 result = lookupObjectInList(serviceInstance.getNetworks(), value);
73 vnf = extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
74 result = lookupObjectInList(vnf.getVolumeGroups(), value);
77 vnf = extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
78 result = lookupObjectInList(vnf.getVfModules(), value);
80 case ALLOTTED_RESOURCE_ID:
81 serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
82 result = lookupObjectInList(serviceInstance.getAllottedResources(), value);
84 case CONFIGURATION_ID:
85 serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
86 result = lookupObjectInList(serviceInstance.getConfigurations(), value);
89 serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
90 result = lookupObjectInList(gBBInput.getCustomer().getVpnBindings(), value);
92 case VPN_BONDING_LINK_ID:
93 serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
94 result = lookupObjectInList(serviceInstance.getVpnBondingLinks(),value);
96 case INSTANCE_GROUP_ID:
97 serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
98 result = lookupObjectInList(serviceInstance.getInstanceGroups(), value);
101 throw new BBObjectNotFoundException(key, value);
103 } catch (BBObjectNotFoundException e) { // re-throw parent object not found
105 } catch (Exception e) { // convert all other exceptions to object not found
106 logger.warn("BBObjectNotFoundException in ExtractPojosForBB", "BBObject " + key + " was not found in "
107 + "gBBInput using reference value: " + value);
108 throw new BBObjectNotFoundException(key, value);
111 if (result.isPresent()) {
114 throw new BBObjectNotFoundException(key, value);
118 protected <T> Optional<T> lookupObject(Object obj, String value) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
119 return findValue(obj, value);
122 protected <T> Optional<T> lookupObjectInList(List<?> list, String value) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
123 Optional<T> result = Optional.empty();
124 for (Object obj : list) {
125 result = findValue(obj, value);
126 if (result.isPresent()) {
134 protected <T> Optional<T> findValue(Object obj, String value) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
135 for (Field field : obj.getClass().getDeclaredFields()) {
136 if (field.isAnnotationPresent(Id.class)) {
137 String fieldName = field.getName();
138 fieldName = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, fieldName);
139 String fieldValue = (String) obj.getClass().getMethod("get" + fieldName).invoke(obj);
140 if (fieldValue.equals(value)) {
141 return Optional.of((T)obj);
146 return Optional.empty();