Merge "Publish trust level notification event"
[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                     yangModelCmHandles.add(yangModelCmHandle);
333                     initialTrustLevelPerCmHandleId.put(cmHandle.getCmHandleId(), cmHandle.getRegistrationTrustLevel());
334                 });
335         return registerNewCmHandles(yangModelCmHandles, initialTrustLevelPerCmHandleId);
336     }
337
338     protected List<CmHandleRegistrationResponse> parseAndProcessDeletedCmHandlesInRegistration(
339         final List<String> tobeRemovedCmHandles) {
340         final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
341             new ArrayList<>(tobeRemovedCmHandles.size());
342         final Collection<YangModelCmHandle> yangModelCmHandles =
343             inventoryPersistence.getYangModelCmHandles(tobeRemovedCmHandles);
344
345         updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETING);
346
347         final Set<String> notDeletedCmHandles = new HashSet<>();
348         for (final List<String> tobeRemovedCmHandleBatch : Lists.partition(tobeRemovedCmHandles, DELETE_BATCH_SIZE)) {
349             try {
350                 batchDeleteCmHandlesFromDbAndModuleSyncMap(tobeRemovedCmHandleBatch);
351                 tobeRemovedCmHandleBatch.forEach(cmHandleId ->
352                     cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId)));
353
354             } catch (final RuntimeException batchException) {
355                 log.error("Unable to de-register cm-handle batch, retrying on each cm handle");
356                 for (final String cmHandleId : tobeRemovedCmHandleBatch) {
357                     final CmHandleRegistrationResponse cmHandleRegistrationResponse =
358                         deleteCmHandleAndGetCmHandleRegistrationResponse(cmHandleId);
359                     cmHandleRegistrationResponses.add(cmHandleRegistrationResponse);
360                     if (cmHandleRegistrationResponse.getStatus() != CmHandleRegistrationResponse.Status.SUCCESS) {
361                         notDeletedCmHandles.add(cmHandleId);
362                     }
363                 }
364             }
365         }
366
367         yangModelCmHandles.removeIf(yangModelCmHandle -> notDeletedCmHandles.contains(yangModelCmHandle.getId()));
368         updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETED);
369
370         return cmHandleRegistrationResponses;
371     }
372
373     protected List<CmHandleRegistrationResponse> parseAndProcessUpgradedCmHandlesInRegistration(
374         final DmiPluginRegistration dmiPluginRegistration) {
375
376         final List<String> upgradedCmHandleIds = dmiPluginRegistration.getUpgradedCmHandles().getCmHandles();
377
378         final Map<YangModelCmHandle, CmHandleState> acceptedCmHandleStatePerCmHandle
379                 = new HashMap<>(upgradedCmHandleIds.size());
380
381         final Map<NcmpResponseStatus, List<String>> failedCmHandlesPerResponseStatus
382                 = new EnumMap<>(NcmpResponseStatus.class);
383         final List<String> nonExistingCmHandleIds = new ArrayList<>();
384         final List<String> nonReadyCmHandleIds = new ArrayList<>();
385         final List<String> invalidCmHandleIds = new ArrayList<>();
386
387         upgradedCmHandleIds.forEach(cmHandleId -> {
388             try {
389                 if (cmHandleQueries.cmHandleHasState(cmHandleId, CmHandleState.READY)) {
390                     final YangModelCmHandle yangModelCmHandleWithUpgradeDetails
391                             = createYangModelCmHandle(dmiPluginRegistration, cmHandleId);
392                     acceptedCmHandleStatePerCmHandle.put(yangModelCmHandleWithUpgradeDetails, CmHandleState.LOCKED);
393                 } else {
394                     nonReadyCmHandleIds.add(cmHandleId);
395                 }
396             } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
397                 log.error("Unable to find data node for cm handle id : {} , caused by : {}",
398                         cmHandleId, dataNodeNotFoundException.getMessage());
399                 nonExistingCmHandleIds.add(cmHandleId);
400             } catch (final DataValidationException dataValidationException) {
401                 log.error("Unable to upgrade cm handle id: {}, caused by : {}",
402                         cmHandleId, dataValidationException.getMessage());
403                 invalidCmHandleIds.add(cmHandleId);
404             }
405         });
406         failedCmHandlesPerResponseStatus.put(CM_HANDLES_NOT_READY, nonReadyCmHandleIds);
407         failedCmHandlesPerResponseStatus.put(CM_HANDLES_NOT_FOUND, nonExistingCmHandleIds);
408         failedCmHandlesPerResponseStatus.put(CM_HANDLE_INVALID_ID, invalidCmHandleIds);
409         return mergeAllCmHandleResponses(acceptedCmHandleStatePerCmHandle, failedCmHandlesPerResponseStatus);
410     }
411
412     private static YangModelCmHandle createYangModelCmHandle(final DmiPluginRegistration dmiPluginRegistration,
413                                                              final String cmHandleId) {
414         final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle();
415         ncmpServiceCmHandle.setCmHandleId(cmHandleId);
416         final String moduleSetTag = dmiPluginRegistration.getUpgradedCmHandles().getModuleSetTag();
417         final String lockReasonWithModuleSetTag = MessageFormat.format(
418                 ModuleOperationsUtils.MODULE_SET_TAG_MESSAGE_FORMAT, moduleSetTag);
419         ncmpServiceCmHandle.setCompositeState(new CompositeStateBuilder().withCmHandleState(CmHandleState.READY)
420                 .withLockReason(MODULE_UPGRADE, lockReasonWithModuleSetTag).build());
421         return YangModelCmHandle.toYangModelCmHandle(dmiPluginRegistration.getDmiPlugin(),
422                 dmiPluginRegistration.getDmiDataPlugin(), dmiPluginRegistration.getDmiModelPlugin(),
423                 ncmpServiceCmHandle, moduleSetTag);
424     }
425
426     private CmHandleRegistrationResponse deleteCmHandleAndGetCmHandleRegistrationResponse(final String cmHandleId) {
427         try {
428             deleteCmHandleFromDbAndModuleSyncMap(cmHandleId);
429             return CmHandleRegistrationResponse.createSuccessResponse(cmHandleId);
430         } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
431             log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
432                 cmHandleId, dataNodeNotFoundException.getMessage());
433             return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND);
434         } catch (final DataValidationException dataValidationException) {
435             log.error("Unable to de-register cm-handle id: {}, caused by: {}",
436                 cmHandleId, dataValidationException.getMessage());
437             return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID);
438         } catch (final Exception exception) {
439             log.error("Unable to de-register cm-handle id : {} , caused by : {}", cmHandleId, exception.getMessage());
440             return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception);
441         }
442     }
443
444     private void updateCmHandleStateBatch(final Collection<YangModelCmHandle> yangModelCmHandles,
445                                           final CmHandleState cmHandleState) {
446         final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>(yangModelCmHandles.size());
447         yangModelCmHandles.forEach(yangModelCmHandle -> cmHandleStatePerCmHandle.put(yangModelCmHandle, cmHandleState));
448         lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
449     }
450
451     private void deleteCmHandleFromDbAndModuleSyncMap(final String cmHandleId) {
452         inventoryPersistence.deleteSchemaSetWithCascade(cmHandleId);
453         inventoryPersistence.deleteDataNode(NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId
454             + "']");
455         removeDeletedCmHandleFromModuleSyncMap(cmHandleId);
456     }
457
458     private void batchDeleteCmHandlesFromDbAndModuleSyncMap(final Collection<String> tobeRemovedCmHandles) {
459         inventoryPersistence.deleteSchemaSetsWithCascade(tobeRemovedCmHandles);
460         inventoryPersistence.deleteDataNodes(mapCmHandleIdsToXpaths(tobeRemovedCmHandles));
461         tobeRemovedCmHandles.forEach(this::removeDeletedCmHandleFromModuleSyncMap);
462     }
463
464     private Collection<String> mapCmHandleIdsToXpaths(final Collection<String> cmHandles) {
465         return cmHandles.stream()
466             .map(cmHandleId -> NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId + "']")
467             .collect(Collectors.toSet());
468     }
469
470     // CPS-1239 Robustness cleaning of in progress cache
471     private void removeDeletedCmHandleFromModuleSyncMap(final String deletedCmHandleId) {
472         if (moduleSyncStartedOnCmHandles.remove(deletedCmHandleId) != null) {
473             log.debug("{} removed from in progress map", deletedCmHandleId);
474         }
475     }
476
477     private List<CmHandleRegistrationResponse> registerNewCmHandles(final List<YangModelCmHandle> yangModelCmHandles,
478                                                                     final Map<String, TrustLevel>
479                                                                             initialTrustLevelPerCmHandleId) {
480         final Set<String> cmHandleIds = initialTrustLevelPerCmHandleId.keySet();
481         try {
482             lcmEventsCmHandleStateHandler.initiateStateAdvised(yangModelCmHandles);
483             trustLevelManager.handleInitialRegistrationOfTrustLevels(initialTrustLevelPerCmHandleId);
484             return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
485         } catch (final AlreadyDefinedException alreadyDefinedException) {
486             return CmHandleRegistrationResponse.createFailureResponses(
487                     alreadyDefinedException.getAlreadyDefinedObjectNames(), CM_HANDLE_ALREADY_EXIST);
488         } catch (final Exception exception) {
489             return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, exception);
490         }
491     }
492
493     private List<CmHandleRegistrationResponse> mergeAllCmHandleResponses(
494             final Map<YangModelCmHandle, CmHandleState> acceptedCmHandleStatePerCmHandle,
495             final Map<NcmpResponseStatus, List<String>> failedCmHandlesPerResponseStatus) {
496         final List<CmHandleRegistrationResponse> cmHandleUpgradeResponses
497                 = upgradeCmHandles(acceptedCmHandleStatePerCmHandle);
498         failedCmHandlesPerResponseStatus.forEach((ncmpResponseStatus, cmHandleIds) ->
499             cmHandleIds.forEach(cmHandleId -> cmHandleUpgradeResponses.add(CmHandleRegistrationResponse
500                 .createFailureResponse(cmHandleId, ncmpResponseStatus))));
501         return cmHandleUpgradeResponses;
502     }
503
504     private List<CmHandleRegistrationResponse> upgradeCmHandles(final Map<YangModelCmHandle, CmHandleState>
505                                                                     cmHandleStatePerCmHandle) {
506         final List<String> cmHandleIds = getCmHandleIds(cmHandleStatePerCmHandle);
507         log.info("Moving cm handles : {} into locked (for upgrade) state.", cmHandleIds);
508         try {
509             lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
510             return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
511         } catch (final Exception e) {
512             log.error("Unable to update cmHandleIds : {} , caused by : {}", cmHandleIds, e.getMessage());
513             return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, e);
514         }
515     }
516
517     private static List<String> getCmHandleIds(final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle) {
518         return cmHandleStatePerCmHandle.keySet().stream().map(YangModelCmHandle::getId).toList();
519     }
520
521     private void setTrustLevelPerDmiPlugin(final DmiPluginRegistration dmiPluginRegistration) {
522         if (DmiPluginRegistration.isNullEmptyOrBlank(dmiPluginRegistration.getDmiDataPlugin())) {
523             trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiPlugin(), TrustLevel.COMPLETE);
524         } else {
525             trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiDataPlugin(), TrustLevel.COMPLETE);
526         }
527     }
528
529 }