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 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.ncmp.api.impl;
25 import com.fasterxml.jackson.core.JsonProcessingException;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.List;
30 import lombok.extern.slf4j.Slf4j;
31 import org.onap.cps.api.CpsDataService;
32 import org.onap.cps.api.CpsQueryService;
33 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
34 import org.onap.cps.ncmp.api.models.CmHandle;
35 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
36 import org.onap.cps.ncmp.api.models.PersistenceCmHandle;
37 import org.onap.cps.ncmp.api.models.PersistenceCmHandlesList;
38 import org.onap.cps.spi.FetchDescendantsOption;
39 import org.onap.cps.spi.exceptions.DataValidationException;
40 import org.onap.cps.spi.model.DataNode;
41 import org.springframework.stereotype.Service;
42 import org.springframework.util.StringUtils;
46 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
48 private static final String NF_PROXY_DATASPACE_NAME = "NFP-Operational";
50 private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
52 private static final String NCMP_ANCHOR_NAME = "ncmp-dmi-registry";
54 private static final String NCMP_DATA_TOP_PATH = "/dmi-registry";
56 private CpsDataService cpsDataService;
58 private ObjectMapper objectMapper;
60 private CpsQueryService cpsQueryService;
63 * Constructor Injection for Dependencies.
64 * @param cpsDataService Data Service Interface
65 * @param cpsQueryService Query Service Interface
66 * @param objectMapper Object Mapper
68 public NetworkCmProxyDataServiceImpl(final CpsDataService cpsDataService,
69 final CpsQueryService cpsQueryService, final ObjectMapper objectMapper) {
70 this.cpsDataService = cpsDataService;
71 this.cpsQueryService = cpsQueryService;
72 this.objectMapper = objectMapper;
75 private String getDataspaceName() {
76 return NF_PROXY_DATASPACE_NAME;
80 public DataNode getDataNode(final String cmHandle, final String xpath,
81 final FetchDescendantsOption fetchDescendantsOption) {
82 return cpsDataService.getDataNode(getDataspaceName(), cmHandle, xpath, fetchDescendantsOption);
86 public Collection<DataNode> queryDataNodes(final String cmHandle, final String cpsPath,
87 final FetchDescendantsOption fetchDescendantsOption) {
88 return cpsQueryService.queryDataNodes(getDataspaceName(), cmHandle, cpsPath, fetchDescendantsOption);
92 public void createDataNode(final String cmHandle, final String parentNodeXpath, final String jsonData) {
93 if (!StringUtils.hasText(parentNodeXpath) || "/".equals(parentNodeXpath)) {
94 cpsDataService.saveData(getDataspaceName(), cmHandle, jsonData);
96 cpsDataService.saveData(getDataspaceName(), cmHandle, parentNodeXpath, jsonData);
101 public void addListNodeElements(final String cmHandle, final String parentNodeXpath, final String jsonData) {
102 cpsDataService.saveListNodeData(getDataspaceName(), cmHandle, parentNodeXpath, jsonData);
106 public void updateNodeLeaves(final String cmHandle, final String parentNodeXpath, final String jsonData) {
107 cpsDataService.updateNodeLeaves(getDataspaceName(), cmHandle, parentNodeXpath, jsonData);
111 public void replaceNodeTree(final String cmHandle, final String parentNodeXpath, final String jsonData) {
112 cpsDataService.replaceNodeTree(getDataspaceName(), cmHandle, parentNodeXpath, jsonData);
116 public void updateDmiPluginRegistration(final DmiPluginRegistration dmiPluginRegistration) {
118 final List<PersistenceCmHandle> persistenceCmHandles =
120 for (final CmHandle cmHandle: dmiPluginRegistration.getCreatedCmHandles()) {
121 final PersistenceCmHandle persistenceCmHandle = new PersistenceCmHandle();
122 persistenceCmHandle.setDmiServiceName(dmiPluginRegistration.getDmiPlugin());
123 persistenceCmHandle.setId(cmHandle.getCmHandle());
124 persistenceCmHandle.setAdditionalProperties(cmHandle.getCmHandleProperties());
125 persistenceCmHandles.add(persistenceCmHandle);
127 final PersistenceCmHandlesList persistenceCmHandlesList = new PersistenceCmHandlesList();
128 persistenceCmHandlesList.setCmHandles(persistenceCmHandles);
129 final String cmHandleJsonData = objectMapper.writeValueAsString(persistenceCmHandlesList);
130 cpsDataService.saveListNodeData(NCMP_DATASPACE_NAME, NCMP_ANCHOR_NAME, NCMP_DATA_TOP_PATH,
132 } catch (final JsonProcessingException e) {
133 throw new DataValidationException(
134 "Parsing error occurred while processing DMI Plugin Registration" + dmiPluginRegistration, e