e2a1bdc46ab41875803ac6c9cb37e31e148f16fa
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / onap / so / apihandlerinfra / validation / ModelInfoValidation.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  * 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(!requestScope.equals(ModelType.instanceGroup.toString())){
46
47                         if(!empty(modelInfo.getModelNameVersionId())){
48                                 modelInfo.setModelVersionId(modelInfo.getModelNameVersionId());
49                         }
50                         // modelCustomizationId is required when usePreLoad is false for v4 and higher for VF Module Create
51                         if(requestParameters != null && reqVersion >= 4 && requestScope.equalsIgnoreCase(ModelType.vfModule.name()) && action == Action.createInstance && !requestParameters.isUsePreload()) {
52                                 if(!UUIDChecker.isValidUUID(modelInfo.getModelCustomizationId())) {
53                                         throw new ValidationException("modelCustomizationId");
54                                 }
55                         }
56
57                         // modelCustomizationId is required for v5 and higher for VF Module Replace
58                         if(requestParameters != null && reqVersion > 4 && requestScope.equalsIgnoreCase(ModelType.vfModule.name()) && action == Action.replaceInstance) {
59                                 if(!UUIDChecker.isValidUUID(modelInfo.getModelCustomizationId())) {
60                                         throw new ValidationException("modelCustomizationId");
61                                 }
62                         }
63
64                         // modelCustomizationId or modelCustomizationName are required for VNF Replace
65                         if(requestParameters != null && reqVersion > 4 && requestScope.equalsIgnoreCase(ModelType.vnf.name()) && action == Action.replaceInstance || action == Action.recreateInstance) {
66                                 if(!UUIDChecker.isValidUUID(modelInfo.getModelCustomizationId()) && modelInfo.getModelCustomizationName() == null) {
67                                         throw new ValidationException("modelCustomizationId or modelCustomizationName");
68                                 }
69                         }
70
71                         //is required for serviceInstance delete macro when aLaCarte=false (v3)
72                         //create and updates except for network (except v4)
73                         if (empty (modelInfo.getModelInvariantId ()) && ((reqVersion >2 && (aLaCarteFlag != null && !aLaCarteFlag) && requestScope.equalsIgnoreCase(ModelType.service.name()) && action == Action.deleteInstance) ||
74                                         !(reqVersion < 4 && requestScope.equalsIgnoreCase (ModelType.network.name ())) && 
75                                         (action == Action.createInstance || action == Action.updateInstance || action == Action.enablePort || action == Action.disablePort || action == Action.addRelationships || action == Action.removeRelationships ||
76                                         (requestScope.equalsIgnoreCase(ModelType.configuration.name()) && (action == Action.activateInstance || action == Action.deactivateInstance))))) {
77                                 throw new ValidationException ("modelInvariantId");
78                         }
79                         if(empty(modelInfo.getModelInvariantId()) && (requestScope.equalsIgnoreCase(ModelType.vfModule.name()) && action == Action.scaleOut)){
80                                 throw new ValidationException("modelInvariantId");
81                         }
82                         if(empty(modelInfo.getModelInvariantId()) && (requestScope.equalsIgnoreCase(ModelType.vnf.name()) && action == Action.recreateInstance)){
83                                 throw new ValidationException("modelInvariantId", true);
84                         }
85                         if (!empty (modelInfo.getModelInvariantId ()) && !UUIDChecker.isValidUUID (modelInfo.getModelInvariantId ())) {
86                                 throw new ValidationException ("modelInvariantId format");
87                         }
88
89                         if(reqVersion >= 4 && !(requestScope.equalsIgnoreCase(ModelType.configuration.name())) && empty (modelInfo.getModelName ()) && (action == Action.createInstance || action == Action.updateInstance || 
90                                         action == Action.addRelationships || action == Action.removeRelationships || action == Action.recreateInstance || ((action == Action.deleteInstance || action == Action.scaleOut) && (requestScope.equalsIgnoreCase (ModelType.vfModule.name ()))))){
91                                 throw new ValidationException ("modelName", true);
92                         }
93
94                         if (empty (modelInfo.getModelVersion ()) && !(requestScope.equalsIgnoreCase(ModelType.configuration.name())) && 
95                                         (!(reqVersion < 4 && requestScope.equalsIgnoreCase (ModelType.network.name ())) 
96                                                         && (action == Action.createInstance || action == Action.updateInstance || action == Action.addRelationships || action == Action.removeRelationships || action == Action.scaleOut))) {
97                                 throw new ValidationException ("modelVersion");
98                         }
99
100                         if(empty(modelInfo.getModelVersion()) && (requestScope.equalsIgnoreCase(ModelType.vnf.name()) && action == Action.recreateInstance)){
101                                 throw new ValidationException("modelVersion", true);
102                         }
103
104                         // is required for serviceInstance delete macro when aLaCarte=false in v4
105                         if (reqVersion >= 4 && empty (modelInfo.getModelVersionId()) && (((aLaCarteFlag != null && !aLaCarteFlag) && requestScope.equalsIgnoreCase(ModelType.service.name()) && action == Action.deleteInstance) ||
106                                         (action == Action.createInstance || action == Action.updateInstance || action == Action.enablePort || action == Action.disablePort || action == Action.addRelationships || action == Action.removeRelationships ||
107                                         (requestScope.equalsIgnoreCase(ModelType.configuration.name()) && (action == Action.activateInstance || action == Action.deactivateInstance))))) {
108                                 throw new ValidationException ("modelVersionId");
109                         }
110                         if(empty(modelInfo.getModelVersionId()) && (requestScope.equalsIgnoreCase(ModelType.vnf.name()) && action == Action.recreateInstance)){
111                                 throw new ValidationException("modelVersionId", true);
112                         }
113                         if(empty(modelInfo.getModelVersionId()) && (requestScope.equalsIgnoreCase(ModelType.vfModule.name()) && action == Action.scaleOut)){
114                                 throw new ValidationException("modelVersionId");
115                         }
116
117                         if(requestScope.equalsIgnoreCase(ModelType.vnf.name()) && action != Action.deleteInstance && empty (modelInfo.getModelCustomizationName ())) {
118                                 if (!UUIDChecker.isValidUUID (modelInfo.getModelCustomizationId())) {
119                                         throw new ValidationException ("modelCustomizationId or modelCustomizationName");
120                                 }
121                         }
122
123                         if(reqVersion >= 4 && (!UUIDChecker.isValidUUID (modelInfo.getModelCustomizationId())) && (requestScope.equalsIgnoreCase (ModelType.network.name ()) || requestScope.equalsIgnoreCase(ModelType.configuration.name()))
124                                         && (action == Action.updateInstance || action == Action.createInstance)){
125                                 throw new ValidationException ("modelCustomizationId");
126                         }
127                         if(empty(modelInfo.getModelCustomizationId()) && requestScope.equalsIgnoreCase(ModelType.vfModule.name()) && action == Action.scaleOut && !(requestParameters.getTestApi() == TestApi.VNF_API.name() && requestParameters.isUsePreload() == true)){
128                                 throw new ValidationException ("modelCustomizationId");
129                         }
130                 }else{
131                         if(empty(modelInfo.getModelVersionId()) && action == Action.createInstance){
132                                 throw new ValidationException("modelVersionId", true);
133                         }
134                 }
135                 return info;
136         }
137 }