Merge "Fix for the snapshot in Arqullian Tests"
[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 //import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.request.information.RequestInformation;
37 //import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.sdnc.request.header.SdncRequestHeader;
38
39 /**
40  * Created by 10112215 on 2017/9/20.
41  */
42 public abstract class AbstractBuilder<IN, OUT> {
43
44      public static final String OPERATION_TYPE = "operationType";
45      public static final String RESOURCE_TYPE = "resourceType";
46
47      public static enum RequestAction {
48           CreateNetworkInstance(0, "CreateNetworkInstance"),
49           ActivateNetworkInstance(1, "ActivateNetworkInstance"),
50           CreateServiceInstance(2, "CreateServiceInstance"),
51           DeleteServiceInstance(3, "DeleteServiceInstance"),
52           DeleteNetworkInstance(4, "DeleteNetworkInstance"),
53           CreateVnfInstance(5, "CreateVnfInstance"),
54           ActivateVnfInstance(6, "ActivateVnfInstance"),
55           DeleteVnfInstance(7, "DeleteVnfInstance"),
56           CreateVfModuleInstance(8, "CreateVfModuleInstance"),
57           ActivateVfModuleInstance(9, "ActivateVfModuleInstance"),
58           DeleteVfModuleInstance(10, "DeleteVfModuleInstance"),
59           CreateContrailRouteInstance(11, "CreateContrailRouteInstance"),
60           DeleteContrailRouteInstance(12, "DeleteContrailRouteInstance"),
61           CreateSecurityZoneInstance(13, "CreateSecurityZoneInstance"),
62           DeleteSecurityZoneInstance(14, "DeleteSecurityZoneInstance"),
63           ActivateDCINetworkInstance(15, "ActivateDCINetworkInstance"),
64           DeActivateDCINetworkInstance(16, "DeActivateDCINetworkInstance");
65
66           String name;
67           int value;
68
69           private RequestAction(int value, String name) {
70                this.value = value;
71                this.name = name;
72           }
73
74           public String getName() {
75                return this.name;
76           }
77
78           public int getIntValue() {
79                return this.value;
80           }
81      }
82
83      public enum SvcAction {
84           Reserve(0, "reserve"),
85           Assign(1, "assign"),
86           Activate(2, "activate"),
87           Delete(3, "delete"),
88           Changeassign(4, "changeassign"),
89           Changedelete(5, "changedelete"),
90           Rollback(6, "rollback"),
91           Deactivate(7, "deactivate"),
92           Unassign(8, "unassign"),
93           Create(9, "create");
94
95           String name;
96           int value;
97
98           private SvcAction(int value, String name) {
99                this.value = value;
100                this.name = name;
101           }
102
103           public String getName() {
104                return this.name;
105           }
106
107           public int getIntValue() {
108                return this.value;
109           }
110      }
111
112      protected String requestId = null;
113
114      abstract OUT build(DelegateExecution execution, IN input) throws Exception;
115
116      protected String getRequestActoin(DelegateExecution execution) {
117           String action = /*RequestInformation.*/RequestAction.CreateNetworkInstance.getName();
118           String operType = (String) execution.getVariable(OPERATION_TYPE);
119           String resourceType = ((String) execution.getVariable(RESOURCE_TYPE)).toLowerCase();
120           if (!StringUtils.isBlank(operType)) {
121                if (RequestsDbConstant.OperationType.DELETE.equalsIgnoreCase(operType)) {
122                     if (isOverlay(resourceType)) {
123                          action = /*RequestInformation.*/RequestAction.DeActivateDCINetworkInstance.getName();
124                     } else if (isUnderlay(resourceType)) {
125                          action = /*RequestInformation.*/RequestAction.DeleteNetworkInstance.getName();
126                     } else {
127                          action = /*RequestInformation.*/RequestAction.DeleteServiceInstance.getName();
128                     }
129                } else if (RequestsDbConstant.OperationType.CREATE.equalsIgnoreCase(operType)) {
130                     if (isOverlay(resourceType)) {
131                          action = /*RequestInformation.*/RequestAction.ActivateDCINetworkInstance.getName();
132                     } else if (isUnderlay(resourceType)) {
133                          action = /*RequestInformation.*/RequestAction.CreateNetworkInstance.getName();
134                     } else {
135                          action = /*RequestInformation.*/RequestAction.CreateServiceInstance.getName();
136                     }
137                }
138           }
139           return action;
140      }
141
142      private boolean isOverlay(String resourceType) {
143           return !StringUtils.isBlank(resourceType) && resourceType.contains("overlay");
144      }
145
146      private boolean isUnderlay(String resourceType) {
147           return !StringUtils.isBlank(resourceType) && resourceType.contains("underlay");
148      }
149
150      protected String getSvcAction(DelegateExecution execution) {
151           String action = /*SdncRequestHeader.*/SvcAction.Create.getName();
152           String operType = (String) execution.getVariable(OPERATION_TYPE);
153           String resourceType = ((String) execution.getVariable(RESOURCE_TYPE)).toLowerCase();
154           if (!StringUtils.isBlank(operType)) {
155                if (RequestsDbConstant.OperationType.DELETE.equalsIgnoreCase(operType)) {
156                     if (isOverlay(resourceType)) {
157                          action = /*SdncRequestHeader.*/SvcAction.Deactivate.getName();
158                     } else if (isUnderlay(resourceType)) {
159                          action = /*SdncRequestHeader.*/SvcAction.Delete.getName();
160                     } else {
161                          action = /*SdncRequestHeader.*/SvcAction.Unassign.getName();
162                     }
163                } else if (RequestsDbConstant.OperationType.CREATE.equalsIgnoreCase(operType)) {
164                     if (isOverlay(resourceType)) {
165                          action = /*SdncRequestHeader.*/SvcAction.Activate.getName();
166                     } else if (isUnderlay(resourceType)) {
167                          action = /*SdncRequestHeader.*/SvcAction.Create.getName();
168                     } else {
169                          action = /*SdncRequestHeader.*/SvcAction.Assign.getName();
170                     }
171                }
172           }
173           return action;
174      }
175
176      protected synchronized String getRequestId(DelegateExecution execution) {
177           if (StringUtils.isBlank(requestId)) {
178                requestId = (String) execution.getVariable("msoRequestId");
179                if (StringUtils.isBlank(requestId)) {
180                     requestId = UUID.randomUUID().toString();
181                }
182           }
183           return requestId;
184      }
185
186      protected OnapModelInformationEntity getOnapServiceModelInformationEntity(DelegateExecution execution) {
187           OnapModelInformationEntity onapModelInformationEntity = new OnapModelInformationEntity();
188           {
189                String modelInvariantUuid = (String) execution.getVariable("modelInvariantUuid");
190                String modelVersion = (String) execution.getVariable("modelVersion");
191                String modelUuid = (String) execution.getVariable("modelUuid");
192                String modelName = (String) execution.getVariable("serviceModelName");
193                onapModelInformationEntity.setModelInvariantUuid(modelInvariantUuid);
194                onapModelInformationEntity.setModelVersion(modelVersion);
195                onapModelInformationEntity.setModelUuid(modelUuid);
196                onapModelInformationEntity.setModelName(modelName);
197           }
198           return onapModelInformationEntity;
199      }
200      
201      protected OnapModelInformationEntity getOnapNetworkModelInformationEntity(DelegateExecution execution) {
202          OnapModelInformationEntity onapModelInformationEntity = new OnapModelInformationEntity();
203          {
204               String modelInvariantUuid = (String) execution.getVariable("resourceInvariantUUID");
205               String modelVersion = (String) execution.getVariable("modelVersion");
206               String modelUuid = (String) execution.getVariable("resourceUUID");
207               String modelName = (String) execution.getVariable("resourceType");
208               onapModelInformationEntity.setModelInvariantUuid(modelInvariantUuid);
209               onapModelInformationEntity.setModelVersion(modelVersion);
210               onapModelInformationEntity.setModelUuid(modelUuid);
211               onapModelInformationEntity.setModelName(modelName);
212          }
213          return onapModelInformationEntity;
214     }
215
216      protected List<ParamEntity> getParamEntities(Map<String, String> inputs) {
217           List<ParamEntity> paramEntityList = new ArrayList<>();
218           if (inputs != null && !inputs.isEmpty()) {
219                inputs.keySet().forEach(key -> {
220                     ParamEntity paramEntity = new ParamEntity();
221                     paramEntity.setName(key);
222                     paramEntity.setValue(inputs.get(key));
223                     paramEntityList.add(paramEntity);
224                });
225           }
226           return paramEntityList;
227      }
228
229      protected RequestInformationEntity getRequestInformationEntity(DelegateExecution execution) {
230           RequestInformationEntity requestInformationEntity = new RequestInformationEntity();
231           {
232                requestInformationEntity.setRequestId(getRequestId(execution));
233                requestInformationEntity.setRequestAction(getRequestActoin(execution));
234           }
235           return requestInformationEntity;
236      }
237
238      protected ServiceInformationEntity getServiceInformationEntity(DelegateExecution execution) {
239           ServiceInformationEntity serviceInformationEntity = new ServiceInformationEntity();
240           serviceInformationEntity.setServiceId("VOLTE_SERVICE_ID");
241           serviceInformationEntity.setSubscriptionServiceType((String) execution.getVariable("serviceType"));
242           serviceInformationEntity.setOnapModelInformation(getOnapServiceModelInformationEntity(execution));
243           serviceInformationEntity.setServiceInstanceId((String) execution.getVariable("serviceInstanceId"));
244           serviceInformationEntity.setGlobalCustomerId((String) execution.getVariable("globalSubscriberId"));
245           return serviceInformationEntity;
246      }
247
248      protected String getServiceInstanceName(DelegateExecution execution) {
249           return (String) execution.getVariable("serviceInstanceName");
250      }
251 }