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.LinkedHashMap;
30 import java.util.List;
32 import javax.validation.constraints.NotNull;
33 import lombok.extern.slf4j.Slf4j;
34 import org.onap.cps.api.CpsDataService;
35 import org.onap.cps.api.CpsQueryService;
36 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
37 import org.onap.cps.ncmp.api.impl.exception.NcmpException;
38 import org.onap.cps.ncmp.api.impl.operation.DmiOperations;
39 import org.onap.cps.ncmp.api.models.CmHandle;
40 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
41 import org.onap.cps.ncmp.api.models.GenericRequestBody;
42 import org.onap.cps.ncmp.api.models.PersistenceCmHandle;
43 import org.onap.cps.ncmp.api.models.PersistenceCmHandlesList;
44 import org.onap.cps.spi.FetchDescendantsOption;
45 import org.onap.cps.spi.exceptions.DataValidationException;
46 import org.onap.cps.spi.model.DataNode;
47 import org.springframework.http.HttpStatus;
48 import org.springframework.http.ResponseEntity;
49 import org.springframework.stereotype.Service;
50 import org.springframework.util.StringUtils;
55 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
57 private static final String NF_PROXY_DATASPACE_NAME = "NFP-Operational";
59 private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
61 private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
63 private CpsDataService cpsDataService;
65 private ObjectMapper objectMapper;
67 private CpsQueryService cpsQueryService;
69 private DmiOperations dmiOperations;
72 * Constructor Injection for Dependencies.
73 * @param dmiOperations dmi operation
74 * @param cpsDataService Data Service Interface
75 * @param cpsQueryService Query Service Interface
76 * @param objectMapper Object Mapper
78 public NetworkCmProxyDataServiceImpl(final DmiOperations dmiOperations, final CpsDataService cpsDataService,
79 final CpsQueryService cpsQueryService, final ObjectMapper objectMapper) {
80 this.dmiOperations = dmiOperations;
81 this.cpsDataService = cpsDataService;
82 this.cpsQueryService = cpsQueryService;
83 this.objectMapper = objectMapper;
86 private String getDataspaceName() {
87 return NF_PROXY_DATASPACE_NAME;
91 public DataNode getDataNode(final String cmHandle, final String xpath,
92 final FetchDescendantsOption fetchDescendantsOption) {
93 return cpsDataService.getDataNode(getDataspaceName(), cmHandle, xpath, fetchDescendantsOption);
97 public Collection<DataNode> queryDataNodes(final String cmHandle, final String cpsPath,
98 final FetchDescendantsOption fetchDescendantsOption) {
99 return cpsQueryService.queryDataNodes(getDataspaceName(), cmHandle, cpsPath, fetchDescendantsOption);
103 public void createDataNode(final String cmHandle, final String parentNodeXpath, final String jsonData) {
104 if (!StringUtils.hasText(parentNodeXpath) || "/".equals(parentNodeXpath)) {
105 cpsDataService.saveData(getDataspaceName(), cmHandle, jsonData);
107 cpsDataService.saveData(getDataspaceName(), cmHandle, parentNodeXpath, jsonData);
112 public void addListNodeElements(final String cmHandle, final String parentNodeXpath, final String jsonData) {
113 cpsDataService.saveListNodeData(getDataspaceName(), cmHandle, parentNodeXpath, jsonData);
117 public void updateNodeLeaves(final String cmHandle, final String parentNodeXpath, final String jsonData) {
118 cpsDataService.updateNodeLeaves(getDataspaceName(), cmHandle, parentNodeXpath, jsonData);
122 public void replaceNodeTree(final String cmHandle, final String parentNodeXpath, final String jsonData) {
123 cpsDataService.replaceNodeTree(getDataspaceName(), cmHandle, parentNodeXpath, jsonData);
127 public void updateDmiPluginRegistration(final DmiPluginRegistration dmiPluginRegistration) {
129 final List<PersistenceCmHandle> persistenceCmHandles =
131 for (final CmHandle cmHandle: dmiPluginRegistration.getCreatedCmHandles()) {
132 final var persistenceCmHandle = new PersistenceCmHandle();
133 persistenceCmHandle.setDmiServiceName(dmiPluginRegistration.getDmiPlugin());
134 persistenceCmHandle.setId(cmHandle.getCmHandleID());
135 persistenceCmHandle.setAdditionalProperties(cmHandle.getCmHandleProperties());
136 persistenceCmHandles.add(persistenceCmHandle);
138 final var persistenceCmHandlesList = new PersistenceCmHandlesList();
139 persistenceCmHandlesList.setCmHandles(persistenceCmHandles);
140 final String cmHandleJsonData = objectMapper.writeValueAsString(persistenceCmHandlesList);
141 cpsDataService.saveListNodeData(NCMP_DATASPACE_NAME,
142 NCMP_DMI_REGISTRY_ANCHOR,
145 } catch (final JsonProcessingException e) {
146 throw new DataValidationException(
147 "Parsing error occurred while processing DMI Plugin Registration" + dmiPluginRegistration, e
153 public Object getResourceDataOperationalForCmHandle(final @NotNull String cmHandle,
154 final @NotNull String resourceIdentifier,
155 final String acceptParam,
156 final String fieldsQueryParam,
157 final Integer depthQueryParam) {
159 final var dataNode = fetchDataNodeFromDmiRegistryForCmHandle(cmHandle);
160 final var dmiServiceName = String.valueOf(dataNode.getLeaves().get("dmi-service-name"));
161 final Collection<DataNode> additionalPropsList = dataNode.getChildDataNodes();
162 final var jsonBody = prepareOperationBody(GenericRequestBody.OperationEnum.READ, additionalPropsList);
163 final ResponseEntity<Object> response = dmiOperations.getResouceDataOperationalFromDmi(dmiServiceName,
170 return handleResponse(response);
174 public Object getResourceDataPassThroughRunningForCmHandle(final @NotNull String cmHandle,
175 final @NotNull String resourceIdentifier,
178 final Integer depth) {
179 final var cmHandleDataNode = fetchDataNodeFromDmiRegistryForCmHandle(cmHandle);
180 final var dmiServiceName = String.valueOf(cmHandleDataNode.getLeaves().get("dmi-service-name"));
181 final Collection<DataNode> additionalPropsList = cmHandleDataNode.getChildDataNodes();
182 final var dmiRequesBody = prepareOperationBody(GenericRequestBody.OperationEnum.READ, additionalPropsList);
183 final ResponseEntity<Object> response = dmiOperations.getResouceDataPassThroughRunningFromDmi(dmiServiceName,
190 return handleResponse(response);
193 private DataNode fetchDataNodeFromDmiRegistryForCmHandle(final String cmHandle) {
194 final String xpathForDmiRegistryToFetchCmHandle = "/dmi-registry/cm-handles[@id='" + cmHandle + "']";
195 final var dataNode = cpsDataService.getDataNode(NCMP_DATASPACE_NAME,
196 NCMP_DMI_REGISTRY_ANCHOR,
197 xpathForDmiRegistryToFetchCmHandle,
198 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
202 private String prepareOperationBody(final GenericRequestBody.OperationEnum operation,
203 final Collection<DataNode> additionalPropertyList) {
204 final var requestBody = new GenericRequestBody();
205 final Map<String, String> additionalPropertyMap = getAdditionalPropertiesMap(additionalPropertyList);
206 requestBody.setOperation(GenericRequestBody.OperationEnum.READ);
207 requestBody.setCmHandleProperties(additionalPropertyMap);
209 final var requestJson = objectMapper.writeValueAsString(requestBody);
211 } catch (final JsonProcessingException je) {
212 log.error("Parsing error occurred while converting Object to JSON.");
213 throw new NcmpException("Parsing error occurred while converting given object to JSON.",
218 private Map<String, String> getAdditionalPropertiesMap(final Collection<DataNode> additionalPropertyList) {
219 if (additionalPropertyList == null || additionalPropertyList.size() == 0) {
222 final Map<String, String> additionalPropertyMap = new LinkedHashMap<>();
223 for (final var node: additionalPropertyList) {
224 additionalPropertyMap.put(String.valueOf(node.getLeaves().get("name")),
225 String.valueOf(node.getLeaves().get("value")));
227 return additionalPropertyMap;
230 private Object handleResponse(final ResponseEntity<Object> responseEntity) {
231 if (responseEntity.getStatusCode() == HttpStatus.OK) {
232 return responseEntity.getBody();
234 throw new NcmpException("Not able to get resource data.",
235 "DMI status code: " + responseEntity.getStatusCodeValue()
236 + ", DMI response body: " + responseEntity.getBody());