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