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.CmResourceAddress;
77 import org.onap.cps.ncmp.api.models.DataOperationRequest;
78 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
79 import org.onap.cps.ncmp.api.models.DmiPluginRegistrationResponse;
80 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
81 import org.onap.cps.spi.FetchDescendantsOption;
82 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
83 import org.onap.cps.spi.exceptions.CpsException;
84 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
85 import org.onap.cps.spi.exceptions.DataValidationException;
86 import org.onap.cps.spi.model.ModuleDefinition;
87 import org.onap.cps.spi.model.ModuleReference;
88 import org.onap.cps.utils.JsonObjectMapper;
89 import org.springframework.beans.factory.annotation.Qualifier;
90 import org.springframework.http.ResponseEntity;
91 import org.springframework.stereotype.Service;
95 @RequiredArgsConstructor
96 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
98 private static final int DELETE_BATCH_SIZE = 100;
99 private final JsonObjectMapper jsonObjectMapper;
100 private final DmiDataOperations dmiDataOperations;
101 private final NetworkCmProxyDataServicePropertyHandler networkCmProxyDataServicePropertyHandler;
102 private final InventoryPersistence inventoryPersistence;
103 private final CmHandleQueries cmHandleQueries;
104 private final NetworkCmProxyCmHandleQueryService networkCmProxyCmHandleQueryService;
105 private final LcmEventsCmHandleStateHandler lcmEventsCmHandleStateHandler;
106 private final CpsDataService cpsDataService;
107 private final IMap<String, Object> moduleSyncStartedOnCmHandles;
109 @Qualifier(TrustLevelCacheConfig.TRUST_LEVEL_PER_DMI_PLUGIN)
110 private final Map<String, TrustLevel> trustLevelPerDmiPlugin;
112 private final TrustLevelManager trustLevelManager;
113 private final AlternateIdChecker alternateIdChecker;
116 public DmiPluginRegistrationResponse updateDmiRegistrationAndSyncModule(
117 final DmiPluginRegistration dmiPluginRegistration) {
119 dmiPluginRegistration.validateDmiPluginRegistration();
120 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse = new DmiPluginRegistrationResponse();
122 setTrustLevelPerDmiPlugin(dmiPluginRegistration);
124 processRemovedCmHandles(dmiPluginRegistration, dmiPluginRegistrationResponse);
126 processCreatedCmHandles(dmiPluginRegistration, dmiPluginRegistrationResponse);
128 processUpdatedCmHandles(dmiPluginRegistration, dmiPluginRegistrationResponse);
130 processUpgradedCmHandles(dmiPluginRegistration, dmiPluginRegistrationResponse);
132 return dmiPluginRegistrationResponse;
136 public Object getResourceDataForCmHandle(final CmResourceAddress cmResourceAddress,
137 final String optionsParamInQuery,
138 final String topicParamInQuery,
139 final String requestId,
140 final String authorization) {
141 final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(cmResourceAddress,
146 return responseEntity.getBody();
150 public Object getResourceDataForCmHandle(final CmResourceAddress cmResourceAddress,
151 final FetchDescendantsOption fetchDescendantsOption) {
152 return cpsDataService.getDataNodes(cmResourceAddress.datastoreName(),
153 cmResourceAddress.cmHandleId(),
154 cmResourceAddress.resourceIdentifier(),
155 fetchDescendantsOption).iterator().next();
159 public void executeDataOperationForCmHandles(final String topicParamInQuery,
160 final DataOperationRequest dataOperationRequest,
161 final String requestId,
162 final String authorization) {
163 dmiDataOperations.requestResourceDataFromDmi(topicParamInQuery, dataOperationRequest, requestId,
168 public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
169 final String resourceIdentifier,
170 final OperationType operationType,
171 final String requestData,
172 final String dataType,
173 final String authorization) {
174 return dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier,
175 operationType, requestData, dataType, authorization);
179 public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
180 return inventoryPersistence.getYangResourcesModuleReferences(cmHandleId);
184 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
185 return inventoryPersistence.getModuleDefinitionsByCmHandleId(cmHandleId);
189 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleAndModule(final String cmHandleId,
190 final String moduleName,
191 final String moduleRevision) {
192 return inventoryPersistence.getModuleDefinitionsByCmHandleAndModule(cmHandleId, moduleName, moduleRevision);
196 * Retrieve cm handles with details for the given query parameters.
198 * @param cmHandleQueryApiParameters cm handle query parameters
199 * @return cm handles with details
202 public Collection<NcmpServiceCmHandle> executeCmHandleSearch(
203 final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
204 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
205 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
206 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
207 return networkCmProxyCmHandleQueryService.queryCmHandles(cmHandleQueryServiceParameters);
211 * Retrieve cm handle ids for the given query parameters.
213 * @param cmHandleQueryApiParameters cm handle query parameters
214 * @return cm handle ids
217 public Collection<String> executeCmHandleIdSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
218 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
219 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
220 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
221 return networkCmProxyCmHandleQueryService.queryCmHandleIds(cmHandleQueryServiceParameters);
225 * Set the data sync enabled flag, along with the data sync state
226 * based on the data sync enabled boolean for the cm handle id provided.
228 * @param cmHandleId cm handle id
229 * @param dataSyncEnabledTargetValue data sync enabled flag
232 public void setDataSyncEnabled(final String cmHandleId, final Boolean dataSyncEnabledTargetValue) {
233 final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
234 if (dataSyncEnabledTargetValue.equals(compositeState.getDataSyncEnabled())) {
235 log.info("Data-Sync Enabled flag is already: {} ", dataSyncEnabledTargetValue);
238 if (CmHandleState.READY.equals(compositeState.getCmHandleState())) {
239 final DataStoreSyncState dataStoreSyncState = compositeState.getDataStores()
240 .getOperationalDataStore().getDataStoreSyncState();
241 if (Boolean.FALSE.equals(dataSyncEnabledTargetValue)
242 && DataStoreSyncState.SYNCHRONIZED.equals(dataStoreSyncState)) {
243 // TODO : This is hard-coded for onap dmi that need to be addressed
244 cpsDataService.deleteDataNode(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId,
245 "/netconf-state", OffsetDateTime.now());
247 CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabledTargetValue, compositeState);
248 inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
250 throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: "
251 + compositeState.getCmHandleState());
256 * Get all cm handle IDs by DMI plugin identifier.
258 * @param dmiPluginIdentifier DMI plugin identifier
259 * @return set of cm handle IDs
262 public Collection<String> getAllCmHandleIdsByDmiPluginIdentifier(final String dmiPluginIdentifier) {
263 return cmHandleQueries.getCmHandleIdsByDmiPluginIdentifier(dmiPluginIdentifier);
267 * Get all cm handle IDs by various properties.
269 * @param cmHandleQueryServiceParameters cm handle query parameters
270 * @return set of cm handle IDs
273 public Collection<String> executeCmHandleIdSearchForInventory(
274 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
275 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, InventoryQueryConditions.ALL_CONDITION_NAMES);
276 return networkCmProxyCmHandleQueryService.queryCmHandleIdsForInventory(cmHandleQueryServiceParameters);
280 * Retrieve cm handle details for a given cm handle.
282 * @param cmHandleId cm handle identifier
283 * @return cm handle details
286 public NcmpServiceCmHandle getNcmpServiceCmHandle(final String cmHandleId) {
287 return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(
288 inventoryPersistence.getYangModelCmHandle(cmHandleId));
292 * Get cm handle public properties for a given cm handle id.
294 * @param cmHandleId cm handle identifier
295 * @return cm handle public properties
298 public Map<String, String> getCmHandlePublicProperties(final String cmHandleId) {
299 final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
300 final List<YangModelCmHandle.Property> yangModelPublicProperties = yangModelCmHandle.getPublicProperties();
301 final Map<String, String> cmHandlePublicProperties = new HashMap<>();
302 YangDataConverter.asPropertiesMap(yangModelPublicProperties, cmHandlePublicProperties);
303 return cmHandlePublicProperties;
307 * Get cm handle composite state for a given cm handle id.
309 * @param cmHandleId cm handle identifier
310 * @return cm handle state
313 public CompositeState getCmHandleCompositeState(final String cmHandleId) {
314 return inventoryPersistence.getYangModelCmHandle(cmHandleId).getCompositeState();
317 protected void processRemovedCmHandles(final DmiPluginRegistration dmiPluginRegistration,
318 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse) {
319 final List<String> tobeRemovedCmHandleIds = dmiPluginRegistration.getRemovedCmHandles();
320 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
321 new ArrayList<>(tobeRemovedCmHandleIds.size());
322 final Collection<YangModelCmHandle> yangModelCmHandles =
323 inventoryPersistence.getYangModelCmHandles(tobeRemovedCmHandleIds);
324 updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETING);
326 final Set<String> notDeletedCmHandles = new HashSet<>();
327 for (final List<String> tobeRemovedCmHandleBatch : Lists.partition(tobeRemovedCmHandleIds, DELETE_BATCH_SIZE)) {
329 batchDeleteCmHandlesFromDbAndModuleSyncMap(tobeRemovedCmHandleBatch);
330 tobeRemovedCmHandleBatch.forEach(cmHandleId ->
331 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId)));
333 } catch (final RuntimeException batchException) {
334 log.error("Unable to de-register cm-handle batch, retrying on each cm handle");
335 for (final String cmHandleId : tobeRemovedCmHandleBatch) {
336 final CmHandleRegistrationResponse cmHandleRegistrationResponse =
337 deleteCmHandleAndGetCmHandleRegistrationResponse(cmHandleId);
338 cmHandleRegistrationResponses.add(cmHandleRegistrationResponse);
339 if (cmHandleRegistrationResponse.getStatus() != CmHandleRegistrationResponse.Status.SUCCESS) {
340 notDeletedCmHandles.add(cmHandleId);
345 yangModelCmHandles.removeIf(yangModelCmHandle -> notDeletedCmHandles.contains(yangModelCmHandle.getId()));
346 updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETED);
347 dmiPluginRegistrationResponse.setRemovedCmHandles(cmHandleRegistrationResponses);
350 protected void processCreatedCmHandles(final DmiPluginRegistration dmiPluginRegistration,
351 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse) {
352 final List<NcmpServiceCmHandle> ncmpServiceCmHandles = dmiPluginRegistration.getCreatedCmHandles();
353 final List<CmHandleRegistrationResponse> failedCmHandleRegistrationResponses = new ArrayList<>();
356 final Collection<String> rejectedCmHandleIds
357 = checkAlternateIds(ncmpServiceCmHandles, failedCmHandleRegistrationResponses);
359 final Collection<String> succeededCmHandleIds = persistCmHandlesWithState(dmiPluginRegistration,
360 dmiPluginRegistrationResponse, ncmpServiceCmHandles, rejectedCmHandleIds);
362 processTrustLevels(ncmpServiceCmHandles, succeededCmHandleIds);
364 } catch (final AlreadyDefinedException alreadyDefinedException) {
365 failedCmHandleRegistrationResponses.addAll(CmHandleRegistrationResponse.createFailureResponsesFromXpaths(
366 alreadyDefinedException.getAlreadyDefinedObjectNames(), CM_HANDLE_ALREADY_EXIST));
367 } catch (final Exception exception) {
368 final Collection<String> cmHandleIds =
369 ncmpServiceCmHandles.stream().map(NcmpServiceCmHandle::getCmHandleId).collect(Collectors.toList());
370 failedCmHandleRegistrationResponses.addAll(CmHandleRegistrationResponse
371 .createFailureResponses(cmHandleIds, exception));
373 final List<CmHandleRegistrationResponse> mergedCmHandleRegistrationResponses
374 = new ArrayList<>(failedCmHandleRegistrationResponses);
375 mergedCmHandleRegistrationResponses.addAll(dmiPluginRegistrationResponse.getCreatedCmHandles());
377 dmiPluginRegistrationResponse.setCreatedCmHandles(mergedCmHandleRegistrationResponses);
380 protected void processUpdatedCmHandles(final DmiPluginRegistration dmiPluginRegistration,
381 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse) {
382 dmiPluginRegistrationResponse.setUpdatedCmHandles(networkCmProxyDataServicePropertyHandler
383 .updateCmHandleProperties(dmiPluginRegistration.getUpdatedCmHandles()));
386 protected void processUpgradedCmHandles(
387 final DmiPluginRegistration dmiPluginRegistration,
388 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse) {
390 final List<String> cmHandleIds = dmiPluginRegistration.getUpgradedCmHandles().getCmHandles();
391 final String upgradedModuleSetTag = dmiPluginRegistration.getUpgradedCmHandles().getModuleSetTag();
392 final Map<YangModelCmHandle, CmHandleState> acceptedCmHandleStatePerCmHandle
393 = new HashMap<>(cmHandleIds.size());
394 final List<CmHandleRegistrationResponse> cmHandleUpgradeResponses = new ArrayList<>(cmHandleIds.size());
396 for (final String cmHandleId : cmHandleIds) {
398 final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
399 if (yangModelCmHandle.getCompositeState().getCmHandleState() == CmHandleState.READY) {
400 if (moduleUpgradeCanBeSkipped(yangModelCmHandle, upgradedModuleSetTag)) {
401 cmHandleUpgradeResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId));
403 updateYangModelCmHandleForUpgrade(yangModelCmHandle, upgradedModuleSetTag);
404 acceptedCmHandleStatePerCmHandle.put(yangModelCmHandle, CmHandleState.LOCKED);
407 cmHandleUpgradeResponses.add(
408 CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_READY));
410 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
411 log.error("Unable to find data node for cm handle id : {} , caused by : {}",
412 cmHandleId, dataNodeNotFoundException.getMessage());
413 cmHandleUpgradeResponses.add(
414 CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND));
415 } catch (final DataValidationException dataValidationException) {
416 log.error("Unable to upgrade cm handle id: {}, caused by : {}",
417 cmHandleId, dataValidationException.getMessage());
418 cmHandleUpgradeResponses.add(
419 CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID));
422 cmHandleUpgradeResponses.addAll(upgradeCmHandles(acceptedCmHandleStatePerCmHandle));
423 dmiPluginRegistrationResponse.setUpgradedCmHandles(cmHandleUpgradeResponses);
426 private Collection<String> checkAlternateIds(
427 final List<NcmpServiceCmHandle> cmHandlesToBeCreated,
428 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses) {
429 final Collection<String> rejectedCmHandleIds = alternateIdChecker
430 .getIdsOfCmHandlesWithRejectedAlternateId(cmHandlesToBeCreated, AlternateIdChecker.Operation.CREATE);
431 cmHandleRegistrationResponses.addAll(CmHandleRegistrationResponse.createFailureResponses(
432 rejectedCmHandleIds, ALTERNATE_ID_ALREADY_ASSOCIATED));
433 return rejectedCmHandleIds;
436 private List<String> persistCmHandlesWithState(final DmiPluginRegistration dmiPluginRegistration,
437 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse,
438 final List<NcmpServiceCmHandle> cmHandlesToBeCreated,
439 final Collection<String> rejectedCmHandleIds) {
440 final List<String> succeededCmHandleIds = new ArrayList<>(cmHandlesToBeCreated.size());
441 final List<YangModelCmHandle> yangModelCmHandlesToRegister = new ArrayList<>(cmHandlesToBeCreated.size());
442 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
443 new ArrayList<>(cmHandlesToBeCreated.size());
444 for (final NcmpServiceCmHandle ncmpServiceCmHandle: cmHandlesToBeCreated) {
445 if (!rejectedCmHandleIds.contains(ncmpServiceCmHandle.getCmHandleId())) {
446 yangModelCmHandlesToRegister.add(getYangModelCmHandle(dmiPluginRegistration, ncmpServiceCmHandle));
447 cmHandleRegistrationResponses.add(
448 CmHandleRegistrationResponse.createSuccessResponse(ncmpServiceCmHandle.getCmHandleId()));
449 succeededCmHandleIds.add(ncmpServiceCmHandle.getCmHandleId());
452 lcmEventsCmHandleStateHandler.initiateStateAdvised(yangModelCmHandlesToRegister);
453 dmiPluginRegistrationResponse.setCreatedCmHandles(cmHandleRegistrationResponses);
454 return succeededCmHandleIds;
457 private YangModelCmHandle getYangModelCmHandle(final DmiPluginRegistration dmiPluginRegistration,
458 final NcmpServiceCmHandle ncmpServiceCmHandle) {
459 return YangModelCmHandle.toYangModelCmHandle(
460 dmiPluginRegistration.getDmiPlugin(),
461 dmiPluginRegistration.getDmiDataPlugin(),
462 dmiPluginRegistration.getDmiModelPlugin(),
464 ncmpServiceCmHandle.getModuleSetTag(),
465 ncmpServiceCmHandle.getAlternateId(),
466 ncmpServiceCmHandle.getDataProducerIdentifier());
469 private void processTrustLevels(final Collection<NcmpServiceCmHandle> cmHandlesToBeCreated,
470 final Collection<String> succeededCmHandleIds) {
471 final Map<String, TrustLevel> initialTrustLevelPerCmHandleId = new HashMap<>(cmHandlesToBeCreated.size());
472 for (final NcmpServiceCmHandle ncmpServiceCmHandle: cmHandlesToBeCreated) {
473 if (succeededCmHandleIds.contains(ncmpServiceCmHandle.getCmHandleId())) {
474 initialTrustLevelPerCmHandleId.put(ncmpServiceCmHandle.getCmHandleId(),
475 ncmpServiceCmHandle.getRegistrationTrustLevel());
478 trustLevelManager.handleInitialRegistrationOfTrustLevels(initialTrustLevelPerCmHandleId);
481 private static boolean moduleUpgradeCanBeSkipped(final YangModelCmHandle yangModelCmHandle,
482 final String upgradedModuleSetTag) {
483 if (StringUtils.isBlank(upgradedModuleSetTag)) {
486 return yangModelCmHandle.getModuleSetTag().equals(upgradedModuleSetTag);
489 private static void updateYangModelCmHandleForUpgrade(final YangModelCmHandle yangModelCmHandle,
490 final String upgradedModuleSetTag) {
491 final String lockReasonWithModuleSetTag = String.format(ModuleOperationsUtils.MODULE_SET_TAG_MESSAGE_FORMAT,
492 upgradedModuleSetTag);
493 yangModelCmHandle.setCompositeState(new CompositeStateBuilder().withCmHandleState(CmHandleState.READY)
494 .withLockReason(MODULE_UPGRADE, lockReasonWithModuleSetTag).build());
497 private CmHandleRegistrationResponse deleteCmHandleAndGetCmHandleRegistrationResponse(final String cmHandleId) {
499 deleteCmHandleFromDbAndModuleSyncMap(cmHandleId);
500 return CmHandleRegistrationResponse.createSuccessResponse(cmHandleId);
501 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
502 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
503 cmHandleId, dataNodeNotFoundException.getMessage());
504 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND);
505 } catch (final DataValidationException dataValidationException) {
506 log.error("Unable to de-register cm-handle id: {}, caused by: {}",
507 cmHandleId, dataValidationException.getMessage());
508 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID);
509 } catch (final Exception exception) {
510 log.error("Unable to de-register cm-handle id : {} , caused by : {}", cmHandleId, exception.getMessage());
511 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception);
515 private void updateCmHandleStateBatch(final Collection<YangModelCmHandle> yangModelCmHandles,
516 final CmHandleState cmHandleState) {
517 final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>(yangModelCmHandles.size());
518 yangModelCmHandles.forEach(yangModelCmHandle -> cmHandleStatePerCmHandle.put(yangModelCmHandle, cmHandleState));
519 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
522 private void deleteCmHandleFromDbAndModuleSyncMap(final String cmHandleId) {
523 inventoryPersistence.deleteSchemaSetWithCascade(cmHandleId);
524 inventoryPersistence.deleteDataNode(NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId + "']");
525 removeDeletedCmHandleFromModuleSyncMap(cmHandleId);
528 private void batchDeleteCmHandlesFromDbAndModuleSyncMap(final Collection<String> cmHandleIds) {
529 inventoryPersistence.deleteSchemaSetsWithCascade(cmHandleIds);
530 inventoryPersistence.deleteDataNodes(mapCmHandleIdsToXpaths(cmHandleIds));
531 cmHandleIds.forEach(this::removeDeletedCmHandleFromModuleSyncMap);
534 private Collection<String> mapCmHandleIdsToXpaths(final Collection<String> cmHandles) {
535 return cmHandles.stream()
536 .map(cmHandleId -> NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId + "']")
537 .collect(Collectors.toSet());
540 // CPS-1239 Robustness cleaning of in progress cache
541 private void removeDeletedCmHandleFromModuleSyncMap(final String cmHandleId) {
542 if (moduleSyncStartedOnCmHandles.remove(cmHandleId) != null) {
543 log.debug("{} removed from in progress map", cmHandleId);
547 private List<CmHandleRegistrationResponse> upgradeCmHandles(final Map<YangModelCmHandle, CmHandleState>
548 cmHandleStatePerCmHandle) {
549 final List<String> cmHandleIds = getCmHandleIds(cmHandleStatePerCmHandle);
550 log.info("Moving cm handles : {} into locked (for upgrade) state.", cmHandleIds);
552 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
553 return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
554 } catch (final Exception e) {
555 log.error("Unable to update cmHandleIds : {} , caused by : {}", cmHandleIds, e.getMessage());
556 return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, e);
560 private static List<String> getCmHandleIds(final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle) {
561 return cmHandleStatePerCmHandle.keySet().stream().map(YangModelCmHandle::getId).toList();
564 private void setTrustLevelPerDmiPlugin(final DmiPluginRegistration dmiPluginRegistration) {
565 if (DmiPluginRegistration.isNullEmptyOrBlank(dmiPluginRegistration.getDmiDataPlugin())) {
566 trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiPlugin(), TrustLevel.COMPLETE);
568 trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiDataPlugin(), TrustLevel.COMPLETE);