Configuration object status to Inventoried
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / bpmn / servicedecomposition / tasks / ExtractPojosForBB.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
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.servicedecomposition.tasks;
24
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;
40
41 @Component
42 public class ExtractPojosForBB {
43
44     private static final Logger logger = LoggerFactory.getLogger(ExtractPojosForBB.class);
45
46     public <T> T extractByKey(BuildingBlockExecution execution, ResourceKey key) throws BBObjectNotFoundException {
47         return extractByKey(execution, key, execution.getLookupMap().get(key));
48     }
49
50     protected <T> T extractByKey(BuildingBlockExecution execution, ResourceKey key, String value)
51             throws BBObjectNotFoundException {
52
53         Optional<T> result = Optional.empty();
54         GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
55         try {
56             ServiceInstance serviceInstance;
57             GenericVnf vnf;
58             switch (key) {
59                 case SERVICE_INSTANCE_ID:
60                     result = lookupObjectInList(gBBInput.getCustomer().getServiceSubscription().getServiceInstances(),
61                             value);
62                     break;
63                 case GENERIC_VNF_ID:
64                     serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
65                     result = lookupObjectInList(serviceInstance.getVnfs(), value);
66                     break;
67                 case NETWORK_ID:
68                     serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
69                     result = lookupObjectInList(serviceInstance.getNetworks(), value);
70                     break;
71                 case VOLUME_GROUP_ID:
72                     vnf = extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
73                     result = lookupObjectInList(vnf.getVolumeGroups(), value);
74                     break;
75                 case VF_MODULE_ID:
76                     vnf = extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
77                     result = lookupObjectInList(vnf.getVfModules(), value);
78                     break;
79                 case ALLOTTED_RESOURCE_ID:
80                     serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
81                     result = lookupObjectInList(serviceInstance.getAllottedResources(), value);
82                     break;
83                 case CONFIGURATION_ID:
84                     serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
85                     result = lookupObjectInList(serviceInstance.getConfigurations(), value);
86                     break;
87                 case VPN_ID:
88                     serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
89                     result = lookupObjectInList(gBBInput.getCustomer().getVpnBindings(), value);
90                     break;
91                 case VPN_BONDING_LINK_ID:
92                     serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
93                     result = lookupObjectInList(serviceInstance.getVpnBondingLinks(), value);
94                     break;
95                 case INSTANCE_GROUP_ID:
96                     serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
97                     result = lookupObjectInList(serviceInstance.getInstanceGroups(), value);
98                     break;
99                 default:
100                     throw new BBObjectNotFoundException(key, value);
101             }
102         } catch (BBObjectNotFoundException e) { // re-throw parent object not found
103             throw e;
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);
108         }
109
110         if (result.isPresent()) {
111             return result.get();
112         } else {
113             throw new BBObjectNotFoundException(key, value);
114         }
115     }
116
117     protected <T> Optional<T> lookupObject(Object obj, String value) throws IllegalAccessException,
118             IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
119         return findValue(obj, value);
120     }
121
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()) {
128                 break;
129             }
130         }
131         return result;
132
133     }
134
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);
144                 }
145             }
146         }
147
148         return Optional.empty();
149     }
150 }