Add optional observed timestamp in the cps data api
[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  *  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
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  *  SPDX-License-Identifier: Apache-2.0
21  *  ============LICENSE_END=========================================================
22  */
23
24 package org.onap.cps.ncmp.api.impl;
25
26 import com.fasterxml.jackson.core.JsonProcessingException;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import java.time.OffsetDateTime;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.LinkedHashMap;
32 import java.util.LinkedList;
33 import java.util.List;
34 import java.util.Map;
35 import javax.validation.constraints.NotNull;
36 import lombok.extern.slf4j.Slf4j;
37 import org.onap.cps.api.CpsDataService;
38 import org.onap.cps.api.CpsQueryService;
39 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
40 import org.onap.cps.ncmp.api.impl.exception.NcmpException;
41 import org.onap.cps.ncmp.api.impl.operation.DmiOperations;
42 import org.onap.cps.ncmp.api.models.CmHandle;
43 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
44 import org.onap.cps.ncmp.api.models.GenericRequestBody;
45 import org.onap.cps.ncmp.api.models.PersistenceCmHandle;
46 import org.onap.cps.ncmp.api.models.PersistenceCmHandlesList;
47 import org.onap.cps.spi.FetchDescendantsOption;
48 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
49 import org.onap.cps.spi.exceptions.DataValidationException;
50 import org.onap.cps.spi.model.DataNode;
51 import org.springframework.http.HttpStatus;
52 import org.springframework.http.ResponseEntity;
53 import org.springframework.stereotype.Service;
54 import org.springframework.util.StringUtils;
55
56
57 @Slf4j
58 @Service
59 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
60
61     private static final String NF_PROXY_DATASPACE_NAME = "NFP-Operational";
62
63     private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
64
65     private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
66
67     private static final OffsetDateTime NO_TIMESTAMP = null;
68
69     private CpsDataService cpsDataService;
70
71     private ObjectMapper objectMapper;
72
73     private CpsQueryService cpsQueryService;
74
75     private DmiOperations dmiOperations;
76
77     /**
78      * Constructor Injection for Dependencies.
79      *
80      * @param dmiOperations   dmi operation
81      * @param cpsDataService  Data Service Interface
82      * @param cpsQueryService Query Service Interface
83      * @param objectMapper    Object Mapper
84      */
85     public NetworkCmProxyDataServiceImpl(final DmiOperations dmiOperations, final CpsDataService cpsDataService,
86         final CpsQueryService cpsQueryService, final ObjectMapper objectMapper) {
87         this.dmiOperations = dmiOperations;
88         this.cpsDataService = cpsDataService;
89         this.cpsQueryService = cpsQueryService;
90         this.objectMapper = objectMapper;
91     }
92
93     private String getDataspaceName() {
94         return NF_PROXY_DATASPACE_NAME;
95     }
96
97     @Override
98     public DataNode getDataNode(final String cmHandle, final String xpath,
99         final FetchDescendantsOption fetchDescendantsOption) {
100         return cpsDataService.getDataNode(getDataspaceName(), cmHandle, xpath, fetchDescendantsOption);
101     }
102
103     @Override
104     public Collection<DataNode> queryDataNodes(final String cmHandle, final String cpsPath,
105         final FetchDescendantsOption fetchDescendantsOption) {
106         return cpsQueryService.queryDataNodes(getDataspaceName(), cmHandle, cpsPath, fetchDescendantsOption);
107     }
108
109     @Override
110     public void createDataNode(final String cmHandle, final String parentNodeXpath, final String jsonData) {
111         if (!StringUtils.hasText(parentNodeXpath) || "/".equals(parentNodeXpath)) {
112             cpsDataService.saveData(getDataspaceName(), cmHandle, jsonData, NO_TIMESTAMP);
113         } else {
114             cpsDataService.saveData(getDataspaceName(), cmHandle, parentNodeXpath, jsonData, NO_TIMESTAMP);
115         }
116     }
117
118     @Override
119     public void addListNodeElements(final String cmHandle, final String parentNodeXpath, final String jsonData) {
120         cpsDataService.saveListNodeData(getDataspaceName(), cmHandle, parentNodeXpath, jsonData, NO_TIMESTAMP);
121     }
122
123     @Override
124     public void updateNodeLeaves(final String cmHandle, final String parentNodeXpath, final String jsonData) {
125         cpsDataService.updateNodeLeaves(getDataspaceName(), cmHandle, parentNodeXpath, jsonData, NO_TIMESTAMP);
126     }
127
128     @Override
129     public void replaceNodeTree(final String cmHandle, final String parentNodeXpath, final String jsonData) {
130         cpsDataService.replaceNodeTree(getDataspaceName(), cmHandle, parentNodeXpath, jsonData, NO_TIMESTAMP);
131     }
132
133     @Override
134     public void updateDmiPluginRegistration(final DmiPluginRegistration dmiPluginRegistration) {
135         if (dmiPluginRegistration.getCreatedCmHandles() != null) {
136             parseAndCreateCmHandlesInDmiRegistration(dmiPluginRegistration);
137         }
138         if (dmiPluginRegistration.getUpdatedCmHandles() != null) {
139             parseAndUpdateCmHandlesInDmiRegistration(dmiPluginRegistration);
140         }
141         if (dmiPluginRegistration.getRemovedCmHandles() != null) {
142             parseAndRemoveCmHandlesInDmiRegistration(dmiPluginRegistration);
143         }
144     }
145
146     private void parseAndCreateCmHandlesInDmiRegistration(final DmiPluginRegistration dmiPluginRegistration) {
147         try {
148             final List<PersistenceCmHandle> createdPersistenceCmHandles =
149                 new LinkedList<>();
150             for (final CmHandle cmHandle : dmiPluginRegistration.getCreatedCmHandles()) {
151                 createdPersistenceCmHandles.add(toPersistenceCmHandle(dmiPluginRegistration, cmHandle));
152             }
153             final PersistenceCmHandlesList persistenceCmHandlesList = new PersistenceCmHandlesList();
154             persistenceCmHandlesList.setCmHandles(createdPersistenceCmHandles);
155             final String cmHandleJsonData = objectMapper.writeValueAsString(persistenceCmHandlesList);
156             cpsDataService.saveListNodeData(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, "/dmi-registry",
157                 cmHandleJsonData, NO_TIMESTAMP);
158         } catch (final JsonProcessingException e) {
159             log.error("Parsing error occurred while converting Object to JSON for Dmi Registry.");
160             throw new DataValidationException(
161                 "Parsing error occurred while processing DMI Plugin Registration" + dmiPluginRegistration, e
162                 .getMessage(), e);
163         }
164     }
165
166     private void parseAndUpdateCmHandlesInDmiRegistration(final DmiPluginRegistration dmiPluginRegistration) {
167         try {
168             final List<PersistenceCmHandle> updatedPersistenceCmHandles =
169                 new LinkedList<>();
170             for (final CmHandle cmHandle : dmiPluginRegistration.getUpdatedCmHandles()) {
171                 updatedPersistenceCmHandles.add(toPersistenceCmHandle(dmiPluginRegistration, cmHandle));
172             }
173             final PersistenceCmHandlesList persistenceCmHandlesList = new PersistenceCmHandlesList();
174             persistenceCmHandlesList.setCmHandles(updatedPersistenceCmHandles);
175             final String cmHandlesJsonData = objectMapper.writeValueAsString(persistenceCmHandlesList);
176             cpsDataService.updateNodeLeavesAndExistingDescendantLeaves(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
177                 "/dmi-registry", cmHandlesJsonData, NO_TIMESTAMP);
178         } catch (final JsonProcessingException e) {
179             log.error("Parsing error occurred while converting Object to JSON Dmi Registry.");
180             throw new DataValidationException(
181                 "Parsing error occurred while processing DMI Plugin Registration" + dmiPluginRegistration, e
182                 .getMessage(), e);
183         }
184     }
185
186     private void parseAndRemoveCmHandlesInDmiRegistration(final DmiPluginRegistration dmiPluginRegistration) {
187         for (final String cmHandle: dmiPluginRegistration.getRemovedCmHandles()) {
188             try {
189                 cpsDataService.deleteListNodeData(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
190                     "/dmi-registry/cm-handles[@id='" + cmHandle + "']", NO_TIMESTAMP);
191             } catch (final DataNodeNotFoundException e) {
192                 log.warn("Datanode {} not deleted message {}", cmHandle, e.getMessage());
193             }
194         }
195     }
196
197     @Override
198     public Object getResourceDataOperationalForCmHandle(final @NotNull String cmHandle,
199         final @NotNull String resourceIdentifier,
200         final String acceptParam,
201         final String fieldsQueryParam,
202         final Integer depthQueryParam) {
203
204         final var dataNode = fetchDataNodeFromDmiRegistryForCmHandle(cmHandle);
205         final var dmiServiceName = String.valueOf(dataNode.getLeaves().get("dmi-service-name"));
206         final Collection<DataNode> additionalPropsList = dataNode.getChildDataNodes();
207         final var jsonBody = prepareOperationBody(GenericRequestBody.OperationEnum.READ, additionalPropsList);
208         final ResponseEntity<Object> response = dmiOperations.getResouceDataOperationalFromDmi(dmiServiceName,
209             cmHandle,
210             resourceIdentifier,
211             fieldsQueryParam,
212             depthQueryParam,
213             acceptParam,
214             jsonBody);
215         return handleResponse(response);
216     }
217
218     @Override
219     public Object getResourceDataPassThroughRunningForCmHandle(final @NotNull String cmHandle,
220         final @NotNull String resourceIdentifier,
221         final String accept,
222         final String fields,
223         final Integer depth) {
224         final var cmHandleDataNode = fetchDataNodeFromDmiRegistryForCmHandle(cmHandle);
225         final var dmiServiceName = String.valueOf(cmHandleDataNode.getLeaves().get("dmi-service-name"));
226         final Collection<DataNode> additionalPropsList = cmHandleDataNode.getChildDataNodes();
227         final var dmiRequesBody = prepareOperationBody(GenericRequestBody.OperationEnum.READ, additionalPropsList);
228         final ResponseEntity<Object> response = dmiOperations.getResouceDataPassThroughRunningFromDmi(dmiServiceName,
229             cmHandle,
230             resourceIdentifier,
231             fields,
232             depth,
233             accept,
234             dmiRequesBody);
235         return handleResponse(response);
236     }
237
238     private DataNode fetchDataNodeFromDmiRegistryForCmHandle(final String cmHandle) {
239         final String xpathForDmiRegistryToFetchCmHandle = "/dmi-registry/cm-handles[@id='" + cmHandle + "']";
240         final var dataNode = cpsDataService.getDataNode(NCMP_DATASPACE_NAME,
241             NCMP_DMI_REGISTRY_ANCHOR,
242             xpathForDmiRegistryToFetchCmHandle,
243             FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
244         return dataNode;
245     }
246
247     private String prepareOperationBody(final GenericRequestBody.OperationEnum operation,
248         final Collection<DataNode> additionalPropertyList) {
249         final var requestBody = new GenericRequestBody();
250         final Map<String, String> additionalPropertyMap = getAdditionalPropertiesMap(additionalPropertyList);
251         requestBody.setOperation(GenericRequestBody.OperationEnum.READ);
252         requestBody.setCmHandleProperties(additionalPropertyMap);
253         try {
254             final var requestJson = objectMapper.writeValueAsString(requestBody);
255             return requestJson;
256         } catch (final JsonProcessingException je) {
257             log.error("Parsing error occurred while converting Object to JSON.");
258             throw new NcmpException("Parsing error occurred while converting given object to JSON.",
259                 je.getMessage());
260         }
261     }
262
263     private Map<String, String> getAdditionalPropertiesMap(final Collection<DataNode> additionalPropertyList) {
264         if (additionalPropertyList == null || additionalPropertyList.size() == 0) {
265             return null;
266         }
267         final Map<String, String> additionalPropertyMap = new LinkedHashMap<>();
268         for (final var node : additionalPropertyList) {
269             additionalPropertyMap.put(String.valueOf(node.getLeaves().get("name")),
270                 String.valueOf(node.getLeaves().get("value")));
271         }
272         return additionalPropertyMap;
273     }
274
275     private Object handleResponse(final ResponseEntity<Object> responseEntity) {
276         if (responseEntity.getStatusCode() == HttpStatus.OK) {
277             return responseEntity.getBody();
278         } else {
279             throw new NcmpException("Not able to get resource data.",
280                 "DMI status code: " + responseEntity.getStatusCodeValue()
281                     + ", DMI response body: " + responseEntity.getBody());
282         }
283     }
284
285     private PersistenceCmHandle toPersistenceCmHandle(final DmiPluginRegistration dmiPluginRegistration,
286         final CmHandle cmHandle) {
287         final PersistenceCmHandle persistenceCmHandle = new PersistenceCmHandle();
288         persistenceCmHandle.setDmiServiceName(dmiPluginRegistration.getDmiPlugin());
289         persistenceCmHandle.setId(cmHandle.getCmHandleID());
290         if (cmHandle.getCmHandleProperties() == null) {
291             persistenceCmHandle.setAdditionalProperties(Collections.EMPTY_MAP);
292         } else {
293             persistenceCmHandle.setAdditionalProperties(cmHandle.getCmHandleProperties());
294         }
295         return persistenceCmHandle;
296     }
297
298 }