Replaced all tabs with spaces in java and pom.xml
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / java / org / onap / so / bpmn / infrastructure / workflow / serviceTask / client / builder / AbstractBuilder.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.infrastructure.workflow.serviceTask.client.builder;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.UUID;
27 import org.apache.commons.lang3.StringUtils;
28 import org.camunda.bpm.engine.delegate.DelegateExecution;
29 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.entity.OnapModelInformationEntity;
30 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.entity.ParamEntity;
31 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.entity.RequestInformationEntity;
32 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.entity.ServiceInformationEntity;
33 import org.onap.so.requestsdb.RequestsDbConstant;
34
35 public abstract class AbstractBuilder<I, O> {
36
37     public static final String OPERATION_TYPE = "operationType";
38     public static final String RESOURCE_TYPE = "resourceType";
39
40     public enum RequestAction {
41         CREATE_NETWORK_INSTANCE(0, "CreateNetworkInstance"), ACTIVATE_NETWORK_INSTANCE(1,
42                 "ActivateNetworkInstance"), CREATE_SERVICE_INSTANCE(2,
43                         "CreateServiceInstance"), DELETE_SERVICE_INSTANCE(3,
44                                 "DeleteServiceInstance"), DELETE_NETWORK_INSTANCE(4,
45                                         "DeleteNetworkInstance"), CREATE_VNF_INSTANCE(5,
46                                                 "CreateVnfInstance"), ACTIVATE_VNF_INSTANCE(6,
47                                                         "ActivateVnfInstance"), DELETE_VNF_INSTANCE(7,
48                                                                 "DeleteVnfInstance"), CREATE_VF_MODULE_INSTANCE(8,
49                                                                         "CreateVfModuleInstance"), ACTIVATE_VF_MODULE_INSTANCE(
50                                                                                 9,
51                                                                                 "ActivateVfModuleInstance"), DELETE_VF_MODULE_INSTANCE(
52                                                                                         10,
53                                                                                         "DeleteVfModuleInstance"), CREATE_CONTRAIL_ROUTE_INSTANCE(
54                                                                                                 11,
55                                                                                                 "CreateContrailRouteInstance"), DELETE_CONTRAIL_ROUTE_INSTANCE(
56                                                                                                         12,
57                                                                                                         "DeleteContrailRouteInstance"), CREATE_SECURITY_ZONE_INSTANCE(
58                                                                                                                 13,
59                                                                                                                 "CreateSecurityZoneInstance"), DELETE_SECURITY_ZONE_INSTANCE(
60                                                                                                                         14,
61                                                                                                                         "DeleteSecurityZoneInstance"), ACTIVATE_DCI_NETWORK_INSTANCE(
62                                                                                                                                 15,
63                                                                                                                                 "ActivateDCINetworkInstance"), DEACTIVATE_DCI_NETWORK_INSTANCE(
64                                                                                                                                         16,
65                                                                                                                                         "DeActivateDCINetworkInstance");
66
67         String name;
68         int value;
69
70         private RequestAction(int value, String name) {
71             this.value = value;
72             this.name = name;
73         }
74
75         public String getName() {
76             return this.name;
77         }
78
79         public int getIntValue() {
80             return this.value;
81         }
82     }
83
84     public enum SvcAction {
85         RESERVE(0, "reserve"), ASSIGN(1, "assign"), ACTIVATE(2, "activate"), DELETE(3, "delete"), CHANGEASSIGN(4,
86                 "changeassign"), CHANGEDELETE(5, "changedelete"), ROLLBACK(6,
87                         "rollback"), DEACTIVATE(7, "deactivate"), UNASSIGN(8, "unassign"), CREATE(9, "create");
88
89         String name;
90         int value;
91
92         private SvcAction(int value, String name) {
93             this.value = value;
94             this.name = name;
95         }
96
97         public String getName() {
98             return this.name;
99         }
100
101         public int getIntValue() {
102             return this.value;
103         }
104     }
105
106     protected String requestId = null;
107
108     abstract O build(DelegateExecution execution, I input) throws Exception;
109
110     protected String getRequestAction(DelegateExecution execution) {
111         String action = /* RequestInformation. */RequestAction.CREATE_NETWORK_INSTANCE.getName();
112         String operType = (String) execution.getVariable(OPERATION_TYPE);
113         String resourceType = (String) execution.getVariable(RESOURCE_TYPE);
114         if (!StringUtils.isBlank(operType)) {
115             if (RequestsDbConstant.OperationType.DELETE.equalsIgnoreCase(operType)) {
116                 if (isOverlay(resourceType)) {
117                     action = /* RequestInformation. */RequestAction.DEACTIVATE_DCI_NETWORK_INSTANCE.getName();
118                 } else if (isUnderlay(resourceType)) {
119                     action = /* RequestInformation. */RequestAction.DELETE_NETWORK_INSTANCE.getName();
120                 } else {
121                     action = /* RequestInformation. */RequestAction.DELETE_SERVICE_INSTANCE.getName();
122                 }
123             } else if (RequestsDbConstant.OperationType.CREATE.equalsIgnoreCase(operType)) {
124                 if (isOverlay(resourceType)) {
125                     action = /* RequestInformation. */RequestAction.ACTIVATE_DCI_NETWORK_INSTANCE.getName();
126                 } else if (isUnderlay(resourceType)) {
127                     action = /* RequestInformation. */RequestAction.CREATE_NETWORK_INSTANCE.getName();
128                 } else {
129                     action = /* RequestInformation. */RequestAction.CREATE_SERVICE_INSTANCE.getName();
130                 }
131             }
132         }
133         return action;
134     }
135
136     private boolean isOverlay(String resourceType) {
137         return !StringUtils.isBlank(resourceType) && resourceType.toLowerCase().contains("overlay");
138     }
139
140     private boolean isUnderlay(String resourceType) {
141         return !StringUtils.isBlank(resourceType) && resourceType.toLowerCase().contains("underlay");
142     }
143
144     protected String getSvcAction(DelegateExecution execution) {
145         String action = /* SdncRequestHeader. */SvcAction.CREATE.getName();
146         String operType = (String) execution.getVariable(OPERATION_TYPE);
147         String resourceType = (String) execution.getVariable(RESOURCE_TYPE);
148         if (!StringUtils.isBlank(operType)) {
149             if (RequestsDbConstant.OperationType.DELETE.equalsIgnoreCase(operType)) {
150                 if (isOverlay(resourceType)) {
151                     action = /* SdncRequestHeader. */SvcAction.DEACTIVATE.getName();
152                 } else if (isUnderlay(resourceType)) {
153                     action = /* SdncRequestHeader. */SvcAction.DELETE.getName();
154                 } else {
155                     action = /* SdncRequestHeader. */SvcAction.UNASSIGN.getName();
156                 }
157             } else if (RequestsDbConstant.OperationType.CREATE.equalsIgnoreCase(operType)) {
158                 if (isOverlay(resourceType)) {
159                     action = /* SdncRequestHeader. */SvcAction.ACTIVATE.getName();
160                 } else if (isUnderlay(resourceType)) {
161                     action = /* SdncRequestHeader. */SvcAction.CREATE.getName();
162                 } else {
163                     action = /* SdncRequestHeader. */SvcAction.ASSIGN.getName();
164                 }
165             }
166         }
167         return action;
168     }
169
170     protected synchronized String getRequestId(DelegateExecution execution) {
171         if (StringUtils.isBlank(requestId)) {
172             requestId = (String) execution.getVariable("msoRequestId");
173             if (StringUtils.isBlank(requestId)) {
174                 requestId = UUID.randomUUID().toString();
175             }
176         }
177         return requestId;
178     }
179
180     protected OnapModelInformationEntity getOnapServiceModelInformationEntity(DelegateExecution execution) {
181         OnapModelInformationEntity onapModelInformationEntity = new OnapModelInformationEntity();
182         String modelInvariantUuid = (String) execution.getVariable("modelInvariantUuid");
183         String modelVersion = (String) execution.getVariable("modelVersion");
184         String modelUuid = (String) execution.getVariable("modelUuid");
185         String modelName = (String) execution.getVariable("serviceModelName");
186         onapModelInformationEntity.setModelInvariantUuid(modelInvariantUuid);
187         onapModelInformationEntity.setModelVersion(modelVersion);
188         onapModelInformationEntity.setModelUuid(modelUuid);
189         onapModelInformationEntity.setModelName(modelName);
190         return onapModelInformationEntity;
191     }
192
193     protected OnapModelInformationEntity getOnapNetworkModelInformationEntity(DelegateExecution execution) {
194         OnapModelInformationEntity onapModelInformationEntity = new OnapModelInformationEntity();
195         String modelInvariantUuid = (String) execution.getVariable("resourceInvariantUUID");
196         String modelVersion = (String) execution.getVariable("modelVersion");
197         String modelUuid = (String) execution.getVariable("resourceUUID");
198         String modelName = (String) execution.getVariable(RESOURCE_TYPE);
199         onapModelInformationEntity.setModelInvariantUuid(modelInvariantUuid);
200         onapModelInformationEntity.setModelVersion(modelVersion);
201         onapModelInformationEntity.setModelUuid(modelUuid);
202         onapModelInformationEntity.setModelName(modelName);
203         return onapModelInformationEntity;
204     }
205
206     protected List<ParamEntity> getParamEntities(Map<String, String> inputs) {
207         List<ParamEntity> paramEntityList = new ArrayList<>();
208         if (inputs != null && !inputs.isEmpty()) {
209             inputs.keySet().forEach(key -> {
210                 ParamEntity paramEntity = new ParamEntity();
211                 paramEntity.setName(key);
212                 paramEntity.setValue(inputs.get(key));
213                 paramEntityList.add(paramEntity);
214             });
215         }
216         return paramEntityList;
217     }
218
219     protected RequestInformationEntity getRequestInformationEntity(DelegateExecution execution) {
220         RequestInformationEntity requestInformationEntity = new RequestInformationEntity();
221         requestInformationEntity.setRequestId(getRequestId(execution));
222         requestInformationEntity.setRequestAction(getRequestAction(execution));
223         return requestInformationEntity;
224     }
225
226     protected ServiceInformationEntity getServiceInformationEntity(DelegateExecution execution) {
227         ServiceInformationEntity serviceInformationEntity = new ServiceInformationEntity();
228         serviceInformationEntity.setServiceId((String) execution.getVariable("serviceInstanceId"));
229         serviceInformationEntity.setSubscriptionServiceType((String) execution.getVariable("serviceType"));
230         serviceInformationEntity.setOnapModelInformation(getOnapServiceModelInformationEntity(execution));
231         serviceInformationEntity.setServiceInstanceId((String) execution.getVariable("serviceInstanceId"));
232         serviceInformationEntity.setGlobalCustomerId((String) execution.getVariable("globalSubscriberId"));
233         return serviceInformationEntity;
234     }
235
236     protected String getServiceInstanceName(DelegateExecution execution) {
237         return (String) execution.getVariable("serviceInstanceName");
238     }
239 }