2 * ============LICENSE_START=======================================================
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
24 package org.onap.so.apihandlerinfra.validation;
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;
40 public class RelatedInstancesValidation implements ValidationRule {
42 private static Logger logger = LoggerFactory.getLogger(RelatedInstancesValidation.class);
44 private static boolean empty(String s) {
45 return (s == null || s.trim().isEmpty());
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;
71 if (requestScope.equalsIgnoreCase(ModelType.service.name())) {
72 serviceInstanceType = modelInfo.getModelName();
73 info.setServiceInstanceType(serviceInstanceType);
75 if (requestScope.equalsIgnoreCase(ModelType.network.name())) {
76 networkType = modelInfo.getModelName();
77 info.setNetworkType(networkType);
79 if (instanceList != null) {
80 for (RelatedInstanceList relatedInstanceList : instanceList) {
81 RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();
83 ModelInfo relatedInstanceModelInfo = relatedInstance.getModelInfo();
84 if (relatedInstanceModelInfo == null) {
85 throw new ValidationException("modelInfo in relatedInstance");
88 if (relatedInstanceModelInfo.getModelType() == null) {
89 throw new ValidationException("modelType in relatedInstance");
92 if (empty(relatedInstance.getInstanceName())
93 && ModelType.pnf.equals(relatedInstanceModelInfo.getModelType())) {
94 throw new ValidationException("instanceName in relatedInstance for pnf modelType");
97 if (!empty(relatedInstance.getInstanceName())) {
98 if (!relatedInstance.getInstanceName().matches(Constants.VALID_INSTANCE_NAME_FORMAT)) {
99 throw new ValidationException("instanceName format in relatedInstance");
103 if (empty(relatedInstance.getInstanceId())
104 && !ModelType.pnf.equals(relatedInstanceModelInfo.getModelType())) {
105 throw new ValidationException("instanceId in relatedInstance");
108 if (!empty(relatedInstance.getInstanceId())
109 && !UUIDChecker.isValidUUID(relatedInstance.getInstanceId())) {
110 throw new ValidationException("instanceId format in relatedInstance");
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);
117 if (requestScope.equalsIgnoreCase(ModelType.instanceGroup.toString())
118 && relatedInstanceModelInfo.getModelType().equals(ModelType.service)) {
119 isRelatedServiceInstancePresent = true;
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);
127 if (empty(relatedInstance.getInstanceName())
128 && ModelType.network.equals(relatedInstanceModelInfo.getModelType())) {
129 throw new ValidationException("instanceName in relatedInstance for network modelType", true);
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))) {
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");
153 if (!empty(relatedInstanceModelInfo.getModelInvariantId())
154 && !UUIDChecker.isValidUUID(relatedInstanceModelInfo.getModelInvariantId())) {
155 throw new ValidationException("modelInvariantId format in relatedInstance");
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;
169 if (ModelType.connectionPoint.equals(relatedInstanceModelInfo.getModelType())
170 && ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
171 isConnectionPointPresent = true;
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");
184 if (relatedInstanceModelInfo.getModelType().equals(ModelType.service)
185 && !(requestScope.equalsIgnoreCase(ModelType.instanceGroup.toString())
186 && action == Action.createInstance)) {
187 isRelatedServiceInstancePresent = true;
188 if (!relatedInstance.getInstanceId().equals(sir.getServiceInstanceId())) {
189 throw new ValidationException(
190 "serviceInstanceId matching the serviceInstanceId in request URI");
192 serviceModelName = relatedInstanceModelInfo.getModelName();
193 asdcServiceModelVersion = relatedInstanceModelInfo.getModelVersion();
194 } else if (relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)
195 && !(ModelType.configuration.name().equalsIgnoreCase(requestScope))) {
196 isRelatedVnfInstancePresent = true;
197 if (!relatedInstance.getInstanceId().equals(sir.getVnfInstanceId())) {
198 throw new ValidationException("vnfInstanceId matching the vnfInstanceId in request URI");
200 vnfModelName = relatedInstanceModelInfo.getModelCustomizationName();
201 } else if (relatedInstanceModelInfo.getModelType().equals(ModelType.volumeGroup)) {
202 volumeGroupId = relatedInstance.getInstanceId();
206 if (action == Action.createInstance && ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
207 if (!isSourceVnfPresent) {
208 throw new ValidationException("source vnf relatedInstance for Port Configuration");
211 if (!isDestinationVnfPresent) {
212 throw new ValidationException("destination vnf relatedInstance for Port Configuration");
216 if ((action == Action.enablePort || action == Action.disablePort)
217 && ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
218 if (!isConnectionPointPresent) {
219 throw new ValidationException("connectionPoint relatedInstance for Port Configuration");
222 if (requestScope.equals(ModelType.instanceGroup.toString())) {
223 if (!isRelatedServiceInstancePresent) {
224 throw new ValidationException("related service instance for instanceGroup request", true);
227 if (requestScope.equalsIgnoreCase(ModelType.volumeGroup.name())) {
228 if (!isRelatedServiceInstancePresent) {
229 throw new ValidationException("related service instance for volumeGroup request");
231 if (!isRelatedVnfInstancePresent) {
232 throw new ValidationException("related vnf instance for volumeGroup request");
234 serviceInstanceType = serviceModelName;
235 vnfType = serviceModelName + "/" + vnfModelName;
236 info.setServiceInstanceType(serviceInstanceType);
237 info.setVnfType(vnfType);
238 } else if (requestScope.equalsIgnoreCase(ModelType.vfModule.name())) {
239 if (!isRelatedServiceInstancePresent) {
240 throw new ValidationException("related service instance for vfModule request");
242 if (!isRelatedVnfInstancePresent) {
243 throw new ValidationException("related vnf instance for vfModule request");
245 vfModuleModelName = modelInfo.getModelName();
246 serviceInstanceType = serviceModelName;
247 vnfType = serviceModelName + "/" + vnfModelName;
248 vfModuleType = vnfType + "::" + vfModuleModelName;
249 sir.setVolumeGroupInstanceId(volumeGroupId);
250 info.setVfModuleModelName(vfModuleModelName);
251 info.setVnfType(vnfType);
252 info.setServiceInstanceType(serviceInstanceType);
253 info.setVfModuleType(vfModuleType);
254 } else if (requestScope.equalsIgnoreCase(ModelType.vnf.name())) {
255 if (!isRelatedServiceInstancePresent) {
256 throw new ValidationException("related service instance for vnf request");
258 vnfType = serviceModelName + "/" + sir.getRequestDetails().getModelInfo().getModelCustomizationName();
259 info.setVnfType(vnfType);
261 } else if (((requestScope.equalsIgnoreCase(ModelType.vnf.name())
262 || requestScope.equalsIgnoreCase(ModelType.volumeGroup.name())
263 || requestScope.equalsIgnoreCase(ModelType.vfModule.name())
264 || requestScope.equalsIgnoreCase(ModelType.configuration.name()))
265 && (action == Action.createInstance || action == Action.enablePort || action == Action.disablePort))
267 && (requestScope.equalsIgnoreCase(ModelType.volumeGroup.name())
268 || requestScope.equalsIgnoreCase(ModelType.vfModule.name()))
269 && action == Action.updateInstance
270 || (requestScope.equalsIgnoreCase(ModelType.vfModule.name()) && action == Action.scaleOut))
271 || (requestScope.equalsIgnoreCase(ModelType.service.name())
272 && (action.equals(Action.addRelationships) || action.equals(Action.removeRelationships)))) {
273 logger.debug("related instance exception");
274 throw new ValidationException("related instances");
276 if (instanceList == null && requestScope.equalsIgnoreCase(ModelType.instanceGroup.toString())
277 && action == Action.createInstance) {
278 throw new ValidationException("relatedInstanceList", true);
280 info.setVfModuleModelName(vfModuleModelName);
281 info.setServiceInstanceType(serviceInstanceType);
282 info.setVnfType(vnfType);
283 info.setAsdcServiceModelVersion(asdcServiceModelVersion);
284 info.setVfModuleType(vfModuleType);