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