2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 Nokia. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.dcae.common.validator;
22 import io.vavr.control.Try;
23 import org.json.JSONException;
24 import org.onap.dcae.common.model.VesEvent;
25 import org.onap.dcae.restapi.ApiException;
26 import org.onap.dcae.restapi.EventValidatorException;
28 import java.util.List;
30 import static java.util.stream.Collectors.toSet;
31 import static org.onap.dcae.restapi.ApiException.DIFFERENT_DOMAIN_FIELDS_IN_BATCH_EVENT;
32 import static org.onap.dcae.restapi.ApiException.DIFFERENT_STND_DEFINED_NAMESPACE_WHEN_DOMAIN_STND_DEFINED;
34 public class BatchEventValidator {
36 private BatchEventValidator() {
40 * Check if value of domain fields are the same in every event,
41 * in case of stndDefined check stndDefinedNamespace fields
43 * @param events list of checked ves events
44 * @throws EventValidatorException when domain fields value or stndDefinedNamespace fields value are note the same
46 public static void executeBatchEventValidation(List<VesEvent> events) throws EventValidatorException {
47 if (hasNotEveryEventSameDomain(events)) {
48 throw new EventValidatorException(DIFFERENT_DOMAIN_FIELDS_IN_BATCH_EVENT);
50 if (isDomainStndDefined(events) && hasNotSameStndDefinedNamespace(events)) {
51 throw new EventValidatorException(DIFFERENT_STND_DEFINED_NAMESPACE_WHEN_DOMAIN_STND_DEFINED);
55 private static boolean hasNotEveryEventSameDomain(List<VesEvent> events) {
56 return events.stream()
57 .map(VesEvent::getDomain)
62 private static boolean hasNotSameStndDefinedNamespace(List<VesEvent> events) {
63 return Try.of(() -> isAllStndDefinedNamespace(events))
64 .getOrElseThrow(() -> new EventValidatorException(ApiException.MISSING_NAMESPACE_PARAMETER));
67 private static boolean isAllStndDefinedNamespace(List<VesEvent> events) {
68 return events.stream()
69 .map(e -> e.getStndDefinedNamespace().orElse(""))
74 private static boolean isDomainStndDefined(List<VesEvent> events) throws JSONException{
75 return events.stream()
76 .allMatch((e -> e.getDomain().equals("stndDefined")));