Replaced all tabs with spaces in java and pom.xml
[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 import org.onap.so.apihandlerinfra.Action;
27 import org.onap.so.apihandlerinfra.Actions;
28 import org.onap.so.exceptions.ValidationException;
29 import org.onap.so.serviceinstancebeans.ModelInfo;
30 import org.onap.so.serviceinstancebeans.Networks;
31 import org.onap.so.serviceinstancebeans.Service;
32 import org.onap.so.serviceinstancebeans.VfModules;
33 import org.onap.so.serviceinstancebeans.Vnfs;
34
35 public class UserParamsValidation implements ValidationRule {
36     @Override
37     public ValidationInformation validate(ValidationInformation info) throws ValidationException {
38         Service validate = info.getUserParams();
39         Actions action = info.getAction();
40
41         if (validate.getModelInfo() == null) {
42             throw new ValidationException("modelInfo in userParams", true);
43         } else if (validate.getModelInfo().getModelType() == null) {
44             throw new ValidationException("modelType in userParams service modelInfo", true);
45         } else if (validate.getModelInfo().getModelVersionId() == null) {
46             throw new ValidationException("modelVersionId in userParams service modelInfo", true);
47         }
48         modelInfoValidation(info.getSir().getRequestDetails().getModelInfo(), validate.getModelInfo());
49         if (validate.getInstanceName() != null && info.getRequestInfo().getInstanceName() != null) {
50             instanceNameValidation(info, validate);
51         }
52         for (Vnfs vnf : validate.getResources().getVnfs()) {
53             if (vnf.getModelInfo() == null) {
54                 throw new ValidationException("modelInfo in userParams vnf resources", true);
55             } else if (vnf.getModelInfo().getModelCustomizationId() == null) {
56                 throw new ValidationException("modelCustomizationId in userParams vnf resources", true);
57             } else if (vnf.getModelInfo().getModelVersionId() == null) {
58                 throw new ValidationException("modelVersionId in userParams vnf resources", true);
59             }
60             if (vnf.getCloudConfiguration() == null) {
61                 throw new ValidationException("cloudConfiguration in userParams vnf resources", true);
62             }
63             if (action == Action.createInstance || action == Action.assignInstance) {
64                 if (vnf.getPlatform() == null) {
65                     throw new ValidationException("platform in userParams vnf resources", true);
66                 }
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
107     public void instanceNameValidation(ValidationInformation info, Service validate) throws ValidationException {
108         if (!info.getRequestInfo().getInstanceName().equals(validate.getInstanceName())) {
109             throw new ValidationException("instanceName in requestInfo", "instanceName in userParams service");
110         }
111     }
112
113     public void modelInfoValidation(ModelInfo info, ModelInfo userParamInfo) throws ValidationException {
114         if (!info.getModelType().equals(userParamInfo.getModelType())) {
115             throw new ValidationException("modelType in modelInfo", "modelType in userParams service");
116         }
117         if ((info.getModelInvariantId() != null && userParamInfo.getModelInvariantId() != null)
118                 && (!info.getModelInvariantId().equals(userParamInfo.getModelInvariantId()))) {
119             throw new ValidationException("modelInvariantId in modelInfo", "modelInvariantId in userParams service");
120         }
121         if (!info.getModelVersionId().equals(userParamInfo.getModelVersionId())) {
122             throw new ValidationException("modelVersionId in modelInfo", "modelVersionId in userParams service");
123         }
124         if ((info.getModelName() != null && userParamInfo.getModelName() != null)
125                 && (!info.getModelName().equals(userParamInfo.getModelName()))) {
126             throw new ValidationException("modelName in modelInfo", "modelName in userParams service");
127         }
128         if ((info.getModelVersion() != null && userParamInfo.getModelVersion() != null)
129                 && (!info.getModelVersion().equals(userParamInfo.getModelVersion()))) {
130             throw new ValidationException("modelVersion in modelInfo", "modelVersion in userParams service");
131         }
132         if ((info.getModelCustomizationId() != null && userParamInfo.getModelCustomizationId() != null)
133                 && (!info.getModelCustomizationId().equals(userParamInfo.getModelCustomizationId()))) {
134             throw new ValidationException("modelCustomizationId in modelInfo",
135                     "modelCustomizationId in userParams service");
136         }
137     }
138 }