aboutsummaryrefslogtreecommitdiffstats
path: root/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/validation/ModelInfoValidation.java
blob: e2a1bdc46ab41875803ac6c9cb37e31e148f16fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.so.apihandlerinfra.validation;

import org.onap.so.apihandlerinfra.Action;
import org.onap.so.apihandlerinfra.Actions;
import org.onap.so.apihandlerinfra.TestApi;
import org.onap.so.exceptions.ValidationException;
import org.onap.so.serviceinstancebeans.ModelInfo;
import org.onap.so.serviceinstancebeans.ModelType;
import org.onap.so.serviceinstancebeans.RequestParameters;
import org.onap.so.utils.UUIDChecker;
public class ModelInfoValidation implements ValidationRule{
	private static boolean empty(String s) {
		return (s == null || s.trim().isEmpty());
	}
	@Override
	public ValidationInformation validate(ValidationInformation info) throws ValidationException{
		ModelInfo modelInfo = info.getSir().getRequestDetails().getModelInfo();
		RequestParameters requestParameters = info.getReqParameters();
		String requestScope = info.getRequestScope();
		Actions action = info.getAction();
		int reqVersion = info.getReqVersion();
		Boolean aLaCarteFlag = info.getALaCarteFlag();

		if(!requestScope.equals(ModelType.instanceGroup.toString())){

			if(!empty(modelInfo.getModelNameVersionId())){
				modelInfo.setModelVersionId(modelInfo.getModelNameVersionId());
			}
			// modelCustomizationId is required when usePreLoad is false for v4 and higher for VF Module Create
			if(requestParameters != null && reqVersion >= 4 && requestScope.equalsIgnoreCase(ModelType.vfModule.name()) && action == Action.createInstance && !requestParameters.isUsePreload()) {
				if(!UUIDChecker.isValidUUID(modelInfo.getModelCustomizationId())) {
					throw new ValidationException("modelCustomizationId");
				}
			}

			// modelCustomizationId is required for v5 and higher for VF Module Replace
			if(requestParameters != null && reqVersion > 4 && requestScope.equalsIgnoreCase(ModelType.vfModule.name()) && action == Action.replaceInstance) {
				if(!UUIDChecker.isValidUUID(modelInfo.getModelCustomizationId())) {
					throw new ValidationException("modelCustomizationId");
				}
			}

			// modelCustomizationId or modelCustomizationName are required for VNF Replace
			if(requestParameters != null && reqVersion > 4 && requestScope.equalsIgnoreCase(ModelType.vnf.name()) && action == Action.replaceInstance || action == Action.recreateInstance) {
				if(!UUIDChecker.isValidUUID(modelInfo.getModelCustomizationId()) && modelInfo.getModelCustomizationName() == null) {
					throw new ValidationException("modelCustomizationId or modelCustomizationName");
				}
			}

			//is required for serviceInstance delete macro when aLaCarte=false (v3)
			//create and updates except for network (except v4)
			if (empty (modelInfo.getModelInvariantId ()) && ((reqVersion >2 && (aLaCarteFlag != null && !aLaCarteFlag) && requestScope.equalsIgnoreCase(ModelType.service.name()) && action == Action.deleteInstance) ||
					!(reqVersion < 4 && requestScope.equalsIgnoreCase (ModelType.network.name ())) && 
					(action == Action.createInstance || action == Action.updateInstance || action == Action.enablePort || action == Action.disablePort || action == Action.addRelationships || action == Action.removeRelationships ||
					(requestScope.equalsIgnoreCase(ModelType.configuration.name()) && (action == Action.activateInstance || action == Action.deactivateInstance))))) {
				throw new ValidationException ("modelInvariantId");
			}
			if(empty(modelInfo.getModelInvariantId()) && (requestScope.equalsIgnoreCase(ModelType.vfModule.name()) && action == Action.scaleOut)){
				throw new ValidationException("modelInvariantId");
			}
			if(empty(modelInfo.getModelInvariantId()) && (requestScope.equalsIgnoreCase(ModelType.vnf.name()) && action == Action.recreateInstance)){
				throw new ValidationException("modelInvariantId", true);
			}
			if (!empty (modelInfo.getModelInvariantId ()) && !UUIDChecker.isValidUUID (modelInfo.getModelInvariantId ())) {
				throw new ValidationException ("modelInvariantId format");
			}

			if(reqVersion >= 4 && !(requestScope.equalsIgnoreCase(ModelType.configuration.name())) && empty (modelInfo.getModelName ()) && (action == Action.createInstance || action == Action.updateInstance || 
					action == Action.addRelationships || action == Action.removeRelationships || action == Action.recreateInstance || ((action == Action.deleteInstance || action == Action.scaleOut) && (requestScope.equalsIgnoreCase (ModelType.vfModule.name ()))))){
				throw new ValidationException ("modelName", true);
			}

			if (empty (modelInfo.getModelVersion ()) && !(requestScope.equalsIgnoreCase(ModelType.configuration.name())) && 
					(!(reqVersion < 4 && requestScope.equalsIgnoreCase (ModelType.network.name ())) 
							&& (action == Action.createInstance || action == Action.updateInstance || action == Action.addRelationships || action == Action.removeRelationships || action == Action.scaleOut))) {
				throw new ValidationException ("modelVersion");
			}

			if(empty(modelInfo.getModelVersion()) && (requestScope.equalsIgnoreCase(ModelType.vnf.name()) && action == Action.recreateInstance)){
				throw new ValidationException("modelVersion", true);
			}

			// is required for serviceInstance delete macro when aLaCarte=false in v4
			if (reqVersion >= 4 && empty (modelInfo.getModelVersionId()) && (((aLaCarteFlag != null && !aLaCarteFlag) && requestScope.equalsIgnoreCase(ModelType.service.name()) && action == Action.deleteInstance) ||
					(action == Action.createInstance || action == Action.updateInstance || action == Action.enablePort || action == Action.disablePort || action == Action.addRelationships || action == Action.removeRelationships ||
					(requestScope.equalsIgnoreCase(ModelType.configuration.name()) && (action == Action.activateInstance || action == Action.deactivateInstance))))) {
				throw new ValidationException ("modelVersionId");
			}
			if(empty(modelInfo.getModelVersionId()) && (requestScope.equalsIgnoreCase(ModelType.vnf.name()) && action == Action.recreateInstance)){
				throw new ValidationException("modelVersionId", true);
			}
			if(empty(modelInfo.getModelVersionId()) && (requestScope.equalsIgnoreCase(ModelType.vfModule.name()) && action == Action.scaleOut)){
				throw new ValidationException("modelVersionId");
			}

			if(requestScope.equalsIgnoreCase(ModelType.vnf.name()) && action != Action.deleteInstance && empty (modelInfo.getModelCustomizationName ())) {
				if (!UUIDChecker.isValidUUID (modelInfo.getModelCustomizationId())) {
					throw new ValidationException ("modelCustomizationId or modelCustomizationName");
				}
			}

			if(reqVersion >= 4 && (!UUIDChecker.isValidUUID (modelInfo.getModelCustomizationId())) && (requestScope.equalsIgnoreCase (ModelType.network.name ()) || requestScope.equalsIgnoreCase(ModelType.configuration.name()))
					&& (action == Action.updateInstance || action == Action.createInstance)){
				throw new ValidationException ("modelCustomizationId");
			}
			if(empty(modelInfo.getModelCustomizationId()) && requestScope.equalsIgnoreCase(ModelType.vfModule.name()) && action == Action.scaleOut && !(requestParameters.getTestApi() == TestApi.VNF_API.name() && requestParameters.isUsePreload() == true)){
				throw new ValidationException ("modelCustomizationId");
			}
		}else{
			if(empty(modelInfo.getModelVersionId()) && action == Action.createInstance){
				throw new ValidationException("modelVersionId", true);
			}
		}
		return info;
	}
}