edb92ef68b1866c3ad431f8175b38624b597b081
[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.exceptions.ValidationException;
27 import org.onap.so.serviceinstancebeans.ModelType;
28 import org.onap.so.serviceinstancebeans.RequestParameters;
29
30 public class RequestParametersValidation implements ValidationRule{
31         private static boolean empty(String s) {
32           return (s == null || s.trim().isEmpty());
33     }
34         @Override
35         public ValidationInformation validate(ValidationInformation info) throws ValidationException{
36                 int reqVersion = info.getReqVersion();
37                 String requestScope = info.getRequestScope();
38                 Actions action = info.getAction();
39                 RequestParameters requestParameters = info.getReqParameters(); 
40               
41                 if (requestScope.equalsIgnoreCase(ModelType.service.name()) && (action == Action.createInstance || action == Action.assignInstance)) {
42                 if (requestParameters == null) {
43                         throw new ValidationException ("requestParameters");
44                 }
45                 if (empty (requestParameters.getSubscriptionServiceType())) {
46                         throw new ValidationException ("subscriptionServiceType");
47                 }
48         }
49                 if(reqVersion >= 4){
50                 if(Action.addRelationships.equals(action) || Action.removeRelationships.equals(action)) {
51                         if(requestParameters == null || requestParameters.getALaCarte() == null) {
52                                 throw new ValidationException ("aLaCarte in requestParameters");
53                         }
54                 }
55         }
56                 if(requestParameters == null && !requestScope.equalsIgnoreCase(ModelType.service.name())){
57                         info.setALaCarteFlag(true);
58                 }
59         if(requestParameters != null){
60                 if(requestScope.equalsIgnoreCase(ModelType.vnf.name())){
61                         if(action == Action.updateInstance){
62                                 if(requestParameters.isUsePreload() == null){
63                                         requestParameters.setUsePreload(true);
64                                 }
65                         }
66                         if(action == Action.replaceInstance){
67                                 if(requestParameters.getRebuildVolumeGroups() == null){
68                                         requestParameters.setRebuildVolumeGroups(false);
69                                 }
70                         }
71                 }
72                 if(requestScope.equalsIgnoreCase(ModelType.vfModule.name())){
73                         if(action == Action.createInstance || action == Action.updateInstance){                         
74                                 if(requestParameters.isUsePreload() == null){                                   
75                                         if(reqVersion >= 4){                                            
76                                                 if (requestParameters.getALaCarte() == null || requestParameters.getALaCarte() == false) {                                                      
77                                                         requestParameters.setUsePreload(false);
78                                                 }
79                                                 else {                                                  
80                                                         requestParameters.setUsePreload(true);
81                                                 }
82                                         }
83                                         else {                                  
84                                                 requestParameters.setUsePreload(true);
85                                         }
86                                 }
87                         }
88                 }
89                 if(reqVersion >= 4){
90                        if(requestParameters.getALaCarte() != null){
91                                 info.setALaCarteFlag(requestParameters.getALaCarte());
92                        }else if(requestScope.equalsIgnoreCase(ModelType.service.name())){
93                            if(action == Action.createInstance || action == Action.deleteInstance || action == Action.activateInstance || action == Action.deactivateInstance){
94                                    if(requestParameters.getALaCarte() == null){
95                                            requestParameters.setaLaCarte(false);
96                                            info.setALaCarteFlag(requestParameters.getALaCarte());
97                                    }
98                            }
99                        }else{
100                            info.setALaCarteFlag(true);
101                        }
102                 }else{
103                         info.setALaCarteFlag(true);
104                 }
105         }
106                 return info;
107         }
108 }