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;
110 private final Map<String, Collection<ModuleReference>> moduleSetTagCache;
113 public DmiPluginRegistrationResponse updateDmiRegistrationAndSyncModule(
114 final DmiPluginRegistration dmiPluginRegistration) {
115 cacheAlternateIds(dmiPluginRegistration.getCreatedCmHandles());
116 dmiPluginRegistration.validateDmiPluginRegistration();
117 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse = new DmiPluginRegistrationResponse();
119 setTrustLevelPerDmiPlugin(dmiPluginRegistration);
121 if (!dmiPluginRegistration.getRemovedCmHandles().isEmpty()) {
122 dmiPluginRegistrationResponse.setRemovedCmHandles(
123 parseAndProcessDeletedCmHandlesInRegistration(dmiPluginRegistration.getRemovedCmHandles()));
126 if (!dmiPluginRegistration.getCreatedCmHandles().isEmpty()) {
127 dmiPluginRegistrationResponse.setCreatedCmHandles(
128 parseAndProcessCreatedCmHandlesInRegistration(dmiPluginRegistration));
130 if (!dmiPluginRegistration.getUpdatedCmHandles().isEmpty()) {
131 dmiPluginRegistrationResponse.setUpdatedCmHandles(
132 networkCmProxyDataServicePropertyHandler
133 .updateCmHandleProperties(dmiPluginRegistration.getUpdatedCmHandles()));
135 if (dmiPluginRegistration.getUpgradedCmHandles() != null
136 && !dmiPluginRegistration.getUpgradedCmHandles().getCmHandles().isEmpty()) {
137 dmiPluginRegistrationResponse.setUpgradedCmHandles(
138 parseAndProcessUpgradedCmHandlesInRegistration(dmiPluginRegistration));
141 return dmiPluginRegistrationResponse;
145 public Object getResourceDataForCmHandle(final String datastoreName,
146 final String cmHandleId,
147 final String resourceIdentifier,
148 final String optionsParamInQuery,
149 final String topicParamInQuery,
150 final String requestId) {
151 final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(datastoreName, cmHandleId,
156 return responseEntity.getBody();
160 public Object getResourceDataForCmHandle(final String datastoreName,
161 final String cmHandleId,
162 final String resourceIdentifier,
163 final FetchDescendantsOption fetchDescendantsOption) {
164 return cpsDataService.getDataNodes(datastoreName, cmHandleId, resourceIdentifier,
165 fetchDescendantsOption).iterator().next();
169 public void executeDataOperationForCmHandles(final String topicParamInQuery,
170 final DataOperationRequest
171 dataOperationRequest,
172 final String requestId) {
173 dmiDataOperations.requestResourceDataFromDmi(topicParamInQuery, dataOperationRequest, requestId);
177 public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
178 final String resourceIdentifier,
179 final OperationType operationType,
180 final String requestData,
181 final String dataType) {
182 return dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier,
183 operationType, requestData, dataType);
187 public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
188 return inventoryPersistence.getYangResourcesModuleReferences(cmHandleId);
192 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
193 return inventoryPersistence.getModuleDefinitionsByCmHandleId(cmHandleId);
197 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleAndModule(final String cmHandleId,
198 final String moduleName,
199 final String moduleRevision) {
200 return inventoryPersistence.getModuleDefinitionsByCmHandleAndModule(cmHandleId, moduleName, moduleRevision);
204 * Retrieve cm handles with details for the given query parameters.
206 * @param cmHandleQueryApiParameters cm handle query parameters
207 * @return cm handles with details
210 public Collection<NcmpServiceCmHandle> executeCmHandleSearch(
211 final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
212 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
213 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
214 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
215 return networkCmProxyCmHandleQueryService.queryCmHandles(cmHandleQueryServiceParameters);
219 * Retrieve cm handle ids for the given query parameters.
221 * @param cmHandleQueryApiParameters cm handle query parameters
222 * @return cm handle ids
225 public Collection<String> executeCmHandleIdSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
226 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
227 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
228 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
229 return networkCmProxyCmHandleQueryService.queryCmHandleIds(cmHandleQueryServiceParameters);
233 * Set the data sync enabled flag, along with the data sync state
234 * based on the data sync enabled boolean for the cm handle id provided.
236 * @param cmHandleId cm handle id
237 * @param dataSyncEnabledTargetValue data sync enabled flag
240 public void setDataSyncEnabled(final String cmHandleId, final Boolean dataSyncEnabledTargetValue) {
241 final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
242 if (dataSyncEnabledTargetValue.equals(compositeState.getDataSyncEnabled())) {
243 log.info("Data-Sync Enabled flag is already: {} ", dataSyncEnabledTargetValue);
246 if (CmHandleState.READY.equals(compositeState.getCmHandleState())) {
247 final DataStoreSyncState dataStoreSyncState = compositeState.getDataStores()
248 .getOperationalDataStore().getDataStoreSyncState();
249 if (Boolean.FALSE.equals(dataSyncEnabledTargetValue)
250 && DataStoreSyncState.SYNCHRONIZED.equals(dataStoreSyncState)) {
251 // TODO : This is hard-coded for onap dmi that need to be addressed
252 cpsDataService.deleteDataNode(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId,
253 "/netconf-state", OffsetDateTime.now());
255 CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabledTargetValue, compositeState);
256 inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
258 throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: "
259 + compositeState.getCmHandleState());
264 * Get all cm handle IDs by DMI plugin identifier.
266 * @param dmiPluginIdentifier DMI plugin identifier
267 * @return set of cm handle IDs
270 public Collection<String> getAllCmHandleIdsByDmiPluginIdentifier(final String dmiPluginIdentifier) {
271 return cmHandleQueries.getCmHandleIdsByDmiPluginIdentifier(dmiPluginIdentifier);
275 * Get all cm handle IDs by various properties.
277 * @param cmHandleQueryServiceParameters cm handle query parameters
278 * @return set of cm handle IDs
281 public Collection<String> executeCmHandleIdSearchForInventory(
282 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
283 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, InventoryQueryConditions.ALL_CONDITION_NAMES);
284 return networkCmProxyCmHandleQueryService.queryCmHandleIdsForInventory(cmHandleQueryServiceParameters);
288 * Retrieve cm handle details for a given cm handle.
290 * @param cmHandleId cm handle identifier
291 * @return cm handle details
294 public NcmpServiceCmHandle getNcmpServiceCmHandle(final String cmHandleId) {
295 return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(
296 inventoryPersistence.getYangModelCmHandle(cmHandleId));
300 * Get cm handle public properties for a given cm handle id.
302 * @param cmHandleId cm handle identifier
303 * @return cm handle public properties
306 public Map<String, String> getCmHandlePublicProperties(final String cmHandleId) {
307 final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
308 final List<YangModelCmHandle.Property> yangModelPublicProperties = yangModelCmHandle.getPublicProperties();
309 final Map<String, String> cmHandlePublicProperties = new HashMap<>();
310 YangDataConverter.asPropertiesMap(yangModelPublicProperties, cmHandlePublicProperties);
311 return cmHandlePublicProperties;
315 * Get cm handle composite state for a given cm handle id.
317 * @param cmHandleId cm handle identifier
318 * @return cm handle state
321 public CompositeState getCmHandleCompositeState(final String cmHandleId) {
322 return inventoryPersistence.getYangModelCmHandle(cmHandleId).getCompositeState();
326 * THis method registers a cm handle and initiates modules sync.
328 * @param dmiPluginRegistration dmi plugin registration information.
329 * @return cm-handle registration response for create cm-handle requests.
331 public List<CmHandleRegistrationResponse> parseAndProcessCreatedCmHandlesInRegistration(
332 final DmiPluginRegistration dmiPluginRegistration) {
333 final List<NcmpServiceCmHandle> cmHandlesToBeCreated = dmiPluginRegistration.getCreatedCmHandles();
334 final Map<String, TrustLevel> initialTrustLevelPerCmHandleId = new HashMap<>(cmHandlesToBeCreated.size());
335 final List<YangModelCmHandle> yangModelCmHandles = new ArrayList<>(cmHandlesToBeCreated.size());
337 .forEach(cmHandle -> {
338 final YangModelCmHandle yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle(
339 dmiPluginRegistration.getDmiPlugin(),
340 dmiPluginRegistration.getDmiDataPlugin(),
341 dmiPluginRegistration.getDmiModelPlugin(),
343 cmHandle.getModuleSetTag(),
344 cmHandle.getAlternateId());
345 yangModelCmHandles.add(yangModelCmHandle);
346 initialTrustLevelPerCmHandleId.put(cmHandle.getCmHandleId(), cmHandle.getRegistrationTrustLevel());
348 return registerNewCmHandles(yangModelCmHandles, initialTrustLevelPerCmHandleId);
351 protected List<CmHandleRegistrationResponse> parseAndProcessDeletedCmHandlesInRegistration(
352 final List<String> tobeRemovedCmHandles) {
353 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
354 new ArrayList<>(tobeRemovedCmHandles.size());
355 final Collection<YangModelCmHandle> yangModelCmHandles =
356 inventoryPersistence.getYangModelCmHandles(tobeRemovedCmHandles);
357 final Set<String> moduleSetTags = yangModelCmHandles.stream().map(YangModelCmHandle::getModuleSetTag)
358 .collect(Collectors.toSet());
359 updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETING);
361 final Set<String> notDeletedCmHandles = new HashSet<>();
362 for (final List<String> tobeRemovedCmHandleBatch : Lists.partition(tobeRemovedCmHandles, DELETE_BATCH_SIZE)) {
364 batchDeleteCmHandlesFromDbAndModuleSyncMap(tobeRemovedCmHandleBatch);
365 moduleSetTags.forEach(moduleSetTagCache::remove);
366 log.debug("Removed module set tags: {}", moduleSetTags);
367 tobeRemovedCmHandleBatch.forEach(cmHandleId ->
368 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId)));
370 } catch (final RuntimeException batchException) {
371 log.error("Unable to de-register cm-handle batch, retrying on each cm handle");
372 for (final String cmHandleId : tobeRemovedCmHandleBatch) {
373 final CmHandleRegistrationResponse cmHandleRegistrationResponse =
374 deleteCmHandleAndGetCmHandleRegistrationResponse(cmHandleId);
375 cmHandleRegistrationResponses.add(cmHandleRegistrationResponse);
376 if (cmHandleRegistrationResponse.getStatus() != CmHandleRegistrationResponse.Status.SUCCESS) {
377 notDeletedCmHandles.add(cmHandleId);
383 yangModelCmHandles.removeIf(yangModelCmHandle -> notDeletedCmHandles.contains(yangModelCmHandle.getId()));
384 updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETED);
385 removeEntriesFromAlternateIdCache(yangModelCmHandles);
387 return cmHandleRegistrationResponses;
390 private void removeEntriesFromAlternateIdCache(final Collection<YangModelCmHandle> yangModelCmHandles) {
391 for (final YangModelCmHandle yangModelCmHandle : yangModelCmHandles) {
392 cmHandleIdMapper.removeMapping(yangModelCmHandle.getId());
396 protected List<CmHandleRegistrationResponse> parseAndProcessUpgradedCmHandlesInRegistration(
397 final DmiPluginRegistration dmiPluginRegistration) {
399 final List<String> upgradedCmHandleIds = dmiPluginRegistration.getUpgradedCmHandles().getCmHandles();
401 final Map<YangModelCmHandle, CmHandleState> acceptedCmHandleStatePerCmHandle
402 = new HashMap<>(upgradedCmHandleIds.size());
404 final Map<NcmpResponseStatus, List<String>> failedCmHandlesPerResponseStatus
405 = new EnumMap<>(NcmpResponseStatus.class);
406 final List<String> nonExistingCmHandleIds = new ArrayList<>();
407 final List<String> nonReadyCmHandleIds = new ArrayList<>();
408 final List<String> invalidCmHandleIds = new ArrayList<>();
410 upgradedCmHandleIds.forEach(cmHandleId -> {
412 if (cmHandleQueries.cmHandleHasState(cmHandleId, CmHandleState.READY)) {
413 final YangModelCmHandle yangModelCmHandleWithUpgradeDetails
414 = createYangModelCmHandle(dmiPluginRegistration, cmHandleId);
415 acceptedCmHandleStatePerCmHandle.put(yangModelCmHandleWithUpgradeDetails, CmHandleState.LOCKED);
417 nonReadyCmHandleIds.add(cmHandleId);
419 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
420 log.error("Unable to find data node for cm handle id : {} , caused by : {}",
421 cmHandleId, dataNodeNotFoundException.getMessage());
422 nonExistingCmHandleIds.add(cmHandleId);
423 } catch (final DataValidationException dataValidationException) {
424 log.error("Unable to upgrade cm handle id: {}, caused by : {}",
425 cmHandleId, dataValidationException.getMessage());
426 invalidCmHandleIds.add(cmHandleId);
429 failedCmHandlesPerResponseStatus.put(CM_HANDLES_NOT_READY, nonReadyCmHandleIds);
430 failedCmHandlesPerResponseStatus.put(CM_HANDLES_NOT_FOUND, nonExistingCmHandleIds);
431 failedCmHandlesPerResponseStatus.put(CM_HANDLE_INVALID_ID, invalidCmHandleIds);
432 return mergeAllCmHandleResponses(acceptedCmHandleStatePerCmHandle, failedCmHandlesPerResponseStatus);
435 private static YangModelCmHandle createYangModelCmHandle(final DmiPluginRegistration dmiPluginRegistration,
436 final String cmHandleId) {
437 final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle();
438 ncmpServiceCmHandle.setCmHandleId(cmHandleId);
439 final String moduleSetTag = dmiPluginRegistration.getUpgradedCmHandles().getModuleSetTag();
440 final String lockReasonWithModuleSetTag = MessageFormat.format(
441 ModuleOperationsUtils.MODULE_SET_TAG_MESSAGE_FORMAT, moduleSetTag);
442 ncmpServiceCmHandle.setCompositeState(new CompositeStateBuilder().withCmHandleState(CmHandleState.READY)
443 .withLockReason(MODULE_UPGRADE, lockReasonWithModuleSetTag).build());
444 return YangModelCmHandle.toYangModelCmHandle(dmiPluginRegistration.getDmiPlugin(),
445 dmiPluginRegistration.getDmiDataPlugin(), dmiPluginRegistration.getDmiModelPlugin(),
446 ncmpServiceCmHandle, moduleSetTag, ncmpServiceCmHandle.getAlternateId());
449 private CmHandleRegistrationResponse deleteCmHandleAndGetCmHandleRegistrationResponse(final String cmHandleId) {
451 deleteCmHandleFromDbAndModuleSyncMap(cmHandleId);
452 return CmHandleRegistrationResponse.createSuccessResponse(cmHandleId);
453 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
454 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
455 cmHandleId, dataNodeNotFoundException.getMessage());
456 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND);
457 } catch (final DataValidationException dataValidationException) {
458 log.error("Unable to de-register cm-handle id: {}, caused by: {}",
459 cmHandleId, dataValidationException.getMessage());
460 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID);
461 } catch (final Exception exception) {
462 log.error("Unable to de-register cm-handle id : {} , caused by : {}", cmHandleId, exception.getMessage());
463 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception);
467 private void updateCmHandleStateBatch(final Collection<YangModelCmHandle> yangModelCmHandles,
468 final CmHandleState cmHandleState) {
469 final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>(yangModelCmHandles.size());
470 yangModelCmHandles.forEach(yangModelCmHandle -> cmHandleStatePerCmHandle.put(yangModelCmHandle, cmHandleState));
471 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
474 private void deleteCmHandleFromDbAndModuleSyncMap(final String cmHandleId) {
475 inventoryPersistence.deleteSchemaSetWithCascade(cmHandleId);
476 inventoryPersistence.deleteDataNode(NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId
478 removeDeletedCmHandleFromModuleSyncMap(cmHandleId);
481 private void batchDeleteCmHandlesFromDbAndModuleSyncMap(final Collection<String> tobeRemovedCmHandles) {
482 inventoryPersistence.deleteSchemaSetsWithCascade(tobeRemovedCmHandles);
483 inventoryPersistence.deleteDataNodes(mapCmHandleIdsToXpaths(tobeRemovedCmHandles));
484 tobeRemovedCmHandles.forEach(this::removeDeletedCmHandleFromModuleSyncMap);
487 private Collection<String> mapCmHandleIdsToXpaths(final Collection<String> cmHandles) {
488 return cmHandles.stream()
489 .map(cmHandleId -> NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId + "']")
490 .collect(Collectors.toSet());
493 // CPS-1239 Robustness cleaning of in progress cache
494 private void removeDeletedCmHandleFromModuleSyncMap(final String deletedCmHandleId) {
495 if (moduleSyncStartedOnCmHandles.remove(deletedCmHandleId) != null) {
496 log.debug("{} removed from in progress map", deletedCmHandleId);
500 private List<CmHandleRegistrationResponse> registerNewCmHandles(final List<YangModelCmHandle> yangModelCmHandles,
501 final Map<String, TrustLevel>
502 initialTrustLevelPerCmHandleId) {
503 final Set<String> cmHandleIds = initialTrustLevelPerCmHandleId.keySet();
505 lcmEventsCmHandleStateHandler.initiateStateAdvised(yangModelCmHandles);
506 trustLevelManager.handleInitialRegistrationOfTrustLevels(initialTrustLevelPerCmHandleId);
507 return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
508 } catch (final AlreadyDefinedException alreadyDefinedException) {
509 return CmHandleRegistrationResponse.createFailureResponses(
510 alreadyDefinedException.getAlreadyDefinedObjectNames(), CM_HANDLE_ALREADY_EXIST);
511 } catch (final Exception exception) {
512 return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, exception);
516 private List<CmHandleRegistrationResponse> mergeAllCmHandleResponses(
517 final Map<YangModelCmHandle, CmHandleState> acceptedCmHandleStatePerCmHandle,
518 final Map<NcmpResponseStatus, List<String>> failedCmHandlesPerResponseStatus) {
519 final List<CmHandleRegistrationResponse> cmHandleUpgradeResponses
520 = upgradeCmHandles(acceptedCmHandleStatePerCmHandle);
521 failedCmHandlesPerResponseStatus.forEach((ncmpResponseStatus, cmHandleIds) ->
522 cmHandleIds.forEach(cmHandleId -> cmHandleUpgradeResponses.add(CmHandleRegistrationResponse
523 .createFailureResponse(cmHandleId, ncmpResponseStatus))));
524 return cmHandleUpgradeResponses;
527 private List<CmHandleRegistrationResponse> upgradeCmHandles(final Map<YangModelCmHandle, CmHandleState>
528 cmHandleStatePerCmHandle) {
529 final List<String> cmHandleIds = getCmHandleIds(cmHandleStatePerCmHandle);
530 log.info("Moving cm handles : {} into locked (for upgrade) state.", cmHandleIds);
532 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
533 return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
534 } catch (final Exception e) {
535 log.error("Unable to update cmHandleIds : {} , caused by : {}", cmHandleIds, e.getMessage());
536 return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, e);
540 private static List<String> getCmHandleIds(final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle) {
541 return cmHandleStatePerCmHandle.keySet().stream().map(YangModelCmHandle::getId).toList();
544 private void setTrustLevelPerDmiPlugin(final DmiPluginRegistration dmiPluginRegistration) {
545 if (DmiPluginRegistration.isNullEmptyOrBlank(dmiPluginRegistration.getDmiDataPlugin())) {
546 trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiPlugin(), TrustLevel.COMPLETE);
548 trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiDataPlugin(), TrustLevel.COMPLETE);
552 private void cacheAlternateIds(final Collection<NcmpServiceCmHandle> ncmpServiceCmHandles) {
553 for (final NcmpServiceCmHandle ncmpServiceCmHandle : ncmpServiceCmHandles) {
554 if (!StringUtils.isEmpty(ncmpServiceCmHandle.getAlternateId())) {
555 cmHandleIdMapper.addMapping(ncmpServiceCmHandle.getCmHandleId(), ncmpServiceCmHandle.getAlternateId());