c6fae6e872838177ed49c9ef3cd84d2ebaac9ca2
[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 || action == Action.recreateInstance) {
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         if(empty(modelInfo.getModelInvariantId()) && (requestScope.equalsIgnoreCase(ModelType.vnf.name()) && action == Action.recreateInstance)){
81                 throw new ValidationException("modelInvariantId", true);
82         }
83         if (!empty (modelInfo.getModelInvariantId ()) && !UUIDChecker.isValidUUID (modelInfo.getModelInvariantId ())) {
84                 throw new ValidationException ("modelInvariantId format");
85         }
86
87         if(reqVersion >= 4 && !(requestScope.equalsIgnoreCase(ModelType.configuration.name())) && empty (modelInfo.getModelName ()) && (action == Action.createInstance || action == Action.updateInstance || 
88                         action == Action.addRelationships || action == Action.removeRelationships || action == Action.recreateInstance || ((action == Action.deleteInstance || action == Action.scaleOut) && (requestScope.equalsIgnoreCase (ModelType.vfModule.name ()))))){
89                 throw new ValidationException ("modelName", true);
90         }
91
92         if (empty (modelInfo.getModelVersion ()) && !(requestScope.equalsIgnoreCase(ModelType.configuration.name())) && 
93                         (!(reqVersion < 4 && requestScope.equalsIgnoreCase (ModelType.network.name ())) 
94                                         && (action == Action.createInstance || action == Action.updateInstance || action == Action.addRelationships || action == Action.removeRelationships || action == Action.scaleOut))) {
95                 throw new ValidationException ("modelVersion");
96         }
97         
98         if(empty(modelInfo.getModelVersion()) && (requestScope.equalsIgnoreCase(ModelType.vnf.name()) && action == Action.recreateInstance)){
99                 throw new ValidationException("modelVersion", true);
100         }
101
102         // is required for serviceInstance delete macro when aLaCarte=false in v4
103         if (reqVersion >= 4 && empty (modelInfo.getModelVersionId()) && (((aLaCarteFlag != null && !aLaCarteFlag) && requestScope.equalsIgnoreCase(ModelType.service.name()) && action == Action.deleteInstance) ||
104                         (action == Action.createInstance || action == Action.updateInstance || action == Action.enablePort || action == Action.disablePort || action == Action.addRelationships || action == Action.removeRelationships ||
105                         (requestScope.equalsIgnoreCase(ModelType.configuration.name()) && (action == Action.activateInstance || action == Action.deactivateInstance))))) {
106                 throw new ValidationException ("modelVersionId");
107          }
108         if(empty(modelInfo.getModelVersionId()) && (requestScope.equalsIgnoreCase(ModelType.vnf.name()) && action == Action.recreateInstance)){
109                 throw new ValidationException("modelVersionId", true);
110         }
111         if(empty(modelInfo.getModelVersionId()) && (requestScope.equalsIgnoreCase(ModelType.vfModule.name()) && action == Action.scaleOut)){
112                 throw new ValidationException("modelVersionId");
113         }
114         
115         if(requestScope.equalsIgnoreCase(ModelType.vnf.name()) && action != Action.deleteInstance && empty (modelInfo.getModelCustomizationName ())) {
116                 if (!UUIDChecker.isValidUUID (modelInfo.getModelCustomizationId())) {
117                         throw new ValidationException ("modelCustomizationId or modelCustomizationName");
118                 }
119         }
120
121         if(reqVersion >= 4 && (!UUIDChecker.isValidUUID (modelInfo.getModelCustomizationId())) && (requestScope.equalsIgnoreCase (ModelType.network.name ()) || requestScope.equalsIgnoreCase(ModelType.configuration.name()))
122                         && (action == Action.updateInstance || action == Action.createInstance)){
123                 throw new ValidationException ("modelCustomizationId");
124         }
125         if(empty(modelInfo.getModelCustomizationId()) && action == Action.scaleOut && !(requestParameters.getTestApi() == TestApi.VNF_API.name() && requestParameters.isUsePreload() == true)){
126                 throw new ValidationException ("modelCustomizationId");
127         }
128         return info;
129         }
130 }