summaryrefslogtreecommitdiffstats
path: root/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/validation/RelatedInstancesValidation.java
blob: 3b87d91d3f33ccd3941e6a3b4ba99432c16d1671 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
 * ================================================================================
 * Modifications Copyright (c) 2019 Samsung
 * ================================================================================
 * 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.Constants;
import org.onap.so.exceptions.ValidationException;
import org.onap.so.serviceinstancebeans.InstanceDirection;
import org.onap.so.serviceinstancebeans.ModelInfo;
import org.onap.so.serviceinstancebeans.ModelType;
import org.onap.so.serviceinstancebeans.RelatedInstance;
import org.onap.so.serviceinstancebeans.RelatedInstanceList;
import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
import org.onap.so.utils.UUIDChecker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RelatedInstancesValidation implements ValidationRule {

    private static Logger logger = LoggerFactory.getLogger(RelatedInstancesValidation.class);

    private static boolean empty(String s) {
        return (s == null || s.trim().isEmpty());
    }

    @Override
    public ValidationInformation validate(ValidationInformation info) throws ValidationException {
        ServiceInstancesRequest sir = info.getSir();
        Actions action = info.getAction();
        int reqVersion = info.getReqVersion();
        String requestScope = info.getRequestScope();
        String serviceInstanceType = null;
        String networkType = null;
        String vnfType = null;
        String vfModuleType = null;
        String vfModuleModelName = null;
        ModelInfo modelInfo = info.getSir().getRequestDetails().getModelInfo();
        RelatedInstanceList[] instanceList = sir.getRequestDetails().getRelatedInstanceList();
        String serviceModelName = null;
        String vnfModelName = null;
        String asdcServiceModelVersion = null;
        String volumeGroupId = null;
        boolean isRelatedServiceInstancePresent = false;
        boolean isRelatedVnfInstancePresent = false;
        boolean isSourceVnfPresent = false;
        boolean isDestinationVnfPresent = false;
        boolean isConnectionPointPresent = false;

        if (requestScope.equalsIgnoreCase(ModelType.service.name())) {
            serviceInstanceType = modelInfo.getModelName();
            info.setServiceInstanceType(serviceInstanceType);
        }
        if (requestScope.equalsIgnoreCase(ModelType.network.name())) {
            networkType = modelInfo.getModelName();
            info.setNetworkType(networkType);
        }
        if (instanceList != null) {
            for (RelatedInstanceList relatedInstanceList : instanceList) {
                RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();

                ModelInfo relatedInstanceModelInfo = relatedInstance.getModelInfo();
                if (relatedInstanceModelInfo == null) {
                    throw new ValidationException("modelInfo in relatedInstance");
                }

                if (relatedInstanceModelInfo.getModelType() == null) {
                    throw new ValidationException("modelType in relatedInstance");
                }

                if (empty(relatedInstance.getInstanceName())
                        && ModelType.pnf.equals(relatedInstanceModelInfo.getModelType())) {
                    throw new ValidationException("instanceName in relatedInstance for pnf modelType");
                }

                if (!empty(relatedInstance.getInstanceName())) {
                    if (!relatedInstance.getInstanceName().matches(Constants.VALID_INSTANCE_NAME_FORMAT)) {
                        throw new ValidationException("instanceName format in relatedInstance");
                    }
                }

                if (empty(relatedInstance.getInstanceId())
                        && !ModelType.pnf.equals(relatedInstanceModelInfo.getModelType())) {
                    throw new ValidationException("instanceId in relatedInstance");
                }

                if (!empty(relatedInstance.getInstanceId())
                        && !UUIDChecker.isValidUUID(relatedInstance.getInstanceId())) {
                    throw new ValidationException("instanceId format in relatedInstance");
                }
                if (empty(relatedInstanceModelInfo.getModelVersionId())
                        && requestScope.equals(ModelType.instanceGroup.toString())
                        && relatedInstanceModelInfo.getModelType().equals(ModelType.service)) {
                    throw new ValidationException("modelVersionId in relatedInstance", true);
                }
                if (requestScope.equalsIgnoreCase(ModelType.instanceGroup.toString())
                        && relatedInstanceModelInfo.getModelType().equals(ModelType.service)) {
                    isRelatedServiceInstancePresent = true;
                }

                if (requestScope.equalsIgnoreCase(ModelType.service.name())) {
                    if (empty(relatedInstance.getInstanceName())
                            && ModelType.vpnBinding.equals(relatedInstanceModelInfo.getModelType())) {
                        throw new ValidationException("instanceName in relatedInstance for vpnBinding modelType", true);
                    }
                    if (empty(relatedInstance.getInstanceName())
                            && ModelType.network.equals(relatedInstanceModelInfo.getModelType())) {
                        throw new ValidationException("instanceName in relatedInstance for network modelType", true);
                    }
                }

                if (action != Action.deleteInstance
                        && !requestScope.equalsIgnoreCase(ModelType.instanceGroup.toString())) {
                    if (!(relatedInstanceModelInfo.getModelType().equals(ModelType.volumeGroup)
                            || relatedInstanceModelInfo.getModelType().equals(ModelType.connectionPoint)
                            || relatedInstanceModelInfo.getModelType().equals(ModelType.pnf)
                            || relatedInstanceModelInfo.getModelType().equals(ModelType.networkInstanceGroup)
                            || relatedInstanceModelInfo.getModelType().equals(ModelType.network)
                            || relatedInstanceModelInfo.getModelType().equals(ModelType.vpnBinding))) {

                        if (empty(relatedInstanceModelInfo.getModelInvariantId())) {
                            throw new ValidationException("modelInvariantId in relatedInstance");
                        } else if (reqVersion >= 4 && empty(relatedInstanceModelInfo.getModelVersionId())) {
                            throw new ValidationException("modelVersionId in relatedInstance");
                        } else if (empty(relatedInstanceModelInfo.getModelName())) {
                            throw new ValidationException("modelName in relatedInstance");
                        } else if (empty(relatedInstanceModelInfo.getModelVersion())) {
                            throw new ValidationException("modelVersion in relatedInstance");
                        }
                    }

                    if (!empty(relatedInstanceModelInfo.getModelInvariantId())
                            && !UUIDChecker.isValidUUID(relatedInstanceModelInfo.getModelInvariantId())) {
                        throw new ValidationException("modelInvariantId format in relatedInstance");
                    }

                    if (ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
                        if (InstanceDirection.source.equals(relatedInstance.getInstanceDirection())
                                && relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)) {
                            isSourceVnfPresent = true;
                        } else if (InstanceDirection.destination.equals(relatedInstance.getInstanceDirection())
                                && (relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)
                                        || (relatedInstanceModelInfo.getModelType().equals(ModelType.pnf)))) {
                            isDestinationVnfPresent = true;
                        }
                    }

                    if (ModelType.connectionPoint.equals(relatedInstanceModelInfo.getModelType())
                            && ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
                        isConnectionPointPresent = true;
                    }
                }

                if (empty(relatedInstanceModelInfo.getModelCustomizationName())
                        && relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)) {
                    if (reqVersion >= 4 && empty(relatedInstanceModelInfo.getModelCustomizationId())
                            && action != Action.deleteInstance) {
                        throw new ValidationException(
                                "modelCustomizationName or modelCustomizationId in relatedInstance of vnf");
                    }
                }

                if (relatedInstanceModelInfo.getModelType().equals(ModelType.service)
                        && !(requestScope.equalsIgnoreCase(ModelType.instanceGroup.toString())
                                && action == Action.createInstance)) {
                    isRelatedServiceInstancePresent = true;
                    if (!relatedInstance.getInstanceId().equals(sir.getServiceInstanceId())) {
                        throw new ValidationException(
                                "serviceInstanceId matching the serviceInstanceId in request URI");
                    }
                    serviceModelName = relatedInstanceModelInfo.getModelName();
                    asdcServiceModelVersion = relatedInstanceModelInfo.getModelVersion();
                } else if (relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)
                        && !(ModelType.configuration.name().equalsIgnoreCase(requestScope))) {
                    isRelatedVnfInstancePresent = true;
                    if (!relatedInstance.getInstanceId().equals(sir.getVnfInstanceId())) {
                        throw new ValidationException("vnfInstanceId matching the vnfInstanceId in request URI");
                    }
                    vnfModelName = relatedInstanceModelInfo.getModelCustomizationName();
                } else if (relatedInstanceModelInfo.getModelType().equals(ModelType.volumeGroup)) {
                    volumeGroupId = relatedInstance.getInstanceId();
                }
            }

            if (action == Action.createInstance && ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
                if (!isSourceVnfPresent) {
                    throw new ValidationException("source vnf relatedInstance for Port Configuration");
                }

                if (!isDestinationVnfPresent) {
                    throw new ValidationException("destination vnf relatedInstance for Port Configuration");
                }
            }

            if ((action == Action.enablePort || action == Action.disablePort)
                    && ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
                if (!isConnectionPointPresent) {
                    throw new ValidationException("connectionPoint relatedInstance for Port Configuration");
                }
            }
            if (requestScope.equals(ModelType.instanceGroup.toString())) {
                if (!isRelatedServiceInstancePresent) {
                    throw new ValidationException("related service instance for instanceGroup request", true);
                }
            }
            if (requestScope.equalsIgnoreCase(ModelType.volumeGroup.name())) {
                if (!isRelatedServiceInstancePresent) {
                    throw new ValidationException("related service instance for volumeGroup request");
                }
                if (!isRelatedVnfInstancePresent) {
                    throw new ValidationException("related vnf instance for volumeGroup request");
                }
                serviceInstanceType = serviceModelName;
                vnfType = serviceModelName + "/" + vnfModelName;
                info.setServiceInstanceType(serviceInstanceType);
                info.setVnfType(vnfType);
            } else if (requestScope.equalsIgnoreCase(ModelType.vfModule.name())) {
                if (!isRelatedServiceInstancePresent) {
                    throw new ValidationException("related service instance for vfModule request");
                }
                if (!isRelatedVnfInstancePresent) {
                    throw new ValidationException("related vnf instance for vfModule request");
                }
                vfModuleModelName = modelInfo.getModelName();
                serviceInstanceType = serviceModelName;
                vnfType = serviceModelName + "/" + vnfModelName;
                vfModuleType = vnfType + "::" + vfModuleModelName;
                sir.setVolumeGroupInstanceId(volumeGroupId);
                info.setVfModuleModelName(vfModuleModelName);
                info.setVnfType(vnfType);
                info.setServiceInstanceType(serviceInstanceType);
                info.setVfModuleType(vfModuleType);
            } else if (requestScope.equalsIgnoreCase(ModelType.vnf.name())) {
                if (!isRelatedServiceInstancePresent) {
                    throw new ValidationException("related service instance for vnf request");
                }
                vnfType = serviceModelName + "/" + sir.getRequestDetails().getModelInfo().getModelCustomizationName();
                info.setVnfType(vnfType);
            }
        } else if (((requestScope.equalsIgnoreCase(ModelType.vnf.name())
                || requestScope.equalsIgnoreCase(ModelType.volumeGroup.name())
                || requestScope.equalsIgnoreCase(ModelType.vfModule.name())
                || requestScope.equalsIgnoreCase(ModelType.configuration.name()))
                && (action == Action.createInstance || action == Action.enablePort || action == Action.disablePort))
                || (reqVersion >= 4
                        && (requestScope.equalsIgnoreCase(ModelType.volumeGroup.name())
                                || requestScope.equalsIgnoreCase(ModelType.vfModule.name()))
                        && action == Action.updateInstance
                        || (requestScope.equalsIgnoreCase(ModelType.vfModule.name()) && action == Action.scaleOut))
                || (requestScope.equalsIgnoreCase(ModelType.service.name())
                        && (action.equals(Action.addRelationships) || action.equals(Action.removeRelationships)))) {
            logger.debug("related instance exception");
            throw new ValidationException("related instances");
        }
        if (instanceList == null && requestScope.equalsIgnoreCase(ModelType.instanceGroup.toString())
                && action == Action.createInstance) {
            throw new ValidationException("relatedInstanceList", true);
        }
        info.setVfModuleModelName(vfModuleModelName);
        info.setServiceInstanceType(serviceInstanceType);
        info.setVnfType(vnfType);
        info.setAsdcServiceModelVersion(asdcServiceModelVersion);
        info.setVfModuleType(vfModuleType);
        return info;
    }
}