Use constants for magic numbers in perf tests
[cps.git] / cps-service / src / main / java / org / onap / cps / notification / CpsDataUpdatedEventFactory.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (c) 2021-2022 Bell Canada.
4  * Modifications Copyright (c) 2022 Nordix Foundation
5  * Modifications Copyright (C) 2023 TechMahindra Ltd.
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  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.notification;
24
25 import java.net.URI;
26 import java.net.URISyntaxException;
27 import java.time.OffsetDateTime;
28 import java.time.format.DateTimeFormatter;
29 import java.util.UUID;
30 import lombok.AllArgsConstructor;
31 import org.onap.cps.api.CpsDataService;
32 import org.onap.cps.event.model.Content;
33 import org.onap.cps.event.model.CpsDataUpdatedEvent;
34 import org.onap.cps.event.model.Data;
35 import org.onap.cps.spi.FetchDescendantsOption;
36 import org.onap.cps.spi.model.Anchor;
37 import org.onap.cps.spi.model.DataNode;
38 import org.onap.cps.utils.DataMapUtils;
39 import org.onap.cps.utils.PrefixResolver;
40 import org.springframework.context.annotation.Lazy;
41 import org.springframework.stereotype.Component;
42
43 @Component
44 @AllArgsConstructor(onConstructor = @__(@Lazy))
45 public class CpsDataUpdatedEventFactory {
46
47     private static final URI EVENT_SCHEMA;
48     private static final URI EVENT_SOURCE;
49     private static final String EVENT_TYPE = "org.onap.cps.data-updated-event";
50     private static final DateTimeFormatter DATE_TIME_FORMATTER =
51         DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
52
53     static {
54         try {
55             EVENT_SCHEMA = new URI("urn:cps:org.onap.cps:data-updated-event-schema:v1");
56             EVENT_SOURCE = new URI("urn:cps:org.onap.cps");
57         } catch (final URISyntaxException e) {
58             // As it is fixed string, I don't expect to see this error
59             throw new IllegalArgumentException(e);
60         }
61     }
62
63     @Lazy
64     private final CpsDataService cpsDataService;
65
66     @Lazy
67     private final PrefixResolver prefixResolver;
68
69     /**
70      * Generates CPS Data Updated event. If observedTimestamp is not provided, then current timestamp is used.
71      *
72      * @param anchor            anchor
73      * @param observedTimestamp observedTimestamp
74      * @param operation         operation
75      * @return CpsDataUpdatedEvent
76      */
77     public CpsDataUpdatedEvent createCpsDataUpdatedEvent(final Anchor anchor,
78         final OffsetDateTime observedTimestamp, final Operation operation) {
79         final var dataNode = (operation == Operation.DELETE) ? null :
80             cpsDataService.getDataNodes(anchor.getDataspaceName(), anchor.getName(),
81                 "/", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS).iterator().next();
82         return toCpsDataUpdatedEvent(anchor, dataNode, observedTimestamp, operation);
83     }
84
85     private CpsDataUpdatedEvent toCpsDataUpdatedEvent(final Anchor anchor, final DataNode dataNode,
86         final OffsetDateTime observedTimestamp, final Operation operation) {
87         final var cpsDataUpdatedEvent = new CpsDataUpdatedEvent();
88         cpsDataUpdatedEvent.withContent(createContent(anchor, dataNode, observedTimestamp, operation));
89         cpsDataUpdatedEvent.withId(UUID.randomUUID().toString());
90         cpsDataUpdatedEvent.withSchema(EVENT_SCHEMA);
91         cpsDataUpdatedEvent.withSource(EVENT_SOURCE);
92         cpsDataUpdatedEvent.withType(EVENT_TYPE);
93         return cpsDataUpdatedEvent;
94     }
95
96     private Data createData(final DataNode dataNode, final String prefix) {
97         final Data data = new Data();
98         DataMapUtils.toDataMapWithIdentifier(dataNode, prefix).forEach(data::setAdditionalProperty);
99         return data;
100     }
101
102     private Content createContent(final Anchor anchor, final DataNode dataNode,
103         final OffsetDateTime observedTimestamp, final Operation operation) {
104         final var content = new Content();
105         content.withAnchorName(anchor.getName());
106         content.withDataspaceName(anchor.getDataspaceName());
107         content.withSchemaSetName(anchor.getSchemaSetName());
108         content.withOperation(Content.Operation.fromValue(operation.name()));
109         content.withObservedTimestamp(
110             DATE_TIME_FORMATTER.format(observedTimestamp == null ? OffsetDateTime.now() : observedTimestamp));
111         if (dataNode != null) {
112             final String prefix = prefixResolver.getPrefix(anchor, dataNode.getXpath());
113             content.withData(createData(dataNode, prefix));
114         }
115         return content;
116     }
117 }