Merge "Reorder modifiers"
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / java / org / openecomp / mso / 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.openecomp.mso.bpmn.infrastructure.workflow.serviceTask.client.builder;
22
23 import org.apache.commons.lang3.StringUtils;
24 import org.camunda.bpm.engine.delegate.DelegateExecution;
25 import org.openecomp.mso.bpmn.infrastructure.workflow.serviceTask.client.entity.OnapModelInformationEntity;
26 import org.openecomp.mso.bpmn.infrastructure.workflow.serviceTask.client.entity.ParamEntity;
27 import org.openecomp.mso.bpmn.infrastructure.workflow.serviceTask.client.entity.RequestInformationEntity;
28 import org.openecomp.mso.bpmn.infrastructure.workflow.serviceTask.client.entity.ServiceInformationEntity;
29 import org.openecomp.mso.requestsdb.RequestsDbConstant;
30
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.UUID;
35
36 public abstract class AbstractBuilder<IN, OUT> {
37
38      public static final String OPERATION_TYPE = "operationType";
39      public static final String RESOURCE_TYPE = "resourceType";
40
41      public static enum RequestAction {
42           CreateNetworkInstance(0, "CreateNetworkInstance"),
43           ActivateNetworkInstance(1, "ActivateNetworkInstance"),
44           CreateServiceInstance(2, "CreateServiceInstance"),
45           DeleteServiceInstance(3, "DeleteServiceInstance"),
46           DeleteNetworkInstance(4, "DeleteNetworkInstance"),
47           CreateVnfInstance(5, "CreateVnfInstance"),
48           ActivateVnfInstance(6, "ActivateVnfInstance"),
49           DeleteVnfInstance(7, "DeleteVnfInstance"),
50           CreateVfModuleInstance(8, "CreateVfModuleInstance"),
51           ActivateVfModuleInstance(9, "ActivateVfModuleInstance"),
52           DeleteVfModuleInstance(10, "DeleteVfModuleInstance"),
53           CreateContrailRouteInstance(11, "CreateContrailRouteInstance"),
54           DeleteContrailRouteInstance(12, "DeleteContrailRouteInstance"),
55           CreateSecurityZoneInstance(13, "CreateSecurityZoneInstance"),
56           DeleteSecurityZoneInstance(14, "DeleteSecurityZoneInstance"),
57           ActivateDCINetworkInstance(15, "ActivateDCINetworkInstance"),
58           DeActivateDCINetworkInstance(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 OUT build(DelegateExecution execution, IN input) throws Exception;
109
110      protected String getRequestActoin(DelegateExecution execution) {
111           String action = /*RequestInformation.*/RequestAction.CreateNetworkInstance.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.DeActivateDCINetworkInstance.getName();
118                     } else if (isUnderlay(resourceType)) {
119                          action = /*RequestInformation.*/RequestAction.DeleteNetworkInstance.getName();
120                     } else {
121                          action = /*RequestInformation.*/RequestAction.DeleteServiceInstance.getName();
122                     }
123                } else if (RequestsDbConstant.OperationType.CREATE.equalsIgnoreCase(operType)) {
124                     if (isOverlay(resourceType)) {
125                          action = /*RequestInformation.*/RequestAction.ActivateDCINetworkInstance.getName();
126                     } else if (isUnderlay(resourceType)) {
127                          action = /*RequestInformation.*/RequestAction.CreateNetworkInstance.getName();
128                     } else {
129                          action = /*RequestInformation.*/RequestAction.CreateServiceInstance.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           {
183                String modelInvariantUuid = (String) execution.getVariable("modelInvariantUuid");
184                String modelVersion = (String) execution.getVariable("modelVersion");
185                String modelUuid = (String) execution.getVariable("modelUuid");
186                String modelName = (String) execution.getVariable("serviceModelName");
187                onapModelInformationEntity.setModelInvariantUuid(modelInvariantUuid);
188                onapModelInformationEntity.setModelVersion(modelVersion);
189                onapModelInformationEntity.setModelUuid(modelUuid);
190                onapModelInformationEntity.setModelName(modelName);
191           }
192           return onapModelInformationEntity;
193      }
194      
195      protected OnapModelInformationEntity getOnapNetworkModelInformationEntity(DelegateExecution execution) {
196          OnapModelInformationEntity onapModelInformationEntity = new OnapModelInformationEntity();
197          {
198               String modelInvariantUuid = (String) execution.getVariable("resourceInvariantUUID");
199               String modelVersion = (String) execution.getVariable("modelVersion");
200               String modelUuid = (String) execution.getVariable("resourceUUID");
201               String modelName = (String) execution.getVariable("resourceType");
202               onapModelInformationEntity.setModelInvariantUuid(modelInvariantUuid);
203               onapModelInformationEntity.setModelVersion(modelVersion);
204               onapModelInformationEntity.setModelUuid(modelUuid);
205               onapModelInformationEntity.setModelName(modelName);
206          }
207          return onapModelInformationEntity;
208     }
209
210      protected List<ParamEntity> getParamEntities(Map<String, String> inputs) {
211           List<ParamEntity> paramEntityList = new ArrayList<>();
212           if (inputs != null && !inputs.isEmpty()) {
213                inputs.keySet().forEach(key -> {
214                     ParamEntity paramEntity = new ParamEntity();
215                     paramEntity.setName(key);
216                     paramEntity.setValue(inputs.get(key));
217                     paramEntityList.add(paramEntity);
218                });
219           }
220           return paramEntityList;
221      }
222
223      protected RequestInformationEntity getRequestInformationEntity(DelegateExecution execution) {
224           RequestInformationEntity requestInformationEntity = new RequestInformationEntity();
225           {
226                requestInformationEntity.setRequestId(getRequestId(execution));
227                requestInformationEntity.setRequestAction(getRequestActoin(execution));
228           }
229           return requestInformationEntity;
230      }
231
232      protected ServiceInformationEntity getServiceInformationEntity(DelegateExecution execution) {
233           ServiceInformationEntity serviceInformationEntity = new ServiceInformationEntity();
234           serviceInformationEntity.setServiceId((String) execution.getVariable("serviceInstanceId"));
235           serviceInformationEntity.setSubscriptionServiceType((String) execution.getVariable("serviceType"));
236           serviceInformationEntity.setOnapModelInformation(getOnapServiceModelInformationEntity(execution));
237           serviceInformationEntity.setServiceInstanceId((String) execution.getVariable("serviceInstanceId"));
238           serviceInformationEntity.setGlobalCustomerId((String) execution.getVariable("globalSubscriberId"));
239           return serviceInformationEntity;
240      }
241
242      protected String getServiceInstanceName(DelegateExecution execution) {
243           return (String) execution.getVariable("serviceInstanceName");
244      }
245 }