Merge "Update Model to allow Persisting of alternateId"
[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-2023 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     /**
192      * Retrieve cm handles with details for the given query parameters.
193      *
194      * @param cmHandleQueryApiParameters cm handle query parameters
195      * @return cm handles with details
196      */
197     @Override
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);
204     }
205
206     /**
207      * Retrieve cm handle ids for the given query parameters.
208      *
209      * @param cmHandleQueryApiParameters cm handle query parameters
210      * @return cm handle ids
211      */
212     @Override
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);
218     }
219
220     /**
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.
223      *
224      * @param cmHandleId                 cm handle id
225      * @param dataSyncEnabledTargetValue data sync enabled flag
226      */
227     @Override
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);
232             return;
233         }
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());
242             }
243             CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabledTargetValue, compositeState);
244             inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
245         } else {
246             throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: "
247                 + compositeState.getCmHandleState());
248         }
249     }
250
251     /**
252      * Get all cm handle IDs by DMI plugin identifier.
253      *
254      * @param dmiPluginIdentifier DMI plugin identifier
255      * @return set of cm handle IDs
256      */
257     @Override
258     public Collection<String> getAllCmHandleIdsByDmiPluginIdentifier(final String dmiPluginIdentifier) {
259         return cmHandleQueries.getCmHandleIdsByDmiPluginIdentifier(dmiPluginIdentifier);
260     }
261
262     /**
263      * Get all cm handle IDs by various properties.
264      *
265      * @param cmHandleQueryServiceParameters cm handle query parameters
266      * @return set of cm handle IDs
267      */
268     @Override
269     public Collection<String> executeCmHandleIdSearchForInventory(
270         final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
271         validateCmHandleQueryParameters(cmHandleQueryServiceParameters, InventoryQueryConditions.ALL_CONDITION_NAMES);
272         return networkCmProxyCmHandleQueryService.queryCmHandleIdsForInventory(cmHandleQueryServiceParameters);
273     }
274
275     /**
276      * Retrieve cm handle details for a given cm handle.
277      *
278      * @param cmHandleId cm handle identifier
279      * @return cm handle details
280      */
281     @Override
282     public NcmpServiceCmHandle getNcmpServiceCmHandle(final String cmHandleId) {
283         return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(
284             inventoryPersistence.getYangModelCmHandle(cmHandleId));
285     }
286
287     /**
288      * Get cm handle public properties for a given cm handle id.
289      *
290      * @param cmHandleId cm handle identifier
291      * @return cm handle public properties
292      */
293     @Override
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;
300     }
301
302     /**
303      * Get cm handle composite state for a given cm handle id.
304      *
305      * @param cmHandleId cm handle identifier
306      * @return cm handle state
307      */
308     @Override
309     public CompositeState getCmHandleCompositeState(final String cmHandleId) {
310         return inventoryPersistence.getYangModelCmHandle(cmHandleId).getCompositeState();
311     }
312
313     /**
314      * THis method registers a cm handle and initiates modules sync.
315      *
316      * @param dmiPluginRegistration dmi plugin registration information.
317      * @return cm-handle registration response for create cm-handle requests.
318      */
319     public List<CmHandleRegistrationResponse> parseAndProcessCreatedCmHandlesInRegistration(
320         final DmiPluginRegistration dmiPluginRegistration) {
321         final List<NcmpServiceCmHandle> cmHandlesToBeCreated = dmiPluginRegistration.getCreatedCmHandles();
322         final Map<String, TrustLevel> initialTrustLevelPerCmHandleId = new HashMap<>(cmHandlesToBeCreated.size());
323         final List<YangModelCmHandle> yangModelCmHandles = new ArrayList<>(cmHandlesToBeCreated.size());
324         cmHandlesToBeCreated
325                 .forEach(cmHandle -> {
326                     final YangModelCmHandle yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle(
327                             dmiPluginRegistration.getDmiPlugin(),
328                             dmiPluginRegistration.getDmiDataPlugin(),
329                             dmiPluginRegistration.getDmiModelPlugin(),
330                             cmHandle,
331                             cmHandle.getModuleSetTag(),
332                             cmHandle.getAlternateId());
333                     yangModelCmHandles.add(yangModelCmHandle);
334                     initialTrustLevelPerCmHandleId.put(cmHandle.getCmHandleId(), cmHandle.getRegistrationTrustLevel());
335                 });
336         return registerNewCmHandles(yangModelCmHandles, initialTrustLevelPerCmHandleId);
337     }
338
339     protected List<CmHandleRegistrationResponse> parseAndProcessDeletedCmHandlesInRegistration(
340         final List<String> tobeRemovedCmHandles) {
341         final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
342             new ArrayList<>(tobeRemovedCmHandles.size());
343         final Collection<YangModelCmHandle> yangModelCmHandles =
344             inventoryPersistence.getYangModelCmHandles(tobeRemovedCmHandles);
345
346         updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETING);
347
348         final Set<String> notDeletedCmHandles = new HashSet<>();
349         for (final List<String> tobeRemovedCmHandleBatch : Lists.partition(tobeRemovedCmHandles, DELETE_BATCH_SIZE)) {
350             try {
351                 batchDeleteCmHandlesFromDbAndModuleSyncMap(tobeRemovedCmHandleBatch);
352                 tobeRemovedCmHandleBatch.forEach(cmHandleId ->
353                     cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId)));
354
355             } catch (final RuntimeException batchException) {
356                 log.error("Unable to de-register cm-handle batch, retrying on each cm handle");
357                 for (final String cmHandleId : tobeRemovedCmHandleBatch) {
358                     final CmHandleRegistrationResponse cmHandleRegistrationResponse =
359                         deleteCmHandleAndGetCmHandleRegistrationResponse(cmHandleId);
360                     cmHandleRegistrationResponses.add(cmHandleRegistrationResponse);
361                     if (cmHandleRegistrationResponse.getStatus() != CmHandleRegistrationResponse.Status.SUCCESS) {
362                         notDeletedCmHandles.add(cmHandleId);
363                     }
364                 }
365             }
366         }
367
368         yangModelCmHandles.removeIf(yangModelCmHandle -> notDeletedCmHandles.contains(yangModelCmHandle.getId()));
369         updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETED);
370
371         return cmHandleRegistrationResponses;
372     }
373
374     protected List<CmHandleRegistrationResponse> parseAndProcessUpgradedCmHandlesInRegistration(
375         final DmiPluginRegistration dmiPluginRegistration) {
376
377         final List<String> upgradedCmHandleIds = dmiPluginRegistration.getUpgradedCmHandles().getCmHandles();
378
379         final Map<YangModelCmHandle, CmHandleState> acceptedCmHandleStatePerCmHandle
380                 = new HashMap<>(upgradedCmHandleIds.size());
381
382         final Map<NcmpResponseStatus, List<String>> failedCmHandlesPerResponseStatus
383                 = new EnumMap<>(NcmpResponseStatus.class);
384         final List<String> nonExistingCmHandleIds = new ArrayList<>();
385         final List<String> nonReadyCmHandleIds = new ArrayList<>();
386         final List<String> invalidCmHandleIds = new ArrayList<>();
387
388         upgradedCmHandleIds.forEach(cmHandleId -> {
389             try {
390                 if (cmHandleQueries.cmHandleHasState(cmHandleId, CmHandleState.READY)) {
391                     final YangModelCmHandle yangModelCmHandleWithUpgradeDetails
392                             = createYangModelCmHandle(dmiPluginRegistration, cmHandleId);
393                     acceptedCmHandleStatePerCmHandle.put(yangModelCmHandleWithUpgradeDetails, CmHandleState.LOCKED);
394                 } else {
395                     nonReadyCmHandleIds.add(cmHandleId);
396                 }
397             } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
398                 log.error("Unable to find data node for cm handle id : {} , caused by : {}",
399                         cmHandleId, dataNodeNotFoundException.getMessage());
400                 nonExistingCmHandleIds.add(cmHandleId);
401             } catch (final DataValidationException dataValidationException) {
402                 log.error("Unable to upgrade cm handle id: {}, caused by : {}",
403                         cmHandleId, dataValidationException.getMessage());
404                 invalidCmHandleIds.add(cmHandleId);
405             }
406         });
407         failedCmHandlesPerResponseStatus.put(CM_HANDLES_NOT_READY, nonReadyCmHandleIds);
408         failedCmHandlesPerResponseStatus.put(CM_HANDLES_NOT_FOUND, nonExistingCmHandleIds);
409         failedCmHandlesPerResponseStatus.put(CM_HANDLE_INVALID_ID, invalidCmHandleIds);
410         return mergeAllCmHandleResponses(acceptedCmHandleStatePerCmHandle, failedCmHandlesPerResponseStatus);
411     }
412
413     private static YangModelCmHandle createYangModelCmHandle(final DmiPluginRegistration dmiPluginRegistration,
414                                                              final String cmHandleId) {
415         final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle();
416         ncmpServiceCmHandle.setCmHandleId(cmHandleId);
417         final String moduleSetTag = dmiPluginRegistration.getUpgradedCmHandles().getModuleSetTag();
418         final String lockReasonWithModuleSetTag = MessageFormat.format(
419                 ModuleOperationsUtils.MODULE_SET_TAG_MESSAGE_FORMAT, moduleSetTag);
420         ncmpServiceCmHandle.setCompositeState(new CompositeStateBuilder().withCmHandleState(CmHandleState.READY)
421                 .withLockReason(MODULE_UPGRADE, lockReasonWithModuleSetTag).build());
422         return YangModelCmHandle.toYangModelCmHandle(dmiPluginRegistration.getDmiPlugin(),
423                 dmiPluginRegistration.getDmiDataPlugin(), dmiPluginRegistration.getDmiModelPlugin(),
424                 ncmpServiceCmHandle, moduleSetTag, ncmpServiceCmHandle.getAlternateId());
425     }
426
427     private CmHandleRegistrationResponse deleteCmHandleAndGetCmHandleRegistrationResponse(final String cmHandleId) {
428         try {
429             deleteCmHandleFromDbAndModuleSyncMap(cmHandleId);
430             return CmHandleRegistrationResponse.createSuccessResponse(cmHandleId);
431         } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
432             log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
433                 cmHandleId, dataNodeNotFoundException.getMessage());
434             return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND);
435         } catch (final DataValidationException dataValidationException) {
436             log.error("Unable to de-register cm-handle id: {}, caused by: {}",
437                 cmHandleId, dataValidationException.getMessage());
438             return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID);
439         } catch (final Exception exception) {
440             log.error("Unable to de-register cm-handle id : {} , caused by : {}", cmHandleId, exception.getMessage());
441             return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception);
442         }
443     }
444
445     private void updateCmHandleStateBatch(final Collection<YangModelCmHandle> yangModelCmHandles,
446                                           final CmHandleState cmHandleState) {
447         final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>(yangModelCmHandles.size());
448         yangModelCmHandles.forEach(yangModelCmHandle -> cmHandleStatePerCmHandle.put(yangModelCmHandle, cmHandleState));
449         lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
450     }
451
452     private void deleteCmHandleFromDbAndModuleSyncMap(final String cmHandleId) {
453         inventoryPersistence.deleteSchemaSetWithCascade(cmHandleId);
454         inventoryPersistence.deleteDataNode(NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId
455             + "']");
456         removeDeletedCmHandleFromModuleSyncMap(cmHandleId);
457     }
458
459     private void batchDeleteCmHandlesFromDbAndModuleSyncMap(final Collection<String> tobeRemovedCmHandles) {
460         inventoryPersistence.deleteSchemaSetsWithCascade(tobeRemovedCmHandles);
461         inventoryPersistence.deleteDataNodes(mapCmHandleIdsToXpaths(tobeRemovedCmHandles));
462         tobeRemovedCmHandles.forEach(this::removeDeletedCmHandleFromModuleSyncMap);
463     }
464
465     private Collection<String> mapCmHandleIdsToXpaths(final Collection<String> cmHandles) {
466         return cmHandles.stream()
467             .map(cmHandleId -> NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId + "']")
468             .collect(Collectors.toSet());
469     }
470
471     // CPS-1239 Robustness cleaning of in progress cache
472     private void removeDeletedCmHandleFromModuleSyncMap(final String deletedCmHandleId) {
473         if (moduleSyncStartedOnCmHandles.remove(deletedCmHandleId) != null) {
474             log.debug("{} removed from in progress map", deletedCmHandleId);
475         }
476     }
477
478     private List<CmHandleRegistrationResponse> registerNewCmHandles(final List<YangModelCmHandle> yangModelCmHandles,
479                                                                     final Map<String, TrustLevel>
480                                                                             initialTrustLevelPerCmHandleId) {
481         final Set<String> cmHandleIds = initialTrustLevelPerCmHandleId.keySet();
482         try {
483             lcmEventsCmHandleStateHandler.initiateStateAdvised(yangModelCmHandles);
484             trustLevelManager.handleInitialRegistrationOfTrustLevels(initialTrustLevelPerCmHandleId);
485             return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
486         } catch (final AlreadyDefinedException alreadyDefinedException) {
487             return CmHandleRegistrationResponse.createFailureResponses(
488                     alreadyDefinedException.getAlreadyDefinedObjectNames(), CM_HANDLE_ALREADY_EXIST);
489         } catch (final Exception exception) {
490             return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, exception);
491         }
492     }
493
494     private List<CmHandleRegistrationResponse> mergeAllCmHandleResponses(
495             final Map<YangModelCmHandle, CmHandleState> acceptedCmHandleStatePerCmHandle,
496             final Map<NcmpResponseStatus, List<String>> failedCmHandlesPerResponseStatus) {
497         final List<CmHandleRegistrationResponse> cmHandleUpgradeResponses
498                 = upgradeCmHandles(acceptedCmHandleStatePerCmHandle);
499         failedCmHandlesPerResponseStatus.forEach((ncmpResponseStatus, cmHandleIds) ->
500             cmHandleIds.forEach(cmHandleId -> cmHandleUpgradeResponses.add(CmHandleRegistrationResponse
501                 .createFailureResponse(cmHandleId, ncmpResponseStatus))));
502         return cmHandleUpgradeResponses;
503     }
504
505     private List<CmHandleRegistrationResponse> upgradeCmHandles(final Map<YangModelCmHandle, CmHandleState>
506                                                                     cmHandleStatePerCmHandle) {
507         final List<String> cmHandleIds = getCmHandleIds(cmHandleStatePerCmHandle);
508         log.info("Moving cm handles : {} into locked (for upgrade) state.", cmHandleIds);
509         try {
510             lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
511             return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
512         } catch (final Exception e) {
513             log.error("Unable to update cmHandleIds : {} , caused by : {}", cmHandleIds, e.getMessage());
514             return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, e);
515         }
516     }
517
518     private static List<String> getCmHandleIds(final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle) {
519         return cmHandleStatePerCmHandle.keySet().stream().map(YangModelCmHandle::getId).toList();
520     }
521
522     private void setTrustLevelPerDmiPlugin(final DmiPluginRegistration dmiPluginRegistration) {
523         if (DmiPluginRegistration.isNullEmptyOrBlank(dmiPluginRegistration.getDmiDataPlugin())) {
524             trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiPlugin(), TrustLevel.COMPLETE);
525         } else {
526             trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiDataPlugin(), TrustLevel.COMPLETE);
527         }
528     }
529
530 }