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;
29 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.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.stereotype.Component;
39 import com.google.common.base.CaseFormat;
42 public class ExtractPojosForBB {
44 private static final Logger logger = LoggerFactory.getLogger(ExtractPojosForBB.class);
46 public <T> T extractByKey(BuildingBlockExecution execution, ResourceKey key) throws BBObjectNotFoundException {
47 return extractByKey(execution, key, execution.getLookupMap().get(key));
50 protected <T> T extractByKey(BuildingBlockExecution execution, ResourceKey key, String value)
51 throws BBObjectNotFoundException {
53 Optional<T> result = Optional.empty();
54 GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
56 ServiceInstance serviceInstance;
59 case SERVICE_INSTANCE_ID:
60 result = lookupObjectInList(gBBInput.getCustomer().getServiceSubscription().getServiceInstances(),
64 serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
65 result = lookupObjectInList(serviceInstance.getVnfs(), value);
68 serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
69 result = lookupObjectInList(serviceInstance.getNetworks(), value);
72 vnf = extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
73 result = lookupObjectInList(vnf.getVolumeGroups(), value);
76 vnf = extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
77 result = lookupObjectInList(vnf.getVfModules(), value);
79 case ALLOTTED_RESOURCE_ID:
80 serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
81 result = lookupObjectInList(serviceInstance.getAllottedResources(), value);
83 case CONFIGURATION_ID:
84 serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
85 result = lookupObjectInList(serviceInstance.getConfigurations(), value);
88 serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
89 result = lookupObjectInList(gBBInput.getCustomer().getVpnBindings(), value);
91 case VPN_BONDING_LINK_ID:
92 serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
93 result = lookupObjectInList(serviceInstance.getVpnBondingLinks(), value);
95 case INSTANCE_GROUP_ID:
96 serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
97 result = lookupObjectInList(serviceInstance.getInstanceGroups(), value);
100 throw new BBObjectNotFoundException(key, value);
102 } catch (BBObjectNotFoundException e) { // re-throw parent object not found
104 } catch (Exception e) { // convert all other exceptions to object not found
105 logger.warn("BBObjectNotFoundException in ExtractPojosForBB",
106 "BBObject " + key + " was not found in " + "gBBInput using reference value: " + value);
107 throw new BBObjectNotFoundException(key, value);
110 if (result.isPresent()) {
113 throw new BBObjectNotFoundException(key, value);
117 protected <T> Optional<T> lookupObject(Object obj, String value) throws IllegalAccessException,
118 IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
119 return findValue(obj, value);
122 protected <T> Optional<T> lookupObjectInList(List<?> list, String value) throws IllegalAccessException,
123 IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
124 Optional<T> result = Optional.empty();
125 for (Object obj : list) {
126 result = findValue(obj, value);
127 if (result.isPresent()) {
135 protected <T> Optional<T> findValue(Object obj, String value) throws IllegalAccessException,
136 IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
137 for (Field field : obj.getClass().getDeclaredFields()) {
138 if (field.isAnnotationPresent(Id.class)) {
139 String fieldName = field.getName();
140 fieldName = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, fieldName);
141 String fieldValue = (String) obj.getClass().getMethod("get" + fieldName).invoke(obj);
142 if (fieldValue.equals(value)) {
143 return Optional.of((T) obj);
148 return Optional.empty();