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.apache.commons.lang3.StringUtils;
52 import org.onap.cps.api.CpsDataService;
53 import org.onap.cps.ncmp.api.NcmpResponseStatus;
54 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandleQueryService;
55 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
56 import org.onap.cps.ncmp.api.impl.events.lcm.LcmEventsCmHandleStateHandler;
57 import org.onap.cps.ncmp.api.impl.inventory.CmHandleQueries;
58 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState;
59 import org.onap.cps.ncmp.api.impl.inventory.CompositeState;
60 import org.onap.cps.ncmp.api.impl.inventory.CompositeStateBuilder;
61 import org.onap.cps.ncmp.api.impl.inventory.CompositeStateUtils;
62 import org.onap.cps.ncmp.api.impl.inventory.DataStoreSyncState;
63 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence;
64 import org.onap.cps.ncmp.api.impl.inventory.sync.ModuleOperationsUtils;
65 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations;
66 import org.onap.cps.ncmp.api.impl.operations.OperationType;
67 import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevel;
68 import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevelManager;
69 import org.onap.cps.ncmp.api.impl.utils.CmHandleIdMapper;
70 import org.onap.cps.ncmp.api.impl.utils.CmHandleQueryConditions;
71 import org.onap.cps.ncmp.api.impl.utils.InventoryQueryConditions;
72 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
73 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
74 import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters;
75 import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters;
76 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse;
77 import org.onap.cps.ncmp.api.models.DataOperationRequest;
78 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
79 import org.onap.cps.ncmp.api.models.DmiPluginRegistrationResponse;
80 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
81 import org.onap.cps.spi.FetchDescendantsOption;
82 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
83 import org.onap.cps.spi.exceptions.CpsException;
84 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
85 import org.onap.cps.spi.exceptions.DataValidationException;
86 import org.onap.cps.spi.model.ModuleDefinition;
87 import org.onap.cps.spi.model.ModuleReference;
88 import org.onap.cps.utils.JsonObjectMapper;
89 import org.springframework.http.ResponseEntity;
90 import org.springframework.stereotype.Service;
94 @RequiredArgsConstructor
95 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
97 private static final int DELETE_BATCH_SIZE = 100;
98 private final JsonObjectMapper jsonObjectMapper;
99 private final DmiDataOperations dmiDataOperations;
100 private final NetworkCmProxyDataServicePropertyHandler networkCmProxyDataServicePropertyHandler;
101 private final InventoryPersistence inventoryPersistence;
102 private final CmHandleQueries cmHandleQueries;
103 private final NetworkCmProxyCmHandleQueryService networkCmProxyCmHandleQueryService;
104 private final LcmEventsCmHandleStateHandler lcmEventsCmHandleStateHandler;
105 private final CpsDataService cpsDataService;
106 private final IMap<String, Object> moduleSyncStartedOnCmHandles;
107 private final Map<String, TrustLevel> trustLevelPerDmiPlugin;
108 private final TrustLevelManager trustLevelManager;
109 private final CmHandleIdMapper cmHandleIdMapper;
112 public DmiPluginRegistrationResponse updateDmiRegistrationAndSyncModule(
113 final DmiPluginRegistration dmiPluginRegistration) {
114 cacheAlternateIds(dmiPluginRegistration.getCreatedCmHandles());
115 dmiPluginRegistration.validateDmiPluginRegistration();
116 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse = new DmiPluginRegistrationResponse();
118 setTrustLevelPerDmiPlugin(dmiPluginRegistration);
120 if (!dmiPluginRegistration.getRemovedCmHandles().isEmpty()) {
121 dmiPluginRegistrationResponse.setRemovedCmHandles(
122 parseAndProcessDeletedCmHandlesInRegistration(dmiPluginRegistration.getRemovedCmHandles()));
125 if (!dmiPluginRegistration.getCreatedCmHandles().isEmpty()) {
126 dmiPluginRegistrationResponse.setCreatedCmHandles(
127 parseAndProcessCreatedCmHandlesInRegistration(dmiPluginRegistration));
129 if (!dmiPluginRegistration.getUpdatedCmHandles().isEmpty()) {
130 dmiPluginRegistrationResponse.setUpdatedCmHandles(
131 networkCmProxyDataServicePropertyHandler
132 .updateCmHandleProperties(dmiPluginRegistration.getUpdatedCmHandles()));
134 if (dmiPluginRegistration.getUpgradedCmHandles() != null
135 && !dmiPluginRegistration.getUpgradedCmHandles().getCmHandles().isEmpty()) {
136 dmiPluginRegistrationResponse.setUpgradedCmHandles(
137 parseAndProcessUpgradedCmHandlesInRegistration(dmiPluginRegistration));
140 return dmiPluginRegistrationResponse;
144 public Object getResourceDataForCmHandle(final String datastoreName,
145 final String cmHandleId,
146 final String resourceIdentifier,
147 final String optionsParamInQuery,
148 final String topicParamInQuery,
149 final String requestId) {
150 final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(datastoreName, cmHandleId,
155 return responseEntity.getBody();
159 public Object getResourceDataForCmHandle(final String datastoreName,
160 final String cmHandleId,
161 final String resourceIdentifier,
162 final FetchDescendantsOption fetchDescendantsOption) {
163 return cpsDataService.getDataNodes(datastoreName, cmHandleId, resourceIdentifier,
164 fetchDescendantsOption).iterator().next();
168 public void executeDataOperationForCmHandles(final String topicParamInQuery,
169 final DataOperationRequest
170 dataOperationRequest,
171 final String requestId) {
172 dmiDataOperations.requestResourceDataFromDmi(topicParamInQuery, dataOperationRequest, requestId);
176 public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
177 final String resourceIdentifier,
178 final OperationType operationType,
179 final String requestData,
180 final String dataType) {
181 return dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier,
182 operationType, requestData, dataType);
186 public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
187 return inventoryPersistence.getYangResourcesModuleReferences(cmHandleId);
191 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
192 return inventoryPersistence.getModuleDefinitionsByCmHandleId(cmHandleId);
196 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleAndModule(final String cmHandleId,
197 final String moduleName,
198 final String moduleRevision) {
199 return inventoryPersistence.getModuleDefinitionsByCmHandleAndModule(cmHandleId, moduleName, moduleRevision);
203 * Retrieve cm handles with details for the given query parameters.
205 * @param cmHandleQueryApiParameters cm handle query parameters
206 * @return cm handles with details
209 public Collection<NcmpServiceCmHandle> executeCmHandleSearch(
210 final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
211 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
212 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
213 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
214 return networkCmProxyCmHandleQueryService.queryCmHandles(cmHandleQueryServiceParameters);
218 * Retrieve cm handle ids for the given query parameters.
220 * @param cmHandleQueryApiParameters cm handle query parameters
221 * @return cm handle ids
224 public Collection<String> executeCmHandleIdSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
225 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
226 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
227 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
228 return networkCmProxyCmHandleQueryService.queryCmHandleIds(cmHandleQueryServiceParameters);
232 * Set the data sync enabled flag, along with the data sync state
233 * based on the data sync enabled boolean for the cm handle id provided.
235 * @param cmHandleId cm handle id
236 * @param dataSyncEnabledTargetValue data sync enabled flag
239 public void setDataSyncEnabled(final String cmHandleId, final Boolean dataSyncEnabledTargetValue) {
240 final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
241 if (dataSyncEnabledTargetValue.equals(compositeState.getDataSyncEnabled())) {
242 log.info("Data-Sync Enabled flag is already: {} ", dataSyncEnabledTargetValue);
245 if (CmHandleState.READY.equals(compositeState.getCmHandleState())) {
246 final DataStoreSyncState dataStoreSyncState = compositeState.getDataStores()
247 .getOperationalDataStore().getDataStoreSyncState();
248 if (Boolean.FALSE.equals(dataSyncEnabledTargetValue)
249 && DataStoreSyncState.SYNCHRONIZED.equals(dataStoreSyncState)) {
250 // TODO : This is hard-coded for onap dmi that need to be addressed
251 cpsDataService.deleteDataNode(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId,
252 "/netconf-state", OffsetDateTime.now());
254 CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabledTargetValue, compositeState);
255 inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
257 throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: "
258 + compositeState.getCmHandleState());
263 * Get all cm handle IDs by DMI plugin identifier.
265 * @param dmiPluginIdentifier DMI plugin identifier
266 * @return set of cm handle IDs
269 public Collection<String> getAllCmHandleIdsByDmiPluginIdentifier(final String dmiPluginIdentifier) {
270 return cmHandleQueries.getCmHandleIdsByDmiPluginIdentifier(dmiPluginIdentifier);
274 * Get all cm handle IDs by various properties.
276 * @param cmHandleQueryServiceParameters cm handle query parameters
277 * @return set of cm handle IDs
280 public Collection<String> executeCmHandleIdSearchForInventory(
281 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
282 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, InventoryQueryConditions.ALL_CONDITION_NAMES);
283 return networkCmProxyCmHandleQueryService.queryCmHandleIdsForInventory(cmHandleQueryServiceParameters);
287 * Retrieve cm handle details for a given cm handle.
289 * @param cmHandleId cm handle identifier
290 * @return cm handle details
293 public NcmpServiceCmHandle getNcmpServiceCmHandle(final String cmHandleId) {
294 return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(
295 inventoryPersistence.getYangModelCmHandle(cmHandleId));
299 * Get cm handle public properties for a given cm handle id.
301 * @param cmHandleId cm handle identifier
302 * @return cm handle public properties
305 public Map<String, String> getCmHandlePublicProperties(final String cmHandleId) {
306 final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
307 final List<YangModelCmHandle.Property> yangModelPublicProperties = yangModelCmHandle.getPublicProperties();
308 final Map<String, String> cmHandlePublicProperties = new HashMap<>();
309 YangDataConverter.asPropertiesMap(yangModelPublicProperties, cmHandlePublicProperties);
310 return cmHandlePublicProperties;
314 * Get cm handle composite state for a given cm handle id.
316 * @param cmHandleId cm handle identifier
317 * @return cm handle state
320 public CompositeState getCmHandleCompositeState(final String cmHandleId) {
321 return inventoryPersistence.getYangModelCmHandle(cmHandleId).getCompositeState();
325 * THis method registers a cm handle and initiates modules sync.
327 * @param dmiPluginRegistration dmi plugin registration information.
328 * @return cm-handle registration response for create cm-handle requests.
330 public List<CmHandleRegistrationResponse> parseAndProcessCreatedCmHandlesInRegistration(
331 final DmiPluginRegistration dmiPluginRegistration) {
332 final List<NcmpServiceCmHandle> cmHandlesToBeCreated = dmiPluginRegistration.getCreatedCmHandles();
333 final Map<String, TrustLevel> initialTrustLevelPerCmHandleId = new HashMap<>(cmHandlesToBeCreated.size());
334 final List<YangModelCmHandle> yangModelCmHandles = new ArrayList<>(cmHandlesToBeCreated.size());
336 .forEach(cmHandle -> {
337 final YangModelCmHandle yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle(
338 dmiPluginRegistration.getDmiPlugin(),
339 dmiPluginRegistration.getDmiDataPlugin(),
340 dmiPluginRegistration.getDmiModelPlugin(),
342 cmHandle.getModuleSetTag(),
343 cmHandle.getAlternateId());
344 yangModelCmHandles.add(yangModelCmHandle);
345 initialTrustLevelPerCmHandleId.put(cmHandle.getCmHandleId(), cmHandle.getRegistrationTrustLevel());
347 return registerNewCmHandles(yangModelCmHandles, initialTrustLevelPerCmHandleId);
350 protected List<CmHandleRegistrationResponse> parseAndProcessDeletedCmHandlesInRegistration(
351 final List<String> tobeRemovedCmHandles) {
352 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
353 new ArrayList<>(tobeRemovedCmHandles.size());
354 final Collection<YangModelCmHandle> yangModelCmHandles =
355 inventoryPersistence.getYangModelCmHandles(tobeRemovedCmHandles);
357 updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETING);
359 final Set<String> notDeletedCmHandles = new HashSet<>();
360 for (final List<String> tobeRemovedCmHandleBatch : Lists.partition(tobeRemovedCmHandles, DELETE_BATCH_SIZE)) {
362 batchDeleteCmHandlesFromDbAndModuleSyncMap(tobeRemovedCmHandleBatch);
363 tobeRemovedCmHandleBatch.forEach(cmHandleId ->
364 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId)));
366 } catch (final RuntimeException batchException) {
367 log.error("Unable to de-register cm-handle batch, retrying on each cm handle");
368 for (final String cmHandleId : tobeRemovedCmHandleBatch) {
369 final CmHandleRegistrationResponse cmHandleRegistrationResponse =
370 deleteCmHandleAndGetCmHandleRegistrationResponse(cmHandleId);
371 cmHandleRegistrationResponses.add(cmHandleRegistrationResponse);
372 if (cmHandleRegistrationResponse.getStatus() != CmHandleRegistrationResponse.Status.SUCCESS) {
373 notDeletedCmHandles.add(cmHandleId);
379 yangModelCmHandles.removeIf(yangModelCmHandle -> notDeletedCmHandles.contains(yangModelCmHandle.getId()));
380 updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETED);
381 removeEntriesFromAlternateIdCache(yangModelCmHandles);
383 return cmHandleRegistrationResponses;
386 private void removeEntriesFromAlternateIdCache(final Collection<YangModelCmHandle> yangModelCmHandles) {
387 for (final YangModelCmHandle yangModelCmHandle : yangModelCmHandles) {
388 cmHandleIdMapper.removeMapping(yangModelCmHandle.getId());
392 protected List<CmHandleRegistrationResponse> parseAndProcessUpgradedCmHandlesInRegistration(
393 final DmiPluginRegistration dmiPluginRegistration) {
395 final List<String> upgradedCmHandleIds = dmiPluginRegistration.getUpgradedCmHandles().getCmHandles();
397 final Map<YangModelCmHandle, CmHandleState> acceptedCmHandleStatePerCmHandle
398 = new HashMap<>(upgradedCmHandleIds.size());
400 final Map<NcmpResponseStatus, List<String>> failedCmHandlesPerResponseStatus
401 = new EnumMap<>(NcmpResponseStatus.class);
402 final List<String> nonExistingCmHandleIds = new ArrayList<>();
403 final List<String> nonReadyCmHandleIds = new ArrayList<>();
404 final List<String> invalidCmHandleIds = new ArrayList<>();
406 upgradedCmHandleIds.forEach(cmHandleId -> {
408 if (cmHandleQueries.cmHandleHasState(cmHandleId, CmHandleState.READY)) {
409 final YangModelCmHandle yangModelCmHandleWithUpgradeDetails
410 = createYangModelCmHandle(dmiPluginRegistration, cmHandleId);
411 acceptedCmHandleStatePerCmHandle.put(yangModelCmHandleWithUpgradeDetails, CmHandleState.LOCKED);
413 nonReadyCmHandleIds.add(cmHandleId);
415 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
416 log.error("Unable to find data node for cm handle id : {} , caused by : {}",
417 cmHandleId, dataNodeNotFoundException.getMessage());
418 nonExistingCmHandleIds.add(cmHandleId);
419 } catch (final DataValidationException dataValidationException) {
420 log.error("Unable to upgrade cm handle id: {}, caused by : {}",
421 cmHandleId, dataValidationException.getMessage());
422 invalidCmHandleIds.add(cmHandleId);
425 failedCmHandlesPerResponseStatus.put(CM_HANDLES_NOT_READY, nonReadyCmHandleIds);
426 failedCmHandlesPerResponseStatus.put(CM_HANDLES_NOT_FOUND, nonExistingCmHandleIds);
427 failedCmHandlesPerResponseStatus.put(CM_HANDLE_INVALID_ID, invalidCmHandleIds);
428 return mergeAllCmHandleResponses(acceptedCmHandleStatePerCmHandle, failedCmHandlesPerResponseStatus);
431 private static YangModelCmHandle createYangModelCmHandle(final DmiPluginRegistration dmiPluginRegistration,
432 final String cmHandleId) {
433 final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle();
434 ncmpServiceCmHandle.setCmHandleId(cmHandleId);
435 final String moduleSetTag = dmiPluginRegistration.getUpgradedCmHandles().getModuleSetTag();
436 final String lockReasonWithModuleSetTag = MessageFormat.format(
437 ModuleOperationsUtils.MODULE_SET_TAG_MESSAGE_FORMAT, moduleSetTag);
438 ncmpServiceCmHandle.setCompositeState(new CompositeStateBuilder().withCmHandleState(CmHandleState.READY)
439 .withLockReason(MODULE_UPGRADE, lockReasonWithModuleSetTag).build());
440 return YangModelCmHandle.toYangModelCmHandle(dmiPluginRegistration.getDmiPlugin(),
441 dmiPluginRegistration.getDmiDataPlugin(), dmiPluginRegistration.getDmiModelPlugin(),
442 ncmpServiceCmHandle, moduleSetTag, ncmpServiceCmHandle.getAlternateId());
445 private CmHandleRegistrationResponse deleteCmHandleAndGetCmHandleRegistrationResponse(final String cmHandleId) {
447 deleteCmHandleFromDbAndModuleSyncMap(cmHandleId);
448 return CmHandleRegistrationResponse.createSuccessResponse(cmHandleId);
449 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
450 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
451 cmHandleId, dataNodeNotFoundException.getMessage());
452 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND);
453 } catch (final DataValidationException dataValidationException) {
454 log.error("Unable to de-register cm-handle id: {}, caused by: {}",
455 cmHandleId, dataValidationException.getMessage());
456 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID);
457 } catch (final Exception exception) {
458 log.error("Unable to de-register cm-handle id : {} , caused by : {}", cmHandleId, exception.getMessage());
459 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception);
463 private void updateCmHandleStateBatch(final Collection<YangModelCmHandle> yangModelCmHandles,
464 final CmHandleState cmHandleState) {
465 final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>(yangModelCmHandles.size());
466 yangModelCmHandles.forEach(yangModelCmHandle -> cmHandleStatePerCmHandle.put(yangModelCmHandle, cmHandleState));
467 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
470 private void deleteCmHandleFromDbAndModuleSyncMap(final String cmHandleId) {
471 inventoryPersistence.deleteSchemaSetWithCascade(cmHandleId);
472 inventoryPersistence.deleteDataNode(NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId
474 removeDeletedCmHandleFromModuleSyncMap(cmHandleId);
477 private void batchDeleteCmHandlesFromDbAndModuleSyncMap(final Collection<String> tobeRemovedCmHandles) {
478 inventoryPersistence.deleteSchemaSetsWithCascade(tobeRemovedCmHandles);
479 inventoryPersistence.deleteDataNodes(mapCmHandleIdsToXpaths(tobeRemovedCmHandles));
480 tobeRemovedCmHandles.forEach(this::removeDeletedCmHandleFromModuleSyncMap);
483 private Collection<String> mapCmHandleIdsToXpaths(final Collection<String> cmHandles) {
484 return cmHandles.stream()
485 .map(cmHandleId -> NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId + "']")
486 .collect(Collectors.toSet());
489 // CPS-1239 Robustness cleaning of in progress cache
490 private void removeDeletedCmHandleFromModuleSyncMap(final String deletedCmHandleId) {
491 if (moduleSyncStartedOnCmHandles.remove(deletedCmHandleId) != null) {
492 log.debug("{} removed from in progress map", deletedCmHandleId);
496 private List<CmHandleRegistrationResponse> registerNewCmHandles(final List<YangModelCmHandle> yangModelCmHandles,
497 final Map<String, TrustLevel>
498 initialTrustLevelPerCmHandleId) {
499 final Set<String> cmHandleIds = initialTrustLevelPerCmHandleId.keySet();
501 lcmEventsCmHandleStateHandler.initiateStateAdvised(yangModelCmHandles);
502 trustLevelManager.handleInitialRegistrationOfTrustLevels(initialTrustLevelPerCmHandleId);
503 return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
504 } catch (final AlreadyDefinedException alreadyDefinedException) {
505 return CmHandleRegistrationResponse.createFailureResponses(
506 alreadyDefinedException.getAlreadyDefinedObjectNames(), CM_HANDLE_ALREADY_EXIST);
507 } catch (final Exception exception) {
508 return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, exception);
512 private List<CmHandleRegistrationResponse> mergeAllCmHandleResponses(
513 final Map<YangModelCmHandle, CmHandleState> acceptedCmHandleStatePerCmHandle,
514 final Map<NcmpResponseStatus, List<String>> failedCmHandlesPerResponseStatus) {
515 final List<CmHandleRegistrationResponse> cmHandleUpgradeResponses
516 = upgradeCmHandles(acceptedCmHandleStatePerCmHandle);
517 failedCmHandlesPerResponseStatus.forEach((ncmpResponseStatus, cmHandleIds) ->
518 cmHandleIds.forEach(cmHandleId -> cmHandleUpgradeResponses.add(CmHandleRegistrationResponse
519 .createFailureResponse(cmHandleId, ncmpResponseStatus))));
520 return cmHandleUpgradeResponses;
523 private List<CmHandleRegistrationResponse> upgradeCmHandles(final Map<YangModelCmHandle, CmHandleState>
524 cmHandleStatePerCmHandle) {
525 final List<String> cmHandleIds = getCmHandleIds(cmHandleStatePerCmHandle);
526 log.info("Moving cm handles : {} into locked (for upgrade) state.", cmHandleIds);
528 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
529 return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
530 } catch (final Exception e) {
531 log.error("Unable to update cmHandleIds : {} , caused by : {}", cmHandleIds, e.getMessage());
532 return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, e);
536 private static List<String> getCmHandleIds(final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle) {
537 return cmHandleStatePerCmHandle.keySet().stream().map(YangModelCmHandle::getId).toList();
540 private void setTrustLevelPerDmiPlugin(final DmiPluginRegistration dmiPluginRegistration) {
541 if (DmiPluginRegistration.isNullEmptyOrBlank(dmiPluginRegistration.getDmiDataPlugin())) {
542 trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiPlugin(), TrustLevel.COMPLETE);
544 trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiDataPlugin(), TrustLevel.COMPLETE);
548 private void cacheAlternateIds(final Collection<NcmpServiceCmHandle> ncmpServiceCmHandles) {
549 for (final NcmpServiceCmHandle ncmpServiceCmHandle : ncmpServiceCmHandles) {
550 if (!StringUtils.isEmpty(ncmpServiceCmHandle.getAlternateId())) {
551 cmHandleIdMapper.addMapping(ncmpServiceCmHandle.getCmHandleId(), ncmpServiceCmHandle.getAlternateId());