2 * ============LICENSE_START=======================================================
3 * Copyright (c) 2021 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.
16 * ============LICENSE_END=========================================================
19 package org.onap.cps.notification;
22 import java.net.URISyntaxException;
23 import java.time.OffsetDateTime;
24 import java.time.format.DateTimeFormatter;
25 import java.util.UUID;
26 import org.onap.cps.api.CpsAdminService;
27 import org.onap.cps.api.CpsDataService;
28 import org.onap.cps.event.model.Content;
29 import org.onap.cps.event.model.CpsDataUpdatedEvent;
30 import org.onap.cps.event.model.CpsDataUpdatedEvent.Schema;
31 import org.onap.cps.event.model.Data;
32 import org.onap.cps.spi.FetchDescendantsOption;
33 import org.onap.cps.spi.model.Anchor;
34 import org.onap.cps.spi.model.DataNode;
35 import org.onap.cps.utils.DataMapUtils;
36 import org.springframework.stereotype.Component;
39 class CpsDataUpdatedEventFactory {
41 private static final URI EVENT_SOURCE;
42 private static final String EVENT_TYPE = "org.onap.cps.data-updated-event";
43 private static final DateTimeFormatter dateTimeFormatter =
44 DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
48 EVENT_SOURCE = new URI("urn:cps:org.onap.cps");
49 } catch (final URISyntaxException e) {
50 // As it is fixed string, I don't expect to see this error
51 throw new IllegalArgumentException(e);
55 private CpsDataService cpsDataService;
56 private CpsAdminService cpsAdminService;
58 public CpsDataUpdatedEventFactory(final CpsDataService cpsDataService, final CpsAdminService cpsAdminService) {
59 this.cpsDataService = cpsDataService;
60 this.cpsAdminService = cpsAdminService;
63 CpsDataUpdatedEvent createCpsDataUpdatedEvent(final String dataspaceName, final String anchorName) {
64 final var dataNode = cpsDataService
65 .getDataNode(dataspaceName, anchorName, "/", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
66 final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
67 return toCpsDataUpdatedEvent(anchor, dataNode);
70 private CpsDataUpdatedEvent toCpsDataUpdatedEvent(final Anchor anchor, final DataNode dataNode) {
71 final var cpsDataUpdatedEvent = new CpsDataUpdatedEvent();
72 cpsDataUpdatedEvent.withContent(createContent(anchor, dataNode));
73 cpsDataUpdatedEvent.withId(UUID.randomUUID().toString());
74 cpsDataUpdatedEvent.withSchema(Schema.URN_CPS_ORG_ONAP_CPS_DATA_UPDATED_EVENT_SCHEMA_1_1_0_SNAPSHOT);
75 cpsDataUpdatedEvent.withSource(EVENT_SOURCE);
76 cpsDataUpdatedEvent.withType(EVENT_TYPE);
77 return cpsDataUpdatedEvent;
80 private Data createData(final DataNode dataNode) {
81 final var data = new Data();
82 DataMapUtils.toDataMap(dataNode).forEach(data::setAdditionalProperty);
86 private Content createContent(final Anchor anchor, final DataNode dataNode) {
87 final var content = new Content();
88 content.withAnchorName(anchor.getName());
89 content.withDataspaceName(anchor.getDataspaceName());
90 content.withSchemaSetName(anchor.getSchemaSetName());
91 content.withData(createData(dataNode));
92 content.withObservedTimestamp(dateTimeFormatter.format(OffsetDateTime.now()));