get resource data for operational passthrough
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / NetworkCmProxyDataServiceImpl.java
1 /*
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
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;
24
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;
31 import java.util.Map;
32 import lombok.extern.slf4j.Slf4j;
33 import org.onap.cps.api.CpsDataService;
34 import org.onap.cps.api.CpsQueryService;
35 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
36 import org.onap.cps.ncmp.api.impl.exception.NcmpException;
37 import org.onap.cps.ncmp.api.impl.operation.DmiOperations;
38 import org.onap.cps.ncmp.api.models.CmHandle;
39 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
40 import org.onap.cps.ncmp.api.models.GenericRequestBody;
41 import org.onap.cps.ncmp.api.models.PersistenceCmHandle;
42 import org.onap.cps.ncmp.api.models.PersistenceCmHandlesList;
43 import org.onap.cps.spi.FetchDescendantsOption;
44 import org.onap.cps.spi.exceptions.DataValidationException;
45 import org.onap.cps.spi.model.DataNode;
46 import org.springframework.http.HttpStatus;
47 import org.springframework.http.ResponseEntity;
48 import org.springframework.stereotype.Service;
49 import org.springframework.util.StringUtils;
50
51
52 @Slf4j
53 @Service
54 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
55
56     private static final String NF_PROXY_DATASPACE_NAME = "NFP-Operational";
57
58     private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
59
60     private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
61
62     private CpsDataService cpsDataService;
63
64     private ObjectMapper objectMapper;
65
66     private CpsQueryService cpsQueryService;
67
68     private DmiOperations dmiOperations;
69
70     /**
71      * Constructor Injection for Dependencies.
72      * @param dmiOperations dmi operation
73      * @param cpsDataService Data Service Interface
74      * @param cpsQueryService Query Service Interface
75      * @param objectMapper Object Mapper
76      */
77     public NetworkCmProxyDataServiceImpl(final DmiOperations dmiOperations, final CpsDataService cpsDataService,
78         final CpsQueryService cpsQueryService, final ObjectMapper objectMapper) {
79         this.dmiOperations = dmiOperations;
80         this.cpsDataService = cpsDataService;
81         this.cpsQueryService = cpsQueryService;
82         this.objectMapper = objectMapper;
83     }
84
85     private String getDataspaceName() {
86         return NF_PROXY_DATASPACE_NAME;
87     }
88
89     @Override
90     public DataNode getDataNode(final String cmHandle, final String xpath,
91         final FetchDescendantsOption fetchDescendantsOption) {
92         return cpsDataService.getDataNode(getDataspaceName(), cmHandle, xpath, fetchDescendantsOption);
93     }
94
95     @Override
96     public Collection<DataNode> queryDataNodes(final String cmHandle, final String cpsPath,
97                                                final FetchDescendantsOption fetchDescendantsOption) {
98         return cpsQueryService.queryDataNodes(getDataspaceName(), cmHandle, cpsPath, fetchDescendantsOption);
99     }
100
101     @Override
102     public void createDataNode(final String cmHandle, final String parentNodeXpath, final String jsonData) {
103         if (!StringUtils.hasText(parentNodeXpath) || "/".equals(parentNodeXpath)) {
104             cpsDataService.saveData(getDataspaceName(), cmHandle, jsonData);
105         } else {
106             cpsDataService.saveData(getDataspaceName(), cmHandle, parentNodeXpath, jsonData);
107         }
108     }
109
110     @Override
111     public void addListNodeElements(final String cmHandle, final String parentNodeXpath, final String jsonData) {
112         cpsDataService.saveListNodeData(getDataspaceName(), cmHandle, parentNodeXpath, jsonData);
113     }
114
115     @Override
116     public void updateNodeLeaves(final String cmHandle, final String parentNodeXpath, final String jsonData) {
117         cpsDataService.updateNodeLeaves(getDataspaceName(), cmHandle, parentNodeXpath, jsonData);
118     }
119
120     @Override
121     public void replaceNodeTree(final String cmHandle, final String parentNodeXpath, final String jsonData) {
122         cpsDataService.replaceNodeTree(getDataspaceName(), cmHandle, parentNodeXpath, jsonData);
123     }
124
125     @Override
126     public void updateDmiPluginRegistration(final DmiPluginRegistration dmiPluginRegistration) {
127         try {
128             final List<PersistenceCmHandle> persistenceCmHandles =
129                 new ArrayList<>();
130             for (final CmHandle cmHandle: dmiPluginRegistration.getCreatedCmHandles()) {
131                 final var persistenceCmHandle = new PersistenceCmHandle();
132                 persistenceCmHandle.setDmiServiceName(dmiPluginRegistration.getDmiPlugin());
133                 persistenceCmHandle.setId(cmHandle.getCmHandleID());
134                 persistenceCmHandle.setAdditionalProperties(cmHandle.getCmHandleProperties());
135                 persistenceCmHandles.add(persistenceCmHandle);
136             }
137             final var persistenceCmHandlesList = new PersistenceCmHandlesList();
138             persistenceCmHandlesList.setCmHandles(persistenceCmHandles);
139             final String cmHandleJsonData = objectMapper.writeValueAsString(persistenceCmHandlesList);
140             cpsDataService.saveListNodeData(NCMP_DATASPACE_NAME,
141                     NCMP_DMI_REGISTRY_ANCHOR,
142                     "/dmi-registry",
143                 cmHandleJsonData);
144         } catch (final JsonProcessingException e) {
145             throw new DataValidationException(
146                 "Parsing error occurred while processing DMI Plugin Registration" + dmiPluginRegistration, e
147                 .getMessage(), e);
148         }
149     }
150
151     @Override
152     public Object getResourceDataOperationalFoCmHandle(final String cmHandle,
153                                                        final String resourceIdentifier,
154                                                        final String acceptParam,
155                                                        final String fieldsQueryParam,
156                                                        final Integer depthQueryParam) {
157
158         final DataNode dataNode = fetchDataNodeFromDmiRegistryForCmHandle(cmHandle);
159         final String dmiServiceName = String.valueOf(dataNode.getLeaves().get("dmi-service-name"));
160         final Collection<DataNode> additionalPropsList = dataNode.getChildDataNodes();
161         final String jsonBody = prepareOperationBody(GenericRequestBody.OperationEnum.READ, additionalPropsList);
162         final ResponseEntity<Object> response = dmiOperations.getResouceDataFromDmi(dmiServiceName,
163                 cmHandle,
164                 resourceIdentifier,
165                 fieldsQueryParam,
166                 depthQueryParam,
167                 acceptParam,
168                 jsonBody);
169         return handleResponse(response);
170     }
171
172     private DataNode fetchDataNodeFromDmiRegistryForCmHandle(final String cmHandle) {
173         final String xpathForDmiRegistryToFetchCmHandle = "/dmi-registry/cm-handles[@id='" + cmHandle + "']";
174         final var dataNode = cpsDataService.getDataNode(NCMP_DATASPACE_NAME,
175                 NCMP_DMI_REGISTRY_ANCHOR,
176                 xpathForDmiRegistryToFetchCmHandle,
177                 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
178         return dataNode;
179     }
180
181     private String prepareOperationBody(final GenericRequestBody.OperationEnum operation,
182                                         final Collection<DataNode> additionalPropertyList) {
183         final GenericRequestBody requestBody = new GenericRequestBody();
184         final Map<String, String> additionalPropertyMap = getAdditionalPropertiesMap(additionalPropertyList);
185         requestBody.setOperation(GenericRequestBody.OperationEnum.READ);
186         requestBody.setCmHandleProperties(additionalPropertyMap);
187         try {
188             final String requestJson = objectMapper.writeValueAsString(requestBody);
189             return requestJson;
190         } catch (final JsonProcessingException je) {
191             log.error("Parsing error occurred while converting Object to JSON.");
192             throw new NcmpException("Parsing error occurred while converting given object to JSON.",
193                     je.getMessage());
194         }
195     }
196
197     private Map<String, String> getAdditionalPropertiesMap(final Collection<DataNode> additionalPropertyList) {
198         if (additionalPropertyList == null || additionalPropertyList.size() == 0) {
199             return null;
200         }
201         final Map<String, String> additionalPropertyMap = new LinkedHashMap<>();
202         for (final DataNode node: additionalPropertyList) {
203             additionalPropertyMap.put(String.valueOf(node.getLeaves().get("name")),
204                     String.valueOf(node.getLeaves().get("value")));
205         }
206         return additionalPropertyMap;
207     }
208
209     private Object handleResponse(final ResponseEntity<Object> responseEntity) {
210         if (responseEntity.getStatusCode() == HttpStatus.OK) {
211             return responseEntity.getBody();
212         } else {
213             throw new NcmpException("Not able to get resource data.",
214                     "DMI status code: " + responseEntity.getStatusCodeValue()
215                             + ", DMI response body: " + responseEntity.getBody());
216         }
217     }
218
219
220 }