Send operation information in existing notification events
[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-2022 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.notification.NotificationService;
31 import org.onap.cps.notification.Operation;
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 YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
57
58     @Autowired
59     private NotificationService notificationService;
60
61     @Override
62     public void saveData(final String dataspaceName, final String anchorName, final String jsonData,
63         final OffsetDateTime observedTimestamp) {
64         final var dataNode = buildDataNode(dataspaceName, anchorName, ROOT_NODE_XPATH, jsonData);
65         cpsDataPersistenceService.storeDataNode(dataspaceName, anchorName, dataNode);
66         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, ROOT_NODE_XPATH, Operation.CREATE);
67     }
68
69     @Override
70     public void saveData(final String dataspaceName, final String anchorName, final String parentNodeXpath,
71         final String jsonData, final OffsetDateTime observedTimestamp) {
72         final var dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData);
73         cpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, parentNodeXpath, dataNode);
74         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.CREATE);
75     }
76
77     @Override
78     public void saveListElements(final String dataspaceName, final String anchorName,
79         final String parentNodeXpath, final String jsonData, final OffsetDateTime observedTimestamp) {
80         final Collection<DataNode> listElementDataNodeCollection =
81             buildDataNodes(dataspaceName, anchorName, parentNodeXpath, jsonData);
82         cpsDataPersistenceService.addListElements(dataspaceName, anchorName, parentNodeXpath,
83             listElementDataNodeCollection);
84         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
85     }
86
87     @Override
88     public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
89         final FetchDescendantsOption fetchDescendantsOption) {
90         return cpsDataPersistenceService.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption);
91     }
92
93     @Override
94     public void updateNodeLeaves(final String dataspaceName, final String anchorName, final String parentNodeXpath,
95         final String jsonData, final OffsetDateTime observedTimestamp) {
96         final var dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData);
97         cpsDataPersistenceService
98             .updateDataLeaves(dataspaceName, anchorName, dataNode.getXpath(), dataNode.getLeaves());
99         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
100     }
101
102     @Override
103     public void updateNodeLeavesAndExistingDescendantLeaves(final String dataspaceName, final String anchorName,
104         final String parentNodeXpath,
105         final String dataNodeUpdatesAsJson,
106         final OffsetDateTime observedTimestamp) {
107         final Collection<DataNode> dataNodeUpdates =
108             buildDataNodes(dataspaceName, anchorName,
109                 parentNodeXpath, dataNodeUpdatesAsJson);
110         for (final DataNode dataNodeUpdate : dataNodeUpdates) {
111             processDataNodeUpdate(dataspaceName, anchorName, dataNodeUpdate);
112         }
113         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
114     }
115
116     @Override
117     public void replaceNodeTree(final String dataspaceName, final String anchorName, final String parentNodeXpath,
118         final String jsonData, final OffsetDateTime observedTimestamp) {
119         final var dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData);
120         cpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName, dataNode);
121         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
122     }
123
124     @Override
125     public void replaceListContent(final String dataspaceName, final String anchorName, final String parentNodeXpath,
126                                    final String jsonData, final OffsetDateTime observedTimestamp) {
127         final Collection<DataNode> newListElements =
128             buildDataNodes(dataspaceName, anchorName, parentNodeXpath, jsonData);
129         cpsDataPersistenceService.replaceListContent(dataspaceName, anchorName, parentNodeXpath, newListElements);
130         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
131     }
132
133     @Override
134     public void deleteDataNode(final String dataspaceName, final String anchorName, final String dataNodeXpath,
135                                final OffsetDateTime observedTimestamp) {
136         cpsDataPersistenceService.deleteDataNode(dataspaceName, anchorName, dataNodeXpath);
137         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, dataNodeXpath, Operation.DELETE);
138     }
139
140     @Override
141     public void deleteListOrListElement(final String dataspaceName, final String anchorName, final String listNodeXpath,
142         final OffsetDateTime observedTimestamp) {
143         cpsDataPersistenceService.deleteListDataNode(dataspaceName, anchorName, listNodeXpath);
144         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, listNodeXpath, Operation.DELETE);
145     }
146
147     private DataNode buildDataNode(final String dataspaceName, final String anchorName,
148                                    final String parentNodeXpath, final String jsonData) {
149
150         final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
151         final var schemaContext = getSchemaContext(dataspaceName, anchor.getSchemaSetName());
152
153         if (ROOT_NODE_XPATH.equals(parentNodeXpath)) {
154             final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext);
155             return new DataNodeBuilder().withNormalizedNodeTree(normalizedNode).build();
156         }
157
158         final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath);
159         return new DataNodeBuilder()
160             .withParentNodeXpath(parentNodeXpath)
161             .withNormalizedNodeTree(normalizedNode)
162             .build();
163     }
164
165     private Collection<DataNode> buildDataNodes(final String dataspaceName,
166                                                 final String anchorName,
167                                                 final String parentNodeXpath,
168                                                 final String jsonData) {
169
170         final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
171         final var schemaContext = getSchemaContext(dataspaceName, anchor.getSchemaSetName());
172
173         final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath);
174         final Collection<DataNode> dataNodes = new DataNodeBuilder()
175             .withParentNodeXpath(parentNodeXpath)
176             .withNormalizedNodeTree(normalizedNode)
177             .buildCollection();
178         if (dataNodes.isEmpty()) {
179             throw new DataValidationException("Invalid data.", "No data nodes provided");
180         }
181         return dataNodes;
182
183     }
184
185     private void processDataUpdatedEventAsync(final String dataspaceName, final String anchorName,
186                                               final OffsetDateTime observedTimestamp, final String xpath,
187                                               final Operation operation) {
188         try {
189             notificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, xpath, operation);
190         } catch (final Exception exception) {
191             log.error("Failed to send message to notification service", exception);
192         }
193     }
194
195     private SchemaContext getSchemaContext(final String dataspaceName, final String schemaSetName) {
196         return yangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName).getSchemaContext();
197     }
198
199     private void processDataNodeUpdate(final String dataspaceName, final String anchorName,
200                                        final DataNode dataNodeUpdate) {
201         if (dataNodeUpdate == null) {
202             return;
203         }
204         cpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName, dataNodeUpdate.getXpath(),
205             dataNodeUpdate.getLeaves());
206         final Collection<DataNode> childDataNodeUpdates = dataNodeUpdate.getChildDataNodes();
207         for (final DataNode childDataNodeUpdate : childDataNodeUpdates) {
208             processDataNodeUpdate(dataspaceName, anchorName, childDataNodeUpdate);
209         }
210     }
211
212 }