2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 highstreet technologies GmbH
4 * Modifications Copyright (C) 2021 Nordix Foundation
5 * Modifications Copyright (C) 2021 Pantheon.tech
6 * Modifications Copyright (C) 2021 Bell Canada
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.cps.ncmp.api.impl;
26 import com.fasterxml.jackson.core.JsonProcessingException;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import com.google.gson.Gson;
29 import com.google.gson.JsonArray;
30 import com.google.gson.JsonElement;
31 import com.google.gson.JsonObject;
32 import java.time.OffsetDateTime;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.LinkedHashMap;
38 import java.util.List;
40 import javax.validation.constraints.NotNull;
41 import lombok.extern.slf4j.Slf4j;
42 import org.onap.cps.api.CpsAdminService;
43 import org.onap.cps.api.CpsDataService;
44 import org.onap.cps.api.CpsModuleService;
45 import org.onap.cps.api.CpsQueryService;
46 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
47 import org.onap.cps.ncmp.api.impl.exception.NcmpException;
48 import org.onap.cps.ncmp.api.impl.operation.DmiOperations;
49 import org.onap.cps.ncmp.api.models.CmHandle;
50 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
51 import org.onap.cps.ncmp.api.models.GenericRequestBody;
52 import org.onap.cps.ncmp.api.models.PersistenceCmHandle;
53 import org.onap.cps.ncmp.api.models.PersistenceCmHandlesList;
54 import org.onap.cps.ncmp.api.models.YangResource;
55 import org.onap.cps.spi.FetchDescendantsOption;
56 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
57 import org.onap.cps.spi.exceptions.DataValidationException;
58 import org.onap.cps.spi.model.DataNode;
59 import org.onap.cps.spi.model.ModuleReference;
60 import org.springframework.http.HttpStatus;
61 import org.springframework.http.ResponseEntity;
62 import org.springframework.stereotype.Service;
63 import org.springframework.util.StringUtils;
67 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
69 private static final String NF_PROXY_DATASPACE_NAME = "NFP-Operational";
71 private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
73 private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
75 private static final OffsetDateTime NO_TIMESTAMP = null;
77 private static final String NCMP_DMI_SERVICE_NAME = "dmi-service-name";
79 private CpsDataService cpsDataService;
81 private ObjectMapper objectMapper;
83 private CpsQueryService cpsQueryService;
85 private DmiOperations dmiOperations;
87 private CpsModuleService cpsModuleService;
89 private CpsAdminService cpsAdminService;
91 public static final String NO_NAMESPACE = null;
94 * Constructor Injection for Dependencies.
95 * @param dmiOperations DMI operation
96 * @param cpsDataService Data Service Interface
97 * @param cpsQueryService Query Service Interface
98 * @param objectMapper Object Mapper
100 public NetworkCmProxyDataServiceImpl(final DmiOperations dmiOperations,
101 final CpsModuleService cpsModuleService,
102 final CpsDataService cpsDataService,
103 final CpsQueryService cpsQueryService,
104 final CpsAdminService cpsAdminService,
105 final ObjectMapper objectMapper) {
106 this.dmiOperations = dmiOperations;
107 this.cpsModuleService = cpsModuleService;
108 this.cpsDataService = cpsDataService;
109 this.cpsQueryService = cpsQueryService;
110 this.cpsAdminService = cpsAdminService;
111 this.objectMapper = objectMapper;
115 public DataNode getDataNode(final String cmHandle, final String xpath,
116 final FetchDescendantsOption fetchDescendantsOption) {
117 return cpsDataService.getDataNode(NF_PROXY_DATASPACE_NAME, cmHandle, xpath, fetchDescendantsOption);
121 public Collection<DataNode> queryDataNodes(final String cmHandle, final String cpsPath,
122 final FetchDescendantsOption fetchDescendantsOption) {
123 return cpsQueryService.queryDataNodes(NF_PROXY_DATASPACE_NAME, cmHandle, cpsPath, fetchDescendantsOption);
127 public void createDataNode(final String cmHandle, final String parentNodeXpath, final String jsonData) {
128 if (!StringUtils.hasText(parentNodeXpath) || "/".equals(parentNodeXpath)) {
129 cpsDataService.saveData(NF_PROXY_DATASPACE_NAME, cmHandle, jsonData, NO_TIMESTAMP);
131 cpsDataService.saveData(NF_PROXY_DATASPACE_NAME, cmHandle, parentNodeXpath, jsonData, NO_TIMESTAMP);
136 public void addListNodeElements(final String cmHandle, final String parentNodeXpath, final String jsonData) {
137 cpsDataService.saveListNodeData(NF_PROXY_DATASPACE_NAME, cmHandle, parentNodeXpath, jsonData, NO_TIMESTAMP);
141 public void updateNodeLeaves(final String cmHandle, final String parentNodeXpath, final String jsonData) {
142 cpsDataService.updateNodeLeaves(NF_PROXY_DATASPACE_NAME, cmHandle, parentNodeXpath, jsonData, NO_TIMESTAMP);
146 public void replaceNodeTree(final String cmHandle, final String parentNodeXpath, final String jsonData) {
147 cpsDataService.replaceNodeTree(NF_PROXY_DATASPACE_NAME, cmHandle, parentNodeXpath, jsonData, NO_TIMESTAMP);
151 public void updateDmiRegistrationAndSyncModule(final DmiPluginRegistration dmiPluginRegistration) {
152 if (dmiPluginRegistration.getCreatedCmHandles() != null) {
153 parseAndCreateCmHandlesInDmiRegistrationAndSyncModule(dmiPluginRegistration);
155 if (dmiPluginRegistration.getUpdatedCmHandles() != null) {
156 parseAndUpdateCmHandlesInDmiRegistration(dmiPluginRegistration);
158 if (dmiPluginRegistration.getRemovedCmHandles() != null) {
159 parseAndRemoveCmHandlesInDmiRegistration(dmiPluginRegistration);
164 public Object getResourceDataOperationalForCmHandle(final @NotNull String cmHandle,
165 final @NotNull String resourceIdentifier,
166 final String acceptParam,
167 final String fieldsQueryParam,
168 final Integer depthQueryParam) {
170 final var cmHandleDataNode = fetchDataNodeFromDmiRegistryForCmHandle(cmHandle);
171 final var dmiServiceName = String.valueOf(cmHandleDataNode.getLeaves().get(NCMP_DMI_SERVICE_NAME));
172 final String dmiRequestBody = getGenericRequestBody(cmHandleDataNode);
173 final ResponseEntity<Object> response = dmiOperations.getResourceDataOperationalFromDmi(dmiServiceName,
180 return handleResponse(response);
184 public Object getResourceDataPassThroughRunningForCmHandle(final @NotNull String cmHandle,
185 final @NotNull String resourceIdentifier,
186 final String acceptParam,
188 final Integer depth) {
189 final var cmHandleDataNode = fetchDataNodeFromDmiRegistryForCmHandle(cmHandle);
190 final var dmiServiceName = String.valueOf(cmHandleDataNode.getLeaves().get(NCMP_DMI_SERVICE_NAME));
191 final String dmiRequestBody = getGenericRequestBody(cmHandleDataNode);
192 final ResponseEntity<Object> response = dmiOperations.getResourceDataPassThroughRunningFromDmi(dmiServiceName,
199 return handleResponse(response);
203 public void createResourceDataPassThroughRunningForCmHandle(final @NotNull String cmHandle,
204 final @NotNull String resourceIdentifier,
205 final @NotNull Object requestBody,
206 final String contentType) {
207 final var cmHandleDataNode = fetchDataNodeFromDmiRegistryForCmHandle(cmHandle);
208 final var dmiServiceName = String.valueOf(cmHandleDataNode.getLeaves().get(NCMP_DMI_SERVICE_NAME));
209 final Collection<DataNode> cmHandlePropertiesList = cmHandleDataNode.getChildDataNodes();
210 final Map<String, String> cmHandlePropertiesMap = getCmHandlePropertiesAsMap(cmHandlePropertiesList);
211 final var dmiRequestBodyObject = GenericRequestBody.builder()
212 .operation(GenericRequestBody.OperationEnum.CREATE)
213 .dataType(contentType)
215 .cmHandleProperties(cmHandlePropertiesMap)
217 final var dmiRequestBody = prepareOperationBody(dmiRequestBodyObject);
218 final ResponseEntity<String> responseEntity = dmiOperations
219 .createResourceDataPassThroughRunningFromDmi(dmiServiceName,
223 handleResponseForPost(responseEntity);
227 public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandle) {
228 return cpsModuleService.getYangResourcesModuleReferences(NF_PROXY_DATASPACE_NAME, cmHandle);
231 private DataNode fetchDataNodeFromDmiRegistryForCmHandle(final String cmHandle) {
232 final String xpathForDmiRegistryToFetchCmHandle = "/dmi-registry/cm-handles[@id='" + cmHandle + "']";
233 return cpsDataService.getDataNode(NCMP_DATASPACE_NAME,
234 NCMP_DMI_REGISTRY_ANCHOR,
235 xpathForDmiRegistryToFetchCmHandle,
236 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
239 private String prepareOperationBody(final GenericRequestBody requestBodyObject) {
241 return objectMapper.writeValueAsString(requestBodyObject);
242 } catch (final JsonProcessingException e) {
243 log.error("Parsing error occurred while converting Object to JSON.");
244 throw new NcmpException("Parsing error occurred while converting given object to JSON.",
249 private static Map<String, String> getCmHandlePropertiesAsMap(final Collection<DataNode> cmHandlePropertiesList) {
250 if (cmHandlePropertiesList == null || cmHandlePropertiesList.isEmpty()) {
251 return Collections.emptyMap();
253 final Map<String, String> cmHandlePropertiesMap = new LinkedHashMap<>();
254 for (final var node: cmHandlePropertiesList) {
255 cmHandlePropertiesMap.put(String.valueOf(node.getLeaves().get("name")),
256 String.valueOf(node.getLeaves().get("value")));
258 return cmHandlePropertiesMap;
261 private static Object handleResponse(final @NotNull ResponseEntity<Object> responseEntity) {
262 if (responseEntity.getStatusCode() == HttpStatus.OK) {
263 return responseEntity.getBody();
265 throw new NcmpException("Not able to get resource data.",
266 "DMI status code: " + responseEntity.getStatusCodeValue()
267 + ", DMI response body: " + responseEntity.getBody());
271 private static void handleResponseForPost(final @NotNull ResponseEntity<String> responseEntity) {
272 if (responseEntity.getStatusCode() != HttpStatus.OK) {
273 throw new NcmpException("Not able to create resource data.",
274 "DMI status code: " + responseEntity.getStatusCodeValue()
275 + ", DMI response body: " + responseEntity.getBody());
279 private String getGenericRequestBody(final DataNode cmHandleDataNode) {
280 final Collection<DataNode> cmHandlePropertiesList = cmHandleDataNode.getChildDataNodes();
281 final Map<String, String> cmHandlePropertiesMap = getCmHandlePropertiesAsMap(cmHandlePropertiesList);
282 final var requetBodyObject = GenericRequestBody.builder()
283 .operation(GenericRequestBody.OperationEnum.READ)
284 .cmHandleProperties(cmHandlePropertiesMap)
286 return prepareOperationBody(requetBodyObject);
289 private void parseAndUpdateCmHandlesInDmiRegistration(final DmiPluginRegistration dmiPluginRegistration) {
291 final PersistenceCmHandlesList persistenceCmHandlesList = new PersistenceCmHandlesList();
293 for (final CmHandle cmHandle : dmiPluginRegistration.getUpdatedCmHandles()) {
294 final PersistenceCmHandle persistenceCmHandle =
295 toPersistenceCmHandle(dmiPluginRegistration.getDmiPlugin(), cmHandle);
296 persistenceCmHandlesList.add(persistenceCmHandle);
298 final String cmHandlesJsonData = objectMapper.writeValueAsString(persistenceCmHandlesList);
299 cpsDataService.updateNodeLeavesAndExistingDescendantLeaves(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
300 "/dmi-registry", cmHandlesJsonData, NO_TIMESTAMP);
301 } catch (final JsonProcessingException e) {
302 log.error("Parsing error occurred while converting Object to JSON DMI Registry.");
303 throw new DataValidationException(
304 "Parsing error occurred while processing DMI Plugin Registration" + dmiPluginRegistration, e
309 private void parseAndCreateCmHandlesInDmiRegistrationAndSyncModule(
310 final DmiPluginRegistration dmiPluginRegistration) {
312 final var persistenceCmHandlesList = new PersistenceCmHandlesList();
313 for (final CmHandle cmHandle : dmiPluginRegistration.getCreatedCmHandles()) {
314 final PersistenceCmHandle persistenceCmHandle =
315 toPersistenceCmHandle(dmiPluginRegistration.getDmiPlugin(), cmHandle);
316 persistenceCmHandlesList.add(persistenceCmHandle);
318 final String cmHandleJsonData = objectMapper.writeValueAsString(persistenceCmHandlesList);
320 registerAndSyncNode(dmiPluginRegistration, persistenceCmHandlesList, cmHandleJsonData);
321 } catch (final JsonProcessingException e) {
322 log.error("Parsing error occurred while converting Object to JSON for DMI Registry.");
323 throw new DataValidationException(
324 "Parsing error occurred while processing DMI Plugin Registration" + dmiPluginRegistration, e
329 private void registerAndSyncNode(final DmiPluginRegistration dmiPluginRegistration,
330 final PersistenceCmHandlesList persistenceCmHandlesList,
331 final String cmHandleJsonData) {
332 cpsDataService.saveListNodeData(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, "/dmi-registry",
333 cmHandleJsonData, NO_TIMESTAMP);
335 for (final PersistenceCmHandle persistenceCmHandle : persistenceCmHandlesList.getPersistenceCmHandles()) {
336 createAnchorAndSyncModel(persistenceCmHandle);
340 private static PersistenceCmHandle toPersistenceCmHandle(final String dmiPluginService,
341 final CmHandle cmHandle) {
342 final PersistenceCmHandle persistenceCmHandle = new PersistenceCmHandle();
343 persistenceCmHandle.setDmiServiceName(dmiPluginService);
344 persistenceCmHandle.setId(cmHandle.getCmHandleID());
345 if (cmHandle.getCmHandleProperties() == null) {
346 persistenceCmHandle.setAdditionalProperties(Collections.emptyMap());
348 persistenceCmHandle.setAdditionalProperties(cmHandle.getCmHandleProperties());
350 return persistenceCmHandle;
353 private void parseAndRemoveCmHandlesInDmiRegistration(final DmiPluginRegistration dmiPluginRegistration) {
354 for (final String cmHandle : dmiPluginRegistration.getRemovedCmHandles()) {
356 cpsDataService.deleteListNodeData(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
357 "/dmi-registry/cm-handles[@id='" + cmHandle + "']", NO_TIMESTAMP);
358 } catch (final DataNodeNotFoundException e) {
359 log.warn("Datanode {} not deleted message {}", cmHandle, e.getMessage());
364 protected void createAnchorAndSyncModel(final PersistenceCmHandle cmHandle) {
365 final var modulesForCmHandle =
366 dmiOperations.getResourceFromDmi(cmHandle.getDmiServiceName(), cmHandle.getId(), "modules");
367 final List<ModuleReference> moduleReferencesFromDmiForCmHandle =
368 getModuleReferences(modulesForCmHandle);
369 final var knownModuleReferencesInCps =
370 cpsModuleService.getYangResourceModuleReferences(NF_PROXY_DATASPACE_NAME);
371 final List<ModuleReference> existingModuleReferences = new ArrayList<>();
373 final List<ModuleReference> unknownModuleReferences = new ArrayList<>();
374 for (final ModuleReference moduleReferenceFromDmiForCmHandle : moduleReferencesFromDmiForCmHandle) {
375 if (knownModuleReferencesInCps.contains(moduleReferenceFromDmiForCmHandle)) {
376 existingModuleReferences.add(moduleReferenceFromDmiForCmHandle);
378 unknownModuleReferences.add(moduleReferenceFromDmiForCmHandle);
382 final JsonObject requestBodyAsJson = getRequestBodyAsJson(unknownModuleReferences);
384 final Map<String, String> newYangResourcesModuleNameToContentMap =
385 getNewYangResources(cmHandle, requestBodyAsJson.toString());
387 cpsModuleService.createSchemaSetFromModules(NCMP_DATASPACE_NAME, cmHandle.getId(),
388 newYangResourcesModuleNameToContentMap, existingModuleReferences);
390 cpsAdminService.createAnchor(NCMP_DATASPACE_NAME, cmHandle.getId(), cmHandle.getId());
393 private JsonObject getRequestBodyAsJson(final List<ModuleReference> unknownModuleReferences) {
395 final JsonObject requestBodyAsJson = new JsonObject();
396 requestBodyAsJson.addProperty("operation", "read");
398 final JsonArray moduleReferencesAsJson = getModuleReferencesAsJson(unknownModuleReferences);
400 final JsonObject data = new JsonObject();
401 data.add("modules", moduleReferencesAsJson);
402 requestBodyAsJson.add("data", data);
404 return requestBodyAsJson;
407 private JsonArray getModuleReferencesAsJson(final List<ModuleReference> unknownModuleReferences) {
408 final JsonArray moduleReferences = new JsonArray();
410 for (final ModuleReference moduleReference : unknownModuleReferences) {
411 final JsonObject moduleReferenceAsJson = new JsonObject();
412 moduleReferenceAsJson.addProperty("name", moduleReference.getModuleName());
413 moduleReferenceAsJson.addProperty("revision", moduleReference.getRevision());
414 moduleReferences.add(moduleReferenceAsJson);
416 return moduleReferences;
419 private Map<String, String> getNewYangResources(final PersistenceCmHandle cmHandle, final String jsonData) {
420 final var moduleResourcesAsJsonString = dmiOperations.getResourceFromDmiWithJsonData(
421 cmHandle.getDmiServiceName(), jsonData, cmHandle.getId(), "moduleResources");
423 final JsonArray moduleResources = new Gson().fromJson(moduleResourcesAsJsonString.getBody(),
425 final Map<String, String> newYangResourcesModuleNameToContentMap = new HashMap<>();
427 for (final JsonElement moduleResource : moduleResources) {
428 final YangResource yangResource = toYangResource((JsonObject) moduleResource);
429 newYangResourcesModuleNameToContentMap.put(yangResource.getModuleName(), yangResource.getYangSource());
431 return newYangResourcesModuleNameToContentMap;
434 private static YangResource toYangResource(final JsonObject yangResourceAsJson) {
435 final YangResource yangResource = new YangResource();
436 yangResource.setModuleName(yangResourceAsJson.get("moduleName").getAsString());
437 yangResource.setRevision(yangResourceAsJson.get("revision").getAsString());
438 final String yangSourceJson = yangResourceAsJson.get("yangSource").getAsString();
440 String yangSource = JsonUtils.removeWrappingTokens(yangSourceJson);
441 yangSource = JsonUtils.removeRedundantEscapeCharacters(yangSource);
442 yangResource.setYangSource(yangSource);
447 private List<ModuleReference> getModuleReferences(final ResponseEntity<String> response) {
448 final List<ModuleReference> modulesFromDmiForCmHandle = new ArrayList<>();
449 final JsonObject convertedObject = new Gson().fromJson(response.getBody(), JsonObject.class);
450 final JsonArray moduleReferencesAsJson = convertedObject.getAsJsonArray("schemas");
451 for (final JsonElement moduleReferenceAsJson : moduleReferencesAsJson) {
452 final ModuleReference moduleReference =
453 toModuleReference((JsonObject) moduleReferenceAsJson);
454 modulesFromDmiForCmHandle.add(moduleReference);
456 return modulesFromDmiForCmHandle;
459 private ModuleReference toModuleReference(final JsonObject moduleReferenceAsJson) {
460 final var moduleReference = new ModuleReference();
461 moduleReference.setModuleName(moduleReferenceAsJson.get("moduleName").getAsString());
462 moduleReference.setRevision(moduleReferenceAsJson.get("revision").getAsString());
463 return moduleReference;