Merge "RTD change to document migration to Spring Boot 3.0"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / inventory / InventoryPersistenceImpl.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2023 Nordix Foundation
4  *  Modifications Copyright (C) 2022 Bell Canada
5  *  Modifications Copyright (C) 2023 TechMahindra Ltd.
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.ncmp.api.impl.inventory;
24
25 import java.time.OffsetDateTime;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import lombok.extern.slf4j.Slf4j;
32 import org.onap.cps.api.CpsAdminService;
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.FetchDescendantsOption;
38 import org.onap.cps.spi.exceptions.DataValidationException;
39 import org.onap.cps.spi.model.DataNode;
40 import org.onap.cps.spi.model.ModuleDefinition;
41 import org.onap.cps.spi.model.ModuleReference;
42 import org.onap.cps.spi.utils.CpsValidator;
43 import org.onap.cps.utils.JsonObjectMapper;
44 import org.springframework.stereotype.Component;
45
46 @Slf4j
47 @Component
48 public class InventoryPersistenceImpl extends NcmpPersistenceImpl implements InventoryPersistence {
49
50     private final CpsModuleService cpsModuleService;
51     private final CpsAdminService cpsAdminService;
52     private final CpsValidator cpsValidator;
53
54     /**
55      * initialize an inventory persistence object.
56      *
57      * @param jsonObjectMapper json mapper object
58      * @param cpsDataService   cps data service instance
59      * @param cpsModuleService cps module service instance
60      * @param cpsValidator     cps validation service instance
61      * @param cpsAdminService  cps admin service instance
62      */
63     public InventoryPersistenceImpl(final JsonObjectMapper jsonObjectMapper, final CpsDataService cpsDataService,
64                                     final CpsModuleService cpsModuleService, final CpsValidator cpsValidator,
65                                     final CpsAdminService cpsAdminService) {
66         super(jsonObjectMapper, cpsDataService, cpsModuleService, cpsValidator);
67         this.cpsModuleService = cpsModuleService;
68         this.cpsAdminService = cpsAdminService;
69         this.cpsValidator = cpsValidator;
70     }
71
72
73     @Override
74     public CompositeState getCmHandleState(final String cmHandleId) {
75         final DataNode stateAsDataNode = cpsDataService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
76                         createCmHandleXPath(cmHandleId) + "/state", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS)
77                 .iterator().next();
78         cpsValidator.validateNameCharacters(cmHandleId);
79         return new CompositeStateBuilder().fromDataNode(stateAsDataNode).build();
80     }
81
82     @Override
83     public void saveCmHandleState(final String cmHandleId, final CompositeState compositeState) {
84         final String cmHandleJsonData = createStateJsonData(jsonObjectMapper.asJsonString(compositeState));
85         cpsDataService.updateDataNodeAndDescendants(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
86                 createCmHandleXPath(cmHandleId), cmHandleJsonData, OffsetDateTime.now());
87     }
88
89     @Override
90     public void saveCmHandleStateBatch(final Map<String, CompositeState> cmHandleStatePerCmHandleId) {
91         final Map<String, String> cmHandlesJsonDataMap = new HashMap<>();
92         cmHandleStatePerCmHandleId.forEach((cmHandleId, compositeState) -> cmHandlesJsonDataMap.put(
93                 createCmHandleXPath(cmHandleId),
94                 createStateJsonData(jsonObjectMapper.asJsonString(compositeState))));
95         cpsDataService.updateDataNodesAndDescendants(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
96                 cmHandlesJsonDataMap, OffsetDateTime.now());
97     }
98
99     @Override
100     public YangModelCmHandle getYangModelCmHandle(final String cmHandleId) {
101         cpsValidator.validateNameCharacters(cmHandleId);
102         final DataNode dataNode = getCmHandleDataNode(cmHandleId).iterator().next();
103         return YangDataConverter.convertCmHandleToYangModel(dataNode, cmHandleId);
104     }
105
106     @Override
107     public Collection<YangModelCmHandle> getYangModelCmHandles(final Collection<String> cmHandleIds) {
108         final Collection<String> validCmHandleIds = new ArrayList<>(cmHandleIds.size());
109         cmHandleIds.forEach(cmHandleId -> {
110             try {
111                 cpsValidator.validateNameCharacters(cmHandleId);
112                 validCmHandleIds.add(cmHandleId);
113             } catch (final DataValidationException dataValidationException) {
114                 log.error("DataValidationException in CmHandleId {} to be ignored",
115                         dataValidationException.getMessage());
116             }
117         });
118         return YangDataConverter.convertDataNodesToYangModelCmHandles(getCmHandleDataNodes(validCmHandleIds));
119     }
120
121     @Override
122     public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
123         return cpsModuleService.getModuleDefinitionsByAnchorName(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
124     }
125
126     @Override
127     public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
128         cpsValidator.validateNameCharacters(cmHandleId);
129         return cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
130     }
131
132     @Override
133     public void saveCmHandle(final YangModelCmHandle yangModelCmHandle) {
134         final String cmHandleJsonData =
135                 createCmHandleJsonData(jsonObjectMapper.asJsonString(yangModelCmHandle));
136         cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT,
137                 cmHandleJsonData, NO_TIMESTAMP);
138     }
139
140     @Override
141     public void saveCmHandleBatch(final Collection<YangModelCmHandle> yangModelCmHandles) {
142         final List<String> cmHandlesJsonData = new ArrayList<>();
143         yangModelCmHandles.forEach(yangModelCmHandle -> cmHandlesJsonData.add(
144                 createCmHandleJsonData(jsonObjectMapper.asJsonString(yangModelCmHandle))));
145         cpsDataService.saveListElementsBatch(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
146                 NCMP_DMI_REGISTRY_PARENT, cmHandlesJsonData, NO_TIMESTAMP);
147     }
148
149     @Override
150     public Collection<DataNode> getCmHandleDataNode(final String cmHandleId) {
151         return this.getDataNode(createCmHandleXPath(cmHandleId));
152     }
153
154     @Override
155     public Collection<DataNode> getCmHandleDataNodes(final Collection<String> cmHandleIds) {
156         final Collection<String> xpaths = new ArrayList<>(cmHandleIds.size());
157         cmHandleIds.forEach(cmHandleId -> xpaths.add(createCmHandleXPath(cmHandleId)));
158         return this.getDataNodes(xpaths);
159     }
160
161     @Override
162     public Collection<String> getCmHandleIdsWithGivenModules(final Collection<String> moduleNamesForQuery) {
163         return cpsAdminService.queryAnchorNames(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, moduleNamesForQuery);
164     }
165
166     private static String createCmHandleXPath(final String cmHandleId) {
167         return NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId + "']";
168     }
169
170     private static String createStateJsonData(final String state) {
171         return "{\"state\":" + state + "}";
172     }
173
174     private static String createCmHandleJsonData(final String yangModelCmHandleAsJson) {
175         return "{\"cm-handles\":[" + yangModelCmHandleAsJson + "]}";
176     }
177 }