2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 highstreet technologies GmbH
4 * Modifications Copyright (C) 2021-2024 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.NcmpResponseStatus.CM_HANDLES_NOT_FOUND;
28 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLES_NOT_READY;
29 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLE_ALREADY_EXIST;
30 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLE_INVALID_ID;
31 import static org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory.MODULE_UPGRADE;
32 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DMI_REGISTRY_PARENT;
33 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME;
34 import static org.onap.cps.ncmp.api.impl.utils.RestQueryParametersValidator.validateCmHandleQueryParameters;
36 import com.google.common.collect.Lists;
37 import com.hazelcast.map.IMap;
38 import java.text.MessageFormat;
39 import java.time.OffsetDateTime;
40 import java.util.ArrayList;
41 import java.util.Collection;
42 import java.util.EnumMap;
43 import java.util.HashMap;
44 import java.util.HashSet;
45 import java.util.List;
48 import java.util.stream.Collectors;
49 import lombok.RequiredArgsConstructor;
50 import lombok.extern.slf4j.Slf4j;
51 import org.onap.cps.api.CpsDataService;
52 import org.onap.cps.ncmp.api.NcmpResponseStatus;
53 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandleQueryService;
54 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
55 import org.onap.cps.ncmp.api.impl.events.lcm.LcmEventsCmHandleStateHandler;
56 import org.onap.cps.ncmp.api.impl.inventory.CmHandleQueries;
57 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState;
58 import org.onap.cps.ncmp.api.impl.inventory.CompositeState;
59 import org.onap.cps.ncmp.api.impl.inventory.CompositeStateBuilder;
60 import org.onap.cps.ncmp.api.impl.inventory.CompositeStateUtils;
61 import org.onap.cps.ncmp.api.impl.inventory.DataStoreSyncState;
62 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence;
63 import org.onap.cps.ncmp.api.impl.inventory.sync.ModuleOperationsUtils;
64 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations;
65 import org.onap.cps.ncmp.api.impl.operations.OperationType;
66 import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevel;
67 import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevelManager;
68 import org.onap.cps.ncmp.api.impl.utils.CmHandleQueryConditions;
69 import org.onap.cps.ncmp.api.impl.utils.InventoryQueryConditions;
70 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
71 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
72 import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters;
73 import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters;
74 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse;
75 import org.onap.cps.ncmp.api.models.DataOperationRequest;
76 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
77 import org.onap.cps.ncmp.api.models.DmiPluginRegistrationResponse;
78 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
79 import org.onap.cps.spi.FetchDescendantsOption;
80 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
81 import org.onap.cps.spi.exceptions.CpsException;
82 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
83 import org.onap.cps.spi.exceptions.DataValidationException;
84 import org.onap.cps.spi.model.ModuleDefinition;
85 import org.onap.cps.spi.model.ModuleReference;
86 import org.onap.cps.utils.JsonObjectMapper;
87 import org.springframework.http.ResponseEntity;
88 import org.springframework.stereotype.Service;
92 @RequiredArgsConstructor
93 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
95 private static final int DELETE_BATCH_SIZE = 100;
96 private final JsonObjectMapper jsonObjectMapper;
97 private final DmiDataOperations dmiDataOperations;
98 private final NetworkCmProxyDataServicePropertyHandler networkCmProxyDataServicePropertyHandler;
99 private final InventoryPersistence inventoryPersistence;
100 private final CmHandleQueries cmHandleQueries;
101 private final NetworkCmProxyCmHandleQueryService networkCmProxyCmHandleQueryService;
102 private final LcmEventsCmHandleStateHandler lcmEventsCmHandleStateHandler;
103 private final CpsDataService cpsDataService;
104 private final IMap<String, Object> moduleSyncStartedOnCmHandles;
105 private final Map<String, TrustLevel> trustLevelPerDmiPlugin;
106 private final TrustLevelManager trustLevelManager;
109 public DmiPluginRegistrationResponse updateDmiRegistrationAndSyncModule(
110 final DmiPluginRegistration dmiPluginRegistration) {
111 dmiPluginRegistration.validateDmiPluginRegistration();
112 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse = new DmiPluginRegistrationResponse();
114 setTrustLevelPerDmiPlugin(dmiPluginRegistration);
116 if (!dmiPluginRegistration.getRemovedCmHandles().isEmpty()) {
117 dmiPluginRegistrationResponse.setRemovedCmHandles(
118 parseAndProcessDeletedCmHandlesInRegistration(dmiPluginRegistration.getRemovedCmHandles()));
121 if (!dmiPluginRegistration.getCreatedCmHandles().isEmpty()) {
122 dmiPluginRegistrationResponse.setCreatedCmHandles(
123 parseAndProcessCreatedCmHandlesInRegistration(dmiPluginRegistration));
125 if (!dmiPluginRegistration.getUpdatedCmHandles().isEmpty()) {
126 dmiPluginRegistrationResponse.setUpdatedCmHandles(
127 networkCmProxyDataServicePropertyHandler
128 .updateCmHandleProperties(dmiPluginRegistration.getUpdatedCmHandles()));
130 if (dmiPluginRegistration.getUpgradedCmHandles() != null
131 && !dmiPluginRegistration.getUpgradedCmHandles().getCmHandles().isEmpty()) {
132 dmiPluginRegistrationResponse.setUpgradedCmHandles(
133 parseAndProcessUpgradedCmHandlesInRegistration(dmiPluginRegistration));
136 return dmiPluginRegistrationResponse;
140 public Object getResourceDataForCmHandle(final String datastoreName,
141 final String cmHandleId,
142 final String resourceIdentifier,
143 final String optionsParamInQuery,
144 final String topicParamInQuery,
145 final String requestId) {
146 final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(datastoreName, cmHandleId,
151 return responseEntity.getBody();
155 public Object getResourceDataForCmHandle(final String datastoreName,
156 final String cmHandleId,
157 final String resourceIdentifier,
158 final FetchDescendantsOption fetchDescendantsOption) {
159 return cpsDataService.getDataNodes(datastoreName, cmHandleId, resourceIdentifier,
160 fetchDescendantsOption).iterator().next();
164 public void executeDataOperationForCmHandles(final String topicParamInQuery,
165 final DataOperationRequest
166 dataOperationRequest,
167 final String requestId) {
168 dmiDataOperations.requestResourceDataFromDmi(topicParamInQuery, dataOperationRequest, requestId);
172 public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
173 final String resourceIdentifier,
174 final OperationType operationType,
175 final String requestData,
176 final String dataType) {
177 return dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier,
178 operationType, requestData, dataType);
182 public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
183 return inventoryPersistence.getYangResourcesModuleReferences(cmHandleId);
187 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
188 return inventoryPersistence.getModuleDefinitionsByCmHandleId(cmHandleId);
192 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleAndModule(final String cmHandleId,
193 final String moduleName,
194 final String moduleRevision) {
195 return inventoryPersistence.getModuleDefinitionsByCmHandleAndModule(cmHandleId, moduleName, moduleRevision);
199 * Retrieve cm handles with details for the given query parameters.
201 * @param cmHandleQueryApiParameters cm handle query parameters
202 * @return cm handles with details
205 public Collection<NcmpServiceCmHandle> executeCmHandleSearch(
206 final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
207 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
208 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
209 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
210 return networkCmProxyCmHandleQueryService.queryCmHandles(cmHandleQueryServiceParameters);
214 * Retrieve cm handle ids for the given query parameters.
216 * @param cmHandleQueryApiParameters cm handle query parameters
217 * @return cm handle ids
220 public Collection<String> executeCmHandleIdSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
221 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
222 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
223 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
224 return networkCmProxyCmHandleQueryService.queryCmHandleIds(cmHandleQueryServiceParameters);
228 * Set the data sync enabled flag, along with the data sync state
229 * based on the data sync enabled boolean for the cm handle id provided.
231 * @param cmHandleId cm handle id
232 * @param dataSyncEnabledTargetValue data sync enabled flag
235 public void setDataSyncEnabled(final String cmHandleId, final Boolean dataSyncEnabledTargetValue) {
236 final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
237 if (dataSyncEnabledTargetValue.equals(compositeState.getDataSyncEnabled())) {
238 log.info("Data-Sync Enabled flag is already: {} ", dataSyncEnabledTargetValue);
241 if (CmHandleState.READY.equals(compositeState.getCmHandleState())) {
242 final DataStoreSyncState dataStoreSyncState = compositeState.getDataStores()
243 .getOperationalDataStore().getDataStoreSyncState();
244 if (Boolean.FALSE.equals(dataSyncEnabledTargetValue)
245 && DataStoreSyncState.SYNCHRONIZED.equals(dataStoreSyncState)) {
246 // TODO : This is hard-coded for onap dmi that need to be addressed
247 cpsDataService.deleteDataNode(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId,
248 "/netconf-state", OffsetDateTime.now());
250 CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabledTargetValue, compositeState);
251 inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
253 throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: "
254 + compositeState.getCmHandleState());
259 * Get all cm handle IDs by DMI plugin identifier.
261 * @param dmiPluginIdentifier DMI plugin identifier
262 * @return set of cm handle IDs
265 public Collection<String> getAllCmHandleIdsByDmiPluginIdentifier(final String dmiPluginIdentifier) {
266 return cmHandleQueries.getCmHandleIdsByDmiPluginIdentifier(dmiPluginIdentifier);
270 * Get all cm handle IDs by various properties.
272 * @param cmHandleQueryServiceParameters cm handle query parameters
273 * @return set of cm handle IDs
276 public Collection<String> executeCmHandleIdSearchForInventory(
277 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
278 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, InventoryQueryConditions.ALL_CONDITION_NAMES);
279 return networkCmProxyCmHandleQueryService.queryCmHandleIdsForInventory(cmHandleQueryServiceParameters);
283 * Retrieve cm handle details for a given cm handle.
285 * @param cmHandleId cm handle identifier
286 * @return cm handle details
289 public NcmpServiceCmHandle getNcmpServiceCmHandle(final String cmHandleId) {
290 return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(
291 inventoryPersistence.getYangModelCmHandle(cmHandleId));
295 * Get cm handle public properties for a given cm handle id.
297 * @param cmHandleId cm handle identifier
298 * @return cm handle public properties
301 public Map<String, String> getCmHandlePublicProperties(final String cmHandleId) {
302 final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
303 final List<YangModelCmHandle.Property> yangModelPublicProperties = yangModelCmHandle.getPublicProperties();
304 final Map<String, String> cmHandlePublicProperties = new HashMap<>();
305 YangDataConverter.asPropertiesMap(yangModelPublicProperties, cmHandlePublicProperties);
306 return cmHandlePublicProperties;
310 * Get cm handle composite state for a given cm handle id.
312 * @param cmHandleId cm handle identifier
313 * @return cm handle state
316 public CompositeState getCmHandleCompositeState(final String cmHandleId) {
317 return inventoryPersistence.getYangModelCmHandle(cmHandleId).getCompositeState();
321 * THis method registers a cm handle and initiates modules sync.
323 * @param dmiPluginRegistration dmi plugin registration information.
324 * @return cm-handle registration response for create cm-handle requests.
326 public List<CmHandleRegistrationResponse> parseAndProcessCreatedCmHandlesInRegistration(
327 final DmiPluginRegistration dmiPluginRegistration) {
328 final List<NcmpServiceCmHandle> cmHandlesToBeCreated = dmiPluginRegistration.getCreatedCmHandles();
329 final Map<String, TrustLevel> initialTrustLevelPerCmHandleId = new HashMap<>(cmHandlesToBeCreated.size());
330 final List<YangModelCmHandle> yangModelCmHandles = new ArrayList<>(cmHandlesToBeCreated.size());
332 .forEach(cmHandle -> {
333 final YangModelCmHandle yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle(
334 dmiPluginRegistration.getDmiPlugin(),
335 dmiPluginRegistration.getDmiDataPlugin(),
336 dmiPluginRegistration.getDmiModelPlugin(),
338 cmHandle.getModuleSetTag(),
339 cmHandle.getAlternateId());
340 yangModelCmHandles.add(yangModelCmHandle);
341 initialTrustLevelPerCmHandleId.put(cmHandle.getCmHandleId(), cmHandle.getRegistrationTrustLevel());
343 return registerNewCmHandles(yangModelCmHandles, initialTrustLevelPerCmHandleId);
346 protected List<CmHandleRegistrationResponse> parseAndProcessDeletedCmHandlesInRegistration(
347 final List<String> tobeRemovedCmHandles) {
348 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
349 new ArrayList<>(tobeRemovedCmHandles.size());
350 final Collection<YangModelCmHandle> yangModelCmHandles =
351 inventoryPersistence.getYangModelCmHandles(tobeRemovedCmHandles);
353 updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETING);
355 final Set<String> notDeletedCmHandles = new HashSet<>();
356 for (final List<String> tobeRemovedCmHandleBatch : Lists.partition(tobeRemovedCmHandles, DELETE_BATCH_SIZE)) {
358 batchDeleteCmHandlesFromDbAndModuleSyncMap(tobeRemovedCmHandleBatch);
359 tobeRemovedCmHandleBatch.forEach(cmHandleId ->
360 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId)));
362 } catch (final RuntimeException batchException) {
363 log.error("Unable to de-register cm-handle batch, retrying on each cm handle");
364 for (final String cmHandleId : tobeRemovedCmHandleBatch) {
365 final CmHandleRegistrationResponse cmHandleRegistrationResponse =
366 deleteCmHandleAndGetCmHandleRegistrationResponse(cmHandleId);
367 cmHandleRegistrationResponses.add(cmHandleRegistrationResponse);
368 if (cmHandleRegistrationResponse.getStatus() != CmHandleRegistrationResponse.Status.SUCCESS) {
369 notDeletedCmHandles.add(cmHandleId);
375 yangModelCmHandles.removeIf(yangModelCmHandle -> notDeletedCmHandles.contains(yangModelCmHandle.getId()));
376 updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETED);
378 return cmHandleRegistrationResponses;
381 protected List<CmHandleRegistrationResponse> parseAndProcessUpgradedCmHandlesInRegistration(
382 final DmiPluginRegistration dmiPluginRegistration) {
384 final List<String> upgradedCmHandleIds = dmiPluginRegistration.getUpgradedCmHandles().getCmHandles();
386 final Map<YangModelCmHandle, CmHandleState> acceptedCmHandleStatePerCmHandle
387 = new HashMap<>(upgradedCmHandleIds.size());
389 final Map<NcmpResponseStatus, List<String>> failedCmHandlesPerResponseStatus
390 = new EnumMap<>(NcmpResponseStatus.class);
391 final List<String> nonExistingCmHandleIds = new ArrayList<>();
392 final List<String> nonReadyCmHandleIds = new ArrayList<>();
393 final List<String> invalidCmHandleIds = new ArrayList<>();
395 upgradedCmHandleIds.forEach(cmHandleId -> {
397 if (cmHandleQueries.cmHandleHasState(cmHandleId, CmHandleState.READY)) {
398 final YangModelCmHandle yangModelCmHandleWithUpgradeDetails
399 = createYangModelCmHandle(dmiPluginRegistration, cmHandleId);
400 acceptedCmHandleStatePerCmHandle.put(yangModelCmHandleWithUpgradeDetails, CmHandleState.LOCKED);
402 nonReadyCmHandleIds.add(cmHandleId);
404 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
405 log.error("Unable to find data node for cm handle id : {} , caused by : {}",
406 cmHandleId, dataNodeNotFoundException.getMessage());
407 nonExistingCmHandleIds.add(cmHandleId);
408 } catch (final DataValidationException dataValidationException) {
409 log.error("Unable to upgrade cm handle id: {}, caused by : {}",
410 cmHandleId, dataValidationException.getMessage());
411 invalidCmHandleIds.add(cmHandleId);
414 failedCmHandlesPerResponseStatus.put(CM_HANDLES_NOT_READY, nonReadyCmHandleIds);
415 failedCmHandlesPerResponseStatus.put(CM_HANDLES_NOT_FOUND, nonExistingCmHandleIds);
416 failedCmHandlesPerResponseStatus.put(CM_HANDLE_INVALID_ID, invalidCmHandleIds);
417 return mergeAllCmHandleResponses(acceptedCmHandleStatePerCmHandle, failedCmHandlesPerResponseStatus);
420 private static YangModelCmHandle createYangModelCmHandle(final DmiPluginRegistration dmiPluginRegistration,
421 final String cmHandleId) {
422 final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle();
423 ncmpServiceCmHandle.setCmHandleId(cmHandleId);
424 final String moduleSetTag = dmiPluginRegistration.getUpgradedCmHandles().getModuleSetTag();
425 final String lockReasonWithModuleSetTag = MessageFormat.format(
426 ModuleOperationsUtils.MODULE_SET_TAG_MESSAGE_FORMAT, moduleSetTag);
427 ncmpServiceCmHandle.setCompositeState(new CompositeStateBuilder().withCmHandleState(CmHandleState.READY)
428 .withLockReason(MODULE_UPGRADE, lockReasonWithModuleSetTag).build());
429 return YangModelCmHandle.toYangModelCmHandle(dmiPluginRegistration.getDmiPlugin(),
430 dmiPluginRegistration.getDmiDataPlugin(), dmiPluginRegistration.getDmiModelPlugin(),
431 ncmpServiceCmHandle, moduleSetTag, ncmpServiceCmHandle.getAlternateId());
434 private CmHandleRegistrationResponse deleteCmHandleAndGetCmHandleRegistrationResponse(final String cmHandleId) {
436 deleteCmHandleFromDbAndModuleSyncMap(cmHandleId);
437 return CmHandleRegistrationResponse.createSuccessResponse(cmHandleId);
438 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
439 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
440 cmHandleId, dataNodeNotFoundException.getMessage());
441 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND);
442 } catch (final DataValidationException dataValidationException) {
443 log.error("Unable to de-register cm-handle id: {}, caused by: {}",
444 cmHandleId, dataValidationException.getMessage());
445 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID);
446 } catch (final Exception exception) {
447 log.error("Unable to de-register cm-handle id : {} , caused by : {}", cmHandleId, exception.getMessage());
448 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception);
452 private void updateCmHandleStateBatch(final Collection<YangModelCmHandle> yangModelCmHandles,
453 final CmHandleState cmHandleState) {
454 final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>(yangModelCmHandles.size());
455 yangModelCmHandles.forEach(yangModelCmHandle -> cmHandleStatePerCmHandle.put(yangModelCmHandle, cmHandleState));
456 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
459 private void deleteCmHandleFromDbAndModuleSyncMap(final String cmHandleId) {
460 inventoryPersistence.deleteSchemaSetWithCascade(cmHandleId);
461 inventoryPersistence.deleteDataNode(NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId
463 removeDeletedCmHandleFromModuleSyncMap(cmHandleId);
466 private void batchDeleteCmHandlesFromDbAndModuleSyncMap(final Collection<String> tobeRemovedCmHandles) {
467 inventoryPersistence.deleteSchemaSetsWithCascade(tobeRemovedCmHandles);
468 inventoryPersistence.deleteDataNodes(mapCmHandleIdsToXpaths(tobeRemovedCmHandles));
469 tobeRemovedCmHandles.forEach(this::removeDeletedCmHandleFromModuleSyncMap);
472 private Collection<String> mapCmHandleIdsToXpaths(final Collection<String> cmHandles) {
473 return cmHandles.stream()
474 .map(cmHandleId -> NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId + "']")
475 .collect(Collectors.toSet());
478 // CPS-1239 Robustness cleaning of in progress cache
479 private void removeDeletedCmHandleFromModuleSyncMap(final String deletedCmHandleId) {
480 if (moduleSyncStartedOnCmHandles.remove(deletedCmHandleId) != null) {
481 log.debug("{} removed from in progress map", deletedCmHandleId);
485 private List<CmHandleRegistrationResponse> registerNewCmHandles(final List<YangModelCmHandle> yangModelCmHandles,
486 final Map<String, TrustLevel>
487 initialTrustLevelPerCmHandleId) {
488 final Set<String> cmHandleIds = initialTrustLevelPerCmHandleId.keySet();
490 lcmEventsCmHandleStateHandler.initiateStateAdvised(yangModelCmHandles);
491 trustLevelManager.handleInitialRegistrationOfTrustLevels(initialTrustLevelPerCmHandleId);
492 return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
493 } catch (final AlreadyDefinedException alreadyDefinedException) {
494 return CmHandleRegistrationResponse.createFailureResponses(
495 alreadyDefinedException.getAlreadyDefinedObjectNames(), CM_HANDLE_ALREADY_EXIST);
496 } catch (final Exception exception) {
497 return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, exception);
501 private List<CmHandleRegistrationResponse> mergeAllCmHandleResponses(
502 final Map<YangModelCmHandle, CmHandleState> acceptedCmHandleStatePerCmHandle,
503 final Map<NcmpResponseStatus, List<String>> failedCmHandlesPerResponseStatus) {
504 final List<CmHandleRegistrationResponse> cmHandleUpgradeResponses
505 = upgradeCmHandles(acceptedCmHandleStatePerCmHandle);
506 failedCmHandlesPerResponseStatus.forEach((ncmpResponseStatus, cmHandleIds) ->
507 cmHandleIds.forEach(cmHandleId -> cmHandleUpgradeResponses.add(CmHandleRegistrationResponse
508 .createFailureResponse(cmHandleId, ncmpResponseStatus))));
509 return cmHandleUpgradeResponses;
512 private List<CmHandleRegistrationResponse> upgradeCmHandles(final Map<YangModelCmHandle, CmHandleState>
513 cmHandleStatePerCmHandle) {
514 final List<String> cmHandleIds = getCmHandleIds(cmHandleStatePerCmHandle);
515 log.info("Moving cm handles : {} into locked (for upgrade) state.", cmHandleIds);
517 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
518 return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
519 } catch (final Exception e) {
520 log.error("Unable to update cmHandleIds : {} , caused by : {}", cmHandleIds, e.getMessage());
521 return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, e);
525 private static List<String> getCmHandleIds(final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle) {
526 return cmHandleStatePerCmHandle.keySet().stream().map(YangModelCmHandle::getId).toList();
529 private void setTrustLevelPerDmiPlugin(final DmiPluginRegistration dmiPluginRegistration) {
530 if (DmiPluginRegistration.isNullEmptyOrBlank(dmiPluginRegistration.getDmiDataPlugin())) {
531 trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiPlugin(), TrustLevel.COMPLETE);
533 trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiDataPlugin(), TrustLevel.COMPLETE);