Change serviceInstance CM retrieval
[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                     if (gBBInput.getCustomer().getServiceSubscription() == null
61                             && gBBInput.getServiceInstance() != null) {
62                         result = Optional.of((T) gBBInput.getServiceInstance());
63                     } else {
64                         result = lookupObjectInList(
65                                 gBBInput.getCustomer().getServiceSubscription().getServiceInstances(), value);
66                     }
67                     break;
68                 case GENERIC_VNF_ID:
69                     serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
70                     result = lookupObjectInList(serviceInstance.getVnfs(), value);
71                     break;
72                 case NETWORK_ID:
73                     serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
74                     result = lookupObjectInList(serviceInstance.getNetworks(), value);
75                     break;
76                 case VOLUME_GROUP_ID:
77                     vnf = extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
78                     result = lookupObjectInList(vnf.getVolumeGroups(), value);
79                     break;
80                 case VF_MODULE_ID:
81                     vnf = extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
82                     result = lookupObjectInList(vnf.getVfModules(), value);
83                     break;
84                 case ALLOTTED_RESOURCE_ID:
85                     serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
86                     result = lookupObjectInList(serviceInstance.getAllottedResources(), value);
87                     break;
88                 case CONFIGURATION_ID:
89                     serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
90                     result = lookupObjectInList(serviceInstance.getConfigurations(), value);
91                     break;
92                 case VPN_ID:
93                     serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
94                     result = lookupObjectInList(gBBInput.getCustomer().getVpnBindings(), value);
95                     break;
96                 case VPN_BONDING_LINK_ID:
97                     serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
98                     result = lookupObjectInList(serviceInstance.getVpnBondingLinks(), value);
99                     break;
100                 case INSTANCE_GROUP_ID:
101                     serviceInstance = extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
102                     result = lookupObjectInList(serviceInstance.getInstanceGroups(), value);
103                     break;
104                 default:
105                     throw new BBObjectNotFoundException(key, value);
106             }
107         } catch (BBObjectNotFoundException e) { // re-throw parent object not found
108             throw e;
109         } catch (Exception e) { // convert all other exceptions to object not found
110             logger.warn("BBObjectNotFoundException in ExtractPojosForBB",
111                     "BBObject " + key + " was not found in " + "gBBInput using reference value: " + value);
112             throw new BBObjectNotFoundException(key, value);
113         }
114
115         if (result.isPresent()) {
116             return result.get();
117         } else {
118             throw new BBObjectNotFoundException(key, value);
119         }
120     }
121
122     protected <T> Optional<T> lookupObject(Object obj, String value) throws IllegalAccessException,
123             IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
124         return findValue(obj, value);
125     }
126
127     protected <T> Optional<T> lookupObjectInList(List<?> list, String value) throws IllegalAccessException,
128             IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
129         Optional<T> result = Optional.empty();
130         for (Object obj : list) {
131             result = findValue(obj, value);
132             if (result.isPresent()) {
133                 break;
134             }
135         }
136         return result;
137
138     }
139
140     protected <T> Optional<T> findValue(Object obj, String value) throws IllegalAccessException,
141             IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
142         for (Field field : obj.getClass().getDeclaredFields()) {
143             if (field.isAnnotationPresent(Id.class)) {
144                 String fieldName = field.getName();
145                 fieldName = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, fieldName);
146                 String fieldValue = (String) obj.getClass().getMethod("get" + fieldName).invoke(obj);
147                 if (fieldValue.equals(value)) {
148                     return Optional.of((T) obj);
149                 }
150             }
151         }
152
153         return Optional.empty();
154     }
155 }