Adjust RelatedInstancesValidation to enable child service requests
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / onap / so / apihandlerinfra / validation / RelatedInstancesValidation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7  * ================================================================================
8  * Modifications Copyright (c) 2019 Samsung
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.so.apihandlerinfra.validation;
25
26 import org.onap.so.apihandlerinfra.Action;
27 import org.onap.so.apihandlerinfra.Actions;
28 import org.onap.so.apihandlerinfra.Constants;
29 import org.onap.so.exceptions.ValidationException;
30 import org.onap.so.serviceinstancebeans.InstanceDirection;
31 import org.onap.so.serviceinstancebeans.ModelInfo;
32 import org.onap.so.serviceinstancebeans.ModelType;
33 import org.onap.so.serviceinstancebeans.RelatedInstance;
34 import org.onap.so.serviceinstancebeans.RelatedInstanceList;
35 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
36 import org.onap.so.utils.UUIDChecker;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public class RelatedInstancesValidation implements ValidationRule {
41
42     private static Logger logger = LoggerFactory.getLogger(RelatedInstancesValidation.class);
43
44     private static boolean empty(String s) {
45         return (s == null || s.trim().isEmpty());
46     }
47
48     @Override
49     public ValidationInformation validate(ValidationInformation info) throws ValidationException {
50         ServiceInstancesRequest sir = info.getSir();
51         Actions action = info.getAction();
52         int reqVersion = info.getReqVersion();
53         String requestScope = info.getRequestScope();
54         String serviceInstanceType = null;
55         String networkType = null;
56         String vnfType = null;
57         String vfModuleType = null;
58         String vfModuleModelName = null;
59         ModelInfo modelInfo = info.getSir().getRequestDetails().getModelInfo();
60         RelatedInstanceList[] instanceList = sir.getRequestDetails().getRelatedInstanceList();
61         String serviceModelName = null;
62         String vnfModelName = null;
63         String asdcServiceModelVersion = null;
64         String volumeGroupId = null;
65         boolean isRelatedServiceInstancePresent = false;
66         boolean isRelatedVnfInstancePresent = false;
67         boolean isSourceVnfPresent = false;
68         boolean isDestinationVnfPresent = false;
69         boolean isConnectionPointPresent = false;
70
71         if (requestScope.equalsIgnoreCase(ModelType.service.name())) {
72             serviceInstanceType = modelInfo.getModelName();
73             info.setServiceInstanceType(serviceInstanceType);
74         }
75         if (requestScope.equalsIgnoreCase(ModelType.network.name())) {
76             networkType = modelInfo.getModelName();
77             info.setNetworkType(networkType);
78         }
79         if (instanceList != null) {
80             for (RelatedInstanceList relatedInstanceList : instanceList) {
81                 RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();
82
83                 ModelInfo relatedInstanceModelInfo = relatedInstance.getModelInfo();
84                 if (relatedInstanceModelInfo == null) {
85                     throw new ValidationException("modelInfo in relatedInstance");
86                 }
87
88                 if (relatedInstanceModelInfo.getModelType() == null) {
89                     throw new ValidationException("modelType in relatedInstance");
90                 }
91
92                 if (empty(relatedInstance.getInstanceName())
93                         && ModelType.pnf.equals(relatedInstanceModelInfo.getModelType())) {
94                     throw new ValidationException("instanceName in relatedInstance for pnf modelType");
95                 }
96
97                 if (!empty(relatedInstance.getInstanceName())) {
98                     if (!relatedInstance.getInstanceName().matches(Constants.VALID_INSTANCE_NAME_FORMAT)) {
99                         throw new ValidationException("instanceName format in relatedInstance");
100                     }
101                 }
102
103                 if (empty(relatedInstance.getInstanceId())
104                         && !ModelType.pnf.equals(relatedInstanceModelInfo.getModelType())) {
105                     throw new ValidationException("instanceId in relatedInstance");
106                 }
107
108                 if (!empty(relatedInstance.getInstanceId())
109                         && !UUIDChecker.isValidUUID(relatedInstance.getInstanceId())) {
110                     throw new ValidationException("instanceId format in relatedInstance");
111                 }
112                 if (empty(relatedInstanceModelInfo.getModelVersionId())
113                         && requestScope.equals(ModelType.instanceGroup.toString())
114                         && relatedInstanceModelInfo.getModelType().equals(ModelType.service)) {
115                     throw new ValidationException("modelVersionId in relatedInstance", true);
116                 }
117                 if (requestScope.equalsIgnoreCase(ModelType.instanceGroup.toString())
118                         && relatedInstanceModelInfo.getModelType().equals(ModelType.service)) {
119                     isRelatedServiceInstancePresent = true;
120                 }
121
122                 if (requestScope.equalsIgnoreCase(ModelType.service.name())) {
123                     if (empty(relatedInstance.getInstanceName())
124                             && ModelType.vpnBinding.equals(relatedInstanceModelInfo.getModelType())) {
125                         throw new ValidationException("instanceName in relatedInstance for vpnBinding modelType", true);
126                     }
127                     if (empty(relatedInstance.getInstanceName())
128                             && ModelType.network.equals(relatedInstanceModelInfo.getModelType())) {
129                         throw new ValidationException("instanceName in relatedInstance for network modelType", true);
130                     }
131                 }
132
133                 if (action != Action.deleteInstance
134                         && !requestScope.equalsIgnoreCase(ModelType.instanceGroup.toString())) {
135                     if (!(relatedInstanceModelInfo.getModelType().equals(ModelType.volumeGroup)
136                             || relatedInstanceModelInfo.getModelType().equals(ModelType.connectionPoint)
137                             || relatedInstanceModelInfo.getModelType().equals(ModelType.pnf)
138                             || relatedInstanceModelInfo.getModelType().equals(ModelType.networkInstanceGroup)
139                             || relatedInstanceModelInfo.getModelType().equals(ModelType.network)
140                             || relatedInstanceModelInfo.getModelType().equals(ModelType.vpnBinding))) {
141
142                         if (empty(relatedInstanceModelInfo.getModelInvariantId())) {
143                             throw new ValidationException("modelInvariantId in relatedInstance");
144                         } else if (reqVersion >= 4 && empty(relatedInstanceModelInfo.getModelVersionId())) {
145                             throw new ValidationException("modelVersionId in relatedInstance");
146                         } else if (empty(relatedInstanceModelInfo.getModelName())) {
147                             throw new ValidationException("modelName in relatedInstance");
148                         } else if (empty(relatedInstanceModelInfo.getModelVersion())) {
149                             throw new ValidationException("modelVersion in relatedInstance");
150                         }
151                     }
152
153                     if (!empty(relatedInstanceModelInfo.getModelInvariantId())
154                             && !UUIDChecker.isValidUUID(relatedInstanceModelInfo.getModelInvariantId())) {
155                         throw new ValidationException("modelInvariantId format in relatedInstance");
156                     }
157
158                     if (ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
159                         if (InstanceDirection.source.equals(relatedInstance.getInstanceDirection())
160                                 && relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)) {
161                             isSourceVnfPresent = true;
162                         } else if (InstanceDirection.destination.equals(relatedInstance.getInstanceDirection())
163                                 && (relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)
164                                         || (relatedInstanceModelInfo.getModelType().equals(ModelType.pnf)))) {
165                             isDestinationVnfPresent = true;
166                         }
167                     }
168
169                     if (ModelType.connectionPoint.equals(relatedInstanceModelInfo.getModelType())
170                             && ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
171                         isConnectionPointPresent = true;
172                     }
173                 }
174
175                 if (empty(relatedInstanceModelInfo.getModelCustomizationName())
176                         && relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)) {
177                     if (reqVersion >= 4 && empty(relatedInstanceModelInfo.getModelCustomizationId())
178                             && action != Action.deleteInstance) {
179                         throw new ValidationException(
180                                 "modelCustomizationName or modelCustomizationId in relatedInstance of vnf");
181                     }
182                 }
183
184                 if (relatedInstanceModelInfo.getModelType().equals(ModelType.service)
185                         && !(requestScope.equalsIgnoreCase(ModelType.instanceGroup.toString())
186                                 && action == Action.createInstance)) {
187                     isRelatedServiceInstancePresent = true;
188                     // InstanceDirection.source indicates child service and this validation is for all
189                     // except child services.
190                     if (!InstanceDirection.source.equals(relatedInstance.getInstanceDirection())
191                             && !relatedInstance.getInstanceId().equals(sir.getServiceInstanceId())) {
192                         throw new ValidationException(
193                                 "serviceInstanceId matching the serviceInstanceId in request URI");
194                     }
195                     serviceModelName = relatedInstanceModelInfo.getModelName();
196                     asdcServiceModelVersion = relatedInstanceModelInfo.getModelVersion();
197                 } else if (relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)
198                         && !(ModelType.configuration.name().equalsIgnoreCase(requestScope))) {
199                     isRelatedVnfInstancePresent = true;
200                     if (!relatedInstance.getInstanceId().equals(sir.getVnfInstanceId())) {
201                         throw new ValidationException("vnfInstanceId matching the vnfInstanceId in request URI");
202                     }
203                     vnfModelName = relatedInstanceModelInfo.getModelCustomizationName();
204                 } else if (relatedInstanceModelInfo.getModelType().equals(ModelType.volumeGroup)) {
205                     volumeGroupId = relatedInstance.getInstanceId();
206                 }
207             }
208
209             if (action == Action.createInstance && ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
210                 if (!isSourceVnfPresent) {
211                     throw new ValidationException("source vnf relatedInstance for Port Configuration");
212                 }
213
214                 if (!isDestinationVnfPresent) {
215                     throw new ValidationException("destination vnf relatedInstance for Port Configuration");
216                 }
217             }
218
219             if ((action == Action.enablePort || action == Action.disablePort)
220                     && ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
221                 if (!isConnectionPointPresent) {
222                     throw new ValidationException("connectionPoint relatedInstance for Port Configuration");
223                 }
224             }
225             if (requestScope.equals(ModelType.instanceGroup.toString())) {
226                 if (!isRelatedServiceInstancePresent) {
227                     throw new ValidationException("related service instance for instanceGroup request", true);
228                 }
229             }
230             if (requestScope.equalsIgnoreCase(ModelType.volumeGroup.name())) {
231                 if (!isRelatedServiceInstancePresent) {
232                     throw new ValidationException("related service instance for volumeGroup request");
233                 }
234                 if (!isRelatedVnfInstancePresent) {
235                     throw new ValidationException("related vnf instance for volumeGroup request");
236                 }
237                 serviceInstanceType = serviceModelName;
238                 vnfType = serviceModelName + "/" + vnfModelName;
239                 info.setServiceInstanceType(serviceInstanceType);
240                 info.setVnfType(vnfType);
241             } else if (requestScope.equalsIgnoreCase(ModelType.vfModule.name())) {
242                 if (!isRelatedServiceInstancePresent) {
243                     throw new ValidationException("related service instance for vfModule request");
244                 }
245                 if (!isRelatedVnfInstancePresent) {
246                     throw new ValidationException("related vnf instance for vfModule request");
247                 }
248                 vfModuleModelName = modelInfo.getModelName();
249                 serviceInstanceType = serviceModelName;
250                 vnfType = serviceModelName + "/" + vnfModelName;
251                 vfModuleType = vnfType + "::" + vfModuleModelName;
252                 sir.setVolumeGroupInstanceId(volumeGroupId);
253                 info.setVfModuleModelName(vfModuleModelName);
254                 info.setVnfType(vnfType);
255                 info.setServiceInstanceType(serviceInstanceType);
256                 info.setVfModuleType(vfModuleType);
257             } else if (requestScope.equalsIgnoreCase(ModelType.vnf.name())) {
258                 if (!isRelatedServiceInstancePresent) {
259                     throw new ValidationException("related service instance for vnf request");
260                 }
261                 vnfType = serviceModelName + "/" + sir.getRequestDetails().getModelInfo().getModelCustomizationName();
262                 info.setVnfType(vnfType);
263             }
264         } else if (((requestScope.equalsIgnoreCase(ModelType.vnf.name())
265                 || requestScope.equalsIgnoreCase(ModelType.volumeGroup.name())
266                 || requestScope.equalsIgnoreCase(ModelType.vfModule.name())
267                 || requestScope.equalsIgnoreCase(ModelType.configuration.name()))
268                 && (action == Action.createInstance || action == Action.enablePort || action == Action.disablePort))
269                 || (reqVersion >= 4
270                         && (requestScope.equalsIgnoreCase(ModelType.volumeGroup.name())
271                                 || requestScope.equalsIgnoreCase(ModelType.vfModule.name()))
272                         && action == Action.updateInstance
273                         || (requestScope.equalsIgnoreCase(ModelType.vfModule.name()) && action == Action.scaleOut))
274                 || (requestScope.equalsIgnoreCase(ModelType.service.name())
275                         && (action.equals(Action.addRelationships) || action.equals(Action.removeRelationships)))) {
276             logger.debug("related instance exception");
277             throw new ValidationException("related instances");
278         }
279         if (instanceList == null && requestScope.equalsIgnoreCase(ModelType.instanceGroup.toString())
280                 && action == Action.createInstance) {
281             throw new ValidationException("relatedInstanceList", true);
282         }
283         info.setVfModuleModelName(vfModuleModelName);
284         info.setServiceInstanceType(serviceInstanceType);
285         info.setVnfType(vnfType);
286         info.setAsdcServiceModelVersion(asdcServiceModelVersion);
287         info.setVfModuleType(vfModuleType);
288         return info;
289     }
290 }