ensure data for si matches on macro requests
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / onap / so / apihandlerinfra / validation / UserParamsValidation.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 java.util.ArrayList;
25 import java.util.List;
26
27 import org.onap.so.apihandlerinfra.Action;
28 import org.onap.so.apihandlerinfra.Actions;
29 import org.onap.so.exceptions.ValidationException;
30 import org.onap.so.serviceinstancebeans.ModelInfo;
31 import org.onap.so.serviceinstancebeans.Networks;
32 import org.onap.so.serviceinstancebeans.Service;
33 import org.onap.so.serviceinstancebeans.VfModules;
34 import org.onap.so.serviceinstancebeans.Vnfs;
35
36 public class UserParamsValidation implements ValidationRule{
37         @Override
38         public ValidationInformation validate(ValidationInformation info) throws ValidationException{
39                 Service validate = info.getUserParams();
40                 Actions action = info.getAction();
41                 
42                 if(validate.getModelInfo() == null){
43                         throw new ValidationException ("modelInfo in userParams", true);
44                 }else if(validate.getModelInfo().getModelType() == null){
45                         throw new ValidationException("modelType in userParams service modelInfo", true);
46                 }else if(validate.getModelInfo().getModelVersionId() == null){
47                         throw new ValidationException("modelVersionId in userParams service modelInfo", true);
48                 }
49                 modelInfoValidation(info.getSir().getRequestDetails().getModelInfo(), validate.getModelInfo());
50                 if(validate.getInstanceName() != null && info.getRequestInfo().getInstanceName() != null){
51                         instanceNameValidation(info, validate);
52                 }
53                 for(Vnfs vnf : validate.getResources().getVnfs()){
54                         if(vnf.getModelInfo() == null){
55                                 throw new ValidationException ("modelInfo in userParams vnf resources", true);
56                         }else if(vnf.getModelInfo().getModelCustomizationId() == null){
57                                 throw new ValidationException ("modelCustomizationId in userParams vnf resources", true);
58                         }else if(vnf.getModelInfo().getModelVersionId() == null){
59                                 throw new ValidationException("modelVersionId in userParams vnf resources", true);
60                         }
61                         if(vnf.getCloudConfiguration() == null){
62                                 throw new ValidationException ("cloudConfiguration in userParams vnf resources", true);
63                         }
64                         if(action == Action.createInstance || action == Action.assignInstance){
65                                 if(vnf.getPlatform() == null){
66                                         throw new ValidationException ("platform in userParams vnf resources", true);
67                                 }if(vnf.getProductFamilyId() == null){
68                                         throw new ValidationException ("productFamilyId in userParams vnf resources", true);
69                                 }
70                         }
71                         if (vnf.getPlatform() != null && vnf.getPlatform().getPlatformName() == null){
72                                 throw new ValidationException ("platformName in userParams vnf resources", true);
73                         }
74                         if(vnf.getVfModules().isEmpty()){
75                                 throw new ValidationException ("vfModules in userParams vnf resources", true);
76                         }
77                         for(VfModules vfModules : vnf.getVfModules()){
78                                 if(vfModules.getModelInfo() == null){
79                                         throw new ValidationException ("modelInfo in userParams vfModules resources", true);
80                                 }else if(vfModules.getModelInfo().getModelCustomizationId() == null){
81                                         throw new ValidationException ("modelCustomizationId in userParams vfModule resources", true);
82                                 }else if(vfModules.getModelInfo().getModelVersionId() == null){
83                                         throw new ValidationException("modelVersionId in userParams vfModule resources", true);
84                                 }
85                         }
86                 }
87                 
88                 List<Networks> validateNetworks = new ArrayList<>();
89                 validateNetworks = validate.getResources().getNetworks();
90                 if(validateNetworks != null){
91                         for(Networks networks : validateNetworks){
92                                 if(networks.getModelInfo() == null){
93                                         throw new ValidationException ("modelInfo in userParams network resources", true);
94                                 }else if(networks.getModelInfo().getModelCustomizationId() == null){
95                                         throw new ValidationException ("modelCustomizationId in userParams network resources", true);
96                                 }else if(networks.getModelInfo().getModelVersionId() == null){
97                                         throw new ValidationException("modelVersionId in userParams network resources", true);
98                                 }
99                                 if(networks.getCloudConfiguration() == null){
100                                         throw new ValidationException ("cloudConfiguration in userParams network resources", true);
101                                 }
102                         }
103                 } 
104                 return info;
105         }
106         public void instanceNameValidation(ValidationInformation info, Service validate) throws ValidationException{
107                 if(!info.getRequestInfo().getInstanceName().equals(validate.getInstanceName())){
108                         throw new ValidationException("instanceName in requestInfo", "instanceName in userParams service");
109                 }
110         }
111         public void modelInfoValidation(ModelInfo info, ModelInfo userParamInfo) throws ValidationException{
112                 if(!info.getModelType().equals(userParamInfo.getModelType())){
113                         throw new ValidationException("modelType in modelInfo", "modelType in userParams service");
114                 }
115                 if((info.getModelInvariantId() != null && userParamInfo.getModelInvariantId() != null) &&
116                                 (!info.getModelInvariantId().equals(userParamInfo.getModelInvariantId()))){
117                         throw new ValidationException("modelInvariantId in modelInfo", "modelInvariantId in userParams service");
118                 }
119                 if(!info.getModelVersionId().equals(userParamInfo.getModelVersionId())){
120                         throw new ValidationException("modelVersionId in modelInfo", "modelVersionId in userParams service");
121                 }
122                 if((info.getModelName() != null && userParamInfo.getModelName() != null) &&
123                                 (!info.getModelName().equals(userParamInfo.getModelName()))){
124                         throw new ValidationException("modelName in modelInfo", "modelName in userParams service");
125                 }
126                 if((info.getModelVersion() != null && userParamInfo.getModelVersion() != null) &&
127                         (!info.getModelVersion().equals(userParamInfo.getModelVersion()))){
128                         throw new ValidationException("modelVersion in modelInfo", "modelVersion in userParams service");
129                 }
130                 if((info.getModelCustomizationId() != null && userParamInfo.getModelCustomizationId() != null) &&
131                         (!info.getModelCustomizationId().equals(userParamInfo.getModelCustomizationId()))){
132                         throw new ValidationException("modelCustomizationId in modelInfo", "modelCustomizationId in userParams service");
133                 }
134         }
135 }