Replace cambria with DmaaP client
[dcaegen2/collectors/ves.git] / src / main / java / org / onap / dcae / common / validator / BatchEventValidator.java
1 /*
2  * ============LICENSE_START=======================================================
3  * VES Collector
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.dcae.common.validator;
21
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;
27
28 import java.util.List;
29
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;
33
34 public class BatchEventValidator {
35
36     private BatchEventValidator() {
37     }
38
39     /**
40      * Check if value of domain fields are the same in every event,
41      * in case of stndDefined check stndDefinedNamespace fields
42      *
43      * @param events list of checked ves events
44      * @throws EventValidatorException when domain fields value or stndDefinedNamespace fields value are note the same
45      */
46     public static void executeBatchEventValidation(List<VesEvent> events) throws EventValidatorException {
47         if (hasNotEveryEventSameDomain(events)) {
48             throw new EventValidatorException(DIFFERENT_DOMAIN_FIELDS_IN_BATCH_EVENT);
49         }
50         if (isDomainStndDefined(events) && hasNotSameStndDefinedNamespace(events)) {
51             throw new EventValidatorException(DIFFERENT_STND_DEFINED_NAMESPACE_WHEN_DOMAIN_STND_DEFINED);
52         }
53     }
54
55     private static boolean hasNotEveryEventSameDomain(List<VesEvent> events) {
56         return events.stream()
57                 .map(VesEvent::getDomain)
58                 .collect(toSet())
59                 .size() != 1;
60     }
61
62     private static boolean hasNotSameStndDefinedNamespace(List<VesEvent> events)  {
63         return Try.of(() -> isAllStndDefinedNamespace(events))
64                 .getOrElseThrow(() -> new EventValidatorException(ApiException.MISSING_NAMESPACE_PARAMETER));
65     }
66
67     private static boolean isAllStndDefinedNamespace(List<VesEvent> events) {
68         return events.stream()
69                 .map(e -> e.getStndDefinedNamespace().orElse(""))
70                 .collect(toSet())
71                 .size() != 1;
72     }
73
74     private static boolean isDomainStndDefined(List<VesEvent> events) throws JSONException{
75         return events.stream()
76                 .allMatch((e -> e.getDomain().equals("stndDefined")));
77     }
78 }