Performance Improvement: Enhance state handler
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / inventory / InventoryPersistence.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Nordix Foundation
4  *  Modifications Copyright (C) 2022 Bell Canada
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.ncmp.api.inventory;
23
24 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME;
25 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NO_TIMESTAMP;
26 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED;
27 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS;
28
29 import java.time.OffsetDateTime;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import lombok.RequiredArgsConstructor;
36 import lombok.extern.slf4j.Slf4j;
37 import org.onap.cps.api.CpsDataService;
38 import org.onap.cps.api.CpsModuleService;
39 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
40 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
41 import org.onap.cps.spi.CpsAdminPersistenceService;
42 import org.onap.cps.spi.CpsDataPersistenceService;
43 import org.onap.cps.spi.FetchDescendantsOption;
44 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException;
45 import org.onap.cps.spi.model.Anchor;
46 import org.onap.cps.spi.model.DataNode;
47 import org.onap.cps.spi.model.ModuleDefinition;
48 import org.onap.cps.spi.model.ModuleReference;
49 import org.onap.cps.utils.CpsValidator;
50 import org.onap.cps.utils.JsonObjectMapper;
51 import org.springframework.stereotype.Component;
52
53 @Slf4j
54 @RequiredArgsConstructor
55 @Component
56 public class InventoryPersistence {
57
58     private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
59
60     private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
61
62     private static final String NCMP_DMI_REGISTRY_PARENT = "/dmi-registry";
63
64     private static final String CM_HANDLE_XPATH_TEMPLATE = "/dmi-registry/cm-handles[@id='" + "%s" + "']";
65
66     private final JsonObjectMapper jsonObjectMapper;
67
68     private final CpsDataService cpsDataService;
69
70     private final CpsModuleService cpsModuleService;
71
72     private final CpsDataPersistenceService cpsDataPersistenceService;
73
74     private final CpsAdminPersistenceService cpsAdminPersistenceService;
75
76     /**
77      * Get the Cm Handle Composite State from the data node.
78      *
79      * @param cmHandleId cm handle id
80      * @return the cm handle composite state
81      */
82     public CompositeState getCmHandleState(final String cmHandleId) {
83         final DataNode stateAsDataNode = cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
84             String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId) + "/state",
85             FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
86         return new CompositeStateBuilder().fromDataNode(stateAsDataNode).build();
87     }
88
89     /**
90      * Save the cm handles state.
91      *
92      * @param cmHandleId    cm handle id
93      * @param compositeState composite state
94      */
95     public void saveCmHandleState(final String cmHandleId, final CompositeState compositeState) {
96         final String cmHandleJsonData = String.format("{\"state\":%s}",
97             jsonObjectMapper.asJsonString(compositeState));
98         cpsDataService.updateDataNodeAndDescendants(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
99             String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId),
100             cmHandleJsonData, OffsetDateTime.now());
101     }
102
103     /**
104      * Save all cm handles states in batch.
105      *
106      * @param cmHandleStatePerCmHandleId contains cm handle id and updated state
107      */
108     public void saveCmHandleStateBatch(final Map<String, CompositeState> cmHandleStatePerCmHandleId) {
109         final Map<String, String> cmHandlesJsonDataMap = new HashMap<>();
110         cmHandleStatePerCmHandleId.forEach((cmHandleId, compositeState) -> cmHandlesJsonDataMap.put(
111                 String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId),
112                 String.format("{\"state\":%s}", jsonObjectMapper.asJsonString(compositeState))));
113         cpsDataService.updateDataNodesAndDescendants(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
114                 cmHandlesJsonDataMap, OffsetDateTime.now());
115     }
116
117     /**
118      * This method retrieves DMI service name, DMI properties and the state for a given cm handle.
119      * @param cmHandleId the id of the cm handle
120      * @return yang model cm handle
121      */
122     public YangModelCmHandle getYangModelCmHandle(final String cmHandleId) {
123         CpsValidator.validateNameCharacters(cmHandleId);
124         return YangDataConverter.convertCmHandleToYangModel(getCmHandleDataNode(cmHandleId), cmHandleId);
125     }
126
127     /**
128      * Method to return module definitions by cmHandleId.
129      *
130      * @param cmHandleId cm handle ID
131      * @return a collection of module definitions (moduleName, revision and yang resource content)
132      */
133     public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
134         return cpsModuleService.getModuleDefinitionsByAnchorName(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
135     }
136
137     /**
138      * Method to return module references by cmHandleId.
139      *
140      * @param cmHandleId cm handle ID
141      * @return a collection of module references (moduleName and revision)
142      */
143     public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
144         CpsValidator.validateNameCharacters(cmHandleId);
145         return cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
146     }
147
148     /**
149      * Method to save cmHandle.
150      *
151      * @param yangModelCmHandle cmHandle represented as Yang Model
152      */
153     public void saveCmHandle(final YangModelCmHandle yangModelCmHandle) {
154         final String cmHandleJsonData =
155                 String.format("{\"cm-handles\":[%s]}", jsonObjectMapper.asJsonString(yangModelCmHandle));
156         cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT,
157                 cmHandleJsonData, NO_TIMESTAMP);
158     }
159
160     /**
161      * Method to save batch of cm handles.
162      *
163      * @param yangModelCmHandles cm handle represented as Yang Models
164      */
165     public void saveCmHandleBatch(final Collection<YangModelCmHandle> yangModelCmHandles) {
166         final List<String> cmHandlesJsonData = new ArrayList<>();
167         yangModelCmHandles.forEach(yangModelCmHandle -> cmHandlesJsonData.add(
168                 String.format("{\"cm-handles\":[%s]}", jsonObjectMapper.asJsonString(yangModelCmHandle))));
169         cpsDataService.saveListElementsBatch(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
170                 NCMP_DMI_REGISTRY_PARENT, cmHandlesJsonData, NO_TIMESTAMP);
171     }
172
173     /**
174      * Method to delete a list or a list element.
175      *
176      * @param listElementXpath list element xPath
177      */
178     public void deleteListOrListElement(final String listElementXpath) {
179         cpsDataService.deleteListOrListElement(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
180                 listElementXpath, NO_TIMESTAMP);
181     }
182
183     /**
184      * Method to delete a schema set.
185      *
186      * @param schemaSetName schema set name
187      */
188     public void deleteSchemaSetWithCascade(final String schemaSetName) {
189         try {
190             CpsValidator.validateNameCharacters(schemaSetName);
191             cpsModuleService.deleteSchemaSet(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetName,
192                     CASCADE_DELETE_ALLOWED);
193         } catch (final SchemaSetNotFoundException schemaSetNotFoundException) {
194             log.warn("Schema set {} does not exist or already deleted", schemaSetName);
195         }
196     }
197
198     /**
199      * Get data node via xpath.
200      *
201      * @param xpath xpath
202      * @return data node
203      */
204     public DataNode getDataNode(final String xpath) {
205         return cpsDataPersistenceService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
206                 xpath, INCLUDE_ALL_DESCENDANTS);
207     }
208
209     /**
210      * Get data node of given cm handle.
211      *
212      * @param cmHandleId cmHandle ID
213      * @return data node
214      */
215     public DataNode getCmHandleDataNode(final String cmHandleId) {
216         return this.getDataNode(String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId));
217     }
218
219     /**
220      * Query anchors via module names.
221      *
222      * @param moduleNamesForQuery module names
223      * @return Collection of anchors
224      */
225     public Collection<Anchor> queryAnchors(final Collection<String> moduleNamesForQuery) {
226         return  cpsAdminPersistenceService.queryAnchors(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, moduleNamesForQuery);
227     }
228
229     /**
230      * Method to get all anchors.
231      *
232      * @return Collection of anchors
233      */
234     public Collection<Anchor> getAnchors() {
235         return cpsAdminPersistenceService.getAnchors(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME);
236     }
237
238     /**
239      * Replaces list content by removing all existing elements and inserting the given new elements as data nodes.
240      *
241      * @param parentNodeXpath   parent node xpath
242      * @param dataNodes         datanodes representing the updated data
243      */
244     public void replaceListContent(final String parentNodeXpath, final Collection<DataNode> dataNodes) {
245         cpsDataService.replaceListContent(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
246                 parentNodeXpath, dataNodes, NO_TIMESTAMP);
247     }
248
249     /**
250      * Deletes data node for given anchor and dataspace.
251      *
252      * @param dataNodeXpath data node xpath
253      */
254     public void deleteDataNode(final String dataNodeXpath) {
255         cpsDataService.deleteDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, dataNodeXpath,
256                 NO_TIMESTAMP);
257     }
258 }