0634da726433899ad0d4c8ab2e2d0c0e6c47b3de
[so.git] /
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.name();
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.equals(operType)) {
122                     if (isOverlay(resourceType)) {
123                          action = /*RequestInformation.*/RequestAction.DeActivateDCINetworkInstance.name();
124                     } else if (isUnderlay(resourceType)) {
125                          action = /*RequestInformation.*/RequestAction.DeleteNetworkInstance.name();
126                     } else {
127                          action = /*RequestInformation.*/RequestAction.DeleteServiceInstance.name();
128                     }
129                } else if (RequestsDbConstant.OperationType.CREATE.equals(operType)) {
130                     if (isOverlay(resourceType)) {
131                          action = /*RequestInformation.*/RequestAction.ActivateDCINetworkInstance.name();
132                     } else if (isUnderlay(resourceType)) {
133                          action = /*RequestInformation.*/RequestAction.CreateNetworkInstance.name();
134                     } else {
135                          action = /*RequestInformation.*/RequestAction.CreateServiceInstance.name();
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.name();
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.equals(operType)) {
156                     if (isOverlay(resourceType)) {
157                          action = /*SdncRequestHeader.*/SvcAction.Deactivate.name();
158                     } else {
159                          action = /*SdncRequestHeader.*/SvcAction.Delete.name();
160                     }
161                } else if (RequestsDbConstant.OperationType.CREATE.equals(operType)) {
162                     if (isOverlay(resourceType)) {
163                          action = /*SdncRequestHeader.*/SvcAction.Activate.name();
164                     } else {
165                          action = /*SdncRequestHeader.*/SvcAction.Create.name();
166                     }
167                }
168           }
169           return action;
170      }
171
172      protected synchronized String getRequestId(DelegateExecution execution) {
173           if (StringUtils.isBlank(requestId)) {
174                requestId = (String) execution.getVariable("msoRequestId");
175                if (StringUtils.isBlank(requestId)) {
176                     requestId = UUID.randomUUID().toString();
177                }
178           }
179           return requestId;
180      }
181
182      protected OnapModelInformationEntity getOnapModelInformationEntity(DelegateExecution execution) {
183           OnapModelInformationEntity onapModelInformationEntity = new OnapModelInformationEntity();
184           {
185                String modelInvariantUuid = (String) execution.getVariable("modelInvariantUuid");
186                String modelVersion = (String) execution.getVariable("modelVersion");
187                String modelUuid = (String) execution.getVariable("modelUuid");
188                String modelName = (String) execution.getVariable("modelName");
189                onapModelInformationEntity.setModelInvariantUuid(modelInvariantUuid);
190                onapModelInformationEntity.setModelVersion(modelVersion);
191                onapModelInformationEntity.setModelUuid(modelUuid);
192                onapModelInformationEntity.setModelName(modelName);
193           }
194           return onapModelInformationEntity;
195      }
196
197      protected List<ParamEntity> getParamEntities(Map<String, String> inputs) {
198           List<ParamEntity> paramEntityList = new ArrayList<>();
199           if (inputs != null && !inputs.isEmpty()) {
200                inputs.keySet().forEach(key -> {
201                     ParamEntity paramEntity = new ParamEntity();
202                     paramEntity.setName(key);
203                     paramEntity.setValue(inputs.get(key));
204                     paramEntityList.add(paramEntity);
205                });
206           }
207           return paramEntityList;
208      }
209
210      protected RequestInformationEntity getRequestInformationEntity(DelegateExecution execution) {
211           RequestInformationEntity requestInformationEntity = new RequestInformationEntity();
212           {
213                requestInformationEntity.setRequestId(getRequestId(execution));
214                requestInformationEntity.setRequestAction(getRequestActoin(execution));
215           }
216           return requestInformationEntity;
217      }
218
219      protected ServiceInformationEntity getServiceInformationEntity(DelegateExecution execution) {
220           ServiceInformationEntity serviceInformationEntity = new ServiceInformationEntity();
221           serviceInformationEntity.setServiceId((String) execution.getVariable("productFamilyId"));
222           serviceInformationEntity.setSubscriptionServiceType((String) execution.getVariable("subscriptionServiceType"));
223           serviceInformationEntity.setOnapModelInformation(getOnapModelInformationEntity(execution));
224           serviceInformationEntity.setServiceInstanceId((String) execution.getVariable("serviceInstanceId"));
225           serviceInformationEntity.setGlobalCustomerId((String) execution.getVariable("globalSubscriberId"));
226           return serviceInformationEntity;
227      }
228
229      protected String getServiceInstanceName(DelegateExecution execution) {
230           return (String) execution.getVariable("serviceInstanceName");
231      }
232 }