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.text.MessageFormat;
40 import java.time.OffsetDateTime;
41 import java.util.ArrayList;
42 import java.util.Collection;
43 import java.util.EnumMap;
44 import java.util.HashMap;
45 import java.util.HashSet;
46 import java.util.List;
49 import java.util.stream.Collectors;
50 import lombok.RequiredArgsConstructor;
51 import lombok.extern.slf4j.Slf4j;
52 import org.apache.commons.lang3.StringUtils;
53 import org.onap.cps.api.CpsDataService;
54 import org.onap.cps.ncmp.api.NcmpResponseStatus;
55 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandleQueryService;
56 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
57 import org.onap.cps.ncmp.api.impl.events.lcm.LcmEventsCmHandleStateHandler;
58 import org.onap.cps.ncmp.api.impl.inventory.CmHandleQueries;
59 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState;
60 import org.onap.cps.ncmp.api.impl.inventory.CompositeState;
61 import org.onap.cps.ncmp.api.impl.inventory.CompositeStateBuilder;
62 import org.onap.cps.ncmp.api.impl.inventory.CompositeStateUtils;
63 import org.onap.cps.ncmp.api.impl.inventory.DataStoreSyncState;
64 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence;
65 import org.onap.cps.ncmp.api.impl.inventory.sync.ModuleOperationsUtils;
66 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations;
67 import org.onap.cps.ncmp.api.impl.operations.OperationType;
68 import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevel;
69 import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevelManager;
70 import org.onap.cps.ncmp.api.impl.utils.CmHandleIdMapper;
71 import org.onap.cps.ncmp.api.impl.utils.CmHandleQueryConditions;
72 import org.onap.cps.ncmp.api.impl.utils.InventoryQueryConditions;
73 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
74 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
75 import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters;
76 import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters;
77 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse;
78 import org.onap.cps.ncmp.api.models.DataOperationRequest;
79 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
80 import org.onap.cps.ncmp.api.models.DmiPluginRegistrationResponse;
81 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
82 import org.onap.cps.spi.FetchDescendantsOption;
83 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
84 import org.onap.cps.spi.exceptions.CpsException;
85 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
86 import org.onap.cps.spi.exceptions.DataValidationException;
87 import org.onap.cps.spi.model.ModuleDefinition;
88 import org.onap.cps.spi.model.ModuleReference;
89 import org.onap.cps.utils.JsonObjectMapper;
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;
108 private final Map<String, TrustLevel> trustLevelPerDmiPlugin;
109 private final TrustLevelManager trustLevelManager;
110 private final CmHandleIdMapper cmHandleIdMapper;
111 private final Map<String, Collection<ModuleReference>> moduleSetTagCache;
114 public DmiPluginRegistrationResponse updateDmiRegistrationAndSyncModule(
115 final DmiPluginRegistration dmiPluginRegistration) {
116 cacheAlternateIds(dmiPluginRegistration.getCreatedCmHandles());
117 dmiPluginRegistration.validateDmiPluginRegistration();
118 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse = new DmiPluginRegistrationResponse();
120 setTrustLevelPerDmiPlugin(dmiPluginRegistration);
122 if (!dmiPluginRegistration.getRemovedCmHandles().isEmpty()) {
123 dmiPluginRegistrationResponse.setRemovedCmHandles(
124 parseAndProcessDeletedCmHandlesInRegistration(dmiPluginRegistration.getRemovedCmHandles()));
127 if (!dmiPluginRegistration.getCreatedCmHandles().isEmpty()) {
128 dmiPluginRegistrationResponse.setCreatedCmHandles(
129 parseAndProcessCreatedCmHandlesInRegistration(dmiPluginRegistration));
131 if (!dmiPluginRegistration.getUpdatedCmHandles().isEmpty()) {
132 dmiPluginRegistrationResponse.setUpdatedCmHandles(
133 networkCmProxyDataServicePropertyHandler
134 .updateCmHandleProperties(dmiPluginRegistration.getUpdatedCmHandles()));
136 if (dmiPluginRegistration.getUpgradedCmHandles() != null
137 && !dmiPluginRegistration.getUpgradedCmHandles().getCmHandles().isEmpty()) {
138 dmiPluginRegistrationResponse.setUpgradedCmHandles(
139 parseAndProcessUpgradedCmHandlesInRegistration(dmiPluginRegistration));
142 return dmiPluginRegistrationResponse;
146 public Object getResourceDataForCmHandle(final String datastoreName,
147 final String cmHandleId,
148 final String resourceIdentifier,
149 final String optionsParamInQuery,
150 final String topicParamInQuery,
151 final String requestId) {
152 final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(datastoreName, cmHandleId,
157 return responseEntity.getBody();
161 public Object getResourceDataForCmHandle(final String datastoreName,
162 final String cmHandleId,
163 final String resourceIdentifier,
164 final FetchDescendantsOption fetchDescendantsOption) {
165 return cpsDataService.getDataNodes(datastoreName, cmHandleId, resourceIdentifier,
166 fetchDescendantsOption).iterator().next();
170 public void executeDataOperationForCmHandles(final String topicParamInQuery,
171 final DataOperationRequest
172 dataOperationRequest,
173 final String requestId) {
174 dmiDataOperations.requestResourceDataFromDmi(topicParamInQuery, dataOperationRequest, requestId);
178 public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
179 final String resourceIdentifier,
180 final OperationType operationType,
181 final String requestData,
182 final String dataType) {
183 return dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier,
184 operationType, requestData, dataType);
188 public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
189 return inventoryPersistence.getYangResourcesModuleReferences(cmHandleId);
193 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
194 return inventoryPersistence.getModuleDefinitionsByCmHandleId(cmHandleId);
198 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleAndModule(final String cmHandleId,
199 final String moduleName,
200 final String moduleRevision) {
201 return inventoryPersistence.getModuleDefinitionsByCmHandleAndModule(cmHandleId, moduleName, moduleRevision);
205 * Retrieve cm handles with details for the given query parameters.
207 * @param cmHandleQueryApiParameters cm handle query parameters
208 * @return cm handles with details
211 public Collection<NcmpServiceCmHandle> executeCmHandleSearch(
212 final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
213 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
214 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
215 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
216 return networkCmProxyCmHandleQueryService.queryCmHandles(cmHandleQueryServiceParameters);
220 * Retrieve cm handle ids for the given query parameters.
222 * @param cmHandleQueryApiParameters cm handle query parameters
223 * @return cm handle ids
226 public Collection<String> executeCmHandleIdSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
227 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
228 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
229 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
230 return networkCmProxyCmHandleQueryService.queryCmHandleIds(cmHandleQueryServiceParameters);
234 * Set the data sync enabled flag, along with the data sync state
235 * based on the data sync enabled boolean for the cm handle id provided.
237 * @param cmHandleId cm handle id
238 * @param dataSyncEnabledTargetValue data sync enabled flag
241 public void setDataSyncEnabled(final String cmHandleId, final Boolean dataSyncEnabledTargetValue) {
242 final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
243 if (dataSyncEnabledTargetValue.equals(compositeState.getDataSyncEnabled())) {
244 log.info("Data-Sync Enabled flag is already: {} ", dataSyncEnabledTargetValue);
247 if (CmHandleState.READY.equals(compositeState.getCmHandleState())) {
248 final DataStoreSyncState dataStoreSyncState = compositeState.getDataStores()
249 .getOperationalDataStore().getDataStoreSyncState();
250 if (Boolean.FALSE.equals(dataSyncEnabledTargetValue)
251 && DataStoreSyncState.SYNCHRONIZED.equals(dataStoreSyncState)) {
252 // TODO : This is hard-coded for onap dmi that need to be addressed
253 cpsDataService.deleteDataNode(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId,
254 "/netconf-state", OffsetDateTime.now());
256 CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabledTargetValue, compositeState);
257 inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
259 throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: "
260 + compositeState.getCmHandleState());
265 * Get all cm handle IDs by DMI plugin identifier.
267 * @param dmiPluginIdentifier DMI plugin identifier
268 * @return set of cm handle IDs
271 public Collection<String> getAllCmHandleIdsByDmiPluginIdentifier(final String dmiPluginIdentifier) {
272 return cmHandleQueries.getCmHandleIdsByDmiPluginIdentifier(dmiPluginIdentifier);
276 * Get all cm handle IDs by various properties.
278 * @param cmHandleQueryServiceParameters cm handle query parameters
279 * @return set of cm handle IDs
282 public Collection<String> executeCmHandleIdSearchForInventory(
283 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
284 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, InventoryQueryConditions.ALL_CONDITION_NAMES);
285 return networkCmProxyCmHandleQueryService.queryCmHandleIdsForInventory(cmHandleQueryServiceParameters);
289 * Retrieve cm handle details for a given cm handle.
291 * @param cmHandleId cm handle identifier
292 * @return cm handle details
295 public NcmpServiceCmHandle getNcmpServiceCmHandle(final String cmHandleId) {
296 return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(
297 inventoryPersistence.getYangModelCmHandle(cmHandleId));
301 * Get cm handle public properties for a given cm handle id.
303 * @param cmHandleId cm handle identifier
304 * @return cm handle public properties
307 public Map<String, String> getCmHandlePublicProperties(final String cmHandleId) {
308 final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
309 final List<YangModelCmHandle.Property> yangModelPublicProperties = yangModelCmHandle.getPublicProperties();
310 final Map<String, String> cmHandlePublicProperties = new HashMap<>();
311 YangDataConverter.asPropertiesMap(yangModelPublicProperties, cmHandlePublicProperties);
312 return cmHandlePublicProperties;
316 * Get cm handle composite state for a given cm handle id.
318 * @param cmHandleId cm handle identifier
319 * @return cm handle state
322 public CompositeState getCmHandleCompositeState(final String cmHandleId) {
323 return inventoryPersistence.getYangModelCmHandle(cmHandleId).getCompositeState();
327 * THis method registers a cm handle and initiates modules sync.
329 * @param dmiPluginRegistration dmi plugin registration information.
330 * @return cm-handle registration response for create cm-handle requests.
332 public List<CmHandleRegistrationResponse> parseAndProcessCreatedCmHandlesInRegistration(
333 final DmiPluginRegistration dmiPluginRegistration) {
334 final List<NcmpServiceCmHandle> cmHandlesToBeCreated = dmiPluginRegistration.getCreatedCmHandles();
335 final Map<String, TrustLevel> initialTrustLevelPerCmHandleId = new HashMap<>(cmHandlesToBeCreated.size());
336 final List<YangModelCmHandle> yangModelCmHandles = new ArrayList<>(cmHandlesToBeCreated.size());
338 .forEach(cmHandle -> {
339 final YangModelCmHandle yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle(
340 dmiPluginRegistration.getDmiPlugin(),
341 dmiPluginRegistration.getDmiDataPlugin(),
342 dmiPluginRegistration.getDmiModelPlugin(),
344 cmHandle.getModuleSetTag(),
345 cmHandle.getAlternateId());
346 yangModelCmHandles.add(yangModelCmHandle);
347 initialTrustLevelPerCmHandleId.put(cmHandle.getCmHandleId(), cmHandle.getRegistrationTrustLevel());
349 return registerNewCmHandles(yangModelCmHandles, initialTrustLevelPerCmHandleId);
352 protected List<CmHandleRegistrationResponse> parseAndProcessDeletedCmHandlesInRegistration(
353 final List<String> tobeRemovedCmHandles) {
354 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
355 new ArrayList<>(tobeRemovedCmHandles.size());
356 final Collection<YangModelCmHandle> yangModelCmHandles =
357 inventoryPersistence.getYangModelCmHandles(tobeRemovedCmHandles);
358 final Set<String> moduleSetTags = yangModelCmHandles.stream().map(YangModelCmHandle::getModuleSetTag)
359 .collect(Collectors.toSet());
360 updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETING);
362 final Set<String> notDeletedCmHandles = new HashSet<>();
363 for (final List<String> tobeRemovedCmHandleBatch : Lists.partition(tobeRemovedCmHandles, DELETE_BATCH_SIZE)) {
365 batchDeleteCmHandlesFromDbAndModuleSyncMap(tobeRemovedCmHandleBatch);
366 moduleSetTags.forEach(moduleSetTagCache::remove);
367 log.debug("Removed module set tags: {}", moduleSetTags);
368 tobeRemovedCmHandleBatch.forEach(cmHandleId ->
369 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId)));
371 } catch (final RuntimeException batchException) {
372 log.error("Unable to de-register cm-handle batch, retrying on each cm handle");
373 for (final String cmHandleId : tobeRemovedCmHandleBatch) {
374 final CmHandleRegistrationResponse cmHandleRegistrationResponse =
375 deleteCmHandleAndGetCmHandleRegistrationResponse(cmHandleId);
376 cmHandleRegistrationResponses.add(cmHandleRegistrationResponse);
377 if (cmHandleRegistrationResponse.getStatus() != CmHandleRegistrationResponse.Status.SUCCESS) {
378 notDeletedCmHandles.add(cmHandleId);
384 yangModelCmHandles.removeIf(yangModelCmHandle -> notDeletedCmHandles.contains(yangModelCmHandle.getId()));
385 updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETED);
386 removeEntriesFromAlternateIdCache(yangModelCmHandles);
388 return cmHandleRegistrationResponses;
391 private void removeEntriesFromAlternateIdCache(final Collection<YangModelCmHandle> yangModelCmHandles) {
392 for (final YangModelCmHandle yangModelCmHandle : yangModelCmHandles) {
393 cmHandleIdMapper.removeMapping(yangModelCmHandle.getId());
397 protected List<CmHandleRegistrationResponse> parseAndProcessUpgradedCmHandlesInRegistration(
398 final DmiPluginRegistration dmiPluginRegistration) {
400 final List<String> upgradedCmHandleIds = dmiPluginRegistration.getUpgradedCmHandles().getCmHandles();
402 final Map<YangModelCmHandle, CmHandleState> acceptedCmHandleStatePerCmHandle
403 = new HashMap<>(upgradedCmHandleIds.size());
405 final Map<NcmpResponseStatus, List<String>> failedCmHandlesPerResponseStatus
406 = new EnumMap<>(NcmpResponseStatus.class);
407 final List<String> nonExistingCmHandleIds = new ArrayList<>();
408 final List<String> nonReadyCmHandleIds = new ArrayList<>();
409 final List<String> invalidCmHandleIds = new ArrayList<>();
411 upgradedCmHandleIds.forEach(cmHandleId -> {
413 if (cmHandleQueries.cmHandleHasState(cmHandleId, CmHandleState.READY)) {
414 final YangModelCmHandle yangModelCmHandleWithUpgradeDetails
415 = createYangModelCmHandle(dmiPluginRegistration, cmHandleId);
416 acceptedCmHandleStatePerCmHandle.put(yangModelCmHandleWithUpgradeDetails, CmHandleState.LOCKED);
418 nonReadyCmHandleIds.add(cmHandleId);
420 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
421 log.error("Unable to find data node for cm handle id : {} , caused by : {}",
422 cmHandleId, dataNodeNotFoundException.getMessage());
423 nonExistingCmHandleIds.add(cmHandleId);
424 } catch (final DataValidationException dataValidationException) {
425 log.error("Unable to upgrade cm handle id: {}, caused by : {}",
426 cmHandleId, dataValidationException.getMessage());
427 invalidCmHandleIds.add(cmHandleId);
430 failedCmHandlesPerResponseStatus.put(CM_HANDLES_NOT_READY, nonReadyCmHandleIds);
431 failedCmHandlesPerResponseStatus.put(CM_HANDLES_NOT_FOUND, nonExistingCmHandleIds);
432 failedCmHandlesPerResponseStatus.put(CM_HANDLE_INVALID_ID, invalidCmHandleIds);
433 return mergeAllCmHandleResponses(acceptedCmHandleStatePerCmHandle, failedCmHandlesPerResponseStatus);
436 private static YangModelCmHandle createYangModelCmHandle(final DmiPluginRegistration dmiPluginRegistration,
437 final String cmHandleId) {
438 final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle();
439 ncmpServiceCmHandle.setCmHandleId(cmHandleId);
440 final String moduleSetTag = dmiPluginRegistration.getUpgradedCmHandles().getModuleSetTag();
441 final String lockReasonWithModuleSetTag = MessageFormat.format(
442 ModuleOperationsUtils.MODULE_SET_TAG_MESSAGE_FORMAT, moduleSetTag);
443 ncmpServiceCmHandle.setCompositeState(new CompositeStateBuilder().withCmHandleState(CmHandleState.READY)
444 .withLockReason(MODULE_UPGRADE, lockReasonWithModuleSetTag).build());
445 return YangModelCmHandle.toYangModelCmHandle(dmiPluginRegistration.getDmiPlugin(),
446 dmiPluginRegistration.getDmiDataPlugin(), dmiPluginRegistration.getDmiModelPlugin(),
447 ncmpServiceCmHandle, moduleSetTag, ncmpServiceCmHandle.getAlternateId());
450 private CmHandleRegistrationResponse deleteCmHandleAndGetCmHandleRegistrationResponse(final String cmHandleId) {
452 deleteCmHandleFromDbAndModuleSyncMap(cmHandleId);
453 return CmHandleRegistrationResponse.createSuccessResponse(cmHandleId);
454 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
455 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
456 cmHandleId, dataNodeNotFoundException.getMessage());
457 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND);
458 } catch (final DataValidationException dataValidationException) {
459 log.error("Unable to de-register cm-handle id: {}, caused by: {}",
460 cmHandleId, dataValidationException.getMessage());
461 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID);
462 } catch (final Exception exception) {
463 log.error("Unable to de-register cm-handle id : {} , caused by : {}", cmHandleId, exception.getMessage());
464 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception);
468 private void updateCmHandleStateBatch(final Collection<YangModelCmHandle> yangModelCmHandles,
469 final CmHandleState cmHandleState) {
470 final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>(yangModelCmHandles.size());
471 yangModelCmHandles.forEach(yangModelCmHandle -> cmHandleStatePerCmHandle.put(yangModelCmHandle, cmHandleState));
472 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
475 private void deleteCmHandleFromDbAndModuleSyncMap(final String cmHandleId) {
476 inventoryPersistence.deleteSchemaSetWithCascade(cmHandleId);
477 inventoryPersistence.deleteDataNode(NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId
479 removeDeletedCmHandleFromModuleSyncMap(cmHandleId);
482 private void batchDeleteCmHandlesFromDbAndModuleSyncMap(final Collection<String> tobeRemovedCmHandles) {
483 inventoryPersistence.deleteSchemaSetsWithCascade(tobeRemovedCmHandles);
484 inventoryPersistence.deleteDataNodes(mapCmHandleIdsToXpaths(tobeRemovedCmHandles));
485 tobeRemovedCmHandles.forEach(this::removeDeletedCmHandleFromModuleSyncMap);
488 private Collection<String> mapCmHandleIdsToXpaths(final Collection<String> cmHandles) {
489 return cmHandles.stream()
490 .map(cmHandleId -> NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId + "']")
491 .collect(Collectors.toSet());
494 // CPS-1239 Robustness cleaning of in progress cache
495 private void removeDeletedCmHandleFromModuleSyncMap(final String deletedCmHandleId) {
496 if (moduleSyncStartedOnCmHandles.remove(deletedCmHandleId) != null) {
497 log.debug("{} removed from in progress map", deletedCmHandleId);
501 private List<CmHandleRegistrationResponse> registerNewCmHandles(final List<YangModelCmHandle> yangModelCmHandles,
502 final Map<String, TrustLevel>
503 initialTrustLevelPerCmHandleId) {
504 final List<CmHandleRegistrationResponse> failureResponses = new ArrayList<>();
505 final List<YangModelCmHandle> acceptedYangModelCmHandles = new ArrayList<>(yangModelCmHandles.size());
506 final Set<String> acceptedCmHandleIds = new HashSet<>(yangModelCmHandles.size());
507 for (final YangModelCmHandle yangModelCmHandle : yangModelCmHandles) {
508 if (cmHandleIdMapper.isDuplicateId(yangModelCmHandle.getId(), yangModelCmHandle.getAlternateId())) {
509 initialTrustLevelPerCmHandleId.remove(yangModelCmHandle.getId());
510 failureResponses.add(CmHandleRegistrationResponse.createFailureResponse(
511 yangModelCmHandle.getId(), ALTERNATE_ID_ALREADY_ASSOCIATED));
513 acceptedCmHandleIds.add(yangModelCmHandle.getId());
514 acceptedYangModelCmHandles.add(yangModelCmHandle);
518 lcmEventsCmHandleStateHandler.initiateStateAdvised(acceptedYangModelCmHandles);
519 trustLevelManager.handleInitialRegistrationOfTrustLevels(initialTrustLevelPerCmHandleId);
520 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = CmHandleRegistrationResponse
521 .createSuccessResponses(acceptedCmHandleIds);
522 cmHandleRegistrationResponses.addAll(failureResponses);
523 return cmHandleRegistrationResponses;
524 } catch (final AlreadyDefinedException alreadyDefinedException) {
525 final List<CmHandleRegistrationResponse> alreadyDefinedResponses = CmHandleRegistrationResponse
526 .createFailureResponses(
527 alreadyDefinedException.getAlreadyDefinedObjectNames(), CM_HANDLE_ALREADY_EXIST);
528 failureResponses.addAll(alreadyDefinedResponses);
529 return failureResponses;
530 } catch (final Exception exception) {
531 return CmHandleRegistrationResponse
532 .createFailureResponses(initialTrustLevelPerCmHandleId.keySet(), exception);
536 private List<CmHandleRegistrationResponse> mergeAllCmHandleResponses(
537 final Map<YangModelCmHandle, CmHandleState> acceptedCmHandleStatePerCmHandle,
538 final Map<NcmpResponseStatus, List<String>> failedCmHandlesPerResponseStatus) {
539 final List<CmHandleRegistrationResponse> cmHandleUpgradeResponses
540 = upgradeCmHandles(acceptedCmHandleStatePerCmHandle);
541 failedCmHandlesPerResponseStatus.forEach((ncmpResponseStatus, cmHandleIds) ->
542 cmHandleIds.forEach(cmHandleId -> cmHandleUpgradeResponses.add(CmHandleRegistrationResponse
543 .createFailureResponse(cmHandleId, ncmpResponseStatus))));
544 return cmHandleUpgradeResponses;
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);
572 private void cacheAlternateIds(final Collection<NcmpServiceCmHandle> ncmpServiceCmHandles) {
573 for (final NcmpServiceCmHandle ncmpServiceCmHandle : ncmpServiceCmHandles) {
574 if (!StringUtils.isEmpty(ncmpServiceCmHandle.getAlternateId())) {
575 cmHandleIdMapper.addMapping(ncmpServiceCmHandle.getCmHandleId(), ncmpServiceCmHandle.getAlternateId());