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.config.embeddedcache.TrustLevelCacheConfig;
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.AlternateIdChecker;
69 import org.onap.cps.ncmp.api.impl.utils.CmHandleQueryConditions;
70 import org.onap.cps.ncmp.api.impl.utils.InventoryQueryConditions;
71 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
72 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
73 import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters;
74 import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters;
75 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse;
76 import org.onap.cps.ncmp.api.models.DataOperationRequest;
77 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
78 import org.onap.cps.ncmp.api.models.DmiPluginRegistrationResponse;
79 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
80 import org.onap.cps.spi.FetchDescendantsOption;
81 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
82 import org.onap.cps.spi.exceptions.CpsException;
83 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
84 import org.onap.cps.spi.exceptions.DataValidationException;
85 import org.onap.cps.spi.model.ModuleDefinition;
86 import org.onap.cps.spi.model.ModuleReference;
87 import org.onap.cps.utils.JsonObjectMapper;
88 import org.springframework.beans.factory.annotation.Qualifier;
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 @Qualifier(TrustLevelCacheConfig.TRUST_LEVEL_PER_DMI_PLUGIN_BEAN_NAME)
108 private final Map<String, TrustLevel> trustLevelPerDmiPlugin;
109 private final TrustLevelManager trustLevelManager;
110 private final AlternateIdChecker alternateIdChecker;
113 public DmiPluginRegistrationResponse updateDmiRegistrationAndSyncModule(
114 final DmiPluginRegistration dmiPluginRegistration) {
116 dmiPluginRegistration.validateDmiPluginRegistration();
117 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse = new DmiPluginRegistrationResponse();
119 setTrustLevelPerDmiPlugin(dmiPluginRegistration);
121 processRemovedCmHandles(dmiPluginRegistration, dmiPluginRegistrationResponse);
123 processCreatedCmHandles(dmiPluginRegistration, dmiPluginRegistrationResponse);
125 processUpdatedCmHandles(dmiPluginRegistration, dmiPluginRegistrationResponse);
127 processUpgradedCmHandles(dmiPluginRegistration, dmiPluginRegistrationResponse);
129 return dmiPluginRegistrationResponse;
133 public Object getResourceDataForCmHandle(final String datastoreName,
134 final String cmHandleId,
135 final String resourceIdentifier,
136 final String optionsParamInQuery,
137 final String topicParamInQuery,
138 final String requestId) {
139 final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(datastoreName, cmHandleId,
144 return responseEntity.getBody();
148 public Object getResourceDataForCmHandle(final String datastoreName,
149 final String cmHandleId,
150 final String resourceIdentifier,
151 final FetchDescendantsOption fetchDescendantsOption) {
152 return cpsDataService.getDataNodes(datastoreName, cmHandleId, resourceIdentifier,
153 fetchDescendantsOption).iterator().next();
157 public void executeDataOperationForCmHandles(final String topicParamInQuery,
158 final DataOperationRequest
159 dataOperationRequest,
160 final String requestId) {
161 dmiDataOperations.requestResourceDataFromDmi(topicParamInQuery, dataOperationRequest, requestId);
165 public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
166 final String resourceIdentifier,
167 final OperationType operationType,
168 final String requestData,
169 final String dataType) {
170 return dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier,
171 operationType, requestData, dataType);
175 public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
176 return inventoryPersistence.getYangResourcesModuleReferences(cmHandleId);
180 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
181 return inventoryPersistence.getModuleDefinitionsByCmHandleId(cmHandleId);
185 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleAndModule(final String cmHandleId,
186 final String moduleName,
187 final String moduleRevision) {
188 return inventoryPersistence.getModuleDefinitionsByCmHandleAndModule(cmHandleId, moduleName, moduleRevision);
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();
313 protected void processRemovedCmHandles(final DmiPluginRegistration dmiPluginRegistration,
314 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse) {
315 final List<String> tobeRemovedCmHandleIds = dmiPluginRegistration.getRemovedCmHandles();
316 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
317 new ArrayList<>(tobeRemovedCmHandleIds.size());
318 final Collection<YangModelCmHandle> yangModelCmHandles =
319 inventoryPersistence.getYangModelCmHandles(tobeRemovedCmHandleIds);
320 updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETING);
322 final Set<String> notDeletedCmHandles = new HashSet<>();
323 for (final List<String> tobeRemovedCmHandleBatch : Lists.partition(tobeRemovedCmHandleIds, DELETE_BATCH_SIZE)) {
325 batchDeleteCmHandlesFromDbAndModuleSyncMap(tobeRemovedCmHandleBatch);
326 tobeRemovedCmHandleBatch.forEach(cmHandleId ->
327 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId)));
329 } catch (final RuntimeException batchException) {
330 log.error("Unable to de-register cm-handle batch, retrying on each cm handle");
331 for (final String cmHandleId : tobeRemovedCmHandleBatch) {
332 final CmHandleRegistrationResponse cmHandleRegistrationResponse =
333 deleteCmHandleAndGetCmHandleRegistrationResponse(cmHandleId);
334 cmHandleRegistrationResponses.add(cmHandleRegistrationResponse);
335 if (cmHandleRegistrationResponse.getStatus() != CmHandleRegistrationResponse.Status.SUCCESS) {
336 notDeletedCmHandles.add(cmHandleId);
341 yangModelCmHandles.removeIf(yangModelCmHandle -> notDeletedCmHandles.contains(yangModelCmHandle.getId()));
342 updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETED);
343 dmiPluginRegistrationResponse.setRemovedCmHandles(cmHandleRegistrationResponses);
346 protected void processCreatedCmHandles(final DmiPluginRegistration dmiPluginRegistration,
347 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse) {
348 final List<NcmpServiceCmHandle> ncmpServiceCmHandles = dmiPluginRegistration.getCreatedCmHandles();
349 final List<CmHandleRegistrationResponse> failedCmHandleRegistrationResponses = new ArrayList<>();
352 final Collection<String> rejectedCmHandleIds
353 = checkAlternateIds(ncmpServiceCmHandles, failedCmHandleRegistrationResponses);
355 final Collection<String> succeededCmHandleIds = persistCmHandlesWithState(dmiPluginRegistration,
356 dmiPluginRegistrationResponse, ncmpServiceCmHandles, rejectedCmHandleIds);
358 processTrustLevels(ncmpServiceCmHandles, succeededCmHandleIds);
360 } catch (final AlreadyDefinedException alreadyDefinedException) {
361 failedCmHandleRegistrationResponses.addAll(CmHandleRegistrationResponse.createFailureResponsesFromXpaths(
362 alreadyDefinedException.getAlreadyDefinedObjectNames(), CM_HANDLE_ALREADY_EXIST));
363 } catch (final Exception exception) {
364 final Collection<String> cmHandleIds =
365 ncmpServiceCmHandles.stream().map(NcmpServiceCmHandle::getCmHandleId).collect(Collectors.toList());
366 failedCmHandleRegistrationResponses.addAll(CmHandleRegistrationResponse
367 .createFailureResponses(cmHandleIds, exception));
369 final List<CmHandleRegistrationResponse> mergedCmHandleRegistrationResponses
370 = new ArrayList<>(failedCmHandleRegistrationResponses);
371 mergedCmHandleRegistrationResponses.addAll(dmiPluginRegistrationResponse.getCreatedCmHandles());
373 dmiPluginRegistrationResponse.setCreatedCmHandles(mergedCmHandleRegistrationResponses);
376 protected void processUpdatedCmHandles(final DmiPluginRegistration dmiPluginRegistration,
377 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse) {
378 dmiPluginRegistrationResponse.setUpdatedCmHandles(networkCmProxyDataServicePropertyHandler
379 .updateCmHandleProperties(dmiPluginRegistration.getUpdatedCmHandles()));
382 protected void processUpgradedCmHandles(
383 final DmiPluginRegistration dmiPluginRegistration,
384 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse) {
386 final List<String> cmHandleIds = dmiPluginRegistration.getUpgradedCmHandles().getCmHandles();
387 final String upgradedModuleSetTag = dmiPluginRegistration.getUpgradedCmHandles().getModuleSetTag();
388 final Map<YangModelCmHandle, CmHandleState> acceptedCmHandleStatePerCmHandle
389 = new HashMap<>(cmHandleIds.size());
390 final List<CmHandleRegistrationResponse> cmHandleUpgradeResponses = new ArrayList<>(cmHandleIds.size());
392 for (final String cmHandleId : cmHandleIds) {
394 final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
395 if (yangModelCmHandle.getCompositeState().getCmHandleState() == CmHandleState.READY) {
396 if (moduleUpgradeCanBeSkipped(yangModelCmHandle, upgradedModuleSetTag)) {
397 cmHandleUpgradeResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId));
399 updateYangModelCmHandleForUpgrade(yangModelCmHandle, upgradedModuleSetTag);
400 acceptedCmHandleStatePerCmHandle.put(yangModelCmHandle, CmHandleState.LOCKED);
403 cmHandleUpgradeResponses.add(
404 CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_READY));
406 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
407 log.error("Unable to find data node for cm handle id : {} , caused by : {}",
408 cmHandleId, dataNodeNotFoundException.getMessage());
409 cmHandleUpgradeResponses.add(
410 CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND));
411 } catch (final DataValidationException dataValidationException) {
412 log.error("Unable to upgrade cm handle id: {}, caused by : {}",
413 cmHandleId, dataValidationException.getMessage());
414 cmHandleUpgradeResponses.add(
415 CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID));
418 cmHandleUpgradeResponses.addAll(upgradeCmHandles(acceptedCmHandleStatePerCmHandle));
419 dmiPluginRegistrationResponse.setUpgradedCmHandles(cmHandleUpgradeResponses);
422 private Collection<String> checkAlternateIds(
423 final List<NcmpServiceCmHandle> cmHandlesToBeCreated,
424 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses) {
425 final Collection<String> rejectedCmHandleIds = alternateIdChecker
426 .getIdsOfCmHandlesWithRejectedAlternateId(cmHandlesToBeCreated);
427 cmHandleRegistrationResponses.addAll(CmHandleRegistrationResponse.createFailureResponses(
428 rejectedCmHandleIds, ALTERNATE_ID_ALREADY_ASSOCIATED));
429 return rejectedCmHandleIds;
432 private List<String> persistCmHandlesWithState(final DmiPluginRegistration dmiPluginRegistration,
433 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse,
434 final List<NcmpServiceCmHandle> cmHandlesToBeCreated,
435 final Collection<String> rejectedCmHandleIds) {
436 final List<String> succeededCmHandleIds = new ArrayList<>(cmHandlesToBeCreated.size());
437 final List<YangModelCmHandle> yangModelCmHandlesToRegister = new ArrayList<>(cmHandlesToBeCreated.size());
438 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
439 new ArrayList<>(cmHandlesToBeCreated.size());
440 for (final NcmpServiceCmHandle ncmpServiceCmHandle: cmHandlesToBeCreated) {
441 if (!rejectedCmHandleIds.contains(ncmpServiceCmHandle.getCmHandleId())) {
442 yangModelCmHandlesToRegister.add(getYangModelCmHandle(dmiPluginRegistration, ncmpServiceCmHandle));
443 cmHandleRegistrationResponses.add(
444 CmHandleRegistrationResponse.createSuccessResponse(ncmpServiceCmHandle.getCmHandleId()));
445 succeededCmHandleIds.add(ncmpServiceCmHandle.getCmHandleId());
448 lcmEventsCmHandleStateHandler.initiateStateAdvised(yangModelCmHandlesToRegister);
449 dmiPluginRegistrationResponse.setCreatedCmHandles(cmHandleRegistrationResponses);
450 return succeededCmHandleIds;
453 private YangModelCmHandle getYangModelCmHandle(final DmiPluginRegistration dmiPluginRegistration,
454 final NcmpServiceCmHandle ncmpServiceCmHandle) {
455 return YangModelCmHandle.toYangModelCmHandle(
456 dmiPluginRegistration.getDmiPlugin(),
457 dmiPluginRegistration.getDmiDataPlugin(),
458 dmiPluginRegistration.getDmiModelPlugin(),
460 ncmpServiceCmHandle.getModuleSetTag(),
461 ncmpServiceCmHandle.getAlternateId());
464 private void processTrustLevels(final Collection<NcmpServiceCmHandle> cmHandlesToBeCreated,
465 final Collection<String> succeededCmHandleIds) {
466 final Map<String, TrustLevel> initialTrustLevelPerCmHandleId = new HashMap<>(cmHandlesToBeCreated.size());
467 for (final NcmpServiceCmHandle ncmpServiceCmHandle: cmHandlesToBeCreated) {
468 if (succeededCmHandleIds.contains(ncmpServiceCmHandle.getCmHandleId())) {
469 initialTrustLevelPerCmHandleId.put(ncmpServiceCmHandle.getCmHandleId(),
470 ncmpServiceCmHandle.getRegistrationTrustLevel());
473 trustLevelManager.handleInitialRegistrationOfTrustLevels(initialTrustLevelPerCmHandleId);
476 private static boolean moduleUpgradeCanBeSkipped(final YangModelCmHandle yangModelCmHandle,
477 final String upgradedModuleSetTag) {
478 if (StringUtils.isBlank(upgradedModuleSetTag)) {
481 return yangModelCmHandle.getModuleSetTag().equals(upgradedModuleSetTag);
484 private static void updateYangModelCmHandleForUpgrade(final YangModelCmHandle yangModelCmHandle,
485 final String upgradedModuleSetTag) {
486 final String lockReasonWithModuleSetTag = String.format(ModuleOperationsUtils.MODULE_SET_TAG_MESSAGE_FORMAT,
487 upgradedModuleSetTag);
488 yangModelCmHandle.setCompositeState(new CompositeStateBuilder().withCmHandleState(CmHandleState.READY)
489 .withLockReason(MODULE_UPGRADE, lockReasonWithModuleSetTag).build());
492 private CmHandleRegistrationResponse deleteCmHandleAndGetCmHandleRegistrationResponse(final String cmHandleId) {
494 deleteCmHandleFromDbAndModuleSyncMap(cmHandleId);
495 return CmHandleRegistrationResponse.createSuccessResponse(cmHandleId);
496 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
497 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
498 cmHandleId, dataNodeNotFoundException.getMessage());
499 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND);
500 } catch (final DataValidationException dataValidationException) {
501 log.error("Unable to de-register cm-handle id: {}, caused by: {}",
502 cmHandleId, dataValidationException.getMessage());
503 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID);
504 } catch (final Exception exception) {
505 log.error("Unable to de-register cm-handle id : {} , caused by : {}", cmHandleId, exception.getMessage());
506 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception);
510 private void updateCmHandleStateBatch(final Collection<YangModelCmHandle> yangModelCmHandles,
511 final CmHandleState cmHandleState) {
512 final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>(yangModelCmHandles.size());
513 yangModelCmHandles.forEach(yangModelCmHandle -> cmHandleStatePerCmHandle.put(yangModelCmHandle, cmHandleState));
514 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
517 private void deleteCmHandleFromDbAndModuleSyncMap(final String cmHandleId) {
518 inventoryPersistence.deleteSchemaSetWithCascade(cmHandleId);
519 inventoryPersistence.deleteDataNode(NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId + "']");
520 removeDeletedCmHandleFromModuleSyncMap(cmHandleId);
523 private void batchDeleteCmHandlesFromDbAndModuleSyncMap(final Collection<String> cmHandleIds) {
524 inventoryPersistence.deleteSchemaSetsWithCascade(cmHandleIds);
525 inventoryPersistence.deleteDataNodes(mapCmHandleIdsToXpaths(cmHandleIds));
526 cmHandleIds.forEach(this::removeDeletedCmHandleFromModuleSyncMap);
529 private Collection<String> mapCmHandleIdsToXpaths(final Collection<String> cmHandles) {
530 return cmHandles.stream()
531 .map(cmHandleId -> NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId + "']")
532 .collect(Collectors.toSet());
535 // CPS-1239 Robustness cleaning of in progress cache
536 private void removeDeletedCmHandleFromModuleSyncMap(final String cmHandleId) {
537 if (moduleSyncStartedOnCmHandles.remove(cmHandleId) != null) {
538 log.debug("{} removed from in progress map", cmHandleId);
542 private List<CmHandleRegistrationResponse> upgradeCmHandles(final Map<YangModelCmHandle, CmHandleState>
543 cmHandleStatePerCmHandle) {
544 final List<String> cmHandleIds = getCmHandleIds(cmHandleStatePerCmHandle);
545 log.info("Moving cm handles : {} into locked (for upgrade) state.", cmHandleIds);
547 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
548 return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
549 } catch (final Exception e) {
550 log.error("Unable to update cmHandleIds : {} , caused by : {}", cmHandleIds, e.getMessage());
551 return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, e);
555 private static List<String> getCmHandleIds(final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle) {
556 return cmHandleStatePerCmHandle.keySet().stream().map(YangModelCmHandle::getId).toList();
559 private void setTrustLevelPerDmiPlugin(final DmiPluginRegistration dmiPluginRegistration) {
560 if (DmiPluginRegistration.isNullEmptyOrBlank(dmiPluginRegistration.getDmiDataPlugin())) {
561 trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiPlugin(), TrustLevel.COMPLETE);
563 trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiDataPlugin(), TrustLevel.COMPLETE);