8646a74a2f2abd8657b3a951988b02d21260d47e
[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.HashMap;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Optional;
30 import java.util.Set;
31 import org.apache.commons.lang3.StringUtils;
32 import org.onap.so.apihandlerinfra.Action;
33 import org.onap.so.apihandlerinfra.Actions;
34 import org.onap.so.exceptions.ValidationException;
35 import org.onap.so.serviceinstancebeans.ModelInfo;
36 import org.onap.so.serviceinstancebeans.Networks;
37 import org.onap.so.serviceinstancebeans.Service;
38 import org.onap.so.serviceinstancebeans.VfModules;
39 import org.onap.so.serviceinstancebeans.Vnfs;
40
41 public class UserParamsValidation implements ValidationRule {
42     @Override
43     public ValidationInformation validate(ValidationInformation info) throws ValidationException {
44         Service validate = info.getUserParams();
45         if (validate.getModelInfo() == null) {
46             throw new ValidationException("modelInfo in userParams", true);
47         } else if (validate.getModelInfo().getModelType() == null) {
48             throw new ValidationException("modelType in userParams service modelInfo", true);
49         } else if (validate.getModelInfo().getModelVersionId() == null) {
50             throw new ValidationException("modelVersionId in userParams service modelInfo", true);
51         }
52         modelInfoValidation(info.getSir().getRequestDetails().getModelInfo(), validate.getModelInfo());
53         if (validate.getInstanceName() != null && info.getRequestInfo().getInstanceName() != null) {
54             instanceNameValidation(info, validate);
55         }
56
57         Actions action = info.getAction();
58         Map<String, Set<String>> vnfCustomIdToInstanceNames = new HashMap<>();
59         Map<String, Set<String>> vfModuleCustomIdToInstanceNames = new HashMap<>();
60         for (Vnfs vnf : validate.getResources().getVnfs()) {
61             if (vnf.getModelInfo() == null) {
62                 throw new ValidationException("modelInfo in userParams vnf resources", true);
63             } else if (vnf.getModelInfo().getModelCustomizationId() == null) {
64                 throw new ValidationException("modelCustomizationId in userParams vnf resources", true);
65             } else if (vnf.getModelInfo().getModelVersionId() == null) {
66                 throw new ValidationException("modelVersionId in userParams vnf resources", true);
67             }
68             if (vnf.getCloudConfiguration() == null) {
69                 throw new ValidationException("cloudConfiguration in userParams vnf resources", true);
70             }
71             if (action == Action.createInstance || action == Action.assignInstance) {
72                 if (vnf.getPlatform() == null) {
73                     throw new ValidationException("platform in userParams vnf resources", true);
74                 }
75                 if (vnf.getProductFamilyId() == null) {
76                     throw new ValidationException("productFamilyId in userParams vnf resources", true);
77                 }
78             }
79             if (vnf.getPlatform() != null && vnf.getPlatform().getPlatformName() == null) {
80                 throw new ValidationException("platformName in userParams vnf resources", true);
81             }
82
83             String vnfCustomizationId = vnf.getModelInfo().getModelCustomizationId();
84             vnfCustomIdToInstanceNames.putIfAbsent(vnfCustomizationId, new HashSet<>());
85             String vnfInstanceName = StringUtils.defaultString(vnf.getInstanceName());
86             Set<String> vnfVisitedInstanceNames = vnfCustomIdToInstanceNames.get(vnfCustomizationId);
87             if (!vnfVisitedInstanceNames.add(vnfInstanceName)) {
88                 throw new ValidationException(
89                         "instanceName: same instanceName with same modelCustomizationId in userParams vnf resources",
90                         true);
91             }
92             if (vnf.getVfModules().isEmpty()) {
93                 throw new ValidationException("vfModules in userParams vnf resources", true);
94             }
95
96             for (VfModules vfModule : vnf.getVfModules()) {
97                 if (vfModule.getModelInfo() == null) {
98                     throw new ValidationException("modelInfo in userParams vfModules resources", true);
99                 } else if (vfModule.getModelInfo().getModelCustomizationId() == null) {
100                     throw new ValidationException("modelCustomizationId in userParams vfModule resources", true);
101                 } else if (vfModule.getModelInfo().getModelVersionId() == null) {
102                     throw new ValidationException("modelVersionId in userParams vfModule resources", true);
103                 }
104
105                 String vfModulecustomizationId = vfModule.getModelInfo().getModelCustomizationId();
106                 vfModuleCustomIdToInstanceNames.putIfAbsent(vfModulecustomizationId, new HashSet<>());
107                 String vfModuleInstanceName = StringUtils.defaultString(vfModule.getInstanceName());
108                 Set<String> vfModuleVisitedInstanceNames = vfModuleCustomIdToInstanceNames.get(vfModulecustomizationId);
109                 if (!vfModuleVisitedInstanceNames.add(vfModuleInstanceName)) {
110                     throw new ValidationException(
111                             "instanceName: same instanceName with same modelCustomizationId in userParams vfModule resources",
112                             true);
113                 }
114             }
115         }
116         validateDuplicateInstanceNames(vnfCustomIdToInstanceNames, "vnf");
117         validateDuplicateInstanceNames(vfModuleCustomIdToInstanceNames, "vfModule");
118
119         List<Networks> validateNetworks = new ArrayList<>();
120         validateNetworks = validate.getResources().getNetworks();
121         if (validateNetworks != null) {
122             for (Networks networks : validateNetworks) {
123                 if (networks.getModelInfo() == null) {
124                     throw new ValidationException("modelInfo in userParams network resources", true);
125                 } else if (networks.getModelInfo().getModelCustomizationId() == null) {
126                     throw new ValidationException("modelCustomizationId in userParams network resources", true);
127                 } else if (networks.getModelInfo().getModelVersionId() == null) {
128                     throw new ValidationException("modelVersionId in userParams network resources", true);
129                 }
130                 if (networks.getCloudConfiguration() == null) {
131                     throw new ValidationException("cloudConfiguration in userParams network resources", true);
132                 }
133             }
134         }
135         return info;
136     }
137
138     public void instanceNameValidation(ValidationInformation info, Service validate) throws ValidationException {
139         if (!info.getRequestInfo().getInstanceName().equals(validate.getInstanceName())) {
140             throw new ValidationException("instanceName in requestInfo", "instanceName in userParams service");
141         }
142     }
143
144     public void modelInfoValidation(ModelInfo info, ModelInfo userParamInfo) throws ValidationException {
145         if (!info.getModelType().equals(userParamInfo.getModelType())) {
146             throw new ValidationException("modelType in modelInfo", "modelType in userParams service");
147         }
148         if ((info.getModelInvariantId() != null && userParamInfo.getModelInvariantId() != null)
149                 && (!info.getModelInvariantId().equals(userParamInfo.getModelInvariantId()))) {
150             throw new ValidationException("modelInvariantId in modelInfo", "modelInvariantId in userParams service");
151         }
152         if (!info.getModelVersionId().equals(userParamInfo.getModelVersionId())) {
153             throw new ValidationException("modelVersionId in modelInfo", "modelVersionId in userParams service");
154         }
155         if ((info.getModelName() != null && userParamInfo.getModelName() != null)
156                 && (!info.getModelName().equals(userParamInfo.getModelName()))) {
157             throw new ValidationException("modelName in modelInfo", "modelName in userParams service");
158         }
159         if ((info.getModelVersion() != null && userParamInfo.getModelVersion() != null)
160                 && (!info.getModelVersion().equals(userParamInfo.getModelVersion()))) {
161             throw new ValidationException("modelVersion in modelInfo", "modelVersion in userParams service");
162         }
163         if ((info.getModelCustomizationId() != null && userParamInfo.getModelCustomizationId() != null)
164                 && (!info.getModelCustomizationId().equals(userParamInfo.getModelCustomizationId()))) {
165             throw new ValidationException("modelCustomizationId in modelInfo",
166                     "modelCustomizationId in userParams service");
167         }
168     }
169
170     private void validateDuplicateInstanceNames(Map<String, Set<String>> duplicateValidator, String type)
171             throws ValidationException {
172         Set<String> allInstanceNames = new HashSet<>();
173         for (Map.Entry<String, Set<String>> entry : duplicateValidator.entrySet()) {
174             Set<String> instanceNames = entry.getValue();
175             if (instanceNames.size() > 1 && instanceNames.contains(""))
176                 throw new ValidationException(String.format(
177                         "instanceName: instanceName is missing or empty with same modelCustomizationId in userParams %s resources",
178                         type), true);
179
180             for (String instanceName : instanceNames) {
181                 if (!instanceName.isBlank() && !allInstanceNames.add(instanceName)) {
182                     throw new ValidationException(String.format(
183                             "instanceName: same instanceName but different modelCustomizationId (instanceName should be unique)  in userParams %s resources",
184                             type), true);
185                 }
186             }
187         }
188     }
189 }