2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Bell Canada. All rights reserved.
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
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.cps.notification;
23 import java.net.URISyntaxException;
24 import java.time.OffsetDateTime;
25 import java.time.format.DateTimeFormatter;
26 import java.util.UUID;
27 import org.onap.cps.api.CpsAdminService;
28 import org.onap.cps.api.CpsDataService;
29 import org.onap.cps.event.model.Content;
30 import org.onap.cps.event.model.CpsDataUpdatedEvent;
31 import org.onap.cps.event.model.CpsDataUpdatedEvent.Schema;
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.stereotype.Component;
40 class CpsDataUpdatedEventFactory {
42 private static final URI EVENT_SOURCE;
43 private static final String EVENT_TYPE = "org.onap.cps.data-updated-event";
44 private static final DateTimeFormatter dateTimeFormatter =
45 DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
49 EVENT_SOURCE = new URI("urn:cps:org.onap.cps");
50 } catch (final URISyntaxException e) {
51 // As it is fixed string, I don't expect to see this error
52 throw new IllegalArgumentException(e);
56 private CpsDataService cpsDataService;
57 private CpsAdminService cpsAdminService;
59 public CpsDataUpdatedEventFactory(final CpsDataService cpsDataService, final CpsAdminService cpsAdminService) {
60 this.cpsDataService = cpsDataService;
61 this.cpsAdminService = cpsAdminService;
64 CpsDataUpdatedEvent createCpsDataUpdatedEvent(final String dataspaceName, final String anchorName) {
65 final var dataNode = cpsDataService
66 .getDataNode(dataspaceName, anchorName, "/", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
67 final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
68 return toCpsDataUpdatedEvent(anchor, dataNode);
71 private CpsDataUpdatedEvent toCpsDataUpdatedEvent(final Anchor anchor, final DataNode dataNode) {
72 final var cpsDataUpdatedEvent = new CpsDataUpdatedEvent();
73 cpsDataUpdatedEvent.withContent(createContent(anchor, dataNode));
74 cpsDataUpdatedEvent.withId(UUID.randomUUID().toString());
75 cpsDataUpdatedEvent.withSchema(Schema.URN_CPS_ORG_ONAP_CPS_DATA_UPDATED_EVENT_SCHEMA_1_1_0_SNAPSHOT);
76 cpsDataUpdatedEvent.withSource(EVENT_SOURCE);
77 cpsDataUpdatedEvent.withType(EVENT_TYPE);
78 return cpsDataUpdatedEvent;
81 private Data createData(final DataNode dataNode) {
82 final var data = new Data();
83 DataMapUtils.toDataMap(dataNode).forEach((key, value) -> data.setAdditionalProperty(key, value));
87 private Content createContent(final Anchor anchor, final DataNode dataNode) {
88 final var content = new Content();
89 content.withAnchorName(anchor.getName());
90 content.withDataspaceName(anchor.getDataspaceName());
91 content.withSchemaSetName(anchor.getSchemaSetName());
92 content.withData(createData(dataNode));
93 content.withObservedTimestamp(dateTimeFormatter.format(OffsetDateTime.now()));