Containerization feature of SO
[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.exceptions.ValidationException;
27 import org.onap.so.serviceinstancebeans.ModelInfo;
28 import org.onap.so.serviceinstancebeans.ModelType;
29 import org.onap.so.serviceinstancebeans.RequestParameters;
30 import org.onap.so.utils.UUIDChecker;
31 public class ModelInfoValidation implements ValidationRule{
32     private static boolean empty(String s) {
33           return (s == null || s.trim().isEmpty());
34     }
35         @Override
36         public ValidationInformation validate(ValidationInformation info) throws ValidationException{
37                 ModelInfo modelInfo = info.getSir().getRequestDetails().getModelInfo();
38                 RequestParameters requestParameters = info.getReqParameters();
39                 String requestScope = info.getRequestScope();
40                 Actions action = info.getAction();
41                 int reqVersion = info.getReqVersion();
42                 Boolean aLaCarteFlag = info.getALaCarteFlag();
43
44                 if(!empty(modelInfo.getModelNameVersionId())){
45                 modelInfo.setModelVersionId(modelInfo.getModelNameVersionId());
46         }
47                 // modelCustomizationId is required when usePreLoad is false for v4 and higher for VF Module Create
48         if(requestParameters != null && reqVersion >= 4 && requestScope.equalsIgnoreCase(ModelType.vfModule.name()) && action == Action.createInstance && !requestParameters.isUsePreload()) {
49                 if(!UUIDChecker.isValidUUID(modelInfo.getModelCustomizationId())) {
50                         throw new ValidationException("modelCustomizationId");
51                 }
52         }
53         
54         // modelCustomizationId is required for v5 and higher for VF Module Replace
55         if(requestParameters != null && reqVersion > 4 && requestScope.equalsIgnoreCase(ModelType.vfModule.name()) && action == Action.replaceInstance) {
56                 if(!UUIDChecker.isValidUUID(modelInfo.getModelCustomizationId())) {
57                         throw new ValidationException("modelCustomizationId");
58                 }
59         }
60         
61         // modelCustomizationId or modelCustomizationName are required for VNF Replace
62         if(requestParameters != null && reqVersion > 4 && requestScope.equalsIgnoreCase(ModelType.vnf.name()) && action == Action.replaceInstance) {
63                 if(!UUIDChecker.isValidUUID(modelInfo.getModelCustomizationId()) && modelInfo.getModelCustomizationName() == null) {
64                         throw new ValidationException("modelCustomizationId or modelCustomizationName");
65                 }
66         }
67
68         //is required for serviceInstance delete macro when aLaCarte=false (v3)
69         //create and updates except for network (except v4)
70         if (empty (modelInfo.getModelInvariantId ()) && ((reqVersion >2 && (aLaCarteFlag != null && !aLaCarteFlag) && requestScope.equalsIgnoreCase(ModelType.service.name()) && action == Action.deleteInstance) ||
71                 !(reqVersion < 4 && requestScope.equalsIgnoreCase (ModelType.network.name ())) && 
72                 (action == Action.createInstance || action == Action.updateInstance || action == Action.enablePort || action == Action.disablePort || action == Action.addRelationships || action == Action.removeRelationships ||
73                 (requestScope.equalsIgnoreCase(ModelType.configuration.name()) && (action == Action.activateInstance || action == Action.deactivateInstance))))) {
74                 throw new ValidationException ("modelInvariantId");
75         }
76
77         if (!empty (modelInfo.getModelInvariantId ()) && !UUIDChecker.isValidUUID (modelInfo.getModelInvariantId ())) {
78                 throw new ValidationException ("modelInvariantId format");
79         }
80
81         if(reqVersion >= 4 && !(requestScope.equalsIgnoreCase(ModelType.configuration.name())) && empty (modelInfo.getModelName ()) && (action == Action.createInstance || action == Action.updateInstance || 
82                         action == Action.addRelationships || action == Action.removeRelationships || (action == Action.deleteInstance && (requestScope.equalsIgnoreCase (ModelType.vfModule.name ()))))){
83                 throw new ValidationException ("modelName");
84         }
85
86         if (empty (modelInfo.getModelVersion ()) && !(requestScope.equalsIgnoreCase(ModelType.configuration.name())) && 
87                         (!(reqVersion < 4 && requestScope.equalsIgnoreCase (ModelType.network.name ())) 
88                                         && (action == Action.createInstance || action == Action.updateInstance || action == Action.addRelationships || action == Action.removeRelationships))) {
89                 throw new ValidationException ("modelVersion");
90         }
91
92         // is required for serviceInstance delete macro when aLaCarte=false in v4
93         if (reqVersion >= 4 && empty (modelInfo.getModelVersionId()) && (((aLaCarteFlag != null && !aLaCarteFlag) && requestScope.equalsIgnoreCase(ModelType.service.name()) && action == Action.deleteInstance) ||
94                         (action == Action.createInstance || action == Action.updateInstance || action == Action.enablePort || action == Action.disablePort || action == Action.addRelationships || action == Action.removeRelationships ||
95                         (requestScope.equalsIgnoreCase(ModelType.configuration.name()) && (action == Action.activateInstance || action == Action.deactivateInstance))))) {
96                 throw new ValidationException ("modelVersionId");
97          }
98         
99         if(requestScope.equalsIgnoreCase(ModelType.vnf.name()) && action != Action.deleteInstance && empty (modelInfo.getModelCustomizationName ())) {
100                 if (!UUIDChecker.isValidUUID (modelInfo.getModelCustomizationId())) {
101                         throw new ValidationException ("modelCustomizationId or modelCustomizationName");
102                 }
103         }
104
105         if(reqVersion >= 4 && (!UUIDChecker.isValidUUID (modelInfo.getModelCustomizationId())) && (requestScope.equalsIgnoreCase (ModelType.network.name ()) || requestScope.equalsIgnoreCase(ModelType.configuration.name()))
106                         && (action == Action.updateInstance || action == Action.createInstance)){
107                 throw new ValidationException ("modelCustomizationId");
108         }
109         return info;
110         }
111 }