2985ed53bbb235a2b709b6416786ea8a616b0b73
[cps.git] / cps-service / src / main / java / org / onap / cps / notification / CpsDataUpdatedEventFactory.java
1 /*
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
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0
10  *
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  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.notification;
22
23 import java.net.URI;
24 import java.net.URISyntaxException;
25 import java.time.OffsetDateTime;
26 import java.time.format.DateTimeFormatter;
27 import java.util.UUID;
28 import org.onap.cps.api.CpsAdminService;
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;
39
40 @Component
41 public class CpsDataUpdatedEventFactory {
42
43     private static final URI EVENT_SCHEMA;
44     private static final URI EVENT_SOURCE;
45     private static final String EVENT_TYPE = "org.onap.cps.data-updated-event";
46     private static final DateTimeFormatter DATE_TIME_FORMATTER =
47         DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
48
49     static {
50         try {
51             EVENT_SCHEMA = new URI("urn:cps:org.onap.cps:data-updated-event-schema:v1");
52             EVENT_SOURCE = new URI("urn:cps:org.onap.cps");
53         } catch (final URISyntaxException e) {
54             // As it is fixed string, I don't expect to see this error
55             throw new IllegalArgumentException(e);
56         }
57     }
58
59     private final CpsDataService cpsDataService;
60     private final CpsAdminService cpsAdminService;
61
62     public CpsDataUpdatedEventFactory(@Lazy final CpsDataService cpsDataService,
63         final CpsAdminService cpsAdminService) {
64         this.cpsDataService = cpsDataService;
65         this.cpsAdminService = cpsAdminService;
66     }
67
68     /**
69      * Generates CPS Data Updated event. If observedTimestamp is not provided, then current timestamp is used.
70      *
71      * @param dataspaceName     dataspaceName
72      * @param anchorName        anchorName
73      * @param observedTimestamp observedTimestamp
74      * @return CpsDataUpdatedEvent
75      */
76     public CpsDataUpdatedEvent createCpsDataUpdatedEvent(final String dataspaceName, final String anchorName,
77         final OffsetDateTime observedTimestamp) {
78         final var dataNode = cpsDataService
79             .getDataNode(dataspaceName, anchorName, "/", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
80         final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
81         return toCpsDataUpdatedEvent(anchor, dataNode, observedTimestamp);
82     }
83
84     private CpsDataUpdatedEvent toCpsDataUpdatedEvent(final Anchor anchor, final DataNode dataNode,
85         final OffsetDateTime observedTimestamp) {
86         final var cpsDataUpdatedEvent = new CpsDataUpdatedEvent();
87         cpsDataUpdatedEvent.withContent(createContent(anchor, dataNode, observedTimestamp));
88         cpsDataUpdatedEvent.withId(UUID.randomUUID().toString());
89         cpsDataUpdatedEvent.withSchema(EVENT_SCHEMA);
90         cpsDataUpdatedEvent.withSource(EVENT_SOURCE);
91         cpsDataUpdatedEvent.withType(EVENT_TYPE);
92         return cpsDataUpdatedEvent;
93     }
94
95     private Data createData(final DataNode dataNode) {
96         final var data = new Data();
97         DataMapUtils.toDataMap(dataNode).forEach(data::setAdditionalProperty);
98         return data;
99     }
100
101     private Content createContent(final Anchor anchor, final DataNode dataNode,
102         final OffsetDateTime observedTimestamp) {
103         final var content = new Content();
104         content.withAnchorName(anchor.getName());
105         content.withDataspaceName(anchor.getDataspaceName());
106         content.withSchemaSetName(anchor.getSchemaSetName());
107         content.withData(createData(dataNode));
108         content.withObservedTimestamp(
109             DATE_TIME_FORMATTER.format(observedTimestamp == null ? OffsetDateTime.now() : observedTimestamp));
110         return content;
111     }
112 }