b02bd95d975e91c65477b95553be177dbeb0bdf9
[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  * Modifications Copyright (c) 2019 Samsung
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.so.apihandlerinfra.validation;
25
26 import org.onap.so.apihandlerinfra.Action;
27 import org.onap.so.apihandlerinfra.Actions;
28 import org.onap.so.apihandlerinfra.Constants;
29 import org.onap.so.exceptions.ValidationException;
30 import org.onap.so.serviceinstancebeans.InstanceDirection;
31 import org.onap.so.serviceinstancebeans.ModelInfo;
32 import org.onap.so.serviceinstancebeans.ModelType;
33 import org.onap.so.serviceinstancebeans.RelatedInstance;
34 import org.onap.so.serviceinstancebeans.RelatedInstanceList;
35 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
36 import org.onap.so.utils.UUIDChecker;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public class RelatedInstancesValidation implements ValidationRule {
41
42     private static Logger logger = LoggerFactory.getLogger(RelatedInstancesValidation.class);
43
44     private static boolean empty(String s) {
45         return (s == null || s.trim().isEmpty());
46     }
47
48     @Override
49     public ValidationInformation validate(ValidationInformation info) throws ValidationException {
50         ServiceInstancesRequest sir = info.getSir();
51         Actions action = info.getAction();
52         int reqVersion = info.getReqVersion();
53         String requestScope = info.getRequestScope();
54         String serviceInstanceType = null;
55         String networkType = null;
56         String vnfType = null;
57         String vfModuleType = null;
58         String vfModuleModelName = null;
59         ModelInfo modelInfo = info.getSir().getRequestDetails().getModelInfo();
60         RelatedInstanceList[] instanceList = sir.getRequestDetails().getRelatedInstanceList();
61         String serviceModelName = null;
62         String vnfModelName = null;
63         String asdcServiceModelVersion = null;
64         String volumeGroupId = null;
65         boolean isRelatedServiceInstancePresent = false;
66         boolean isRelatedVnfInstancePresent = false;
67         boolean isSourceVnfPresent = false;
68         boolean isDestinationVnfPresent = false;
69         boolean isConnectionPointPresent = false;
70
71         if (requestScope.equalsIgnoreCase(ModelType.service.name())) {
72             serviceInstanceType = modelInfo.getModelName();
73             info.setServiceInstanceType(serviceInstanceType);
74         }
75         if (requestScope.equalsIgnoreCase(ModelType.network.name())) {
76             networkType = modelInfo.getModelName();
77             info.setNetworkType(networkType);
78         }
79         if (instanceList != null) {
80             for (RelatedInstanceList relatedInstanceList : instanceList) {
81                 RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();
82
83                 ModelInfo relatedInstanceModelInfo = relatedInstance.getModelInfo();
84                 if (relatedInstanceModelInfo == null) {
85                     throw new ValidationException("modelInfo in relatedInstance");
86                 }
87
88                 if (relatedInstanceModelInfo.getModelType() == null) {
89                     throw new ValidationException("modelType in relatedInstance");
90                 }
91
92                 if (empty(relatedInstance.getInstanceName())
93                         && ModelType.pnf.equals(relatedInstanceModelInfo.getModelType())) {
94                     throw new ValidationException("instanceName in relatedInstance for pnf modelType");
95                 }
96
97                 if (!empty(relatedInstance.getInstanceName())) {
98                     if (!relatedInstance.getInstanceName().matches(Constants.VALID_INSTANCE_NAME_FORMAT)) {
99                         throw new ValidationException("instanceName format in relatedInstance");
100                     }
101                 }
102
103                 if (empty(relatedInstance.getInstanceId())
104                         && !ModelType.pnf.equals(relatedInstanceModelInfo.getModelType())) {
105                     throw new ValidationException("instanceId in relatedInstance");
106                 }
107
108                 if (!empty(relatedInstance.getInstanceId())
109                         && !UUIDChecker.isValidUUID(relatedInstance.getInstanceId())) {
110                     throw new ValidationException("instanceId format in relatedInstance");
111                 }
112                 if (empty(relatedInstanceModelInfo.getModelVersionId())
113                         && requestScope.equals(ModelType.instanceGroup.toString())
114                         && relatedInstanceModelInfo.getModelType().equals(ModelType.service)) {
115                     throw new ValidationException("modelVersionId in relatedInstance", true);
116                 }
117                 if (requestScope.equalsIgnoreCase(ModelType.instanceGroup.toString())
118                         && relatedInstanceModelInfo.getModelType().equals(ModelType.service)) {
119                     isRelatedServiceInstancePresent = true;
120                 }
121
122                 if (action != Action.deleteInstance
123                         && !requestScope.equalsIgnoreCase(ModelType.instanceGroup.toString())) {
124                     if (!(relatedInstanceModelInfo.getModelType().equals(ModelType.volumeGroup)
125                             || relatedInstanceModelInfo.getModelType().equals(ModelType.connectionPoint)
126                             || relatedInstanceModelInfo.getModelType().equals(ModelType.pnf)
127                             || relatedInstanceModelInfo.getModelType().equals(ModelType.networkInstanceGroup))) {
128
129                         if (empty(relatedInstanceModelInfo.getModelInvariantId())) {
130                             throw new ValidationException("modelInvariantId in relatedInstance");
131                         } else if (reqVersion >= 4 && empty(relatedInstanceModelInfo.getModelVersionId())) {
132                             throw new ValidationException("modelVersionId in relatedInstance");
133                         } else if (empty(relatedInstanceModelInfo.getModelName())) {
134                             throw new ValidationException("modelName in relatedInstance");
135                         } else if (empty(relatedInstanceModelInfo.getModelVersion())) {
136                             throw new ValidationException("modelVersion in relatedInstance");
137                         }
138                     }
139
140                     if (!empty(relatedInstanceModelInfo.getModelInvariantId())
141                             && !UUIDChecker.isValidUUID(relatedInstanceModelInfo.getModelInvariantId())) {
142                         throw new ValidationException("modelInvariantId format in relatedInstance");
143                     }
144
145                     if (ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
146                         if (InstanceDirection.source.equals(relatedInstance.getInstanceDirection())
147                                 && relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)) {
148                             isSourceVnfPresent = true;
149                         } else if (InstanceDirection.destination.equals(relatedInstance.getInstanceDirection())
150                                 && (relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)
151                                         || (relatedInstanceModelInfo.getModelType().equals(ModelType.pnf)))) {
152                             isDestinationVnfPresent = true;
153                         }
154                     }
155
156                     if (ModelType.connectionPoint.equals(relatedInstanceModelInfo.getModelType())
157                             && ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
158                         isConnectionPointPresent = true;
159                     }
160                 }
161
162                 if (empty(relatedInstanceModelInfo.getModelCustomizationName())
163                         && relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)) {
164                     if (reqVersion >= 4 && empty(relatedInstanceModelInfo.getModelCustomizationId())
165                             && action != Action.deleteInstance) {
166                         throw new ValidationException(
167                                 "modelCustomizationName or modelCustomizationId in relatedInstance of vnf");
168                     }
169                 }
170
171                 if (relatedInstanceModelInfo.getModelType().equals(ModelType.service)
172                         && !(requestScope.equalsIgnoreCase(ModelType.instanceGroup.toString())
173                                 && action == Action.createInstance)) {
174                     isRelatedServiceInstancePresent = true;
175                     if (!relatedInstance.getInstanceId().equals(sir.getServiceInstanceId())) {
176                         throw new ValidationException(
177                                 "serviceInstanceId matching the serviceInstanceId in request URI");
178                     }
179                     serviceModelName = relatedInstanceModelInfo.getModelName();
180                     asdcServiceModelVersion = relatedInstanceModelInfo.getModelVersion();
181                 } else if (relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)
182                         && !(ModelType.configuration.name().equalsIgnoreCase(requestScope))) {
183                     isRelatedVnfInstancePresent = true;
184                     if (!relatedInstance.getInstanceId().equals(sir.getVnfInstanceId())) {
185                         throw new ValidationException("vnfInstanceId matching the vnfInstanceId in request URI");
186                     }
187                     vnfModelName = relatedInstanceModelInfo.getModelCustomizationName();
188                 } else if (relatedInstanceModelInfo.getModelType().equals(ModelType.volumeGroup)) {
189                     volumeGroupId = relatedInstance.getInstanceId();
190                 }
191             }
192
193             if (action == Action.createInstance && ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
194                 if (!isSourceVnfPresent) {
195                     throw new ValidationException("source vnf relatedInstance for Port Configuration");
196                 }
197
198                 if (!isDestinationVnfPresent) {
199                     throw new ValidationException("destination vnf relatedInstance for Port Configuration");
200                 }
201             }
202
203             if ((action == Action.enablePort || action == Action.disablePort)
204                     && ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
205                 if (!isConnectionPointPresent) {
206                     throw new ValidationException("connectionPoint relatedInstance for Port Configuration");
207                 }
208             }
209             if (requestScope.equals(ModelType.instanceGroup.toString())) {
210                 if (!isRelatedServiceInstancePresent) {
211                     throw new ValidationException("related service instance for instanceGroup request", true);
212                 }
213             }
214             if (requestScope.equalsIgnoreCase(ModelType.volumeGroup.name())) {
215                 if (!isRelatedServiceInstancePresent) {
216                     throw new ValidationException("related service instance for volumeGroup request");
217                 }
218                 if (!isRelatedVnfInstancePresent) {
219                     throw new ValidationException("related vnf instance for volumeGroup request");
220                 }
221                 serviceInstanceType = serviceModelName;
222                 vnfType = serviceModelName + "/" + vnfModelName;
223                 info.setServiceInstanceType(serviceInstanceType);
224                 info.setVnfType(vnfType);
225             } else if (requestScope.equalsIgnoreCase(ModelType.vfModule.name())) {
226                 if (!isRelatedServiceInstancePresent) {
227                     throw new ValidationException("related service instance for vfModule request");
228                 }
229                 if (!isRelatedVnfInstancePresent) {
230                     throw new ValidationException("related vnf instance for vfModule request");
231                 }
232                 vfModuleModelName = modelInfo.getModelName();
233                 serviceInstanceType = serviceModelName;
234                 vnfType = serviceModelName + "/" + vnfModelName;
235                 vfModuleType = vnfType + "::" + vfModuleModelName;
236                 sir.setVolumeGroupInstanceId(volumeGroupId);
237                 info.setVfModuleModelName(vfModuleModelName);
238                 info.setVnfType(vnfType);
239                 info.setServiceInstanceType(serviceInstanceType);
240                 info.setVfModuleType(vfModuleType);
241             } else if (requestScope.equalsIgnoreCase(ModelType.vnf.name())) {
242                 if (!isRelatedServiceInstancePresent) {
243                     throw new ValidationException("related service instance for vnf request");
244                 }
245                 vnfType = serviceModelName + "/" + sir.getRequestDetails().getModelInfo().getModelCustomizationName();
246                 info.setVnfType(vnfType);
247             }
248         } else if (((requestScope.equalsIgnoreCase(ModelType.vnf.name())
249                 || requestScope.equalsIgnoreCase(ModelType.volumeGroup.name())
250                 || requestScope.equalsIgnoreCase(ModelType.vfModule.name())
251                 || requestScope.equalsIgnoreCase(ModelType.configuration.name()))
252                 && (action == Action.createInstance || action == Action.enablePort || action == Action.disablePort))
253                 || (reqVersion >= 4
254                         && (requestScope.equalsIgnoreCase(ModelType.volumeGroup.name())
255                                 || requestScope.equalsIgnoreCase(ModelType.vfModule.name()))
256                         && action == Action.updateInstance
257                         || (requestScope.equalsIgnoreCase(ModelType.vfModule.name()) && action == Action.scaleOut))
258                 || (requestScope.equalsIgnoreCase(ModelType.service.name())
259                         && (action.equals(Action.addRelationships) || action.equals(Action.removeRelationships)))) {
260             logger.debug("related instance exception");
261             throw new ValidationException("related instances");
262         }
263         if (instanceList == null && requestScope.equalsIgnoreCase(ModelType.instanceGroup.toString())
264                 && action == Action.createInstance) {
265             throw new ValidationException("relatedInstanceList", true);
266         }
267         info.setVfModuleModelName(vfModuleModelName);
268         info.setServiceInstanceType(serviceInstanceType);
269         info.setVnfType(vnfType);
270         info.setAsdcServiceModelVersion(asdcServiceModelVersion);
271         info.setVfModuleType(vfModuleType);
272         return info;
273     }
274 }