Containerization feature of SO
[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  * 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.bpmn.servicedecomposition.tasks;
22
23 import java.lang.reflect.Field;
24 import java.lang.reflect.InvocationTargetException;
25 import java.util.List;
26 import java.util.Optional;
27
28 import javax.persistence.Id;
29
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;
38
39 import com.google.common.base.CaseFormat;
40
41 @Component
42 public class ExtractPojosForBB {
43
44         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, ExtractPojosForBB.class);
45         
46         public <T> T extractByKey(BuildingBlockExecution execution, ResourceKey key, String value)
47                         throws BBObjectNotFoundException {
48
49                 Optional<T> result = Optional.empty();
50                 GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
51                 try {
52                         ServiceInstance serviceInstance;
53                         GenericVnf vnf;
54                         switch (key) {
55                                 case SERVICE_INSTANCE_ID:
56                                         result = lookupObjectInList(gBBInput.getCustomer().getServiceSubscription().getServiceInstances(), value);
57                                         break;
58                                 case GENERIC_VNF_ID:
59                                         serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
60                                         result = lookupObjectInList(serviceInstance.getVnfs(), value);
61                                         break;
62                                 case NETWORK_ID:
63                                         serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
64                                         result = lookupObjectInList(serviceInstance.getNetworks(), value);
65                                         break;
66                                 case VOLUME_GROUP_ID:
67                                         vnf = extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
68                                         result = lookupObjectInList(vnf.getVolumeGroups(), value);
69                                         break;
70                                 case VF_MODULE_ID:
71                                         vnf = extractByKey(execution, ResourceKey.GENERIC_VNF_ID, execution.getLookupMap().get(ResourceKey.GENERIC_VNF_ID));
72                                         result = lookupObjectInList(vnf.getVfModules(), value);
73                                         break;
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);
77                                         break;
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);
81                                         break;
82                                 case VPN_ID:
83                                         serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID, execution.getLookupMap().get(ResourceKey.SERVICE_INSTANCE_ID));
84                                         result = lookupObjectInList(gBBInput.getCustomer().getVpnBindings(), value);
85                                         break;
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);
89                                         break;
90                                 default:
91                                         throw new BBObjectNotFoundException(key, value);
92                         }
93                 } catch (BBObjectNotFoundException e) { // re-throw parent object not found
94                         throw e;
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);
98                 }
99                 
100                 if (result.isPresent()) {
101                         return result.get();
102                 } else {
103                         throw new BBObjectNotFoundException(key, value);
104                 }
105         }
106         
107         protected <T> Optional<T> lookupObject(Object obj, String value) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
108                 return findValue(obj, value);
109         }
110
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()) {
116                                 break;
117                         }
118                 }
119                 return result;
120                 
121         }
122         
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);
131                                 }
132                         }
133                 }
134                 
135                 return Optional.empty();
136         }
137 }