Registration Response for Update and Delete cmhandles operations
[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-2022 Nordix Foundation
5  *  Modifications Copyright (C) 2021 Pantheon.tech
6  *  Modifications Copyright (C) 2021-2022 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 static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DATASPACE_NAME;
27 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DMI_REGISTRY_ANCHOR;
28 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DMI_REGISTRY_PARENT;
29 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME;
30 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NO_TIMESTAMP;
31 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum;
32 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED;
33
34 import com.fasterxml.jackson.core.JsonProcessingException;
35 import java.util.ArrayList;
36 import java.util.Collection;
37 import java.util.HashMap;
38 import java.util.LinkedHashMap;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.UUID;
42 import java.util.regex.Pattern;
43 import java.util.stream.Collectors;
44 import lombok.RequiredArgsConstructor;
45 import lombok.extern.slf4j.Slf4j;
46 import org.onap.cps.api.CpsAdminService;
47 import org.onap.cps.api.CpsDataService;
48 import org.onap.cps.api.CpsModuleService;
49 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
50 import org.onap.cps.ncmp.api.impl.exception.InvalidTopicException;
51 import org.onap.cps.ncmp.api.impl.exception.ServerNcmpException;
52 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations;
53 import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations;
54 import org.onap.cps.ncmp.api.impl.operations.DmiOperations;
55 import org.onap.cps.ncmp.api.impl.operations.YangModelCmHandleRetriever;
56 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
57 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandlesList;
58 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse;
59 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError;
60 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
61 import org.onap.cps.ncmp.api.models.DmiPluginRegistrationResponse;
62 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
63 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
64 import org.onap.cps.spi.exceptions.DataValidationException;
65 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException;
66 import org.onap.cps.spi.model.ModuleReference;
67 import org.onap.cps.utils.JsonObjectMapper;
68 import org.springframework.http.HttpStatus;
69 import org.springframework.http.ResponseEntity;
70 import org.springframework.stereotype.Service;
71
72 @Slf4j
73 @Service
74 @RequiredArgsConstructor
75 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
76
77     private final CpsDataService cpsDataService;
78
79     private final JsonObjectMapper jsonObjectMapper;
80
81     private final DmiDataOperations dmiDataOperations;
82
83     private final DmiModelOperations dmiModelOperations;
84
85     private final CpsModuleService cpsModuleService;
86
87     private final CpsAdminService cpsAdminService;
88
89     private final NetworkCmProxyDataServicePropertyHandler networkCmProxyDataServicePropertyHandler;
90
91     private final YangModelCmHandleRetriever yangModelCmHandleRetriever;
92
93     // valid kafka topic name regex
94     private static final Pattern TOPIC_NAME_PATTERN = Pattern.compile("^[a-zA-Z0-9]([._-](?![._-])|"
95             + "[a-zA-Z0-9]){0,120}[a-zA-Z0-9]$");
96     private static final String NO_REQUEST_ID = null;
97     private static final String NO_TOPIC = null;
98
99     @Override
100     public DmiPluginRegistrationResponse updateDmiRegistrationAndSyncModule(
101         final DmiPluginRegistration dmiPluginRegistration) {
102         dmiPluginRegistration.validateDmiPluginRegistration();
103         final var dmiPluginRegistrationResponse = new DmiPluginRegistrationResponse();
104         try {
105             dmiPluginRegistrationResponse.setRemovedCmHandles(
106                 parseAndRemoveCmHandlesInDmiRegistration(dmiPluginRegistration.getRemovedCmHandles()));
107             if (!dmiPluginRegistration.getCreatedCmHandles().isEmpty()) {
108                 parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration);
109             }
110             if (!dmiPluginRegistration.getUpdatedCmHandles().isEmpty()) {
111                 dmiPluginRegistrationResponse.setUpdatedCmHandles(
112                     networkCmProxyDataServicePropertyHandler
113                         .updateCmHandleProperties(dmiPluginRegistration.getUpdatedCmHandles()));
114             }
115         } catch (final JsonProcessingException | DataNodeNotFoundException e) {
116             final String errorMessage = String.format(
117                     "Error occurred while processing the CM-handle registration request, caused by : [%s]",
118                     e.getMessage());
119             throw new DataValidationException(errorMessage, e.getMessage(), e);
120         }
121         return dmiPluginRegistrationResponse;
122     }
123
124     @Override
125     public Object getResourceDataOperationalForCmHandle(final String cmHandleId,
126                                                         final String resourceIdentifier,
127                                                         final String acceptParamInHeader,
128                                                         final String optionsParamInQuery,
129                                                         final String topicParamInQuery) {
130
131         return validateTopicNameAndGetResourceData(cmHandleId, resourceIdentifier, acceptParamInHeader,
132                 DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL, optionsParamInQuery, topicParamInQuery);
133     }
134
135     @Override
136     public Object getResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
137                                                                final String resourceIdentifier,
138                                                                final String acceptParamInHeader,
139                                                                final String optionsParamInQuery,
140                                                                final String topicParamInQuery) {
141         return validateTopicNameAndGetResourceData(cmHandleId, resourceIdentifier, acceptParamInHeader,
142                 DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING, optionsParamInQuery, topicParamInQuery);
143     }
144
145     @Override
146     public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
147                                                                final String resourceIdentifier,
148                                                                final OperationEnum operation,
149                                                                final String requestData,
150                                                                final String dataType) {
151         return handleResponse(
152             dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(
153                 cmHandleId, resourceIdentifier, operation, requestData, dataType),
154             "Not able to " + operation + " resource data.");
155     }
156
157
158     @Override
159     public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
160         return cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
161     }
162
163     /**
164      * Retrieve cm handle identifiers for the given list of module names.
165      *
166      * @param moduleNames module names.
167      * @return a collection of anchor identifiers
168      */
169     @Override
170     public Collection<String> executeCmHandleHasAllModulesSearch(final Collection<String> moduleNames) {
171         return cpsAdminService.queryAnchorNames(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, moduleNames);
172     }
173
174     /**
175      * Retrieve cm handle details for a given cm handle.
176      *
177      * @param cmHandleId cm handle identifier
178      * @return cm handle details
179      */
180     @Override
181     public NcmpServiceCmHandle getNcmpServiceCmHandle(final String cmHandleId) {
182         final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle();
183         final YangModelCmHandle yangModelCmHandle =
184             yangModelCmHandleRetriever.getDmiServiceNamesAndProperties(cmHandleId);
185         final List<YangModelCmHandle.Property> dmiProperties = yangModelCmHandle.getDmiProperties();
186         final List<YangModelCmHandle.Property> publicProperties = yangModelCmHandle.getPublicProperties();
187         ncmpServiceCmHandle.setCmHandleID(yangModelCmHandle.getId());
188         setDmiProperties(dmiProperties, ncmpServiceCmHandle);
189         setPublicProperties(publicProperties, ncmpServiceCmHandle);
190         return ncmpServiceCmHandle;
191     }
192
193     private void setDmiProperties(final List<YangModelCmHandle.Property> dmiProperties,
194                                   final NcmpServiceCmHandle ncmpServiceCmHandle) {
195         final Map<String, String> dmiPropertiesMap = new LinkedHashMap<>(dmiProperties.size());
196         asPropertiesMap(dmiProperties, dmiPropertiesMap);
197         ncmpServiceCmHandle.setDmiProperties(dmiPropertiesMap);
198     }
199
200     private void setPublicProperties(final List<YangModelCmHandle.Property> publicProperties,
201                                      final NcmpServiceCmHandle ncmpServiceCmHandle) {
202         final Map<String, String> publicPropertiesMap = new LinkedHashMap<>();
203         asPropertiesMap(publicProperties, publicPropertiesMap);
204         ncmpServiceCmHandle.setPublicProperties(publicPropertiesMap);
205     }
206
207     private void asPropertiesMap(final List<YangModelCmHandle.Property> properties,
208                                  final Map<String, String> propertiesMap) {
209         for (final YangModelCmHandle.Property property: properties) {
210             propertiesMap.put(property.getName(), property.getValue());
211         }
212     }
213
214     /**
215      * THis method registers a cm handle and initiates modules sync.
216      *
217      * @param dmiPluginRegistration dmi plugin registration information.
218      * @throws JsonProcessingException thrown if json is malformed or missing.
219      */
220     public void parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(
221         final DmiPluginRegistration dmiPluginRegistration) throws JsonProcessingException {
222         final YangModelCmHandlesList createdYangModelCmHandlesList =
223             getUpdatedYangModelCmHandlesList(dmiPluginRegistration,
224                 dmiPluginRegistration.getCreatedCmHandles());
225         registerAndSyncNewCmHandles(createdYangModelCmHandlesList);
226     }
227
228     private static Object handleResponse(final ResponseEntity<?> responseEntity,
229                                          final String exceptionMessage) {
230         if (responseEntity.getStatusCode().is2xxSuccessful()) {
231             return responseEntity.getBody();
232         } else {
233             throw new ServerNcmpException(exceptionMessage,
234                     "DMI status code: " + responseEntity.getStatusCodeValue()
235                             + ", DMI response body: " + responseEntity.getBody());
236         }
237     }
238
239     private YangModelCmHandlesList getUpdatedYangModelCmHandlesList(
240         final DmiPluginRegistration dmiPluginRegistration,
241         final List<NcmpServiceCmHandle> updatedCmHandles) {
242         return YangModelCmHandlesList.toYangModelCmHandlesList(
243             dmiPluginRegistration.getDmiPlugin(),
244             dmiPluginRegistration.getDmiDataPlugin(),
245             dmiPluginRegistration.getDmiModelPlugin(),
246             updatedCmHandles);
247     }
248
249     private void registerAndSyncNewCmHandles(final YangModelCmHandlesList yangModelCmHandlesList) {
250         final String cmHandleJsonData = jsonObjectMapper.asJsonString(yangModelCmHandlesList);
251         cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT,
252                 cmHandleJsonData, NO_TIMESTAMP);
253
254         for (final YangModelCmHandle yangModelCmHandle : yangModelCmHandlesList.getYangModelCmHandles()) {
255             syncModulesAndCreateAnchor(yangModelCmHandle);
256         }
257     }
258
259     protected void syncModulesAndCreateAnchor(final YangModelCmHandle yangModelCmHandle) {
260         syncAndCreateSchemaSet(yangModelCmHandle);
261         createAnchor(yangModelCmHandle);
262     }
263
264     private List<CmHandleRegistrationResponse> parseAndRemoveCmHandlesInDmiRegistration(
265         final List<String> tobeRemovedCmHandles) {
266         final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
267             new ArrayList<>(tobeRemovedCmHandles.size());
268         for (final String cmHandle : tobeRemovedCmHandles) {
269             try {
270                 deleteSchemaSetWithCascade(cmHandle);
271                 cpsDataService.deleteListOrListElement(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
272                     "/dmi-registry/cm-handles[@id='" + cmHandle + "']", NO_TIMESTAMP);
273                 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandle));
274             } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
275                 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
276                     cmHandle, dataNodeNotFoundException.getMessage());
277                 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse
278                     .createFailureResponse(cmHandle, RegistrationError.CM_HANDLE_DOES_NOT_EXIST));
279             } catch (final Exception exception) {
280                 log.error("Unable to de-register cm-handleIdd : {} , caused by : {}",
281                     cmHandle, exception.getMessage());
282                 cmHandleRegistrationResponses.add(
283                     CmHandleRegistrationResponse.createFailureResponse(cmHandle, exception));
284             }
285         }
286         return cmHandleRegistrationResponses;
287     }
288
289     private void deleteSchemaSetWithCascade(final String schemaSetName) {
290         try {
291             cpsModuleService.deleteSchemaSet(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetName,
292                 CASCADE_DELETE_ALLOWED);
293         } catch (final SchemaSetNotFoundException schemaSetNotFoundException) {
294             log.warn("Schema set {} does not exist or already deleted", schemaSetName);
295         }
296     }
297
298     private void syncAndCreateSchemaSet(final YangModelCmHandle yangModelCmHandle) {
299         final Collection<ModuleReference> moduleReferencesFromCmHandle =
300             dmiModelOperations.getModuleReferences(yangModelCmHandle);
301
302         final Collection<ModuleReference> identifiedNewModuleReferencesFromCmHandle = cpsModuleService
303             .identifyNewModuleReferences(moduleReferencesFromCmHandle);
304
305         final Collection<ModuleReference> existingModuleReferencesFromCmHandle =
306             moduleReferencesFromCmHandle.stream().filter(moduleReferenceFromCmHandle ->
307                 !identifiedNewModuleReferencesFromCmHandle.contains(moduleReferenceFromCmHandle)
308             ).collect(Collectors.toList());
309
310         final Map<String, String> newModuleNameToContentMap;
311         if (identifiedNewModuleReferencesFromCmHandle.isEmpty()) {
312             newModuleNameToContentMap = new HashMap<>();
313         } else {
314             newModuleNameToContentMap = dmiModelOperations.getNewYangResourcesFromDmi(yangModelCmHandle,
315                 identifiedNewModuleReferencesFromCmHandle);
316         }
317         cpsModuleService
318             .createSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, yangModelCmHandle.getId(),
319                 newModuleNameToContentMap, existingModuleReferencesFromCmHandle);
320     }
321
322     private void createAnchor(final YangModelCmHandle yangModelCmHandle) {
323         cpsAdminService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, yangModelCmHandle.getId(),
324             yangModelCmHandle.getId());
325     }
326
327     private static boolean hasTopicParameter(final String topicName) {
328         if (topicName == null) {
329             return false;
330         }
331         if (TOPIC_NAME_PATTERN.matcher(topicName).matches()) {
332             return true;
333         }
334         throw new InvalidTopicException("Topic name " + topicName + " is invalid", "invalid topic");
335     }
336
337     private Map<String, Object> buildDmiResponse(final String requestId) {
338         final Map<String, Object> dmiResponseMap = new HashMap<>();
339         dmiResponseMap.put("requestId", requestId);
340         return dmiResponseMap;
341     }
342
343     private Object validateTopicNameAndGetResourceData(final String cmHandleId,
344                                                        final String resourceIdentifier,
345                                                        final String acceptParamInHeader,
346                                                        final DmiOperations.DataStoreEnum dataStore,
347                                                        final String optionsParamInQuery,
348                                                        final String topicParamInQuery) {
349         final boolean processAsynchronously = hasTopicParameter(topicParamInQuery);
350         if (processAsynchronously) {
351             final String resourceDataRequestId = UUID.randomUUID().toString();
352             return ResponseEntity.status(HttpStatus.OK)
353                     .body(buildDmiResponse(resourceDataRequestId));
354         }
355         final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(
356                 cmHandleId, resourceIdentifier, optionsParamInQuery, acceptParamInHeader,
357                 dataStore, NO_REQUEST_ID, NO_TOPIC);
358         return handleResponse(responseEntity, "Not able to get resource data.");
359     }
360 }