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.operations.DmiDataOperations;
64 import org.onap.cps.ncmp.api.impl.operations.OperationType;
65 import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevel;
66 import org.onap.cps.ncmp.api.impl.utils.CmHandleQueryConditions;
67 import org.onap.cps.ncmp.api.impl.utils.InventoryQueryConditions;
68 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
69 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
70 import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters;
71 import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters;
72 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse;
73 import org.onap.cps.ncmp.api.models.DataOperationRequest;
74 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
75 import org.onap.cps.ncmp.api.models.DmiPluginRegistrationResponse;
76 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
77 import org.onap.cps.spi.FetchDescendantsOption;
78 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
79 import org.onap.cps.spi.exceptions.CpsException;
80 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
81 import org.onap.cps.spi.exceptions.DataValidationException;
82 import org.onap.cps.spi.model.ModuleDefinition;
83 import org.onap.cps.spi.model.ModuleReference;
84 import org.onap.cps.utils.JsonObjectMapper;
85 import org.springframework.http.ResponseEntity;
86 import org.springframework.stereotype.Service;
90 @RequiredArgsConstructor
91 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
93 private static final int DELETE_BATCH_SIZE = 100;
94 private final JsonObjectMapper jsonObjectMapper;
95 private final DmiDataOperations dmiDataOperations;
96 private final NetworkCmProxyDataServicePropertyHandler networkCmProxyDataServicePropertyHandler;
97 private final InventoryPersistence inventoryPersistence;
98 private final CmHandleQueries cmHandleQueries;
99 private final NetworkCmProxyCmHandleQueryService networkCmProxyCmHandleQueryService;
100 private final LcmEventsCmHandleStateHandler lcmEventsCmHandleStateHandler;
101 private final CpsDataService cpsDataService;
102 private final IMap<String, Object> moduleSyncStartedOnCmHandles;
103 private final Map<String, TrustLevel> trustLevelPerCmHandle;
106 public DmiPluginRegistrationResponse updateDmiRegistrationAndSyncModule(
107 final DmiPluginRegistration dmiPluginRegistration) {
108 dmiPluginRegistration.validateDmiPluginRegistration();
109 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse = new DmiPluginRegistrationResponse();
111 if (!dmiPluginRegistration.getRemovedCmHandles().isEmpty()) {
112 dmiPluginRegistrationResponse.setRemovedCmHandles(
113 parseAndProcessDeletedCmHandlesInRegistration(dmiPluginRegistration.getRemovedCmHandles()));
116 if (!dmiPluginRegistration.getCreatedCmHandles().isEmpty()) {
117 populateTrustLevelPerCmHandleCache(dmiPluginRegistration);
118 dmiPluginRegistrationResponse.setCreatedCmHandles(
119 parseAndProcessCreatedCmHandlesInRegistration(dmiPluginRegistration));
121 if (!dmiPluginRegistration.getUpdatedCmHandles().isEmpty()) {
122 dmiPluginRegistrationResponse.setUpdatedCmHandles(
123 networkCmProxyDataServicePropertyHandler
124 .updateCmHandleProperties(dmiPluginRegistration.getUpdatedCmHandles()));
126 if (dmiPluginRegistration.getUpgradedCmHandles() != null
127 && !dmiPluginRegistration.getUpgradedCmHandles().getCmHandles().isEmpty()) {
128 dmiPluginRegistrationResponse.setUpgradedCmHandles(
129 parseAndProcessUpgradedCmHandlesInRegistration(dmiPluginRegistration));
131 return dmiPluginRegistrationResponse;
135 public Object getResourceDataForCmHandle(final String datastoreName,
136 final String cmHandleId,
137 final String resourceIdentifier,
138 final String optionsParamInQuery,
139 final String topicParamInQuery,
140 final String requestId) {
141 final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(datastoreName, cmHandleId,
146 return responseEntity.getBody();
150 public Object getResourceDataForCmHandle(final String datastoreName,
151 final String cmHandleId,
152 final String resourceIdentifier,
153 final FetchDescendantsOption fetchDescendantsOption) {
154 return cpsDataService.getDataNodes(datastoreName, cmHandleId, resourceIdentifier,
155 fetchDescendantsOption).iterator().next();
159 public void executeDataOperationForCmHandles(final String topicParamInQuery,
160 final DataOperationRequest
161 dataOperationRequest,
162 final String requestId) {
163 dmiDataOperations.requestResourceDataFromDmi(topicParamInQuery, dataOperationRequest, requestId);
167 public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
168 final String resourceIdentifier,
169 final OperationType operationType,
170 final String requestData,
171 final String dataType) {
172 return dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier,
173 operationType, requestData, dataType);
177 public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
178 return inventoryPersistence.getYangResourcesModuleReferences(cmHandleId);
182 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
183 return inventoryPersistence.getModuleDefinitionsByCmHandleId(cmHandleId);
187 * Retrieve cm handles with details for the given query parameters.
189 * @param cmHandleQueryApiParameters cm handle query parameters
190 * @return cm handles with details
193 public Collection<NcmpServiceCmHandle> executeCmHandleSearch(
194 final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
195 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
196 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
197 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
198 return networkCmProxyCmHandleQueryService.queryCmHandles(cmHandleQueryServiceParameters);
202 * Retrieve cm handle ids for the given query parameters.
204 * @param cmHandleQueryApiParameters cm handle query parameters
205 * @return cm handle ids
208 public Collection<String> executeCmHandleIdSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
209 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
210 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
211 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
212 return networkCmProxyCmHandleQueryService.queryCmHandleIds(cmHandleQueryServiceParameters);
216 * Set the data sync enabled flag, along with the data sync state
217 * based on the data sync enabled boolean for the cm handle id provided.
219 * @param cmHandleId cm handle id
220 * @param dataSyncEnabledTargetValue data sync enabled flag
223 public void setDataSyncEnabled(final String cmHandleId, final Boolean dataSyncEnabledTargetValue) {
224 final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
225 if (dataSyncEnabledTargetValue.equals(compositeState.getDataSyncEnabled())) {
226 log.info("Data-Sync Enabled flag is already: {} ", dataSyncEnabledTargetValue);
229 if (CmHandleState.READY.equals(compositeState.getCmHandleState())) {
230 final DataStoreSyncState dataStoreSyncState = compositeState.getDataStores()
231 .getOperationalDataStore().getDataStoreSyncState();
232 if (Boolean.FALSE.equals(dataSyncEnabledTargetValue)
233 && DataStoreSyncState.SYNCHRONIZED.equals(dataStoreSyncState)) {
234 // TODO : This is hard-coded for onap dmi that need to be addressed
235 cpsDataService.deleteDataNode(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId,
236 "/netconf-state", OffsetDateTime.now());
238 CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabledTargetValue, compositeState);
239 inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
241 throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: "
242 + compositeState.getCmHandleState());
247 * Get all cm handle IDs by DMI plugin identifier.
249 * @param dmiPluginIdentifier DMI plugin identifier
250 * @return set of cm handle IDs
253 public Collection<String> getAllCmHandleIdsByDmiPluginIdentifier(final String dmiPluginIdentifier) {
254 return cmHandleQueries.getCmHandleIdsByDmiPluginIdentifier(dmiPluginIdentifier);
258 * Get all cm handle IDs by various properties.
260 * @param cmHandleQueryServiceParameters cm handle query parameters
261 * @return set of cm handle IDs
264 public Collection<String> executeCmHandleIdSearchForInventory(
265 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
266 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, InventoryQueryConditions.ALL_CONDITION_NAMES);
267 return networkCmProxyCmHandleQueryService.queryCmHandleIdsForInventory(cmHandleQueryServiceParameters);
271 * Retrieve cm handle details for a given cm handle.
273 * @param cmHandleId cm handle identifier
274 * @return cm handle details
277 public NcmpServiceCmHandle getNcmpServiceCmHandle(final String cmHandleId) {
278 return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(
279 inventoryPersistence.getYangModelCmHandle(cmHandleId));
283 * Get cm handle public properties for a given cm handle id.
285 * @param cmHandleId cm handle identifier
286 * @return cm handle public properties
289 public Map<String, String> getCmHandlePublicProperties(final String cmHandleId) {
290 final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
291 final List<YangModelCmHandle.Property> yangModelPublicProperties = yangModelCmHandle.getPublicProperties();
292 final Map<String, String> cmHandlePublicProperties = new HashMap<>();
293 YangDataConverter.asPropertiesMap(yangModelPublicProperties, cmHandlePublicProperties);
294 return cmHandlePublicProperties;
298 * Get cm handle composite state for a given cm handle id.
300 * @param cmHandleId cm handle identifier
301 * @return cm handle state
304 public CompositeState getCmHandleCompositeState(final String cmHandleId) {
305 return inventoryPersistence.getYangModelCmHandle(cmHandleId).getCompositeState();
309 * THis method registers a cm handle and initiates modules sync.
311 * @param dmiPluginRegistration dmi plugin registration information.
312 * @return cm-handle registration response for create cm-handle requests.
314 public List<CmHandleRegistrationResponse> parseAndProcessCreatedCmHandlesInRegistration(
315 final DmiPluginRegistration dmiPluginRegistration) {
316 final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>();
317 dmiPluginRegistration.getCreatedCmHandles()
318 .forEach(cmHandle -> {
319 final YangModelCmHandle yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle(
320 dmiPluginRegistration.getDmiPlugin(),
321 dmiPluginRegistration.getDmiDataPlugin(),
322 dmiPluginRegistration.getDmiModelPlugin(),
324 cmHandle.getModuleSetTag());
325 cmHandleStatePerCmHandle.put(yangModelCmHandle, CmHandleState.ADVISED);
327 return registerNewCmHandles(cmHandleStatePerCmHandle);
330 protected List<CmHandleRegistrationResponse> parseAndProcessDeletedCmHandlesInRegistration(
331 final List<String> tobeRemovedCmHandles) {
332 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
333 new ArrayList<>(tobeRemovedCmHandles.size());
334 final Collection<YangModelCmHandle> yangModelCmHandles =
335 inventoryPersistence.getYangModelCmHandles(tobeRemovedCmHandles);
337 updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETING);
339 final Set<String> notDeletedCmHandles = new HashSet<>();
340 for (final List<String> tobeRemovedCmHandleBatch : Lists.partition(tobeRemovedCmHandles, DELETE_BATCH_SIZE)) {
342 batchDeleteCmHandlesFromDbAndModuleSyncMap(tobeRemovedCmHandleBatch);
343 tobeRemovedCmHandleBatch.forEach(cmHandleId ->
344 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId)));
346 } catch (final RuntimeException batchException) {
347 log.error("Unable to de-register cm-handle batch, retrying on each cm handle");
348 for (final String cmHandleId : tobeRemovedCmHandleBatch) {
349 final CmHandleRegistrationResponse cmHandleRegistrationResponse =
350 deleteCmHandleAndGetCmHandleRegistrationResponse(cmHandleId);
351 cmHandleRegistrationResponses.add(cmHandleRegistrationResponse);
352 if (cmHandleRegistrationResponse.getStatus() != CmHandleRegistrationResponse.Status.SUCCESS) {
353 notDeletedCmHandles.add(cmHandleId);
359 yangModelCmHandles.removeIf(yangModelCmHandle -> notDeletedCmHandles.contains(yangModelCmHandle.getId()));
360 updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETED);
362 return cmHandleRegistrationResponses;
365 protected List<CmHandleRegistrationResponse> parseAndProcessUpgradedCmHandlesInRegistration(
366 final DmiPluginRegistration dmiPluginRegistration) {
368 final List<String> upgradedCmHandleIds = dmiPluginRegistration.getUpgradedCmHandles().getCmHandles();
370 final Map<YangModelCmHandle, CmHandleState> acceptedCmHandleStatePerCmHandle
371 = new HashMap<>(upgradedCmHandleIds.size());
373 final Map<NcmpResponseStatus, List<String>> failedCmHandlesPerResponseStatus
374 = new EnumMap<>(NcmpResponseStatus.class);
375 final List<String> nonExistingCmHandleIds = new ArrayList<>();
376 final List<String> nonReadyCmHandleIds = new ArrayList<>();
377 final List<String> invalidCmHandleIds = new ArrayList<>();
379 upgradedCmHandleIds.forEach(cmHandleId -> {
381 if (cmHandleQueries.cmHandleHasState(cmHandleId, CmHandleState.READY)) {
382 final YangModelCmHandle yangModelCmHandleWithUpgradeDetails
383 = createYangModelCmHandle(dmiPluginRegistration, cmHandleId);
384 acceptedCmHandleStatePerCmHandle.put(yangModelCmHandleWithUpgradeDetails, CmHandleState.LOCKED);
386 nonReadyCmHandleIds.add(cmHandleId);
388 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
389 log.error("Unable to find data node for cm handle id : {} , caused by : {}",
390 cmHandleId, dataNodeNotFoundException.getMessage());
391 nonExistingCmHandleIds.add(cmHandleId);
392 } catch (final DataValidationException dataValidationException) {
393 log.error("Unable to upgrade cm handle id: {}, caused by : {}",
394 cmHandleId, dataValidationException.getMessage());
395 invalidCmHandleIds.add(cmHandleId);
398 failedCmHandlesPerResponseStatus.put(CM_HANDLES_NOT_READY, nonReadyCmHandleIds);
399 failedCmHandlesPerResponseStatus.put(CM_HANDLES_NOT_FOUND, nonExistingCmHandleIds);
400 failedCmHandlesPerResponseStatus.put(CM_HANDLE_INVALID_ID, invalidCmHandleIds);
401 return mergeAllCmHandleResponses(acceptedCmHandleStatePerCmHandle, failedCmHandlesPerResponseStatus);
404 private static YangModelCmHandle createYangModelCmHandle(final DmiPluginRegistration dmiPluginRegistration,
405 final String cmHandleId) {
406 final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle();
407 ncmpServiceCmHandle.setCmHandleId(cmHandleId);
408 final String moduleSetTag = dmiPluginRegistration.getUpgradedCmHandles().getModuleSetTag();
409 final String lockReasonWithModuleSetTag = MessageFormat.format("ModuleSetTag: {0}", moduleSetTag);
410 ncmpServiceCmHandle.setCompositeState(new CompositeStateBuilder().withCmHandleState(CmHandleState.READY)
411 .withLockReason(MODULE_UPGRADE, lockReasonWithModuleSetTag).build());
412 return YangModelCmHandle.toYangModelCmHandle(dmiPluginRegistration.getDmiPlugin(),
413 dmiPluginRegistration.getDmiDataPlugin(), dmiPluginRegistration.getDmiModelPlugin(),
414 ncmpServiceCmHandle, moduleSetTag);
417 private CmHandleRegistrationResponse deleteCmHandleAndGetCmHandleRegistrationResponse(final String cmHandleId) {
419 deleteCmHandleFromDbAndModuleSyncMap(cmHandleId);
420 return CmHandleRegistrationResponse.createSuccessResponse(cmHandleId);
421 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
422 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
423 cmHandleId, dataNodeNotFoundException.getMessage());
424 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND);
425 } catch (final DataValidationException dataValidationException) {
426 log.error("Unable to de-register cm-handle id: {}, caused by: {}",
427 cmHandleId, dataValidationException.getMessage());
428 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID);
429 } catch (final Exception exception) {
430 log.error("Unable to de-register cm-handle id : {} , caused by : {}", cmHandleId, exception.getMessage());
431 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception);
435 private void updateCmHandleStateBatch(final Collection<YangModelCmHandle> yangModelCmHandles,
436 final CmHandleState cmHandleState) {
437 final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>(yangModelCmHandles.size());
438 yangModelCmHandles.forEach(yangModelCmHandle -> cmHandleStatePerCmHandle.put(yangModelCmHandle, cmHandleState));
439 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
442 private void deleteCmHandleFromDbAndModuleSyncMap(final String cmHandleId) {
443 inventoryPersistence.deleteSchemaSetWithCascade(cmHandleId);
444 inventoryPersistence.deleteDataNode(NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId
446 removeDeletedCmHandleFromModuleSyncMap(cmHandleId);
449 private void batchDeleteCmHandlesFromDbAndModuleSyncMap(final Collection<String> tobeRemovedCmHandles) {
450 inventoryPersistence.deleteSchemaSetsWithCascade(tobeRemovedCmHandles);
451 inventoryPersistence.deleteDataNodes(mapCmHandleIdsToXpaths(tobeRemovedCmHandles));
452 tobeRemovedCmHandles.forEach(this::removeDeletedCmHandleFromModuleSyncMap);
455 private Collection<String> mapCmHandleIdsToXpaths(final Collection<String> cmHandles) {
456 return cmHandles.stream()
457 .map(cmHandleId -> NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId + "']")
458 .collect(Collectors.toSet());
461 // CPS-1239 Robustness cleaning of in progress cache
462 private void removeDeletedCmHandleFromModuleSyncMap(final String deletedCmHandleId) {
463 if (moduleSyncStartedOnCmHandles.remove(deletedCmHandleId) != null) {
464 log.debug("{} removed from in progress map", deletedCmHandleId);
468 private List<CmHandleRegistrationResponse> registerNewCmHandles(final Map<YangModelCmHandle, CmHandleState>
469 cmHandleStatePerCmHandle) {
470 final List<String> cmHandleIds = getCmHandleIds(cmHandleStatePerCmHandle);
472 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
473 return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
474 } catch (final AlreadyDefinedException alreadyDefinedException) {
475 return CmHandleRegistrationResponse.createFailureResponses(
476 alreadyDefinedException.getAlreadyDefinedObjectNames(), CM_HANDLE_ALREADY_EXIST);
477 } catch (final Exception exception) {
478 return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, exception);
482 private List<CmHandleRegistrationResponse> mergeAllCmHandleResponses(
483 final Map<YangModelCmHandle, CmHandleState> acceptedCmHandleStatePerCmHandle,
484 final Map<NcmpResponseStatus, List<String>> failedCmHandlesPerResponseStatus) {
485 final List<CmHandleRegistrationResponse> cmHandleUpgradeResponses
486 = upgradeCmHandles(acceptedCmHandleStatePerCmHandle);
487 failedCmHandlesPerResponseStatus.forEach((ncmpResponseStatus, cmHandleIds) ->
488 cmHandleIds.forEach(cmHandleId -> cmHandleUpgradeResponses.add(CmHandleRegistrationResponse
489 .createFailureResponse(cmHandleId, ncmpResponseStatus))));
490 return cmHandleUpgradeResponses;
493 private List<CmHandleRegistrationResponse> upgradeCmHandles(final Map<YangModelCmHandle, CmHandleState>
494 cmHandleStatePerCmHandle) {
495 final List<String> cmHandleIds = getCmHandleIds(cmHandleStatePerCmHandle);
496 log.info("Moving cm handles : {} into locked (for upgrade) state.", cmHandleIds);
498 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
499 return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
500 } catch (final Exception e) {
501 log.error("Unable to update cmHandleIds : {} , caused by : {}", cmHandleIds, e.getMessage());
502 return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, e);
506 private static List<String> getCmHandleIds(final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle) {
507 return cmHandleStatePerCmHandle.keySet().stream().map(YangModelCmHandle::getId).toList();
510 private void populateTrustLevelPerCmHandleCache(final DmiPluginRegistration dmiPluginRegistration) {
511 for (final NcmpServiceCmHandle cmHandle: dmiPluginRegistration.getCreatedCmHandles()) {
512 if (cmHandle.getRegistrationTrustLevel() == null) {
513 if (trustLevelPerCmHandle.containsKey(cmHandle.getCmHandleId())) {
514 log.warn("CmHandle : {}, Already exists, Initial trustLevel ignored.", cmHandle.getCmHandleId());
516 trustLevelPerCmHandle.put(cmHandle.getCmHandleId(), TrustLevel.COMPLETE);
519 trustLevelPerCmHandle.put(cmHandle.getCmHandleId(), cmHandle.getRegistrationTrustLevel());