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