2 * ============LICENSE_START=======================================================
3 * Copyright (c) 2021-2022 Bell Canada.
4 * Modifications Copyright (c) 2022-2023 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
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.notification;
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 lombok.SneakyThrows;
32 import org.onap.cps.api.CpsDataService;
33 import org.onap.cps.event.model.Content;
34 import org.onap.cps.event.model.CpsDataUpdatedEvent;
35 import org.onap.cps.event.model.Data;
36 import org.onap.cps.spi.FetchDescendantsOption;
37 import org.onap.cps.spi.model.Anchor;
38 import org.onap.cps.spi.model.DataNode;
39 import org.onap.cps.utils.DataMapUtils;
40 import org.onap.cps.utils.PrefixResolver;
41 import org.springframework.context.annotation.Lazy;
42 import org.springframework.stereotype.Component;
45 @AllArgsConstructor(onConstructor = @__(@Lazy))
46 public class CpsDataUpdatedEventFactory {
48 private static final DateTimeFormatter DATE_TIME_FORMATTER =
49 DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
52 private final CpsDataService cpsDataService;
55 private final PrefixResolver prefixResolver;
58 * Generates CPS Data Updated event. If observedTimestamp is not provided, then current timestamp is used.
60 * @param anchor anchor
61 * @param observedTimestamp observedTimestamp
62 * @param operation operation
63 * @return CpsDataUpdatedEvent
65 public CpsDataUpdatedEvent createCpsDataUpdatedEvent(final Anchor anchor,
66 final OffsetDateTime observedTimestamp, final Operation operation) {
67 final var dataNode = (operation == Operation.DELETE) ? null :
68 cpsDataService.getDataNodes(anchor.getDataspaceName(), anchor.getName(),
69 "/", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS).iterator().next();
70 return toCpsDataUpdatedEvent(anchor, dataNode, observedTimestamp, operation);
73 @SneakyThrows(URISyntaxException.class)
74 private CpsDataUpdatedEvent toCpsDataUpdatedEvent(final Anchor anchor,
75 final DataNode dataNode,
76 final OffsetDateTime observedTimestamp,
77 final Operation operation) {
78 final CpsDataUpdatedEvent cpsDataUpdatedEvent = new CpsDataUpdatedEvent();
79 cpsDataUpdatedEvent.withContent(createContent(anchor, dataNode, observedTimestamp, operation));
80 cpsDataUpdatedEvent.withId(UUID.randomUUID().toString());
81 cpsDataUpdatedEvent.withSchema(new URI("urn:cps:org.onap.cps:data-updated-event-schema:v1"));
82 cpsDataUpdatedEvent.withSource(new URI("urn:cps:org.onap.cps"));
83 cpsDataUpdatedEvent.withType("org.onap.cps.data-updated-event");
84 return cpsDataUpdatedEvent;
87 private Data createData(final DataNode dataNode, final String prefix) {
88 final Data data = new Data();
89 DataMapUtils.toDataMapWithIdentifier(dataNode, prefix).forEach(data::setAdditionalProperty);
93 private Content createContent(final Anchor anchor, final DataNode dataNode,
94 final OffsetDateTime observedTimestamp, final Operation operation) {
95 final var content = new Content();
96 content.withAnchorName(anchor.getName());
97 content.withDataspaceName(anchor.getDataspaceName());
98 content.withSchemaSetName(anchor.getSchemaSetName());
99 content.withOperation(Content.Operation.fromValue(operation.name()));
100 content.withObservedTimestamp(
101 DATE_TIME_FORMATTER.format(observedTimestamp == null ? OffsetDateTime.now() : observedTimestamp));
102 if (dataNode != null) {
103 final String prefix = prefixResolver.getPrefix(anchor, dataNode.getXpath());
104 content.withData(createData(dataNode, prefix));