2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 highstreet technologies GmbH
4 * Modifications Copyright (C) 2021-2023 Nordix Foundation
5 * Modifications Copyright (C) 2021 Pantheon.tech
6 * Modifications Copyright (C) 2021-2022 Bell Canada
7 * Modifications Copyright (C) 2023 TechMahindra Ltd.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * SPDX-License-Identifier: Apache-2.0
22 * ============LICENSE_END=========================================================
25 package org.onap.cps.ncmp.api.impl;
27 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME;
28 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum;
29 import static org.onap.cps.ncmp.api.impl.utils.RestQueryParametersValidator.validateCmHandleQueryParameters;
31 import com.google.common.collect.Lists;
32 import com.hazelcast.map.IMap;
33 import java.time.OffsetDateTime;
34 import java.util.ArrayList;
35 import java.util.Collection;
36 import java.util.HashMap;
37 import java.util.HashSet;
38 import java.util.List;
41 import java.util.stream.Collectors;
42 import lombok.RequiredArgsConstructor;
43 import lombok.extern.slf4j.Slf4j;
44 import org.onap.cps.api.CpsDataService;
45 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandlerQueryService;
46 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
47 import org.onap.cps.ncmp.api.impl.event.lcm.LcmEventsCmHandleStateHandler;
48 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations;
49 import org.onap.cps.ncmp.api.impl.operations.DmiOperations;
50 import org.onap.cps.ncmp.api.impl.utils.CmHandleQueryConditions;
51 import org.onap.cps.ncmp.api.impl.utils.InventoryQueryConditions;
52 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
53 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
54 import org.onap.cps.ncmp.api.inventory.CmHandleQueries;
55 import org.onap.cps.ncmp.api.inventory.CmHandleState;
56 import org.onap.cps.ncmp.api.inventory.CompositeState;
57 import org.onap.cps.ncmp.api.inventory.CompositeStateUtils;
58 import org.onap.cps.ncmp.api.inventory.DataStoreSyncState;
59 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
60 import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters;
61 import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters;
62 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse;
63 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError;
64 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
65 import org.onap.cps.ncmp.api.models.DmiPluginRegistrationResponse;
66 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
67 import org.onap.cps.spi.FetchDescendantsOption;
68 import org.onap.cps.spi.exceptions.AlreadyDefinedExceptionBatch;
69 import org.onap.cps.spi.exceptions.CpsException;
70 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
71 import org.onap.cps.spi.exceptions.DataValidationException;
72 import org.onap.cps.spi.model.ModuleDefinition;
73 import org.onap.cps.spi.model.ModuleReference;
74 import org.onap.cps.utils.JsonObjectMapper;
75 import org.springframework.http.ResponseEntity;
76 import org.springframework.stereotype.Service;
80 @RequiredArgsConstructor
81 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
83 private static final int DELETE_BATCH_SIZE = 100;
84 private final JsonObjectMapper jsonObjectMapper;
85 private final DmiDataOperations dmiDataOperations;
86 private final NetworkCmProxyDataServicePropertyHandler networkCmProxyDataServicePropertyHandler;
87 private final InventoryPersistence inventoryPersistence;
88 private final CmHandleQueries cmHandleQueries;
89 private final NetworkCmProxyCmHandlerQueryService networkCmProxyCmHandlerQueryService;
90 private final LcmEventsCmHandleStateHandler lcmEventsCmHandleStateHandler;
91 private final CpsDataService cpsDataService;
92 private final IMap<String, Object> moduleSyncStartedOnCmHandles;
95 public DmiPluginRegistrationResponse updateDmiRegistrationAndSyncModule(
96 final DmiPluginRegistration dmiPluginRegistration) {
97 dmiPluginRegistration.validateDmiPluginRegistration();
98 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse = new DmiPluginRegistrationResponse();
100 if (!dmiPluginRegistration.getRemovedCmHandles().isEmpty()) {
101 dmiPluginRegistrationResponse.setRemovedCmHandles(
102 parseAndRemoveCmHandlesInDmiRegistration(dmiPluginRegistration.getRemovedCmHandles()));
105 if (!dmiPluginRegistration.getCreatedCmHandles().isEmpty()) {
106 dmiPluginRegistrationResponse.setCreatedCmHandles(
107 parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration));
109 if (!dmiPluginRegistration.getUpdatedCmHandles().isEmpty()) {
110 dmiPluginRegistrationResponse.setUpdatedCmHandles(
111 networkCmProxyDataServicePropertyHandler
112 .updateCmHandleProperties(dmiPluginRegistration.getUpdatedCmHandles()));
114 return dmiPluginRegistrationResponse;
118 public Object getResourceDataOperationalForCmHandle(final String cmHandleId,
119 final String resourceIdentifier,
120 final String optionsParamInQuery,
121 final String topicParamInQuery,
122 final String requestId) {
123 final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(cmHandleId,
126 DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL,
127 requestId, topicParamInQuery);
128 return responseEntity.getBody();
132 public Object getResourceDataOperational(final String cmHandleId,
133 final String resourceIdentifier,
134 final FetchDescendantsOption fetchDescendantsOption) {
135 return cpsDataService.getDataNodes(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId, resourceIdentifier,
136 fetchDescendantsOption).iterator().next();
140 public Object getResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
141 final String resourceIdentifier,
142 final String optionsParamInQuery,
143 final String topicParamInQuery,
144 final String requestId) {
145 final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(cmHandleId,
148 DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING,
149 requestId, topicParamInQuery);
150 return responseEntity.getBody();
154 public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
155 final String resourceIdentifier,
156 final OperationEnum operation,
157 final String requestData,
158 final String dataType) {
159 return dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier, operation,
160 requestData, dataType);
164 public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
165 return inventoryPersistence.getYangResourcesModuleReferences(cmHandleId);
169 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
170 return inventoryPersistence.getModuleDefinitionsByCmHandleId(cmHandleId);
174 * Retrieve cm handles with details for the given query parameters.
176 * @param cmHandleQueryApiParameters cm handle query parameters
177 * @return cm handles with details
180 public Set<NcmpServiceCmHandle> executeCmHandleSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
181 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
182 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
183 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
184 return networkCmProxyCmHandlerQueryService.queryCmHandles(cmHandleQueryServiceParameters);
188 * Retrieve cm handle ids for the given query parameters.
190 * @param cmHandleQueryApiParameters cm handle query parameters
191 * @return cm handle ids
194 public Set<String> executeCmHandleIdSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
195 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
196 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
197 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
198 return networkCmProxyCmHandlerQueryService.queryCmHandleIds(cmHandleQueryServiceParameters);
202 * Set the data sync enabled flag, along with the data sync state
203 * based on the data sync enabled boolean for the cm handle id provided.
205 * @param cmHandleId cm handle id
206 * @param dataSyncEnabled data sync enabled flag
209 public void setDataSyncEnabled(final String cmHandleId, final boolean dataSyncEnabled) {
210 final CompositeState compositeState = inventoryPersistence
211 .getCmHandleState(cmHandleId);
212 if (compositeState.getDataSyncEnabled().equals(dataSyncEnabled)) {
213 log.info("Data-Sync Enabled flag is already: {} ", dataSyncEnabled);
214 } else if (compositeState.getCmHandleState() != CmHandleState.READY) {
215 throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: "
216 + compositeState.getCmHandleState());
218 final DataStoreSyncState dataStoreSyncState = compositeState.getDataStores()
219 .getOperationalDataStore().getDataStoreSyncState();
220 if (!dataSyncEnabled && dataStoreSyncState == DataStoreSyncState.SYNCHRONIZED) {
221 cpsDataService.deleteDataNode(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId,
222 "/netconf-state", OffsetDateTime.now());
224 CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabled, compositeState);
225 inventoryPersistence.saveCmHandleState(cmHandleId,
231 * Get all cm handle IDs by DMI plugin identifier.
233 * @param dmiPluginIdentifier DMI plugin identifier
234 * @return set of cm handle IDs
237 public Set<String> getAllCmHandleIdsByDmiPluginIdentifier(final String dmiPluginIdentifier) {
238 final Set<NcmpServiceCmHandle> ncmpServiceCmHandles =
239 cmHandleQueries.getCmHandlesByDmiPluginIdentifier(dmiPluginIdentifier);
240 final Set<String> cmHandleIds = new HashSet<>(ncmpServiceCmHandles.size());
241 ncmpServiceCmHandles.forEach(cmHandle -> cmHandleIds.add(cmHandle.getCmHandleId()));
246 * Get all cm handle IDs by various properties.
248 * @param cmHandleQueryServiceParameters cm handle query parameters
249 * @return set of cm handle IDs
252 public Set<String> executeCmHandleIdSearchForInventory(
253 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
254 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, InventoryQueryConditions.ALL_CONDITION_NAMES);
255 return networkCmProxyCmHandlerQueryService.queryCmHandleIdsForInventory(cmHandleQueryServiceParameters);
259 * Retrieve cm handle details for a given cm handle.
261 * @param cmHandleId cm handle identifier
262 * @return cm handle details
265 public NcmpServiceCmHandle getNcmpServiceCmHandle(final String cmHandleId) {
266 return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(
267 inventoryPersistence.getYangModelCmHandle(cmHandleId));
271 * Get cm handle public properties for a given cm handle id.
273 * @param cmHandleId cm handle identifier
274 * @return cm handle public properties
277 public Map<String, String> getCmHandlePublicProperties(final String cmHandleId) {
278 final YangModelCmHandle yangModelCmHandle =
279 inventoryPersistence.getYangModelCmHandle(cmHandleId);
280 final List<YangModelCmHandle.Property> yangModelPublicProperties = yangModelCmHandle.getPublicProperties();
281 final Map<String, String> cmHandlePublicProperties = new HashMap<>();
282 YangDataConverter.asPropertiesMap(yangModelPublicProperties, cmHandlePublicProperties);
283 return cmHandlePublicProperties;
287 * Get cm handle composite state for a given cm handle id.
289 * @param cmHandleId cm handle identifier
290 * @return cm handle state
293 public CompositeState getCmHandleCompositeState(final String cmHandleId) {
294 return inventoryPersistence.getYangModelCmHandle(cmHandleId).getCompositeState();
298 * THis method registers a cm handle and initiates modules sync.
300 * @param dmiPluginRegistration dmi plugin registration information.
301 * @return cm-handle registration response for create cm-handle requests.
303 public List<CmHandleRegistrationResponse> parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(
304 final DmiPluginRegistration dmiPluginRegistration) {
305 List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>();
306 final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>();
308 dmiPluginRegistration.getCreatedCmHandles()
309 .forEach(cmHandle -> {
310 final YangModelCmHandle yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle(
311 dmiPluginRegistration.getDmiPlugin(),
312 dmiPluginRegistration.getDmiDataPlugin(),
313 dmiPluginRegistration.getDmiModelPlugin(),
315 cmHandleStatePerCmHandle.put(yangModelCmHandle, CmHandleState.ADVISED);
317 cmHandleRegistrationResponses = registerNewCmHandles(cmHandleStatePerCmHandle);
318 } catch (final DataValidationException dataValidationException) {
319 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createFailureResponse(dmiPluginRegistration
320 .getCreatedCmHandles().stream()
321 .map(NcmpServiceCmHandle::getCmHandleId).findFirst().orElse(null),
322 RegistrationError.CM_HANDLE_INVALID_ID));
324 return cmHandleRegistrationResponses;
327 protected List<CmHandleRegistrationResponse> parseAndRemoveCmHandlesInDmiRegistration(
328 final List<String> tobeRemovedCmHandles) {
329 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
330 new ArrayList<>(tobeRemovedCmHandles.size());
331 final Collection<YangModelCmHandle> yangModelCmHandles =
332 inventoryPersistence.getYangModelCmHandles(tobeRemovedCmHandles);
334 updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETING);
336 final Set<String> notDeletedCmHandles = new HashSet<>();
337 for (final List<String> tobeRemovedCmHandleBatch : Lists.partition(tobeRemovedCmHandles, DELETE_BATCH_SIZE)) {
339 batchDeleteCmHandlesFromDbAndModuleSyncMap(tobeRemovedCmHandleBatch);
340 tobeRemovedCmHandleBatch.forEach(cmHandleId ->
341 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId)));
343 } catch (final RuntimeException batchException) {
344 log.error("Unable to de-register cm-handle batch, retrying on each cm handle");
345 for (final String cmHandleId : tobeRemovedCmHandleBatch) {
346 final CmHandleRegistrationResponse cmHandleRegistrationResponse =
347 deleteCmHandleAndGetCmHandleRegistrationResponse(cmHandleId);
348 cmHandleRegistrationResponses.add(cmHandleRegistrationResponse);
349 if (cmHandleRegistrationResponse.getStatus() != CmHandleRegistrationResponse.Status.SUCCESS) {
350 notDeletedCmHandles.add(cmHandleId);
356 yangModelCmHandles.removeIf(yangModelCmHandle -> notDeletedCmHandles.contains(yangModelCmHandle.getId()));
357 updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETED);
359 return cmHandleRegistrationResponses;
362 private CmHandleRegistrationResponse deleteCmHandleAndGetCmHandleRegistrationResponse(final String cmHandleId) {
364 deleteCmHandleFromDbAndModuleSyncMap(cmHandleId);
365 return CmHandleRegistrationResponse.createSuccessResponse(cmHandleId);
366 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
367 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
368 cmHandleId, dataNodeNotFoundException.getMessage());
369 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId,
370 RegistrationError.CM_HANDLE_DOES_NOT_EXIST);
371 } catch (final DataValidationException dataValidationException) {
372 log.error("Unable to de-register cm-handle id: {}, caused by: {}",
373 cmHandleId, dataValidationException.getMessage());
374 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId,
375 RegistrationError.CM_HANDLE_INVALID_ID);
376 } catch (final Exception exception) {
377 log.error("Unable to de-register cm-handle id : {} , caused by : {}", cmHandleId, exception.getMessage());
378 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception);
382 private void updateCmHandleStateBatch(final Collection<YangModelCmHandle> yangModelCmHandles,
383 final CmHandleState cmHandleState) {
384 final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>(yangModelCmHandles.size());
385 yangModelCmHandles.forEach(yangModelCmHandle -> cmHandleStatePerCmHandle.put(yangModelCmHandle, cmHandleState));
386 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
389 private void deleteCmHandleFromDbAndModuleSyncMap(final String cmHandleId) {
390 inventoryPersistence.deleteSchemaSetWithCascade(cmHandleId);
391 inventoryPersistence.deleteDataNode("/dmi-registry/cm-handles[@id='" + cmHandleId + "']");
392 removeDeletedCmHandleFromModuleSyncMap(cmHandleId);
395 private void batchDeleteCmHandlesFromDbAndModuleSyncMap(final Collection<String> tobeRemovedCmHandles) {
396 inventoryPersistence.deleteSchemaSetsWithCascade(tobeRemovedCmHandles);
397 inventoryPersistence.deleteDataNodes(mapCmHandleIdsToXpaths(tobeRemovedCmHandles));
398 tobeRemovedCmHandles.forEach(this::removeDeletedCmHandleFromModuleSyncMap);
401 private Collection<String> mapCmHandleIdsToXpaths(final Collection<String> cmHandles) {
402 return cmHandles.stream()
403 .map(cmHandleId -> "/dmi-registry/cm-handles[@id='" + cmHandleId + "']")
404 .collect(Collectors.toSet());
407 // CPS-1239 Robustness cleaning of in progress cache
408 private void removeDeletedCmHandleFromModuleSyncMap(final String deletedCmHandleId) {
409 if (moduleSyncStartedOnCmHandles.remove(deletedCmHandleId) != null) {
410 log.debug("{} removed from in progress map", deletedCmHandleId);
414 private List<CmHandleRegistrationResponse> registerNewCmHandles(final Map<YangModelCmHandle, CmHandleState>
415 cmHandleStatePerCmHandle) {
416 final List<String> cmHandleIds = cmHandleStatePerCmHandle.keySet().stream().map(YangModelCmHandle::getId)
417 .collect(Collectors.toList());
419 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
420 return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
421 } catch (final AlreadyDefinedExceptionBatch alreadyDefinedExceptionBatch) {
422 return CmHandleRegistrationResponse.createFailureResponses(
423 alreadyDefinedExceptionBatch.getAlreadyDefinedXpaths(),
424 RegistrationError.CM_HANDLE_ALREADY_EXIST);
425 } catch (final Exception exception) {
426 return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, exception);