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.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 * Retrieve cm handles with details for the given query parameters.
194 * @param cmHandleQueryApiParameters cm handle query parameters
195 * @return cm handles with details
198 public Collection<NcmpServiceCmHandle> executeCmHandleSearch(
199 final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
200 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
201 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
202 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
203 return networkCmProxyCmHandleQueryService.queryCmHandles(cmHandleQueryServiceParameters);
207 * Retrieve cm handle ids for the given query parameters.
209 * @param cmHandleQueryApiParameters cm handle query parameters
210 * @return cm handle ids
213 public Collection<String> executeCmHandleIdSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
214 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
215 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
216 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
217 return networkCmProxyCmHandleQueryService.queryCmHandleIds(cmHandleQueryServiceParameters);
221 * Set the data sync enabled flag, along with the data sync state
222 * based on the data sync enabled boolean for the cm handle id provided.
224 * @param cmHandleId cm handle id
225 * @param dataSyncEnabledTargetValue data sync enabled flag
228 public void setDataSyncEnabled(final String cmHandleId, final Boolean dataSyncEnabledTargetValue) {
229 final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
230 if (dataSyncEnabledTargetValue.equals(compositeState.getDataSyncEnabled())) {
231 log.info("Data-Sync Enabled flag is already: {} ", dataSyncEnabledTargetValue);
234 if (CmHandleState.READY.equals(compositeState.getCmHandleState())) {
235 final DataStoreSyncState dataStoreSyncState = compositeState.getDataStores()
236 .getOperationalDataStore().getDataStoreSyncState();
237 if (Boolean.FALSE.equals(dataSyncEnabledTargetValue)
238 && DataStoreSyncState.SYNCHRONIZED.equals(dataStoreSyncState)) {
239 // TODO : This is hard-coded for onap dmi that need to be addressed
240 cpsDataService.deleteDataNode(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId,
241 "/netconf-state", OffsetDateTime.now());
243 CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabledTargetValue, compositeState);
244 inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
246 throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: "
247 + compositeState.getCmHandleState());
252 * Get all cm handle IDs by DMI plugin identifier.
254 * @param dmiPluginIdentifier DMI plugin identifier
255 * @return set of cm handle IDs
258 public Collection<String> getAllCmHandleIdsByDmiPluginIdentifier(final String dmiPluginIdentifier) {
259 return cmHandleQueries.getCmHandleIdsByDmiPluginIdentifier(dmiPluginIdentifier);
263 * Get all cm handle IDs by various properties.
265 * @param cmHandleQueryServiceParameters cm handle query parameters
266 * @return set of cm handle IDs
269 public Collection<String> executeCmHandleIdSearchForInventory(
270 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
271 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, InventoryQueryConditions.ALL_CONDITION_NAMES);
272 return networkCmProxyCmHandleQueryService.queryCmHandleIdsForInventory(cmHandleQueryServiceParameters);
276 * Retrieve cm handle details for a given cm handle.
278 * @param cmHandleId cm handle identifier
279 * @return cm handle details
282 public NcmpServiceCmHandle getNcmpServiceCmHandle(final String cmHandleId) {
283 return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(
284 inventoryPersistence.getYangModelCmHandle(cmHandleId));
288 * Get cm handle public properties for a given cm handle id.
290 * @param cmHandleId cm handle identifier
291 * @return cm handle public properties
294 public Map<String, String> getCmHandlePublicProperties(final String cmHandleId) {
295 final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
296 final List<YangModelCmHandle.Property> yangModelPublicProperties = yangModelCmHandle.getPublicProperties();
297 final Map<String, String> cmHandlePublicProperties = new HashMap<>();
298 YangDataConverter.asPropertiesMap(yangModelPublicProperties, cmHandlePublicProperties);
299 return cmHandlePublicProperties;
303 * Get cm handle composite state for a given cm handle id.
305 * @param cmHandleId cm handle identifier
306 * @return cm handle state
309 public CompositeState getCmHandleCompositeState(final String cmHandleId) {
310 return inventoryPersistence.getYangModelCmHandle(cmHandleId).getCompositeState();
314 * THis method registers a cm handle and initiates modules sync.
316 * @param dmiPluginRegistration dmi plugin registration information.
317 * @return cm-handle registration response for create cm-handle requests.
319 public List<CmHandleRegistrationResponse> parseAndProcessCreatedCmHandlesInRegistration(
320 final DmiPluginRegistration dmiPluginRegistration) {
321 final List<NcmpServiceCmHandle> cmHandlesToBeCreated = dmiPluginRegistration.getCreatedCmHandles();
322 final Map<String, TrustLevel> initialTrustLevelPerCmHandleId = new HashMap<>(cmHandlesToBeCreated.size());
323 final List<YangModelCmHandle> yangModelCmHandles = new ArrayList<>(cmHandlesToBeCreated.size());
325 .forEach(cmHandle -> {
326 final YangModelCmHandle yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle(
327 dmiPluginRegistration.getDmiPlugin(),
328 dmiPluginRegistration.getDmiDataPlugin(),
329 dmiPluginRegistration.getDmiModelPlugin(),
331 cmHandle.getModuleSetTag(),
332 cmHandle.getAlternateId());
333 yangModelCmHandles.add(yangModelCmHandle);
334 initialTrustLevelPerCmHandleId.put(cmHandle.getCmHandleId(), cmHandle.getRegistrationTrustLevel());
336 return registerNewCmHandles(yangModelCmHandles, initialTrustLevelPerCmHandleId);
339 protected List<CmHandleRegistrationResponse> parseAndProcessDeletedCmHandlesInRegistration(
340 final List<String> tobeRemovedCmHandles) {
341 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
342 new ArrayList<>(tobeRemovedCmHandles.size());
343 final Collection<YangModelCmHandle> yangModelCmHandles =
344 inventoryPersistence.getYangModelCmHandles(tobeRemovedCmHandles);
346 updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETING);
348 final Set<String> notDeletedCmHandles = new HashSet<>();
349 for (final List<String> tobeRemovedCmHandleBatch : Lists.partition(tobeRemovedCmHandles, DELETE_BATCH_SIZE)) {
351 batchDeleteCmHandlesFromDbAndModuleSyncMap(tobeRemovedCmHandleBatch);
352 tobeRemovedCmHandleBatch.forEach(cmHandleId ->
353 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId)));
355 } catch (final RuntimeException batchException) {
356 log.error("Unable to de-register cm-handle batch, retrying on each cm handle");
357 for (final String cmHandleId : tobeRemovedCmHandleBatch) {
358 final CmHandleRegistrationResponse cmHandleRegistrationResponse =
359 deleteCmHandleAndGetCmHandleRegistrationResponse(cmHandleId);
360 cmHandleRegistrationResponses.add(cmHandleRegistrationResponse);
361 if (cmHandleRegistrationResponse.getStatus() != CmHandleRegistrationResponse.Status.SUCCESS) {
362 notDeletedCmHandles.add(cmHandleId);
368 yangModelCmHandles.removeIf(yangModelCmHandle -> notDeletedCmHandles.contains(yangModelCmHandle.getId()));
369 updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETED);
371 return cmHandleRegistrationResponses;
374 protected List<CmHandleRegistrationResponse> parseAndProcessUpgradedCmHandlesInRegistration(
375 final DmiPluginRegistration dmiPluginRegistration) {
377 final List<String> upgradedCmHandleIds = dmiPluginRegistration.getUpgradedCmHandles().getCmHandles();
379 final Map<YangModelCmHandle, CmHandleState> acceptedCmHandleStatePerCmHandle
380 = new HashMap<>(upgradedCmHandleIds.size());
382 final Map<NcmpResponseStatus, List<String>> failedCmHandlesPerResponseStatus
383 = new EnumMap<>(NcmpResponseStatus.class);
384 final List<String> nonExistingCmHandleIds = new ArrayList<>();
385 final List<String> nonReadyCmHandleIds = new ArrayList<>();
386 final List<String> invalidCmHandleIds = new ArrayList<>();
388 upgradedCmHandleIds.forEach(cmHandleId -> {
390 if (cmHandleQueries.cmHandleHasState(cmHandleId, CmHandleState.READY)) {
391 final YangModelCmHandle yangModelCmHandleWithUpgradeDetails
392 = createYangModelCmHandle(dmiPluginRegistration, cmHandleId);
393 acceptedCmHandleStatePerCmHandle.put(yangModelCmHandleWithUpgradeDetails, CmHandleState.LOCKED);
395 nonReadyCmHandleIds.add(cmHandleId);
397 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
398 log.error("Unable to find data node for cm handle id : {} , caused by : {}",
399 cmHandleId, dataNodeNotFoundException.getMessage());
400 nonExistingCmHandleIds.add(cmHandleId);
401 } catch (final DataValidationException dataValidationException) {
402 log.error("Unable to upgrade cm handle id: {}, caused by : {}",
403 cmHandleId, dataValidationException.getMessage());
404 invalidCmHandleIds.add(cmHandleId);
407 failedCmHandlesPerResponseStatus.put(CM_HANDLES_NOT_READY, nonReadyCmHandleIds);
408 failedCmHandlesPerResponseStatus.put(CM_HANDLES_NOT_FOUND, nonExistingCmHandleIds);
409 failedCmHandlesPerResponseStatus.put(CM_HANDLE_INVALID_ID, invalidCmHandleIds);
410 return mergeAllCmHandleResponses(acceptedCmHandleStatePerCmHandle, failedCmHandlesPerResponseStatus);
413 private static YangModelCmHandle createYangModelCmHandle(final DmiPluginRegistration dmiPluginRegistration,
414 final String cmHandleId) {
415 final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle();
416 ncmpServiceCmHandle.setCmHandleId(cmHandleId);
417 final String moduleSetTag = dmiPluginRegistration.getUpgradedCmHandles().getModuleSetTag();
418 final String lockReasonWithModuleSetTag = MessageFormat.format(
419 ModuleOperationsUtils.MODULE_SET_TAG_MESSAGE_FORMAT, moduleSetTag);
420 ncmpServiceCmHandle.setCompositeState(new CompositeStateBuilder().withCmHandleState(CmHandleState.READY)
421 .withLockReason(MODULE_UPGRADE, lockReasonWithModuleSetTag).build());
422 return YangModelCmHandle.toYangModelCmHandle(dmiPluginRegistration.getDmiPlugin(),
423 dmiPluginRegistration.getDmiDataPlugin(), dmiPluginRegistration.getDmiModelPlugin(),
424 ncmpServiceCmHandle, moduleSetTag, ncmpServiceCmHandle.getAlternateId());
427 private CmHandleRegistrationResponse deleteCmHandleAndGetCmHandleRegistrationResponse(final String cmHandleId) {
429 deleteCmHandleFromDbAndModuleSyncMap(cmHandleId);
430 return CmHandleRegistrationResponse.createSuccessResponse(cmHandleId);
431 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
432 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
433 cmHandleId, dataNodeNotFoundException.getMessage());
434 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND);
435 } catch (final DataValidationException dataValidationException) {
436 log.error("Unable to de-register cm-handle id: {}, caused by: {}",
437 cmHandleId, dataValidationException.getMessage());
438 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID);
439 } catch (final Exception exception) {
440 log.error("Unable to de-register cm-handle id : {} , caused by : {}", cmHandleId, exception.getMessage());
441 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception);
445 private void updateCmHandleStateBatch(final Collection<YangModelCmHandle> yangModelCmHandles,
446 final CmHandleState cmHandleState) {
447 final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>(yangModelCmHandles.size());
448 yangModelCmHandles.forEach(yangModelCmHandle -> cmHandleStatePerCmHandle.put(yangModelCmHandle, cmHandleState));
449 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
452 private void deleteCmHandleFromDbAndModuleSyncMap(final String cmHandleId) {
453 inventoryPersistence.deleteSchemaSetWithCascade(cmHandleId);
454 inventoryPersistence.deleteDataNode(NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId
456 removeDeletedCmHandleFromModuleSyncMap(cmHandleId);
459 private void batchDeleteCmHandlesFromDbAndModuleSyncMap(final Collection<String> tobeRemovedCmHandles) {
460 inventoryPersistence.deleteSchemaSetsWithCascade(tobeRemovedCmHandles);
461 inventoryPersistence.deleteDataNodes(mapCmHandleIdsToXpaths(tobeRemovedCmHandles));
462 tobeRemovedCmHandles.forEach(this::removeDeletedCmHandleFromModuleSyncMap);
465 private Collection<String> mapCmHandleIdsToXpaths(final Collection<String> cmHandles) {
466 return cmHandles.stream()
467 .map(cmHandleId -> NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId + "']")
468 .collect(Collectors.toSet());
471 // CPS-1239 Robustness cleaning of in progress cache
472 private void removeDeletedCmHandleFromModuleSyncMap(final String deletedCmHandleId) {
473 if (moduleSyncStartedOnCmHandles.remove(deletedCmHandleId) != null) {
474 log.debug("{} removed from in progress map", deletedCmHandleId);
478 private List<CmHandleRegistrationResponse> registerNewCmHandles(final List<YangModelCmHandle> yangModelCmHandles,
479 final Map<String, TrustLevel>
480 initialTrustLevelPerCmHandleId) {
481 final Set<String> cmHandleIds = initialTrustLevelPerCmHandleId.keySet();
483 lcmEventsCmHandleStateHandler.initiateStateAdvised(yangModelCmHandles);
484 trustLevelManager.handleInitialRegistrationOfTrustLevels(initialTrustLevelPerCmHandleId);
485 return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
486 } catch (final AlreadyDefinedException alreadyDefinedException) {
487 return CmHandleRegistrationResponse.createFailureResponses(
488 alreadyDefinedException.getAlreadyDefinedObjectNames(), CM_HANDLE_ALREADY_EXIST);
489 } catch (final Exception exception) {
490 return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, exception);
494 private List<CmHandleRegistrationResponse> mergeAllCmHandleResponses(
495 final Map<YangModelCmHandle, CmHandleState> acceptedCmHandleStatePerCmHandle,
496 final Map<NcmpResponseStatus, List<String>> failedCmHandlesPerResponseStatus) {
497 final List<CmHandleRegistrationResponse> cmHandleUpgradeResponses
498 = upgradeCmHandles(acceptedCmHandleStatePerCmHandle);
499 failedCmHandlesPerResponseStatus.forEach((ncmpResponseStatus, cmHandleIds) ->
500 cmHandleIds.forEach(cmHandleId -> cmHandleUpgradeResponses.add(CmHandleRegistrationResponse
501 .createFailureResponse(cmHandleId, ncmpResponseStatus))));
502 return cmHandleUpgradeResponses;
505 private List<CmHandleRegistrationResponse> upgradeCmHandles(final Map<YangModelCmHandle, CmHandleState>
506 cmHandleStatePerCmHandle) {
507 final List<String> cmHandleIds = getCmHandleIds(cmHandleStatePerCmHandle);
508 log.info("Moving cm handles : {} into locked (for upgrade) state.", cmHandleIds);
510 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
511 return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
512 } catch (final Exception e) {
513 log.error("Unable to update cmHandleIds : {} , caused by : {}", cmHandleIds, e.getMessage());
514 return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, e);
518 private static List<String> getCmHandleIds(final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle) {
519 return cmHandleStatePerCmHandle.keySet().stream().map(YangModelCmHandle::getId).toList();
522 private void setTrustLevelPerDmiPlugin(final DmiPluginRegistration dmiPluginRegistration) {
523 if (DmiPluginRegistration.isNullEmptyOrBlank(dmiPluginRegistration.getDmiDataPlugin())) {
524 trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiPlugin(), TrustLevel.COMPLETE);
526 trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiDataPlugin(), TrustLevel.COMPLETE);