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.ALTERNATE_ID_ALREADY_ASSOCIATED;
28 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLES_NOT_FOUND;
29 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLES_NOT_READY;
30 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLE_ALREADY_EXIST;
31 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLE_INVALID_ID;
32 import static org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory.MODULE_UPGRADE;
33 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DMI_REGISTRY_PARENT;
34 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME;
35 import static org.onap.cps.ncmp.api.impl.utils.RestQueryParametersValidator.validateCmHandleQueryParameters;
37 import com.google.common.collect.Lists;
38 import com.hazelcast.map.IMap;
39 import java.time.OffsetDateTime;
40 import java.util.ArrayList;
41 import java.util.Collection;
42 import java.util.HashMap;
43 import java.util.HashSet;
44 import java.util.List;
47 import java.util.stream.Collectors;
48 import lombok.RequiredArgsConstructor;
49 import lombok.extern.slf4j.Slf4j;
50 import org.apache.commons.lang3.StringUtils;
51 import org.onap.cps.api.CpsDataService;
52 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandleQueryService;
53 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
54 import org.onap.cps.ncmp.api.impl.events.lcm.LcmEventsCmHandleStateHandler;
55 import org.onap.cps.ncmp.api.impl.inventory.CmHandleQueries;
56 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState;
57 import org.onap.cps.ncmp.api.impl.inventory.CompositeState;
58 import org.onap.cps.ncmp.api.impl.inventory.CompositeStateBuilder;
59 import org.onap.cps.ncmp.api.impl.inventory.CompositeStateUtils;
60 import org.onap.cps.ncmp.api.impl.inventory.DataStoreSyncState;
61 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence;
62 import org.onap.cps.ncmp.api.impl.inventory.sync.ModuleOperationsUtils;
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.trustlevel.TrustLevelManager;
67 import org.onap.cps.ncmp.api.impl.utils.AlternateIdChecker;
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;
107 private final AlternateIdChecker alternateIdChecker;
110 public DmiPluginRegistrationResponse updateDmiRegistrationAndSyncModule(
111 final DmiPluginRegistration dmiPluginRegistration) {
113 dmiPluginRegistration.validateDmiPluginRegistration();
114 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse = new DmiPluginRegistrationResponse();
116 setTrustLevelPerDmiPlugin(dmiPluginRegistration);
118 processRemovedCmHandles(dmiPluginRegistration, dmiPluginRegistrationResponse);
120 processCreatedCmHandles(dmiPluginRegistration, dmiPluginRegistrationResponse);
122 processUpdatedCmHandles(dmiPluginRegistration, dmiPluginRegistrationResponse);
124 processUpgradedCmHandles(dmiPluginRegistration, dmiPluginRegistrationResponse);
126 return dmiPluginRegistrationResponse;
130 public Object getResourceDataForCmHandle(final String datastoreName,
131 final String cmHandleId,
132 final String resourceIdentifier,
133 final String optionsParamInQuery,
134 final String topicParamInQuery,
135 final String requestId) {
136 final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(datastoreName, cmHandleId,
141 return responseEntity.getBody();
145 public Object getResourceDataForCmHandle(final String datastoreName,
146 final String cmHandleId,
147 final String resourceIdentifier,
148 final FetchDescendantsOption fetchDescendantsOption) {
149 return cpsDataService.getDataNodes(datastoreName, cmHandleId, resourceIdentifier,
150 fetchDescendantsOption).iterator().next();
154 public void executeDataOperationForCmHandles(final String topicParamInQuery,
155 final DataOperationRequest
156 dataOperationRequest,
157 final String requestId) {
158 dmiDataOperations.requestResourceDataFromDmi(topicParamInQuery, dataOperationRequest, requestId);
162 public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
163 final String resourceIdentifier,
164 final OperationType operationType,
165 final String requestData,
166 final String dataType) {
167 return dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier,
168 operationType, requestData, dataType);
172 public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
173 return inventoryPersistence.getYangResourcesModuleReferences(cmHandleId);
177 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
178 return inventoryPersistence.getModuleDefinitionsByCmHandleId(cmHandleId);
182 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleAndModule(final String cmHandleId,
183 final String moduleName,
184 final String moduleRevision) {
185 return inventoryPersistence.getModuleDefinitionsByCmHandleAndModule(cmHandleId, moduleName, moduleRevision);
189 * Retrieve cm handles with details for the given query parameters.
191 * @param cmHandleQueryApiParameters cm handle query parameters
192 * @return cm handles with details
195 public Collection<NcmpServiceCmHandle> executeCmHandleSearch(
196 final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
197 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
198 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
199 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
200 return networkCmProxyCmHandleQueryService.queryCmHandles(cmHandleQueryServiceParameters);
204 * Retrieve cm handle ids for the given query parameters.
206 * @param cmHandleQueryApiParameters cm handle query parameters
207 * @return cm handle ids
210 public Collection<String> executeCmHandleIdSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
211 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
212 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
213 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
214 return networkCmProxyCmHandleQueryService.queryCmHandleIds(cmHandleQueryServiceParameters);
218 * Set the data sync enabled flag, along with the data sync state
219 * based on the data sync enabled boolean for the cm handle id provided.
221 * @param cmHandleId cm handle id
222 * @param dataSyncEnabledTargetValue data sync enabled flag
225 public void setDataSyncEnabled(final String cmHandleId, final Boolean dataSyncEnabledTargetValue) {
226 final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
227 if (dataSyncEnabledTargetValue.equals(compositeState.getDataSyncEnabled())) {
228 log.info("Data-Sync Enabled flag is already: {} ", dataSyncEnabledTargetValue);
231 if (CmHandleState.READY.equals(compositeState.getCmHandleState())) {
232 final DataStoreSyncState dataStoreSyncState = compositeState.getDataStores()
233 .getOperationalDataStore().getDataStoreSyncState();
234 if (Boolean.FALSE.equals(dataSyncEnabledTargetValue)
235 && DataStoreSyncState.SYNCHRONIZED.equals(dataStoreSyncState)) {
236 // TODO : This is hard-coded for onap dmi that need to be addressed
237 cpsDataService.deleteDataNode(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId,
238 "/netconf-state", OffsetDateTime.now());
240 CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabledTargetValue, compositeState);
241 inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
243 throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: "
244 + compositeState.getCmHandleState());
249 * Get all cm handle IDs by DMI plugin identifier.
251 * @param dmiPluginIdentifier DMI plugin identifier
252 * @return set of cm handle IDs
255 public Collection<String> getAllCmHandleIdsByDmiPluginIdentifier(final String dmiPluginIdentifier) {
256 return cmHandleQueries.getCmHandleIdsByDmiPluginIdentifier(dmiPluginIdentifier);
260 * Get all cm handle IDs by various properties.
262 * @param cmHandleQueryServiceParameters cm handle query parameters
263 * @return set of cm handle IDs
266 public Collection<String> executeCmHandleIdSearchForInventory(
267 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
268 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, InventoryQueryConditions.ALL_CONDITION_NAMES);
269 return networkCmProxyCmHandleQueryService.queryCmHandleIdsForInventory(cmHandleQueryServiceParameters);
273 * Retrieve cm handle details for a given cm handle.
275 * @param cmHandleId cm handle identifier
276 * @return cm handle details
279 public NcmpServiceCmHandle getNcmpServiceCmHandle(final String cmHandleId) {
280 return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(
281 inventoryPersistence.getYangModelCmHandle(cmHandleId));
285 * Get cm handle public properties for a given cm handle id.
287 * @param cmHandleId cm handle identifier
288 * @return cm handle public properties
291 public Map<String, String> getCmHandlePublicProperties(final String cmHandleId) {
292 final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
293 final List<YangModelCmHandle.Property> yangModelPublicProperties = yangModelCmHandle.getPublicProperties();
294 final Map<String, String> cmHandlePublicProperties = new HashMap<>();
295 YangDataConverter.asPropertiesMap(yangModelPublicProperties, cmHandlePublicProperties);
296 return cmHandlePublicProperties;
300 * Get cm handle composite state for a given cm handle id.
302 * @param cmHandleId cm handle identifier
303 * @return cm handle state
306 public CompositeState getCmHandleCompositeState(final String cmHandleId) {
307 return inventoryPersistence.getYangModelCmHandle(cmHandleId).getCompositeState();
310 protected void processRemovedCmHandles(final DmiPluginRegistration dmiPluginRegistration,
311 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse) {
312 final List<String> tobeRemovedCmHandleIds = dmiPluginRegistration.getRemovedCmHandles();
313 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
314 new ArrayList<>(tobeRemovedCmHandleIds.size());
315 final Collection<YangModelCmHandle> yangModelCmHandles =
316 inventoryPersistence.getYangModelCmHandles(tobeRemovedCmHandleIds);
317 updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETING);
319 final Set<String> notDeletedCmHandles = new HashSet<>();
320 for (final List<String> tobeRemovedCmHandleBatch : Lists.partition(tobeRemovedCmHandleIds, DELETE_BATCH_SIZE)) {
322 batchDeleteCmHandlesFromDbAndModuleSyncMap(tobeRemovedCmHandleBatch);
323 tobeRemovedCmHandleBatch.forEach(cmHandleId ->
324 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId)));
326 } catch (final RuntimeException batchException) {
327 log.error("Unable to de-register cm-handle batch, retrying on each cm handle");
328 for (final String cmHandleId : tobeRemovedCmHandleBatch) {
329 final CmHandleRegistrationResponse cmHandleRegistrationResponse =
330 deleteCmHandleAndGetCmHandleRegistrationResponse(cmHandleId);
331 cmHandleRegistrationResponses.add(cmHandleRegistrationResponse);
332 if (cmHandleRegistrationResponse.getStatus() != CmHandleRegistrationResponse.Status.SUCCESS) {
333 notDeletedCmHandles.add(cmHandleId);
338 yangModelCmHandles.removeIf(yangModelCmHandle -> notDeletedCmHandles.contains(yangModelCmHandle.getId()));
339 updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETED);
340 dmiPluginRegistrationResponse.setRemovedCmHandles(cmHandleRegistrationResponses);
343 protected void processCreatedCmHandles(final DmiPluginRegistration dmiPluginRegistration,
344 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse) {
345 final List<NcmpServiceCmHandle> ncmpServiceCmHandles = dmiPluginRegistration.getCreatedCmHandles();
346 final List<CmHandleRegistrationResponse> failedCmHandleRegistrationResponses = new ArrayList<>();
349 final Collection<String> rejectedCmHandleIds
350 = checkAlternateIds(ncmpServiceCmHandles, failedCmHandleRegistrationResponses);
352 final Collection<String> succeededCmHandleIds = persistCmHandlesWithState(dmiPluginRegistration,
353 dmiPluginRegistrationResponse, ncmpServiceCmHandles, rejectedCmHandleIds);
355 processTrustLevels(ncmpServiceCmHandles, succeededCmHandleIds);
357 } catch (final AlreadyDefinedException alreadyDefinedException) {
358 failedCmHandleRegistrationResponses.addAll(CmHandleRegistrationResponse.createFailureResponsesFromXpaths(
359 alreadyDefinedException.getAlreadyDefinedObjectNames(), CM_HANDLE_ALREADY_EXIST));
360 } catch (final Exception exception) {
361 final Collection<String> cmHandleIds =
362 ncmpServiceCmHandles.stream().map(NcmpServiceCmHandle::getCmHandleId).collect(Collectors.toList());
363 failedCmHandleRegistrationResponses.addAll(CmHandleRegistrationResponse
364 .createFailureResponses(cmHandleIds, exception));
366 final List<CmHandleRegistrationResponse> mergedCmHandleRegistrationResponses
367 = new ArrayList<>(failedCmHandleRegistrationResponses);
368 mergedCmHandleRegistrationResponses.addAll(dmiPluginRegistrationResponse.getCreatedCmHandles());
370 dmiPluginRegistrationResponse.setCreatedCmHandles(mergedCmHandleRegistrationResponses);
373 protected void processUpdatedCmHandles(final DmiPluginRegistration dmiPluginRegistration,
374 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse) {
375 dmiPluginRegistrationResponse.setUpdatedCmHandles(networkCmProxyDataServicePropertyHandler
376 .updateCmHandleProperties(dmiPluginRegistration.getUpdatedCmHandles()));
379 protected void processUpgradedCmHandles(
380 final DmiPluginRegistration dmiPluginRegistration,
381 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse) {
383 final List<String> cmHandleIds = dmiPluginRegistration.getUpgradedCmHandles().getCmHandles();
384 final String upgradedModuleSetTag = dmiPluginRegistration.getUpgradedCmHandles().getModuleSetTag();
385 final Map<YangModelCmHandle, CmHandleState> acceptedCmHandleStatePerCmHandle
386 = new HashMap<>(cmHandleIds.size());
387 final List<CmHandleRegistrationResponse> cmHandleUpgradeResponses = new ArrayList<>(cmHandleIds.size());
389 for (final String cmHandleId : cmHandleIds) {
391 final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
392 if (yangModelCmHandle.getCompositeState().getCmHandleState() == CmHandleState.READY) {
393 if (moduleUpgradeCanBeSkipped(yangModelCmHandle, upgradedModuleSetTag)) {
394 cmHandleUpgradeResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId));
396 updateYangModelCmHandleForUpgrade(yangModelCmHandle, upgradedModuleSetTag);
397 acceptedCmHandleStatePerCmHandle.put(yangModelCmHandle, CmHandleState.LOCKED);
400 cmHandleUpgradeResponses.add(
401 CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_READY));
403 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
404 log.error("Unable to find data node for cm handle id : {} , caused by : {}",
405 cmHandleId, dataNodeNotFoundException.getMessage());
406 cmHandleUpgradeResponses.add(
407 CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND));
408 } catch (final DataValidationException dataValidationException) {
409 log.error("Unable to upgrade cm handle id: {}, caused by : {}",
410 cmHandleId, dataValidationException.getMessage());
411 cmHandleUpgradeResponses.add(
412 CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID));
415 cmHandleUpgradeResponses.addAll(upgradeCmHandles(acceptedCmHandleStatePerCmHandle));
416 dmiPluginRegistrationResponse.setUpgradedCmHandles(cmHandleUpgradeResponses);
419 private Collection<String> checkAlternateIds(
420 final List<NcmpServiceCmHandle> cmHandlesToBeCreated,
421 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses) {
422 final Collection<String> rejectedCmHandleIds = alternateIdChecker
423 .getIdsOfCmHandlesWithRejectedAlternateId(cmHandlesToBeCreated);
424 cmHandleRegistrationResponses.addAll(CmHandleRegistrationResponse.createFailureResponses(
425 rejectedCmHandleIds, ALTERNATE_ID_ALREADY_ASSOCIATED));
426 return rejectedCmHandleIds;
429 private List<String> persistCmHandlesWithState(final DmiPluginRegistration dmiPluginRegistration,
430 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse,
431 final List<NcmpServiceCmHandle> cmHandlesToBeCreated,
432 final Collection<String> rejectedCmHandleIds) {
433 final List<String> succeededCmHandleIds = new ArrayList<>(cmHandlesToBeCreated.size());
434 final List<YangModelCmHandle> yangModelCmHandlesToRegister = new ArrayList<>(cmHandlesToBeCreated.size());
435 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
436 new ArrayList<>(cmHandlesToBeCreated.size());
437 for (final NcmpServiceCmHandle ncmpServiceCmHandle: cmHandlesToBeCreated) {
438 if (!rejectedCmHandleIds.contains(ncmpServiceCmHandle.getCmHandleId())) {
439 yangModelCmHandlesToRegister.add(getYangModelCmHandle(dmiPluginRegistration, ncmpServiceCmHandle));
440 cmHandleRegistrationResponses.add(
441 CmHandleRegistrationResponse.createSuccessResponse(ncmpServiceCmHandle.getCmHandleId()));
442 succeededCmHandleIds.add(ncmpServiceCmHandle.getCmHandleId());
445 lcmEventsCmHandleStateHandler.initiateStateAdvised(yangModelCmHandlesToRegister);
446 dmiPluginRegistrationResponse.setCreatedCmHandles(cmHandleRegistrationResponses);
447 return succeededCmHandleIds;
450 private YangModelCmHandle getYangModelCmHandle(final DmiPluginRegistration dmiPluginRegistration,
451 final NcmpServiceCmHandle ncmpServiceCmHandle) {
452 return YangModelCmHandle.toYangModelCmHandle(
453 dmiPluginRegistration.getDmiPlugin(),
454 dmiPluginRegistration.getDmiDataPlugin(),
455 dmiPluginRegistration.getDmiModelPlugin(),
457 ncmpServiceCmHandle.getModuleSetTag(),
458 ncmpServiceCmHandle.getAlternateId());
461 private void processTrustLevels(final Collection<NcmpServiceCmHandle> cmHandlesToBeCreated,
462 final Collection<String> succeededCmHandleIds) {
463 final Map<String, TrustLevel> initialTrustLevelPerCmHandleId = new HashMap<>(cmHandlesToBeCreated.size());
464 for (final NcmpServiceCmHandle ncmpServiceCmHandle: cmHandlesToBeCreated) {
465 if (succeededCmHandleIds.contains(ncmpServiceCmHandle.getCmHandleId())) {
466 initialTrustLevelPerCmHandleId.put(ncmpServiceCmHandle.getCmHandleId(),
467 ncmpServiceCmHandle.getRegistrationTrustLevel());
470 trustLevelManager.handleInitialRegistrationOfTrustLevels(initialTrustLevelPerCmHandleId);
473 private static boolean moduleUpgradeCanBeSkipped(final YangModelCmHandle yangModelCmHandle,
474 final String upgradedModuleSetTag) {
475 if (StringUtils.isBlank(upgradedModuleSetTag)) {
478 return yangModelCmHandle.getModuleSetTag().equals(upgradedModuleSetTag);
481 private static void updateYangModelCmHandleForUpgrade(final YangModelCmHandle yangModelCmHandle,
482 final String upgradedModuleSetTag) {
483 final String lockReasonWithModuleSetTag = String.format(ModuleOperationsUtils.MODULE_SET_TAG_MESSAGE_FORMAT,
484 upgradedModuleSetTag);
485 yangModelCmHandle.setCompositeState(new CompositeStateBuilder().withCmHandleState(CmHandleState.READY)
486 .withLockReason(MODULE_UPGRADE, lockReasonWithModuleSetTag).build());
489 private CmHandleRegistrationResponse deleteCmHandleAndGetCmHandleRegistrationResponse(final String cmHandleId) {
491 deleteCmHandleFromDbAndModuleSyncMap(cmHandleId);
492 return CmHandleRegistrationResponse.createSuccessResponse(cmHandleId);
493 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
494 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
495 cmHandleId, dataNodeNotFoundException.getMessage());
496 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND);
497 } catch (final DataValidationException dataValidationException) {
498 log.error("Unable to de-register cm-handle id: {}, caused by: {}",
499 cmHandleId, dataValidationException.getMessage());
500 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID);
501 } catch (final Exception exception) {
502 log.error("Unable to de-register cm-handle id : {} , caused by : {}", cmHandleId, exception.getMessage());
503 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception);
507 private void updateCmHandleStateBatch(final Collection<YangModelCmHandle> yangModelCmHandles,
508 final CmHandleState cmHandleState) {
509 final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>(yangModelCmHandles.size());
510 yangModelCmHandles.forEach(yangModelCmHandle -> cmHandleStatePerCmHandle.put(yangModelCmHandle, cmHandleState));
511 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
514 private void deleteCmHandleFromDbAndModuleSyncMap(final String cmHandleId) {
515 inventoryPersistence.deleteSchemaSetWithCascade(cmHandleId);
516 inventoryPersistence.deleteDataNode(NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId + "']");
517 removeDeletedCmHandleFromModuleSyncMap(cmHandleId);
520 private void batchDeleteCmHandlesFromDbAndModuleSyncMap(final Collection<String> cmHandleIds) {
521 inventoryPersistence.deleteSchemaSetsWithCascade(cmHandleIds);
522 inventoryPersistence.deleteDataNodes(mapCmHandleIdsToXpaths(cmHandleIds));
523 cmHandleIds.forEach(this::removeDeletedCmHandleFromModuleSyncMap);
526 private Collection<String> mapCmHandleIdsToXpaths(final Collection<String> cmHandles) {
527 return cmHandles.stream()
528 .map(cmHandleId -> NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId + "']")
529 .collect(Collectors.toSet());
532 // CPS-1239 Robustness cleaning of in progress cache
533 private void removeDeletedCmHandleFromModuleSyncMap(final String cmHandleId) {
534 if (moduleSyncStartedOnCmHandles.remove(cmHandleId) != null) {
535 log.debug("{} removed from in progress map", cmHandleId);
539 private List<CmHandleRegistrationResponse> upgradeCmHandles(final Map<YangModelCmHandle, CmHandleState>
540 cmHandleStatePerCmHandle) {
541 final List<String> cmHandleIds = getCmHandleIds(cmHandleStatePerCmHandle);
542 log.info("Moving cm handles : {} into locked (for upgrade) state.", cmHandleIds);
544 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
545 return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
546 } catch (final Exception e) {
547 log.error("Unable to update cmHandleIds : {} , caused by : {}", cmHandleIds, e.getMessage());
548 return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, e);
552 private static List<String> getCmHandleIds(final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle) {
553 return cmHandleStatePerCmHandle.keySet().stream().map(YangModelCmHandle::getId).toList();
556 private void setTrustLevelPerDmiPlugin(final DmiPluginRegistration dmiPluginRegistration) {
557 if (DmiPluginRegistration.isNullEmptyOrBlank(dmiPluginRegistration.getDmiDataPlugin())) {
558 trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiPlugin(), TrustLevel.COMPLETE);
560 trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiDataPlugin(), TrustLevel.COMPLETE);