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