Core logic to update,add or remove cmHandle properties
[cps.git] / cps-service / src / main / java / org / onap / cps / api / impl / CpsDataServiceImpl.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 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.AllArgsConstructor;
28 import lombok.extern.slf4j.Slf4j;
29 import org.onap.cps.api.CpsAdminService;
30 import org.onap.cps.api.CpsDataService;
31 import org.onap.cps.notification.NotificationService;
32 import org.onap.cps.notification.Operation;
33 import org.onap.cps.spi.CpsDataPersistenceService;
34 import org.onap.cps.spi.FetchDescendantsOption;
35 import org.onap.cps.spi.exceptions.DataValidationException;
36 import org.onap.cps.spi.model.Anchor;
37 import org.onap.cps.spi.model.DataNode;
38 import org.onap.cps.spi.model.DataNodeBuilder;
39 import org.onap.cps.utils.YangUtils;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import org.springframework.stereotype.Service;
43
44 @Service
45 @Slf4j
46 @AllArgsConstructor
47 public class CpsDataServiceImpl implements CpsDataService {
48
49     private static final String ROOT_NODE_XPATH = "/";
50
51     private final CpsDataPersistenceService cpsDataPersistenceService;
52     private final CpsAdminService cpsAdminService;
53     private final YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
54     private final NotificationService notificationService;
55
56     @Override
57     public void saveData(final String dataspaceName, final String anchorName, final String jsonData,
58         final OffsetDateTime observedTimestamp) {
59         final var dataNode = buildDataNode(dataspaceName, anchorName, ROOT_NODE_XPATH, jsonData);
60         cpsDataPersistenceService.storeDataNode(dataspaceName, anchorName, dataNode);
61         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, ROOT_NODE_XPATH, Operation.CREATE);
62     }
63
64     @Override
65     public void saveData(final String dataspaceName, final String anchorName, final String parentNodeXpath,
66         final String jsonData, final OffsetDateTime observedTimestamp) {
67         final var dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData);
68         cpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, parentNodeXpath, dataNode);
69         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.CREATE);
70     }
71
72     @Override
73     public void saveListElements(final String dataspaceName, final String anchorName,
74         final String parentNodeXpath, final String jsonData, final OffsetDateTime observedTimestamp) {
75         final Collection<DataNode> listElementDataNodeCollection =
76             buildDataNodes(dataspaceName, anchorName, parentNodeXpath, jsonData);
77         cpsDataPersistenceService.addListElements(dataspaceName, anchorName, parentNodeXpath,
78             listElementDataNodeCollection);
79         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
80     }
81
82     @Override
83     public DataNode getDataNode(final String dataspaceName, final String anchorName, final String xpath,
84         final FetchDescendantsOption fetchDescendantsOption) {
85         return cpsDataPersistenceService.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption);
86     }
87
88     @Override
89     public void updateNodeLeaves(final String dataspaceName, final String anchorName, final String parentNodeXpath,
90         final String jsonData, final OffsetDateTime observedTimestamp) {
91         final var dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData);
92         cpsDataPersistenceService
93             .updateDataLeaves(dataspaceName, anchorName, dataNode.getXpath(), dataNode.getLeaves());
94         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
95     }
96
97     @Override
98     public void updateNodeLeavesAndExistingDescendantLeaves(final String dataspaceName, final String anchorName,
99         final String parentNodeXpath,
100         final String dataNodeUpdatesAsJson,
101         final OffsetDateTime observedTimestamp) {
102         final Collection<DataNode> dataNodeUpdates =
103             buildDataNodes(dataspaceName, anchorName,
104                 parentNodeXpath, dataNodeUpdatesAsJson);
105         for (final DataNode dataNodeUpdate : dataNodeUpdates) {
106             processDataNodeUpdate(dataspaceName, anchorName, dataNodeUpdate);
107         }
108         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
109     }
110
111     @Override
112     public void replaceNodeTree(final String dataspaceName, final String anchorName, final String parentNodeXpath,
113         final String jsonData, final OffsetDateTime observedTimestamp) {
114         final var dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData);
115         cpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName, dataNode);
116         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
117     }
118
119     @Override
120     public void replaceListContent(final String dataspaceName, final String anchorName, final String parentNodeXpath,
121             final String jsonData, final OffsetDateTime observedTimestamp) {
122         final Collection<DataNode> newListElements =
123                 buildDataNodes(dataspaceName, anchorName, parentNodeXpath, jsonData);
124         replaceListContent(dataspaceName, anchorName, parentNodeXpath, newListElements, observedTimestamp);
125     }
126
127     @Override
128     public void replaceListContent(final String dataspaceName, final String anchorName, final String parentNodeXpath,
129             final Collection<DataNode> dataNodes, final OffsetDateTime observedTimestamp) {
130         cpsDataPersistenceService.replaceListContent(dataspaceName, anchorName, parentNodeXpath, dataNodes);
131         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE);
132     }
133
134     @Override
135     public void deleteDataNode(final String dataspaceName, final String anchorName, final String dataNodeXpath,
136                                final OffsetDateTime observedTimestamp) {
137         cpsDataPersistenceService.deleteDataNode(dataspaceName, anchorName, dataNodeXpath);
138         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, dataNodeXpath, Operation.DELETE);
139     }
140
141     @Override
142     public void deleteDataNodes(final String dataspaceName, final String anchorName,
143         final OffsetDateTime observedTimestamp) {
144         final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
145         cpsDataPersistenceService.deleteDataNodes(dataspaceName, anchorName);
146         processDataUpdatedEventAsync(anchor, ROOT_NODE_XPATH, Operation.DELETE, observedTimestamp);
147     }
148
149     @Override
150     public void deleteListOrListElement(final String dataspaceName, final String anchorName, final String listNodeXpath,
151         final OffsetDateTime observedTimestamp) {
152         cpsDataPersistenceService.deleteListDataNode(dataspaceName, anchorName, listNodeXpath);
153         processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, listNodeXpath, Operation.DELETE);
154     }
155
156     private DataNode buildDataNode(final String dataspaceName, final String anchorName,
157                                    final String parentNodeXpath, final String jsonData) {
158
159         final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
160         final var schemaContext = getSchemaContext(dataspaceName, anchor.getSchemaSetName());
161
162         if (ROOT_NODE_XPATH.equals(parentNodeXpath)) {
163             final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext);
164             return new DataNodeBuilder().withNormalizedNodeTree(normalizedNode).build();
165         }
166
167         final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath);
168         return new DataNodeBuilder()
169             .withParentNodeXpath(parentNodeXpath)
170             .withNormalizedNodeTree(normalizedNode)
171             .build();
172     }
173
174     private Collection<DataNode> buildDataNodes(final String dataspaceName,
175                                                 final String anchorName,
176                                                 final String parentNodeXpath,
177                                                 final String jsonData) {
178
179         final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
180         final var schemaContext = getSchemaContext(dataspaceName, anchor.getSchemaSetName());
181
182         final NormalizedNode<?, ?> normalizedNode = YangUtils.parseJsonData(jsonData, schemaContext, parentNodeXpath);
183         final Collection<DataNode> dataNodes = new DataNodeBuilder()
184             .withParentNodeXpath(parentNodeXpath)
185             .withNormalizedNodeTree(normalizedNode)
186             .buildCollection();
187         if (dataNodes.isEmpty()) {
188             throw new DataValidationException("Invalid data.", "No data nodes provided");
189         }
190         return dataNodes;
191
192     }
193
194     private void processDataUpdatedEventAsync(final String dataspaceName, final String anchorName,
195                                               final OffsetDateTime observedTimestamp, final String xpath,
196                                               final Operation operation) {
197         final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
198         this.processDataUpdatedEventAsync(anchor, xpath, operation, observedTimestamp);
199     }
200
201     private void processDataUpdatedEventAsync(final Anchor anchor, final String xpath, final Operation operation,
202         final OffsetDateTime observedTimestamp) {
203         try {
204             notificationService.processDataUpdatedEvent(anchor, observedTimestamp, xpath, operation);
205         } catch (final Exception exception) {
206             //If async message can't be queued for notification service, the initial request should not failed.
207             log.error("Failed to send message to notification service", exception);
208         }
209     }
210
211     private SchemaContext getSchemaContext(final String dataspaceName, final String schemaSetName) {
212         return yangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName).getSchemaContext();
213     }
214
215     private void processDataNodeUpdate(final String dataspaceName, final String anchorName,
216                                        final DataNode dataNodeUpdate) {
217         if (dataNodeUpdate == null) {
218             return;
219         }
220         cpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName, dataNodeUpdate.getXpath(),
221             dataNodeUpdate.getLeaves());
222         final Collection<DataNode> childDataNodeUpdates = dataNodeUpdate.getChildDataNodes();
223         for (final DataNode childDataNodeUpdate : childDataNodeUpdates) {
224             processDataNodeUpdate(dataspaceName, anchorName, childDataNodeUpdate);
225         }
226     }
227
228 }