Add optional observed timestamp in the cps data api
[cps.git] / cps-service / src / main / java / org / onap / cps / api / impl / CpsDataServiceImpl.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  Modifications Copyright (C) 2020-2021 Bell Canada.
5  *  Modifications Copyright (C) 2021 Pantheon.tech
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
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.api.impl;
24
25 import java.time.OffsetDateTime;
26 import java.util.Collection;
27 import lombok.extern.slf4j.Slf4j;
28 import org.onap.cps.api.CpsAdminService;
29 import org.onap.cps.api.CpsDataService;
30 import org.onap.cps.api.CpsModuleService;
31 import org.onap.cps.notification.NotificationService;
32 import org.onap.cps.spi.CpsDataPersistenceService;
33 import org.onap.cps.spi.FetchDescendantsOption;
34 import org.onap.cps.spi.exceptions.DataValidationException;
35 import org.onap.cps.spi.model.DataNode;
36 import org.onap.cps.spi.model.DataNodeBuilder;
37 import org.onap.cps.utils.YangUtils;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.stereotype.Service;
42
43 @Service
44 @Slf4j
45 public class CpsDataServiceImpl implements CpsDataService {
46
47     private static final String ROOT_NODE_XPATH = "/";
48
49     @Autowired
50     private CpsDataPersistenceService cpsDataPersistenceService;
51
52     @Autowired
53     private CpsAdminService cpsAdminService;
54
55     @Autowired
56     private CpsModuleService cpsModuleService;
57
58     @Autowired
59     private YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
60
61     @Autowired
62     private NotificationService notificationService;
63
64     @Override
65     public void saveData(final String dataspaceName, final String anchorName, final String jsonData,
66         final OffsetDateTime observedTimestamp) {
67         final var dataNode = buildDataNodeFromJson(dataspaceName, anchorName, ROOT_NODE_XPATH, jsonData);
68         cpsDataPersistenceService.storeDataNode(dataspaceName, anchorName, dataNode);
69         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
70     }
71
72     @Override
73     public void saveData(final String dataspaceName, final String anchorName, final String parentNodeXpath,
74         final String jsonData, final OffsetDateTime observedTimestamp) {
75         final var dataNode = buildDataNodeFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
76         cpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, parentNodeXpath, dataNode);
77         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
78     }
79
80     @Override
81     public void saveListNodeData(final String dataspaceName, final String anchorName,
82         final String parentNodeXpath, final String jsonData, final OffsetDateTime observedTimestamp) {
83         final Collection<DataNode> dataNodesCollection =
84             buildDataNodeCollectionFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
85         cpsDataPersistenceService.addListDataNodes(dataspaceName, anchorName, parentNodeXpath, dataNodesCollection);
86         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
87     }
88
89     @Override
90     public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
91         final FetchDescendantsOption fetchDescendantsOption) {
92         return cpsDataPersistenceService.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption);
93     }
94
95     @Override
96     public void updateNodeLeaves(final String dataspaceName, final String anchorName, final String parentNodeXpath,
97         final String jsonData, final OffsetDateTime observedTimestamp) {
98         final var dataNode = buildDataNodeFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
99         cpsDataPersistenceService
100             .updateDataLeaves(dataspaceName, anchorName, dataNode.getXpath(), dataNode.getLeaves());
101         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
102     }
103
104     @Override
105     public void updateNodeLeavesAndExistingDescendantLeaves(final String dataspaceName, final String anchorName,
106         final String parentNodeXpath,
107         final String dataNodeUpdatesAsJson,
108         final OffsetDateTime observedTimestamp) {
109         final Collection<DataNode> dataNodeUpdates =
110             buildDataNodeCollectionFromJson(dataspaceName, anchorName, parentNodeXpath, dataNodeUpdatesAsJson);
111         for (final DataNode dataNodeUpdate : dataNodeUpdates) {
112             processDataNodeUpdate(dataspaceName, anchorName, dataNodeUpdate);
113         }
114         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
115     }
116
117     @Override
118     public void replaceNodeTree(final String dataspaceName, final String anchorName, final String parentNodeXpath,
119         final String jsonData, final OffsetDateTime observedTimestamp) {
120         final var dataNode = buildDataNodeFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
121         cpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName, dataNode);
122         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
123     }
124
125     @Override
126     public void replaceListNodeData(final String dataspaceName, final String anchorName, final String parentNodeXpath,
127         final String jsonData, final OffsetDateTime observedTimestamp) {
128         final Collection<DataNode> dataNodes =
129             buildDataNodeCollectionFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData);
130         cpsDataPersistenceService.replaceListDataNodes(dataspaceName, anchorName, parentNodeXpath, dataNodes);
131         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
132     }
133
134     @Override
135     public void deleteListNodeData(final String dataspaceName, final String anchorName, final String listNodeXpath,
136         final OffsetDateTime observedTimestamp) {
137         cpsDataPersistenceService.deleteListDataNodes(dataspaceName, anchorName, listNodeXpath);
138         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp);
139     }
140
141
142     private DataNode buildDataNodeFromJson(final String dataspaceName, final String anchorName,
143         final String parentNodeXpath, final String jsonData) {
144
145         final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
146         final var schemaContext = getSchemaContext(dataspaceName, anchor.getSchemaSetName());
147
148         if (ROOT_NODE_XPATH.equals(parentNodeXpath)) {
149             final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext);
150             return new DataNodeBuilder().withNormalizedNodeTree(normalizedNode).build();
151         }
152
153         final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath);
154         return new DataNodeBuilder()
155             .withParentNodeXpath(parentNodeXpath)
156             .withNormalizedNodeTree(normalizedNode)
157             .build();
158     }
159
160     private Collection<DataNode> buildDataNodeCollectionFromJson(final String dataspaceName, final String anchorName,
161         final String parentNodeXpath, final String jsonData) {
162
163         final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
164         final var schemaContext = getSchemaContext(dataspaceName, anchor.getSchemaSetName());
165
166         final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath);
167         final Collection<DataNode> dataNodes = new DataNodeBuilder()
168             .withParentNodeXpath(parentNodeXpath)
169             .withNormalizedNodeTree(normalizedNode)
170             .buildCollection();
171         if (dataNodes.isEmpty()) {
172             throw new DataValidationException("Invalid list data.", "List node is empty.");
173         }
174         return dataNodes;
175
176     }
177
178     private void processDataUpdatedEventAsync(final String dataspaceName, final String anchorName,
179         final OffsetDateTime observedTimestamp) {
180         try {
181             notificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp);
182         } catch (final Exception exception) {
183             log.error("Failed to send message to notification service", exception);
184         }
185     }
186
187     private SchemaContext getSchemaContext(final String dataspaceName, final String schemaSetName) {
188         return yangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName).getSchemaContext();
189     }
190
191     private void processDataNodeUpdate(final String dataspaceName, final String anchorName,
192                                        final DataNode dataNodeUpdate) {
193         if (dataNodeUpdate == null) {
194             return;
195         }
196         cpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName, dataNodeUpdate.getXpath(),
197             dataNodeUpdate.getLeaves());
198         final Collection<DataNode> childDataNodeUpdates = dataNodeUpdate.getChildDataNodes();
199         for (final DataNode childDataNodeUpdate : childDataNodeUpdates) {
200             processDataNodeUpdate(dataspaceName, anchorName, childDataNodeUpdate);
201         }
202     }
203 }