Extend API: Get Module Definitions
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / NetworkCmProxyDataServiceImpl.java
1 /*
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
12  *
13  *        http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
21  *  SPDX-License-Identifier: Apache-2.0
22  *  ============LICENSE_END=========================================================
23  */
24
25 package org.onap.cps.ncmp.api.impl;
26
27 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLES_NOT_FOUND;
28 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLES_NOT_READY;
29 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLE_ALREADY_EXIST;
30 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLE_INVALID_ID;
31 import static org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory.MODULE_UPGRADE;
32 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DMI_REGISTRY_PARENT;
33 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME;
34 import static org.onap.cps.ncmp.api.impl.utils.RestQueryParametersValidator.validateCmHandleQueryParameters;
35
36 import com.google.common.collect.Lists;
37 import com.hazelcast.map.IMap;
38 import java.text.MessageFormat;
39 import java.time.OffsetDateTime;
40 import java.util.ArrayList;
41 import java.util.Collection;
42 import java.util.EnumMap;
43 import java.util.HashMap;
44 import java.util.HashSet;
45 import java.util.List;
46 import java.util.Map;
47 import java.util.Set;
48 import java.util.stream.Collectors;
49 import lombok.RequiredArgsConstructor;
50 import lombok.extern.slf4j.Slf4j;
51 import org.onap.cps.api.CpsDataService;
52 import org.onap.cps.ncmp.api.NcmpResponseStatus;
53 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandleQueryService;
54 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
55 import org.onap.cps.ncmp.api.impl.events.lcm.LcmEventsCmHandleStateHandler;
56 import org.onap.cps.ncmp.api.impl.inventory.CmHandleQueries;
57 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState;
58 import org.onap.cps.ncmp.api.impl.inventory.CompositeState;
59 import org.onap.cps.ncmp.api.impl.inventory.CompositeStateBuilder;
60 import org.onap.cps.ncmp.api.impl.inventory.CompositeStateUtils;
61 import org.onap.cps.ncmp.api.impl.inventory.DataStoreSyncState;
62 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence;
63 import org.onap.cps.ncmp.api.impl.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.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;
89
90 @Slf4j
91 @Service
92 @RequiredArgsConstructor
93 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
94
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
108     @Override
109     public DmiPluginRegistrationResponse updateDmiRegistrationAndSyncModule(
110         final DmiPluginRegistration dmiPluginRegistration) {
111         dmiPluginRegistration.validateDmiPluginRegistration();
112         final DmiPluginRegistrationResponse dmiPluginRegistrationResponse = new DmiPluginRegistrationResponse();
113
114         setTrustLevelPerDmiPlugin(dmiPluginRegistration);
115
116         if (!dmiPluginRegistration.getRemovedCmHandles().isEmpty()) {
117             dmiPluginRegistrationResponse.setRemovedCmHandles(
118                 parseAndProcessDeletedCmHandlesInRegistration(dmiPluginRegistration.getRemovedCmHandles()));
119         }
120
121         if (!dmiPluginRegistration.getCreatedCmHandles().isEmpty()) {
122             dmiPluginRegistrationResponse.setCreatedCmHandles(
123                 parseAndProcessCreatedCmHandlesInRegistration(dmiPluginRegistration));
124         }
125         if (!dmiPluginRegistration.getUpdatedCmHandles().isEmpty()) {
126             dmiPluginRegistrationResponse.setUpdatedCmHandles(
127                 networkCmProxyDataServicePropertyHandler
128                     .updateCmHandleProperties(dmiPluginRegistration.getUpdatedCmHandles()));
129         }
130         if (dmiPluginRegistration.getUpgradedCmHandles() != null
131             && !dmiPluginRegistration.getUpgradedCmHandles().getCmHandles().isEmpty()) {
132             dmiPluginRegistrationResponse.setUpgradedCmHandles(
133                 parseAndProcessUpgradedCmHandlesInRegistration(dmiPluginRegistration));
134         }
135
136         return dmiPluginRegistrationResponse;
137     }
138
139     @Override
140     public Object getResourceDataForCmHandle(final String datastoreName,
141                                              final String cmHandleId,
142                                              final String resourceIdentifier,
143                                              final String optionsParamInQuery,
144                                              final String topicParamInQuery,
145                                              final String requestId) {
146         final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(datastoreName, cmHandleId,
147             resourceIdentifier,
148             optionsParamInQuery,
149             topicParamInQuery,
150             requestId);
151         return responseEntity.getBody();
152     }
153
154     @Override
155     public Object getResourceDataForCmHandle(final String datastoreName,
156                                              final String cmHandleId,
157                                              final String resourceIdentifier,
158                                              final FetchDescendantsOption fetchDescendantsOption) {
159         return cpsDataService.getDataNodes(datastoreName, cmHandleId, resourceIdentifier,
160             fetchDescendantsOption).iterator().next();
161     }
162
163     @Override
164     public void executeDataOperationForCmHandles(final String topicParamInQuery,
165                                                  final DataOperationRequest
166                                                      dataOperationRequest,
167                                                  final String requestId) {
168         dmiDataOperations.requestResourceDataFromDmi(topicParamInQuery, dataOperationRequest, requestId);
169     }
170
171     @Override
172     public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
173                                                                  final String resourceIdentifier,
174                                                                  final OperationType operationType,
175                                                                  final String requestData,
176                                                                  final String dataType) {
177         return dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier,
178             operationType, requestData, dataType);
179     }
180
181     @Override
182     public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
183         return inventoryPersistence.getYangResourcesModuleReferences(cmHandleId);
184     }
185
186     @Override
187     public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
188         return inventoryPersistence.getModuleDefinitionsByCmHandleId(cmHandleId);
189     }
190
191     @Override
192     public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleAndModule(final String cmHandleId,
193                                                                                 final String moduleName,
194                                                                                 final String moduleRevision) {
195         return inventoryPersistence.getModuleDefinitionsByCmHandleAndModule(cmHandleId, moduleName, moduleRevision);
196     }
197
198     /**
199      * Retrieve cm handles with details for the given query parameters.
200      *
201      * @param cmHandleQueryApiParameters cm handle query parameters
202      * @return cm handles with details
203      */
204     @Override
205     public Collection<NcmpServiceCmHandle> executeCmHandleSearch(
206         final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
207         final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
208             cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
209         validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
210         return networkCmProxyCmHandleQueryService.queryCmHandles(cmHandleQueryServiceParameters);
211     }
212
213     /**
214      * Retrieve cm handle ids for the given query parameters.
215      *
216      * @param cmHandleQueryApiParameters cm handle query parameters
217      * @return cm handle ids
218      */
219     @Override
220     public Collection<String> executeCmHandleIdSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
221         final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
222             cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
223         validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
224         return networkCmProxyCmHandleQueryService.queryCmHandleIds(cmHandleQueryServiceParameters);
225     }
226
227     /**
228      * Set the data sync enabled flag, along with the data sync state
229      * based on the data sync enabled boolean for the cm handle id provided.
230      *
231      * @param cmHandleId                 cm handle id
232      * @param dataSyncEnabledTargetValue data sync enabled flag
233      */
234     @Override
235     public void setDataSyncEnabled(final String cmHandleId, final Boolean dataSyncEnabledTargetValue) {
236         final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
237         if (dataSyncEnabledTargetValue.equals(compositeState.getDataSyncEnabled())) {
238             log.info("Data-Sync Enabled flag is already: {} ", dataSyncEnabledTargetValue);
239             return;
240         }
241         if (CmHandleState.READY.equals(compositeState.getCmHandleState())) {
242             final DataStoreSyncState dataStoreSyncState = compositeState.getDataStores()
243                 .getOperationalDataStore().getDataStoreSyncState();
244             if (Boolean.FALSE.equals(dataSyncEnabledTargetValue)
245                 && DataStoreSyncState.SYNCHRONIZED.equals(dataStoreSyncState)) {
246                 // TODO : This is hard-coded for onap dmi that need to be addressed
247                 cpsDataService.deleteDataNode(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId,
248                     "/netconf-state", OffsetDateTime.now());
249             }
250             CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabledTargetValue, compositeState);
251             inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
252         } else {
253             throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: "
254                 + compositeState.getCmHandleState());
255         }
256     }
257
258     /**
259      * Get all cm handle IDs by DMI plugin identifier.
260      *
261      * @param dmiPluginIdentifier DMI plugin identifier
262      * @return set of cm handle IDs
263      */
264     @Override
265     public Collection<String> getAllCmHandleIdsByDmiPluginIdentifier(final String dmiPluginIdentifier) {
266         return cmHandleQueries.getCmHandleIdsByDmiPluginIdentifier(dmiPluginIdentifier);
267     }
268
269     /**
270      * Get all cm handle IDs by various properties.
271      *
272      * @param cmHandleQueryServiceParameters cm handle query parameters
273      * @return set of cm handle IDs
274      */
275     @Override
276     public Collection<String> executeCmHandleIdSearchForInventory(
277         final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
278         validateCmHandleQueryParameters(cmHandleQueryServiceParameters, InventoryQueryConditions.ALL_CONDITION_NAMES);
279         return networkCmProxyCmHandleQueryService.queryCmHandleIdsForInventory(cmHandleQueryServiceParameters);
280     }
281
282     /**
283      * Retrieve cm handle details for a given cm handle.
284      *
285      * @param cmHandleId cm handle identifier
286      * @return cm handle details
287      */
288     @Override
289     public NcmpServiceCmHandle getNcmpServiceCmHandle(final String cmHandleId) {
290         return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(
291             inventoryPersistence.getYangModelCmHandle(cmHandleId));
292     }
293
294     /**
295      * Get cm handle public properties for a given cm handle id.
296      *
297      * @param cmHandleId cm handle identifier
298      * @return cm handle public properties
299      */
300     @Override
301     public Map<String, String> getCmHandlePublicProperties(final String cmHandleId) {
302         final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
303         final List<YangModelCmHandle.Property> yangModelPublicProperties = yangModelCmHandle.getPublicProperties();
304         final Map<String, String> cmHandlePublicProperties = new HashMap<>();
305         YangDataConverter.asPropertiesMap(yangModelPublicProperties, cmHandlePublicProperties);
306         return cmHandlePublicProperties;
307     }
308
309     /**
310      * Get cm handle composite state for a given cm handle id.
311      *
312      * @param cmHandleId cm handle identifier
313      * @return cm handle state
314      */
315     @Override
316     public CompositeState getCmHandleCompositeState(final String cmHandleId) {
317         return inventoryPersistence.getYangModelCmHandle(cmHandleId).getCompositeState();
318     }
319
320     /**
321      * THis method registers a cm handle and initiates modules sync.
322      *
323      * @param dmiPluginRegistration dmi plugin registration information.
324      * @return cm-handle registration response for create cm-handle requests.
325      */
326     public List<CmHandleRegistrationResponse> parseAndProcessCreatedCmHandlesInRegistration(
327         final DmiPluginRegistration dmiPluginRegistration) {
328         final List<NcmpServiceCmHandle> cmHandlesToBeCreated = dmiPluginRegistration.getCreatedCmHandles();
329         final Map<String, TrustLevel> initialTrustLevelPerCmHandleId = new HashMap<>(cmHandlesToBeCreated.size());
330         final List<YangModelCmHandle> yangModelCmHandles = new ArrayList<>(cmHandlesToBeCreated.size());
331         cmHandlesToBeCreated
332                 .forEach(cmHandle -> {
333                     final YangModelCmHandle yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle(
334                             dmiPluginRegistration.getDmiPlugin(),
335                             dmiPluginRegistration.getDmiDataPlugin(),
336                             dmiPluginRegistration.getDmiModelPlugin(),
337                             cmHandle,
338                             cmHandle.getModuleSetTag(),
339                             cmHandle.getAlternateId());
340                     yangModelCmHandles.add(yangModelCmHandle);
341                     initialTrustLevelPerCmHandleId.put(cmHandle.getCmHandleId(), cmHandle.getRegistrationTrustLevel());
342                 });
343         return registerNewCmHandles(yangModelCmHandles, initialTrustLevelPerCmHandleId);
344     }
345
346     protected List<CmHandleRegistrationResponse> parseAndProcessDeletedCmHandlesInRegistration(
347         final List<String> tobeRemovedCmHandles) {
348         final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
349             new ArrayList<>(tobeRemovedCmHandles.size());
350         final Collection<YangModelCmHandle> yangModelCmHandles =
351             inventoryPersistence.getYangModelCmHandles(tobeRemovedCmHandles);
352
353         updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETING);
354
355         final Set<String> notDeletedCmHandles = new HashSet<>();
356         for (final List<String> tobeRemovedCmHandleBatch : Lists.partition(tobeRemovedCmHandles, DELETE_BATCH_SIZE)) {
357             try {
358                 batchDeleteCmHandlesFromDbAndModuleSyncMap(tobeRemovedCmHandleBatch);
359                 tobeRemovedCmHandleBatch.forEach(cmHandleId ->
360                     cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId)));
361
362             } catch (final RuntimeException batchException) {
363                 log.error("Unable to de-register cm-handle batch, retrying on each cm handle");
364                 for (final String cmHandleId : tobeRemovedCmHandleBatch) {
365                     final CmHandleRegistrationResponse cmHandleRegistrationResponse =
366                         deleteCmHandleAndGetCmHandleRegistrationResponse(cmHandleId);
367                     cmHandleRegistrationResponses.add(cmHandleRegistrationResponse);
368                     if (cmHandleRegistrationResponse.getStatus() != CmHandleRegistrationResponse.Status.SUCCESS) {
369                         notDeletedCmHandles.add(cmHandleId);
370                     }
371                 }
372             }
373         }
374
375         yangModelCmHandles.removeIf(yangModelCmHandle -> notDeletedCmHandles.contains(yangModelCmHandle.getId()));
376         updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETED);
377
378         return cmHandleRegistrationResponses;
379     }
380
381     protected List<CmHandleRegistrationResponse> parseAndProcessUpgradedCmHandlesInRegistration(
382         final DmiPluginRegistration dmiPluginRegistration) {
383
384         final List<String> upgradedCmHandleIds = dmiPluginRegistration.getUpgradedCmHandles().getCmHandles();
385
386         final Map<YangModelCmHandle, CmHandleState> acceptedCmHandleStatePerCmHandle
387                 = new HashMap<>(upgradedCmHandleIds.size());
388
389         final Map<NcmpResponseStatus, List<String>> failedCmHandlesPerResponseStatus
390                 = new EnumMap<>(NcmpResponseStatus.class);
391         final List<String> nonExistingCmHandleIds = new ArrayList<>();
392         final List<String> nonReadyCmHandleIds = new ArrayList<>();
393         final List<String> invalidCmHandleIds = new ArrayList<>();
394
395         upgradedCmHandleIds.forEach(cmHandleId -> {
396             try {
397                 if (cmHandleQueries.cmHandleHasState(cmHandleId, CmHandleState.READY)) {
398                     final YangModelCmHandle yangModelCmHandleWithUpgradeDetails
399                             = createYangModelCmHandle(dmiPluginRegistration, cmHandleId);
400                     acceptedCmHandleStatePerCmHandle.put(yangModelCmHandleWithUpgradeDetails, CmHandleState.LOCKED);
401                 } else {
402                     nonReadyCmHandleIds.add(cmHandleId);
403                 }
404             } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
405                 log.error("Unable to find data node for cm handle id : {} , caused by : {}",
406                         cmHandleId, dataNodeNotFoundException.getMessage());
407                 nonExistingCmHandleIds.add(cmHandleId);
408             } catch (final DataValidationException dataValidationException) {
409                 log.error("Unable to upgrade cm handle id: {}, caused by : {}",
410                         cmHandleId, dataValidationException.getMessage());
411                 invalidCmHandleIds.add(cmHandleId);
412             }
413         });
414         failedCmHandlesPerResponseStatus.put(CM_HANDLES_NOT_READY, nonReadyCmHandleIds);
415         failedCmHandlesPerResponseStatus.put(CM_HANDLES_NOT_FOUND, nonExistingCmHandleIds);
416         failedCmHandlesPerResponseStatus.put(CM_HANDLE_INVALID_ID, invalidCmHandleIds);
417         return mergeAllCmHandleResponses(acceptedCmHandleStatePerCmHandle, failedCmHandlesPerResponseStatus);
418     }
419
420     private static YangModelCmHandle createYangModelCmHandle(final DmiPluginRegistration dmiPluginRegistration,
421                                                              final String cmHandleId) {
422         final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle();
423         ncmpServiceCmHandle.setCmHandleId(cmHandleId);
424         final String moduleSetTag = dmiPluginRegistration.getUpgradedCmHandles().getModuleSetTag();
425         final String lockReasonWithModuleSetTag = MessageFormat.format(
426                 ModuleOperationsUtils.MODULE_SET_TAG_MESSAGE_FORMAT, moduleSetTag);
427         ncmpServiceCmHandle.setCompositeState(new CompositeStateBuilder().withCmHandleState(CmHandleState.READY)
428                 .withLockReason(MODULE_UPGRADE, lockReasonWithModuleSetTag).build());
429         return YangModelCmHandle.toYangModelCmHandle(dmiPluginRegistration.getDmiPlugin(),
430                 dmiPluginRegistration.getDmiDataPlugin(), dmiPluginRegistration.getDmiModelPlugin(),
431                 ncmpServiceCmHandle, moduleSetTag, ncmpServiceCmHandle.getAlternateId());
432     }
433
434     private CmHandleRegistrationResponse deleteCmHandleAndGetCmHandleRegistrationResponse(final String cmHandleId) {
435         try {
436             deleteCmHandleFromDbAndModuleSyncMap(cmHandleId);
437             return CmHandleRegistrationResponse.createSuccessResponse(cmHandleId);
438         } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
439             log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
440                 cmHandleId, dataNodeNotFoundException.getMessage());
441             return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND);
442         } catch (final DataValidationException dataValidationException) {
443             log.error("Unable to de-register cm-handle id: {}, caused by: {}",
444                 cmHandleId, dataValidationException.getMessage());
445             return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID);
446         } catch (final Exception exception) {
447             log.error("Unable to de-register cm-handle id : {} , caused by : {}", cmHandleId, exception.getMessage());
448             return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception);
449         }
450     }
451
452     private void updateCmHandleStateBatch(final Collection<YangModelCmHandle> yangModelCmHandles,
453                                           final CmHandleState cmHandleState) {
454         final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>(yangModelCmHandles.size());
455         yangModelCmHandles.forEach(yangModelCmHandle -> cmHandleStatePerCmHandle.put(yangModelCmHandle, cmHandleState));
456         lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
457     }
458
459     private void deleteCmHandleFromDbAndModuleSyncMap(final String cmHandleId) {
460         inventoryPersistence.deleteSchemaSetWithCascade(cmHandleId);
461         inventoryPersistence.deleteDataNode(NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId
462             + "']");
463         removeDeletedCmHandleFromModuleSyncMap(cmHandleId);
464     }
465
466     private void batchDeleteCmHandlesFromDbAndModuleSyncMap(final Collection<String> tobeRemovedCmHandles) {
467         inventoryPersistence.deleteSchemaSetsWithCascade(tobeRemovedCmHandles);
468         inventoryPersistence.deleteDataNodes(mapCmHandleIdsToXpaths(tobeRemovedCmHandles));
469         tobeRemovedCmHandles.forEach(this::removeDeletedCmHandleFromModuleSyncMap);
470     }
471
472     private Collection<String> mapCmHandleIdsToXpaths(final Collection<String> cmHandles) {
473         return cmHandles.stream()
474             .map(cmHandleId -> NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId + "']")
475             .collect(Collectors.toSet());
476     }
477
478     // CPS-1239 Robustness cleaning of in progress cache
479     private void removeDeletedCmHandleFromModuleSyncMap(final String deletedCmHandleId) {
480         if (moduleSyncStartedOnCmHandles.remove(deletedCmHandleId) != null) {
481             log.debug("{} removed from in progress map", deletedCmHandleId);
482         }
483     }
484
485     private List<CmHandleRegistrationResponse> registerNewCmHandles(final List<YangModelCmHandle> yangModelCmHandles,
486                                                                     final Map<String, TrustLevel>
487                                                                             initialTrustLevelPerCmHandleId) {
488         final Set<String> cmHandleIds = initialTrustLevelPerCmHandleId.keySet();
489         try {
490             lcmEventsCmHandleStateHandler.initiateStateAdvised(yangModelCmHandles);
491             trustLevelManager.handleInitialRegistrationOfTrustLevels(initialTrustLevelPerCmHandleId);
492             return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
493         } catch (final AlreadyDefinedException alreadyDefinedException) {
494             return CmHandleRegistrationResponse.createFailureResponses(
495                     alreadyDefinedException.getAlreadyDefinedObjectNames(), CM_HANDLE_ALREADY_EXIST);
496         } catch (final Exception exception) {
497             return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, exception);
498         }
499     }
500
501     private List<CmHandleRegistrationResponse> mergeAllCmHandleResponses(
502             final Map<YangModelCmHandle, CmHandleState> acceptedCmHandleStatePerCmHandle,
503             final Map<NcmpResponseStatus, List<String>> failedCmHandlesPerResponseStatus) {
504         final List<CmHandleRegistrationResponse> cmHandleUpgradeResponses
505                 = upgradeCmHandles(acceptedCmHandleStatePerCmHandle);
506         failedCmHandlesPerResponseStatus.forEach((ncmpResponseStatus, cmHandleIds) ->
507             cmHandleIds.forEach(cmHandleId -> cmHandleUpgradeResponses.add(CmHandleRegistrationResponse
508                 .createFailureResponse(cmHandleId, ncmpResponseStatus))));
509         return cmHandleUpgradeResponses;
510     }
511
512     private List<CmHandleRegistrationResponse> upgradeCmHandles(final Map<YangModelCmHandle, CmHandleState>
513                                                                     cmHandleStatePerCmHandle) {
514         final List<String> cmHandleIds = getCmHandleIds(cmHandleStatePerCmHandle);
515         log.info("Moving cm handles : {} into locked (for upgrade) state.", cmHandleIds);
516         try {
517             lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
518             return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
519         } catch (final Exception e) {
520             log.error("Unable to update cmHandleIds : {} , caused by : {}", cmHandleIds, e.getMessage());
521             return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, e);
522         }
523     }
524
525     private static List<String> getCmHandleIds(final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle) {
526         return cmHandleStatePerCmHandle.keySet().stream().map(YangModelCmHandle::getId).toList();
527     }
528
529     private void setTrustLevelPerDmiPlugin(final DmiPluginRegistration dmiPluginRegistration) {
530         if (DmiPluginRegistration.isNullEmptyOrBlank(dmiPluginRegistration.getDmiDataPlugin())) {
531             trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiPlugin(), TrustLevel.COMPLETE);
532         } else {
533             trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiDataPlugin(), TrustLevel.COMPLETE);
534         }
535     }
536
537 }