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