1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
/*
* ============LICENSE_START=======================================================
* Copyright (C) 2022-2023 Nordix Foundation
* Modifications Copyright (C) 2022 Bell Canada
* Modifications Copyright (C) 2023 TechMahindra Ltd.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
package org.onap.cps.ncmp.api.inventory;
import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME;
import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NO_TIMESTAMP;
import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED;
import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS;
import io.micrometer.core.annotation.Timed;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.onap.cps.api.CpsAdminService;
import org.onap.cps.api.CpsDataService;
import org.onap.cps.api.CpsModuleService;
import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
import org.onap.cps.spi.FetchDescendantsOption;
import org.onap.cps.spi.exceptions.DataValidationException;
import org.onap.cps.spi.exceptions.SchemaSetNotFoundException;
import org.onap.cps.spi.model.DataNode;
import org.onap.cps.spi.model.ModuleDefinition;
import org.onap.cps.spi.model.ModuleReference;
import org.onap.cps.spi.utils.CpsValidator;
import org.onap.cps.utils.JsonObjectMapper;
import org.springframework.stereotype.Component;
@Slf4j
@RequiredArgsConstructor
@Component
public class InventoryPersistenceImpl implements InventoryPersistence {
private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
private static final String NCMP_DMI_REGISTRY_PARENT = "/dmi-registry";
private final JsonObjectMapper jsonObjectMapper;
private final CpsDataService cpsDataService;
private final CpsModuleService cpsModuleService;
private final CpsAdminService cpsAdminService;
private final CpsValidator cpsValidator;
@Override
public CompositeState getCmHandleState(final String cmHandleId) {
final DataNode stateAsDataNode = cpsDataService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
createCmHandleXPath(cmHandleId) + "/state",
FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS).iterator().next();
cpsValidator.validateNameCharacters(cmHandleId);
return new CompositeStateBuilder().fromDataNode(stateAsDataNode).build();
}
@Override
public void saveCmHandleState(final String cmHandleId, final CompositeState compositeState) {
final String cmHandleJsonData = createStateJsonData(jsonObjectMapper.asJsonString(compositeState));
cpsDataService.updateDataNodeAndDescendants(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
createCmHandleXPath(cmHandleId),
cmHandleJsonData, OffsetDateTime.now());
}
@Override
public void saveCmHandleStateBatch(final Map<String, CompositeState> cmHandleStatePerCmHandleId) {
final Map<String, String> cmHandlesJsonDataMap = new HashMap<>();
cmHandleStatePerCmHandleId.forEach((cmHandleId, compositeState) -> cmHandlesJsonDataMap.put(
createCmHandleXPath(cmHandleId),
createStateJsonData(jsonObjectMapper.asJsonString(compositeState))));
cpsDataService.updateDataNodesAndDescendants(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
cmHandlesJsonDataMap, OffsetDateTime.now());
}
@Override
public YangModelCmHandle getYangModelCmHandle(final String cmHandleId) {
cpsValidator.validateNameCharacters(cmHandleId);
final DataNode dataNode = getCmHandleDataNode(cmHandleId).iterator().next();
return YangDataConverter.convertCmHandleToYangModel(dataNode, cmHandleId);
}
@Override
public Collection<YangModelCmHandle> getYangModelCmHandles(final Collection<String> cmHandleIds) {
final Collection<String> validCmHandleIds = new ArrayList<>(cmHandleIds.size());
cmHandleIds.forEach(cmHandleId -> {
try {
cpsValidator.validateNameCharacters(cmHandleId);
validCmHandleIds.add(cmHandleId);
} catch (final DataValidationException dataValidationException) {
log.error("DataValidationException in CmHandleId {} to be ignored",
dataValidationException.getMessage());
}
});
return YangDataConverter.convertDataNodesToYangModelCmHandles(getCmHandleDataNodes(validCmHandleIds));
}
@Override
public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
return cpsModuleService.getModuleDefinitionsByAnchorName(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
}
@Override
public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
cpsValidator.validateNameCharacters(cmHandleId);
return cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
}
@Override
public void saveCmHandle(final YangModelCmHandle yangModelCmHandle) {
final String cmHandleJsonData =
createCmHandleJsonData(jsonObjectMapper.asJsonString(yangModelCmHandle));
cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT,
cmHandleJsonData, NO_TIMESTAMP);
}
@Override
public void saveCmHandleBatch(final Collection<YangModelCmHandle> yangModelCmHandles) {
final List<String> cmHandlesJsonData = new ArrayList<>();
yangModelCmHandles.forEach(yangModelCmHandle -> cmHandlesJsonData.add(
createCmHandleJsonData(jsonObjectMapper.asJsonString(yangModelCmHandle))));
cpsDataService.saveListElementsBatch(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
NCMP_DMI_REGISTRY_PARENT, cmHandlesJsonData, NO_TIMESTAMP);
}
@Override
public void deleteListOrListElement(final String listElementXpath) {
cpsDataService.deleteListOrListElement(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
listElementXpath, NO_TIMESTAMP);
}
@Override
@Timed(value = "cps.ncmp.inventory.persistence.schemaset.delete",
description = "Time taken to delete a schemaset")
public void deleteSchemaSetWithCascade(final String schemaSetName) {
try {
cpsValidator.validateNameCharacters(schemaSetName);
cpsModuleService.deleteSchemaSet(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetName,
CASCADE_DELETE_ALLOWED);
} catch (final SchemaSetNotFoundException schemaSetNotFoundException) {
log.warn("Schema set {} does not exist or already deleted", schemaSetName);
}
}
@Override
@Timed(value = "cps.ncmp.inventory.persistence.datanode.get",
description = "Time taken to get a data node (from ncmp dmi registry)")
public Collection<DataNode> getDataNode(final String xpath) {
return getDataNode(xpath, INCLUDE_ALL_DESCENDANTS);
}
@Override
@Timed(value = "cps.ncmp.inventory.persistence.datanode.get",
description = "Time taken to get a data node (from ncmp dmi registry)")
public Collection<DataNode> getDataNode(final String xpath, final FetchDescendantsOption fetchDescendantsOption) {
return cpsDataService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
xpath, fetchDescendantsOption);
}
@Override
public Collection<DataNode> getDataNodes(final Collection<String> xpaths) {
return getDataNodes(xpaths, INCLUDE_ALL_DESCENDANTS);
}
@Override
public Collection<DataNode> getDataNodes(final Collection<String> xpaths,
final FetchDescendantsOption fetchDescendantsOption) {
return cpsDataService.getDataNodesForMultipleXpaths(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
xpaths, fetchDescendantsOption);
}
@Override
public Collection<DataNode> getCmHandleDataNode(final String cmHandleId) {
return this.getDataNode(createCmHandleXPath(cmHandleId));
}
@Override
public Collection<DataNode> getCmHandleDataNodes(final Collection<String> cmHandleIds) {
final Collection<String> xpaths = new ArrayList<>(cmHandleIds.size());
cmHandleIds.forEach(cmHandleId -> xpaths.add(createCmHandleXPath(cmHandleId)));
return this.getDataNodes(xpaths);
}
@Override
public Collection<String> getCmHandleIdsWithGivenModules(final Collection<String> moduleNamesForQuery) {
return cpsAdminService.queryAnchorNames(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, moduleNamesForQuery);
}
@Override
public void replaceListContent(final String parentNodeXpath, final Collection<DataNode> dataNodes) {
cpsDataService.replaceListContent(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
parentNodeXpath, dataNodes, NO_TIMESTAMP);
}
@Override
public void deleteDataNode(final String dataNodeXpath) {
cpsDataService.deleteDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, dataNodeXpath, NO_TIMESTAMP);
}
@Override
public void deleteDataNodes(final Collection<String> dataNodeXpaths) {
cpsDataService.deleteDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, dataNodeXpaths, NO_TIMESTAMP);
}
private static String createCmHandleXPath(final String cmHandleId) {
return "/dmi-registry/cm-handles[@id='" + cmHandleId + "']";
}
private static String createStateJsonData(final String state) {
return "{\"state\":" + state + "}";
}
private static String createCmHandleJsonData(final String cmHandleId) {
return "{\"cm-handles\":[" + cmHandleId + "]}";
}
}
|