2 * ============LICENSE_START=======================================================
3 * Copyright (c) 2021-2022 Bell Canada.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.notification;
24 import java.net.URISyntaxException;
25 import java.time.OffsetDateTime;
26 import java.time.format.DateTimeFormatter;
27 import java.util.UUID;
28 import lombok.AllArgsConstructor;
29 import org.onap.cps.api.CpsDataService;
30 import org.onap.cps.event.model.Content;
31 import org.onap.cps.event.model.CpsDataUpdatedEvent;
32 import org.onap.cps.event.model.Data;
33 import org.onap.cps.spi.FetchDescendantsOption;
34 import org.onap.cps.spi.model.Anchor;
35 import org.onap.cps.spi.model.DataNode;
36 import org.onap.cps.utils.DataMapUtils;
37 import org.springframework.context.annotation.Lazy;
38 import org.springframework.stereotype.Component;
41 @AllArgsConstructor(onConstructor = @__(@Lazy))
42 public class CpsDataUpdatedEventFactory {
44 private static final URI EVENT_SCHEMA;
45 private static final URI EVENT_SOURCE;
46 private static final String EVENT_TYPE = "org.onap.cps.data-updated-event";
47 private static final DateTimeFormatter DATE_TIME_FORMATTER =
48 DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
52 EVENT_SCHEMA = new URI("urn:cps:org.onap.cps:data-updated-event-schema:v1");
53 EVENT_SOURCE = new URI("urn:cps:org.onap.cps");
54 } catch (final URISyntaxException e) {
55 // As it is fixed string, I don't expect to see this error
56 throw new IllegalArgumentException(e);
61 private final CpsDataService cpsDataService;
64 * Generates CPS Data Updated event. If observedTimestamp is not provided, then current timestamp is used.
66 * @param anchor anchor
67 * @param observedTimestamp observedTimestamp
68 * @param operation operation
69 * @return CpsDataUpdatedEvent
71 public CpsDataUpdatedEvent createCpsDataUpdatedEvent(final Anchor anchor,
72 final OffsetDateTime observedTimestamp, final Operation operation) {
73 final var dataNode = (operation == Operation.DELETE) ? null :
74 cpsDataService.getDataNode(anchor.getDataspaceName(), anchor.getName(),
75 "/", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
76 return toCpsDataUpdatedEvent(anchor, dataNode, observedTimestamp, operation);
79 private CpsDataUpdatedEvent toCpsDataUpdatedEvent(final Anchor anchor, final DataNode dataNode,
80 final OffsetDateTime observedTimestamp, final Operation operation) {
81 final var cpsDataUpdatedEvent = new CpsDataUpdatedEvent();
82 cpsDataUpdatedEvent.withContent(createContent(anchor, dataNode, observedTimestamp, operation));
83 cpsDataUpdatedEvent.withId(UUID.randomUUID().toString());
84 cpsDataUpdatedEvent.withSchema(EVENT_SCHEMA);
85 cpsDataUpdatedEvent.withSource(EVENT_SOURCE);
86 cpsDataUpdatedEvent.withType(EVENT_TYPE);
87 return cpsDataUpdatedEvent;
90 private Data createData(final DataNode dataNode) {
91 final var data = new Data();
92 DataMapUtils.toDataMap(dataNode).forEach(data::setAdditionalProperty);
96 private Content createContent(final Anchor anchor, final DataNode dataNode,
97 final OffsetDateTime observedTimestamp, final Operation operation) {
98 final var content = new Content();
99 content.withAnchorName(anchor.getName());
100 content.withDataspaceName(anchor.getDataspaceName());
101 content.withSchemaSetName(anchor.getSchemaSetName());
102 content.withOperation(Content.Operation.fromValue(operation.name()));
103 content.withObservedTimestamp(
104 DATE_TIME_FORMATTER.format(observedTimestamp == null ? OffsetDateTime.now() : observedTimestamp));
105 if (dataNode != null) {
106 content.withData(createData(dataNode));