Added vfModule scale out endpoint and validation
[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  * 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
104
105                         if (action != Action.deleteInstance) {
106                                 if(!(   relatedInstanceModelInfo.getModelType().equals(ModelType.volumeGroup) || 
107                                                 relatedInstanceModelInfo.getModelType().equals(ModelType.connectionPoint) ||
108                                                 relatedInstanceModelInfo.getModelType().equals(ModelType.pnf) ||
109                                                 relatedInstanceModelInfo.getModelType().equals(ModelType.networkCollection))) {
110
111                                         if(empty (relatedInstanceModelInfo.getModelInvariantId ())) {
112                                                 throw new ValidationException ("modelInvariantId in relatedInstance");
113                                         } else if(reqVersion >= 4 && empty(relatedInstanceModelInfo.getModelVersionId ())) {
114                                                 throw new ValidationException("modelVersionId in relatedInstance");
115                                         } else if(empty(relatedInstanceModelInfo.getModelName ())) {
116                                                 throw new ValidationException ("modelName in relatedInstance");
117                                         } else if (empty (relatedInstanceModelInfo.getModelVersion ())) {
118                                                 throw new ValidationException ("modelVersion in relatedInstance");
119                                         }
120                                 }
121
122                                 if (!empty (relatedInstanceModelInfo.getModelInvariantId ()) &&
123                                                 !UUIDChecker.isValidUUID (relatedInstanceModelInfo.getModelInvariantId ())) {
124                                         throw new ValidationException ("modelInvariantId format in relatedInstance");
125                                 }
126                                 
127                                 if(ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
128                                         if(InstanceDirection.source.equals(relatedInstance.getInstanceDirection()) && relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)) {
129                                                 isSourceVnfPresent = true;
130                                         } else if(InstanceDirection.destination.equals(relatedInstance.getInstanceDirection()) && 
131                                                         (relatedInstanceModelInfo.getModelType().equals(ModelType.vnf) || (relatedInstanceModelInfo.getModelType().equals(ModelType.pnf) && reqVersion == 6))) {
132                                                 isDestinationVnfPresent = true;
133                                         }
134                                 }
135                                 
136                                 if(ModelType.connectionPoint.equals(relatedInstanceModelInfo.getModelType()) && ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
137                                         isConnectionPointPresent = true;
138                                 }
139                         }
140
141                         if (empty (relatedInstanceModelInfo.getModelCustomizationName ()) && relatedInstanceModelInfo.getModelType ().equals (ModelType.vnf) ) {
142                                 if(reqVersion >=4 && empty (relatedInstanceModelInfo.getModelCustomizationId()) && action != Action.deleteInstance) {
143                                         throw new ValidationException ("modelCustomizationName or modelCustomizationId in relatedInstance of vnf");
144                                 }
145                         }
146
147                         if(relatedInstanceModelInfo.getModelType().equals(ModelType.service)) {
148                                 isRelatedServiceInstancePresent = true;
149                                 if (!relatedInstance.getInstanceId ().equals (sir.getServiceInstanceId ())) {
150                                         throw new ValidationException ("serviceInstanceId matching the serviceInstanceId in request URI");
151                                 }
152                                 serviceModelName = relatedInstanceModelInfo.getModelName ();
153                                 asdcServiceModelVersion = relatedInstanceModelInfo.getModelVersion ();
154                         } else if(relatedInstanceModelInfo.getModelType().equals(ModelType.vnf) && !(ModelType.configuration.name().equalsIgnoreCase(requestScope))) {
155                                 isRelatedVnfInstancePresent = true;
156                                 if (!relatedInstance.getInstanceId ().equals (sir.getVnfInstanceId ())) {
157                                         throw new ValidationException ("vnfInstanceId matching the vnfInstanceId in request URI");
158                                 }
159                                 vnfModelName = relatedInstanceModelInfo.getModelCustomizationName();
160                         } else if(relatedInstanceModelInfo.getModelType().equals(ModelType.volumeGroup)) {                              
161                                 volumeGroupId = relatedInstance.getInstanceId ();
162                         }
163                 }
164
165                 if(action == Action.createInstance && ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
166                         if(!isSourceVnfPresent) {
167                                 throw new ValidationException ("source vnf relatedInstance for Port Configuration");
168                         } 
169                         
170                         if(!isDestinationVnfPresent) {
171                                 throw new ValidationException ("destination vnf relatedInstance for Port Configuration");
172                         }
173                 }
174
175                 if((action == Action.enablePort || action == Action.disablePort) && ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
176                         if(!isConnectionPointPresent) {
177                                 throw new ValidationException ("connectionPoint relatedInstance for Port Configuration");
178                         }
179                 }
180                 
181                 if(requestScope.equalsIgnoreCase (ModelType.volumeGroup.name ())) {
182                         if (!isRelatedServiceInstancePresent) {
183                                 throw new ValidationException ("related service instance for volumeGroup request");
184                         }
185                         if (!isRelatedVnfInstancePresent) {
186                                 throw new ValidationException ("related vnf instance for volumeGroup request");
187                         }
188                         serviceInstanceType = serviceModelName;
189                         vnfType = serviceModelName + "/" + vnfModelName;
190                         info.setServiceInstanceType(serviceInstanceType);
191                         info.setVnfType(vnfType);
192                 }
193                 else if(requestScope.equalsIgnoreCase(ModelType.vfModule.name ())) {
194                         if (!isRelatedServiceInstancePresent) {
195                                 throw new ValidationException ("related service instance for vfModule request");
196                         }
197                         if (!isRelatedVnfInstancePresent) {
198                                 throw new ValidationException ("related vnf instance for vfModule request");
199                         }
200                         vfModuleModelName = modelInfo.getModelName ();
201                         serviceInstanceType = serviceModelName;
202                         vnfType = serviceModelName + "/" + vnfModelName;
203                         vfModuleType = vnfType + "::" + vfModuleModelName;
204                         sir.setVolumeGroupInstanceId (volumeGroupId);
205                         info.setVfModuleModelName(vfModuleModelName);
206                         info.setVnfType(vnfType);
207                         info.setServiceInstanceType(serviceInstanceType);
208                         info.setVfModuleType(vfModuleType);
209                 }
210                 else if (requestScope.equalsIgnoreCase (ModelType.vnf.name ())) {
211                         if (!isRelatedServiceInstancePresent) {
212                                 throw new ValidationException ("related service instance for vnf request");
213                         }
214                         vnfType = serviceModelName + "/" + sir.getRequestDetails().getModelInfo().getModelCustomizationName();
215                         info.setVnfType(vnfType);
216                }
217         }
218         else if ((( requestScope.equalsIgnoreCase(ModelType.vnf.name ()) || requestScope.equalsIgnoreCase(ModelType.volumeGroup.name ()) || requestScope.equalsIgnoreCase(ModelType.vfModule.name ()) 
219                                 || requestScope.equalsIgnoreCase(ModelType.configuration.name())) && (action == Action.createInstance || action == Action.enablePort || action == Action.disablePort)) ||
220                         (reqVersion >= 4 && (requestScope.equalsIgnoreCase(ModelType.volumeGroup.name ()) || requestScope.equalsIgnoreCase(ModelType.vfModule.name ())) && action == Action.updateInstance ||
221                         (requestScope.equalsIgnoreCase(ModelType.vfModule.name ()) && action == Action.scaleOut)) ||
222                                 (requestScope.equalsIgnoreCase(ModelType.service.name()) && (action.equals(Action.addRelationships) || action.equals(Action.removeRelationships)))){
223                  msoLogger.debug ("related instance exception");
224                 throw new ValidationException ("related instances");
225         }
226         info.setVfModuleModelName(vfModuleModelName);
227         info.setServiceInstanceType(serviceInstanceType);
228         info.setVnfType(vnfType);
229         info.setAsdcServiceModelVersion(asdcServiceModelVersion);
230         info.setVfModuleType(vfModuleType);
231                 return info;
232         }
233 }