Refactoring persistence classes
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / inventory / NcmpPersistenceImpl.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl.inventory;
22
23 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED;
24 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS;
25
26 import io.micrometer.core.annotation.Timed;
27 import java.util.Collection;
28 import lombok.RequiredArgsConstructor;
29 import lombok.extern.slf4j.Slf4j;
30 import org.onap.cps.api.CpsDataService;
31 import org.onap.cps.api.CpsModuleService;
32 import org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence;
33 import org.onap.cps.spi.FetchDescendantsOption;
34 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException;
35 import org.onap.cps.spi.model.DataNode;
36 import org.onap.cps.spi.utils.CpsValidator;
37 import org.onap.cps.utils.JsonObjectMapper;
38 import org.springframework.stereotype.Component;
39
40 @Slf4j
41 @RequiredArgsConstructor
42 @Component
43 public class NcmpPersistenceImpl implements NcmpPersistence {
44
45     protected final JsonObjectMapper jsonObjectMapper;
46     protected final CpsDataService cpsDataService;
47     private final CpsModuleService cpsModuleService;
48     private final CpsValidator cpsValidator;
49
50     @Override
51     public void deleteListOrListElement(final String listElementXpath) {
52         cpsDataService.deleteListOrListElement(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, listElementXpath,
53                 NO_TIMESTAMP);
54     }
55
56     @Override
57     @Timed(value = "cps.ncmp.inventory.persistence.schemaset.delete",
58             description = "Time taken to delete a schemaset")
59     public void deleteSchemaSetWithCascade(final String schemaSetName) {
60         try {
61             cpsValidator.validateNameCharacters(schemaSetName);
62             cpsModuleService.deleteSchemaSet(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetName,
63                     CASCADE_DELETE_ALLOWED);
64         } catch (final SchemaSetNotFoundException schemaSetNotFoundException) {
65             log.warn("Schema set {} does not exist or already deleted", schemaSetName);
66         }
67     }
68
69     @Override
70     @Timed(value = "cps.ncmp.inventory.persistence.schemaset.delete.batch",
71         description = "Time taken to delete multiple schemaset")
72     public void deleteSchemaSetsWithCascade(final Collection<String> schemaSetNames) {
73         cpsValidator.validateNameCharacters(schemaSetNames);
74         cpsModuleService.deleteSchemaSetsWithCascade(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetNames);
75     }
76
77     @Override
78     @Timed(value = "cps.ncmp.inventory.persistence.datanode.get",
79             description = "Time taken to get a data node (from ncmp dmi registry)")
80     public Collection<DataNode> getDataNode(final String xpath) {
81         return getDataNode(xpath, INCLUDE_ALL_DESCENDANTS);
82     }
83
84     @Override
85     @Timed(value = "cps.ncmp.inventory.persistence.datanode.get",
86             description = "Time taken to get a data node (from ncmp dmi registry)")
87     public Collection<DataNode> getDataNode(final String xpath, final FetchDescendantsOption fetchDescendantsOption) {
88         return cpsDataService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, xpath,
89                 fetchDescendantsOption);
90     }
91
92     @Override
93     public Collection<DataNode> getDataNodes(final Collection<String> xpaths) {
94         return getDataNodes(xpaths, INCLUDE_ALL_DESCENDANTS);
95     }
96
97     @Override
98     public Collection<DataNode> getDataNodes(final Collection<String> xpaths,
99                                              final FetchDescendantsOption fetchDescendantsOption) {
100         return cpsDataService.getDataNodesForMultipleXpaths(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, xpaths,
101                 fetchDescendantsOption);
102     }
103
104     @Override
105     public void replaceListContent(final String parentNodeXpath, final Collection<DataNode> dataNodes) {
106         cpsDataService.replaceListContent(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, parentNodeXpath, dataNodes,
107                 NO_TIMESTAMP);
108     }
109
110     @Override
111     public void deleteDataNode(final String dataNodeXpath) {
112         cpsDataService.deleteDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, dataNodeXpath, NO_TIMESTAMP);
113     }
114
115     @Override
116     public void deleteDataNodes(final Collection<String> dataNodeXpaths) {
117         cpsDataService.deleteDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, dataNodeXpaths, NO_TIMESTAMP);
118     }
119
120 }