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 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.so.apihandlerinfra.validation;
24 import org.onap.so.apihandlerinfra.Action;
25 import org.onap.so.apihandlerinfra.Actions;
26 import org.onap.so.apihandlerinfra.TestApi;
27 import org.onap.so.exceptions.ValidationException;
28 import org.onap.so.serviceinstancebeans.ModelInfo;
29 import org.onap.so.serviceinstancebeans.ModelType;
30 import org.onap.so.serviceinstancebeans.RequestParameters;
31 import org.onap.so.utils.UUIDChecker;
32 public class ModelInfoValidation implements ValidationRule{
33 private static boolean empty(String s) {
34 return (s == null || s.trim().isEmpty());
37 public ValidationInformation validate(ValidationInformation info) throws ValidationException{
38 ModelInfo modelInfo = info.getSir().getRequestDetails().getModelInfo();
39 RequestParameters requestParameters = info.getReqParameters();
40 String requestScope = info.getRequestScope();
41 Actions action = info.getAction();
42 int reqVersion = info.getReqVersion();
43 Boolean aLaCarteFlag = info.getALaCarteFlag();
45 if(!empty(modelInfo.getModelNameVersionId())){
46 modelInfo.setModelVersionId(modelInfo.getModelNameVersionId());
48 // modelCustomizationId is required when usePreLoad is false for v4 and higher for VF Module Create
49 if(requestParameters != null && reqVersion >= 4 && requestScope.equalsIgnoreCase(ModelType.vfModule.name()) && action == Action.createInstance && !requestParameters.isUsePreload()) {
50 if(!UUIDChecker.isValidUUID(modelInfo.getModelCustomizationId())) {
51 throw new ValidationException("modelCustomizationId");
55 // modelCustomizationId is required for v5 and higher for VF Module Replace
56 if(requestParameters != null && reqVersion > 4 && requestScope.equalsIgnoreCase(ModelType.vfModule.name()) && action == Action.replaceInstance) {
57 if(!UUIDChecker.isValidUUID(modelInfo.getModelCustomizationId())) {
58 throw new ValidationException("modelCustomizationId");
62 // modelCustomizationId or modelCustomizationName are required for VNF Replace
63 if(requestParameters != null && reqVersion > 4 && requestScope.equalsIgnoreCase(ModelType.vnf.name()) && action == Action.replaceInstance) {
64 if(!UUIDChecker.isValidUUID(modelInfo.getModelCustomizationId()) && modelInfo.getModelCustomizationName() == null) {
65 throw new ValidationException("modelCustomizationId or modelCustomizationName");
69 //is required for serviceInstance delete macro when aLaCarte=false (v3)
70 //create and updates except for network (except v4)
71 if (empty (modelInfo.getModelInvariantId ()) && ((reqVersion >2 && (aLaCarteFlag != null && !aLaCarteFlag) && requestScope.equalsIgnoreCase(ModelType.service.name()) && action == Action.deleteInstance) ||
72 !(reqVersion < 4 && requestScope.equalsIgnoreCase (ModelType.network.name ())) &&
73 (action == Action.createInstance || action == Action.updateInstance || action == Action.enablePort || action == Action.disablePort || action == Action.addRelationships || action == Action.removeRelationships ||
74 (requestScope.equalsIgnoreCase(ModelType.configuration.name()) && (action == Action.activateInstance || action == Action.deactivateInstance))))) {
75 throw new ValidationException ("modelInvariantId");
77 if(empty(modelInfo.getModelInvariantId()) && (requestScope.equalsIgnoreCase(ModelType.vfModule.name()) && action == Action.scaleOut)){
78 throw new ValidationException("modelInvariantId");
81 if (!empty (modelInfo.getModelInvariantId ()) && !UUIDChecker.isValidUUID (modelInfo.getModelInvariantId ())) {
82 throw new ValidationException ("modelInvariantId format");
85 if(reqVersion >= 4 && !(requestScope.equalsIgnoreCase(ModelType.configuration.name())) && empty (modelInfo.getModelName ()) && (action == Action.createInstance || action == Action.updateInstance ||
86 action == Action.addRelationships || action == Action.removeRelationships || ((action == Action.deleteInstance || action == Action.scaleOut) && (requestScope.equalsIgnoreCase (ModelType.vfModule.name ()))))){
87 throw new ValidationException ("modelName");
90 if (empty (modelInfo.getModelVersion ()) && !(requestScope.equalsIgnoreCase(ModelType.configuration.name())) &&
91 (!(reqVersion < 4 && requestScope.equalsIgnoreCase (ModelType.network.name ()))
92 && (action == Action.createInstance || action == Action.updateInstance || action == Action.addRelationships || action == Action.removeRelationships || action == Action.scaleOut))) {
93 throw new ValidationException ("modelVersion");
96 // is required for serviceInstance delete macro when aLaCarte=false in v4
97 if (reqVersion >= 4 && empty (modelInfo.getModelVersionId()) && (((aLaCarteFlag != null && !aLaCarteFlag) && requestScope.equalsIgnoreCase(ModelType.service.name()) && action == Action.deleteInstance) ||
98 (action == Action.createInstance || action == Action.updateInstance || action == Action.enablePort || action == Action.disablePort || action == Action.addRelationships || action == Action.removeRelationships ||
99 (requestScope.equalsIgnoreCase(ModelType.configuration.name()) && (action == Action.activateInstance || action == Action.deactivateInstance))))) {
100 throw new ValidationException ("modelVersionId");
102 if(empty(modelInfo.getModelVersionId()) && (requestScope.equalsIgnoreCase(ModelType.vfModule.name()) && action == Action.scaleOut)){
103 throw new ValidationException("modelVersionId");
106 if(requestScope.equalsIgnoreCase(ModelType.vnf.name()) && action != Action.deleteInstance && empty (modelInfo.getModelCustomizationName ())) {
107 if (!UUIDChecker.isValidUUID (modelInfo.getModelCustomizationId())) {
108 throw new ValidationException ("modelCustomizationId or modelCustomizationName");
112 if(reqVersion >= 4 && (!UUIDChecker.isValidUUID (modelInfo.getModelCustomizationId())) && (requestScope.equalsIgnoreCase (ModelType.network.name ()) || requestScope.equalsIgnoreCase(ModelType.configuration.name()))
113 && (action == Action.updateInstance || action == Action.createInstance)){
114 throw new ValidationException ("modelCustomizationId");
116 if(empty(modelInfo.getModelCustomizationId()) && action == Action.scaleOut && !(requestParameters.getTestApi() == TestApi.VNF_API.name() && requestParameters.isUsePreload() == true)){
117 throw new ValidationException ("modelCustomizationId");