5eb30830032e86b09ac8b4698ee857b4dc757e45
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / onap / so / apihandlerinfra / validation / RelatedInstancesValidation.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  * 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         @Override
48         public ValidationInformation validate(ValidationInformation info) throws ValidationException{
49                 ServiceInstancesRequest sir = info.getSir();
50                 Actions action = info.getAction();
51                 int reqVersion = info.getReqVersion();
52                 String requestScope = info.getRequestScope();
53         String serviceInstanceType = null;
54         String networkType = null;
55         String vnfType = null;
56         String vfModuleType = null;
57         String vfModuleModelName = null;
58                 ModelInfo modelInfo = info.getSir().getRequestDetails().getModelInfo();
59                 RelatedInstanceList[] instanceList = sir.getRequestDetails().getRelatedInstanceList();
60                 String serviceModelName = null;
61         String vnfModelName = null;
62         String asdcServiceModelVersion = null;
63         String volumeGroupId = null;
64         boolean isRelatedServiceInstancePresent = false;
65         boolean isRelatedVnfInstancePresent = false;
66         boolean isSourceVnfPresent = false;
67         boolean isDestinationVnfPresent = false;
68         boolean isConnectionPointPresent = false;       
69                 
70         if(requestScope.equalsIgnoreCase(ModelType.service.name())){
71                         serviceInstanceType = modelInfo.getModelName();
72                         info.setServiceInstanceType(serviceInstanceType);
73             }
74             if(requestScope.equalsIgnoreCase(ModelType.network.name())){
75                 networkType = modelInfo.getModelName();
76                 info.setNetworkType(networkType);
77             }
78             if (instanceList != null) {
79                 for(RelatedInstanceList relatedInstanceList : instanceList){
80                         RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();
81
82                         ModelInfo relatedInstanceModelInfo = relatedInstance.getModelInfo ();
83                                 if (relatedInstanceModelInfo == null) {
84                                 throw new ValidationException ("modelInfo in relatedInstance");
85                         }
86
87                         if (relatedInstanceModelInfo.getModelType () == null) {
88                                 throw new ValidationException ("modelType in relatedInstance");
89                         }
90
91                         if(empty(relatedInstance.getInstanceName ()) && ModelType.pnf.equals(relatedInstanceModelInfo.getModelType())) {
92                                 throw new ValidationException ("instanceName in relatedInstance for pnf modelType");
93                         }
94                         
95                         if (!empty (relatedInstance.getInstanceName ())) {
96                         if (!relatedInstance.getInstanceName ().matches (Constants.VALID_INSTANCE_NAME_FORMAT)) {
97                                 throw new ValidationException ("instanceName format in relatedInstance");
98                         }
99                     }
100
101                         if (empty (relatedInstance.getInstanceId ()) && !ModelType.pnf.equals(relatedInstanceModelInfo.getModelType())) {
102                                 throw new ValidationException ("instanceId in relatedInstance");
103                         }
104
105                         if (!empty(relatedInstance.getInstanceId ()) && !UUIDChecker.isValidUUID (relatedInstance.getInstanceId ())) {
106                                 throw new ValidationException ("instanceId format in relatedInstance");
107                         }
108                         if(empty(relatedInstanceModelInfo.getModelVersionId()) && requestScope.equals(ModelType.instanceGroup.toString()) && relatedInstanceModelInfo.getModelType().equals(ModelType.service)){
109                                 throw new ValidationException("modelVersionId in relatedInstance", true);
110                         }
111                         if(requestScope.equalsIgnoreCase(ModelType.instanceGroup.toString()) && relatedInstanceModelInfo.getModelType().equals(ModelType.service)){
112                                 isRelatedServiceInstancePresent = true;
113                         }
114                   
115                         if (action != Action.deleteInstance && !requestScope.equalsIgnoreCase(ModelType.instanceGroup.toString())) {
116                                 if(!(   relatedInstanceModelInfo.getModelType().equals(ModelType.volumeGroup) || 
117                                                 relatedInstanceModelInfo.getModelType().equals(ModelType.connectionPoint) ||
118                                                 relatedInstanceModelInfo.getModelType().equals(ModelType.pnf) ||
119                                                 relatedInstanceModelInfo.getModelType().equals(ModelType.networkInstanceGroup))) {
120
121                                         if(empty (relatedInstanceModelInfo.getModelInvariantId ())) {
122                                                 throw new ValidationException ("modelInvariantId in relatedInstance");
123                                         } else if(reqVersion >= 4 && empty(relatedInstanceModelInfo.getModelVersionId ())) {
124                                                 throw new ValidationException("modelVersionId in relatedInstance");
125                                         } else if(empty(relatedInstanceModelInfo.getModelName ())) {
126                                                 throw new ValidationException ("modelName in relatedInstance");
127                                         } else if (empty (relatedInstanceModelInfo.getModelVersion ())) {
128                                                 throw new ValidationException ("modelVersion in relatedInstance");
129                                         }
130                                 }
131
132                                 if (!empty (relatedInstanceModelInfo.getModelInvariantId ()) &&
133                                                 !UUIDChecker.isValidUUID (relatedInstanceModelInfo.getModelInvariantId ())) {
134                                         throw new ValidationException ("modelInvariantId format in relatedInstance");
135                                 }
136                                 
137                                 if(ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
138                                         if(InstanceDirection.source.equals(relatedInstance.getInstanceDirection()) && relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)) {
139                                                 isSourceVnfPresent = true;
140                                         } else if(InstanceDirection.destination.equals(relatedInstance.getInstanceDirection()) && 
141                                                         (relatedInstanceModelInfo.getModelType().equals(ModelType.vnf) || (relatedInstanceModelInfo.getModelType().equals(ModelType.pnf)))) {
142                                                 isDestinationVnfPresent = true;
143                                         }
144                                 }
145                                 
146                                 if(ModelType.connectionPoint.equals(relatedInstanceModelInfo.getModelType()) && ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
147                                         isConnectionPointPresent = true;
148                                 }
149                         }
150
151                         if (empty (relatedInstanceModelInfo.getModelCustomizationName ()) && relatedInstanceModelInfo.getModelType ().equals (ModelType.vnf) ) {
152                                 if(reqVersion >=4 && empty (relatedInstanceModelInfo.getModelCustomizationId()) && action != Action.deleteInstance) {
153                                         throw new ValidationException ("modelCustomizationName or modelCustomizationId in relatedInstance of vnf");
154                                 }
155                         }
156
157                         if(relatedInstanceModelInfo.getModelType().equals(ModelType.service) && !(requestScope.equalsIgnoreCase(ModelType.instanceGroup.toString()) && action == Action.createInstance)) {
158                                 isRelatedServiceInstancePresent = true;
159                                 if (!relatedInstance.getInstanceId ().equals (sir.getServiceInstanceId ())) {
160                                         throw new ValidationException ("serviceInstanceId matching the serviceInstanceId in request URI");
161                                 }
162                                 serviceModelName = relatedInstanceModelInfo.getModelName ();
163                                 asdcServiceModelVersion = relatedInstanceModelInfo.getModelVersion ();
164                         } else if(relatedInstanceModelInfo.getModelType().equals(ModelType.vnf) && !(ModelType.configuration.name().equalsIgnoreCase(requestScope))) {
165                                 isRelatedVnfInstancePresent = true;
166                                 if (!relatedInstance.getInstanceId ().equals (sir.getVnfInstanceId ())) {
167                                         throw new ValidationException ("vnfInstanceId matching the vnfInstanceId in request URI");
168                                 }
169                                 vnfModelName = relatedInstanceModelInfo.getModelCustomizationName();
170                         } else if(relatedInstanceModelInfo.getModelType().equals(ModelType.volumeGroup)) {                              
171                                 volumeGroupId = relatedInstance.getInstanceId ();
172                         }
173                 }
174
175                 if(action == Action.createInstance && ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
176                         if(!isSourceVnfPresent) {
177                                 throw new ValidationException ("source vnf relatedInstance for Port Configuration");
178                         } 
179                         
180                         if(!isDestinationVnfPresent) {
181                                 throw new ValidationException ("destination vnf relatedInstance for Port Configuration");
182                         }
183                 }
184
185                 if((action == Action.enablePort || action == Action.disablePort) && ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
186                         if(!isConnectionPointPresent) {
187                                 throw new ValidationException ("connectionPoint relatedInstance for Port Configuration");
188                         }
189                 }
190                 if(requestScope.equals(ModelType.instanceGroup.toString())){
191                         if(!isRelatedServiceInstancePresent){
192                                 throw new ValidationException("related service instance for instanceGroup request", true);
193                         }
194                 }
195                 if(requestScope.equalsIgnoreCase (ModelType.volumeGroup.name ())) {
196                         if (!isRelatedServiceInstancePresent) {
197                                 throw new ValidationException ("related service instance for volumeGroup request");
198                         }
199                         if (!isRelatedVnfInstancePresent) {
200                                 throw new ValidationException ("related vnf instance for volumeGroup request");
201                         }
202                         serviceInstanceType = serviceModelName;
203                         vnfType = serviceModelName + "/" + vnfModelName;
204                         info.setServiceInstanceType(serviceInstanceType);
205                         info.setVnfType(vnfType);
206                 }
207                 else if(requestScope.equalsIgnoreCase(ModelType.vfModule.name ())) {
208                         if (!isRelatedServiceInstancePresent) {
209                                 throw new ValidationException ("related service instance for vfModule request");
210                         }
211                         if (!isRelatedVnfInstancePresent) {
212                                 throw new ValidationException ("related vnf instance for vfModule request");
213                         }
214                         vfModuleModelName = modelInfo.getModelName ();
215                         serviceInstanceType = serviceModelName;
216                         vnfType = serviceModelName + "/" + vnfModelName;
217                         vfModuleType = vnfType + "::" + vfModuleModelName;
218                         sir.setVolumeGroupInstanceId (volumeGroupId);
219                         info.setVfModuleModelName(vfModuleModelName);
220                         info.setVnfType(vnfType);
221                         info.setServiceInstanceType(serviceInstanceType);
222                         info.setVfModuleType(vfModuleType);
223                 }
224                 else if (requestScope.equalsIgnoreCase (ModelType.vnf.name ())) {
225                         if (!isRelatedServiceInstancePresent) {
226                                 throw new ValidationException ("related service instance for vnf request");
227                         }
228                         vnfType = serviceModelName + "/" + sir.getRequestDetails().getModelInfo().getModelCustomizationName();
229                         info.setVnfType(vnfType);
230                }
231         }
232         else if ((( requestScope.equalsIgnoreCase(ModelType.vnf.name ()) || requestScope.equalsIgnoreCase(ModelType.volumeGroup.name ()) || requestScope.equalsIgnoreCase(ModelType.vfModule.name ()) 
233                                 || requestScope.equalsIgnoreCase(ModelType.configuration.name())) && (action == Action.createInstance || action == Action.enablePort || action == Action.disablePort)) ||
234                         (reqVersion >= 4 && (requestScope.equalsIgnoreCase(ModelType.volumeGroup.name ()) || requestScope.equalsIgnoreCase(ModelType.vfModule.name ())) && action == Action.updateInstance ||
235                         (requestScope.equalsIgnoreCase(ModelType.vfModule.name ()) && action == Action.scaleOut)) ||
236                                 (requestScope.equalsIgnoreCase(ModelType.service.name()) && (action.equals(Action.addRelationships) || action.equals(Action.removeRelationships)))){
237                  logger.debug("related instance exception");
238                 throw new ValidationException ("related instances");
239         }
240             if(instanceList == null && requestScope.equalsIgnoreCase(ModelType.instanceGroup.toString()) && action == Action.createInstance){
241                 throw new ValidationException("relatedInstanceList", true);
242             }
243         info.setVfModuleModelName(vfModuleModelName);
244         info.setServiceInstanceType(serviceInstanceType);
245         info.setVnfType(vnfType);
246         info.setAsdcServiceModelVersion(asdcServiceModelVersion);
247         info.setVfModuleType(vfModuleType);
248                 return info;
249         }
250 }