aa98d2abf247951968f0303e44f95347d8f8d5d8
[so.git] /
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  * 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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.onap.so.apihandlerinfra.validation;
23
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());
35     }
36         @Override
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();
44
45                 if(!empty(modelInfo.getModelNameVersionId())){
46                 modelInfo.setModelVersionId(modelInfo.getModelNameVersionId());
47         }
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");
52                 }
53         }
54         
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");
59                 }
60         }
61         
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");
66                 }
67         }
68
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");
76         }
77         if(empty(modelInfo.getModelInvariantId()) && (requestScope.equalsIgnoreCase(ModelType.vfModule.name()) && action == Action.scaleOut)){
78                 throw new ValidationException("modelInvariantId");
79         }
80
81         if (!empty (modelInfo.getModelInvariantId ()) && !UUIDChecker.isValidUUID (modelInfo.getModelInvariantId ())) {
82                 throw new ValidationException ("modelInvariantId format");
83         }
84
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");
88         }
89
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");
94         }
95
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");
101          }
102         if(empty(modelInfo.getModelVersionId()) && (requestScope.equalsIgnoreCase(ModelType.vfModule.name()) && action == Action.scaleOut)){
103                 throw new ValidationException("modelVersionId");
104         }
105         
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");
109                 }
110         }
111
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");
115         }
116         if(empty(modelInfo.getModelCustomizationId()) && action == Action.scaleOut && !(requestParameters.getTestApi() == TestApi.VNF_API.name() && requestParameters.isUsePreload() == true)){
117                 throw new ValidationException ("modelCustomizationId");
118         }
119         return info;
120         }
121 }