Containerization feature of SO
[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
28 import org.apache.commons.lang3.StringUtils;
29 import org.camunda.bpm.engine.delegate.DelegateExecution;
30 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.entity.OnapModelInformationEntity;
31 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.entity.ParamEntity;
32 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.entity.RequestInformationEntity;
33 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.entity.ServiceInformationEntity;
34 import org.onap.so.requestsdb.RequestsDbConstant;
35
36 public abstract class AbstractBuilder<I, O> {
37
38      public static final String OPERATION_TYPE = "operationType";
39      public static final String RESOURCE_TYPE = "resourceType";
40
41      public enum RequestAction {
42           CREATE_NETWORK_INSTANCE(0, "CreateNetworkInstance"),
43           ACTIVATE_NETWORK_INSTANCE(1, "ActivateNetworkInstance"),
44           CREATE_SERVICE_INSTANCE(2, "CreateServiceInstance"),
45           DELETE_SERVICE_INSTANCE(3, "DeleteServiceInstance"),
46           DELETE_NETWORK_INSTANCE(4, "DeleteNetworkInstance"),
47           CREATE_VNF_INSTANCE(5, "CreateVnfInstance"),
48           ACTIVATE_VNF_INSTANCE(6, "ActivateVnfInstance"),
49           DELETE_VNF_INSTANCE(7, "DeleteVnfInstance"),
50           CREATE_VF_MODULE_INSTANCE(8, "CreateVfModuleInstance"),
51           ACTIVATE_VF_MODULE_INSTANCE(9, "ActivateVfModuleInstance"),
52           DELETE_VF_MODULE_INSTANCE(10, "DeleteVfModuleInstance"),
53           CREATE_CONTRAIL_ROUTE_INSTANCE(11, "CreateContrailRouteInstance"),
54           DELETE_CONTRAIL_ROUTE_INSTANCE(12, "DeleteContrailRouteInstance"),
55           CREATE_SECURITY_ZONE_INSTANCE(13, "CreateSecurityZoneInstance"),
56           DELETE_SECURITY_ZONE_INSTANCE(14, "DeleteSecurityZoneInstance"),
57           ACTIVATE_DCI_NETWORK_INSTANCE(15, "ActivateDCINetworkInstance"),
58           DEACTIVATE_DCI_NETWORK_INSTANCE(16, "DeActivateDCINetworkInstance");
59
60           String name;
61           int value;
62
63           private RequestAction(int value, String name) {
64                this.value = value;
65                this.name = name;
66           }
67
68           public String getName() {
69                return this.name;
70           }
71
72           public int getIntValue() {
73                return this.value;
74           }
75      }
76
77      public enum SvcAction {
78           RESERVE(0, "reserve"),
79           ASSIGN(1, "assign"),
80           ACTIVATE(2, "activate"),
81           DELETE(3, "delete"),
82           CHANGEASSIGN(4, "changeassign"),
83           CHANGEDELETE(5, "changedelete"),
84           ROLLBACK(6, "rollback"),
85           DEACTIVATE(7, "deactivate"),
86           UNASSIGN(8, "unassign"),
87           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 }