4ec00daea566f14b36b353a6759a81fbf65d9222
[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.HashMap;
43 import java.util.HashSet;
44 import java.util.List;
45 import java.util.Map;
46 import java.util.Set;
47 import java.util.stream.Collectors;
48 import lombok.RequiredArgsConstructor;
49 import lombok.extern.slf4j.Slf4j;
50 import org.onap.cps.api.CpsDataService;
51 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandleQueryService;
52 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
53 import org.onap.cps.ncmp.api.impl.events.lcm.LcmEventsCmHandleStateHandler;
54 import org.onap.cps.ncmp.api.impl.inventory.CmHandleQueries;
55 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState;
56 import org.onap.cps.ncmp.api.impl.inventory.CompositeState;
57 import org.onap.cps.ncmp.api.impl.inventory.CompositeStateBuilder;
58 import org.onap.cps.ncmp.api.impl.inventory.CompositeStateUtils;
59 import org.onap.cps.ncmp.api.impl.inventory.DataStoreSyncState;
60 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence;
61 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations;
62 import org.onap.cps.ncmp.api.impl.operations.OperationType;
63 import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevel;
64 import org.onap.cps.ncmp.api.impl.utils.CmHandleQueryConditions;
65 import org.onap.cps.ncmp.api.impl.utils.InventoryQueryConditions;
66 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
67 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
68 import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters;
69 import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters;
70 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse;
71 import org.onap.cps.ncmp.api.models.DataOperationRequest;
72 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
73 import org.onap.cps.ncmp.api.models.DmiPluginRegistrationResponse;
74 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
75 import org.onap.cps.ncmp.api.models.UpgradedCmHandles;
76 import org.onap.cps.spi.FetchDescendantsOption;
77 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
78 import org.onap.cps.spi.exceptions.CpsException;
79 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
80 import org.onap.cps.spi.exceptions.DataValidationException;
81 import org.onap.cps.spi.model.ModuleDefinition;
82 import org.onap.cps.spi.model.ModuleReference;
83 import org.onap.cps.utils.JsonObjectMapper;
84 import org.springframework.http.ResponseEntity;
85 import org.springframework.stereotype.Service;
86
87 @Slf4j
88 @Service
89 @RequiredArgsConstructor
90 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
91
92     private static final int DELETE_BATCH_SIZE = 100;
93     private final JsonObjectMapper jsonObjectMapper;
94     private final DmiDataOperations dmiDataOperations;
95     private final NetworkCmProxyDataServicePropertyHandler networkCmProxyDataServicePropertyHandler;
96     private final InventoryPersistence inventoryPersistence;
97     private final CmHandleQueries cmHandleQueries;
98     private final NetworkCmProxyCmHandleQueryService networkCmProxyCmHandleQueryService;
99     private final LcmEventsCmHandleStateHandler lcmEventsCmHandleStateHandler;
100     private final CpsDataService cpsDataService;
101     private final IMap<String, Object> moduleSyncStartedOnCmHandles;
102     private final Map<String, TrustLevel> trustLevelPerCmHandle;
103
104     @Override
105     public DmiPluginRegistrationResponse updateDmiRegistrationAndSyncModule(
106             final DmiPluginRegistration dmiPluginRegistration) {
107         dmiPluginRegistration.validateDmiPluginRegistration();
108         final DmiPluginRegistrationResponse dmiPluginRegistrationResponse = new DmiPluginRegistrationResponse();
109
110         if (!dmiPluginRegistration.getRemovedCmHandles().isEmpty()) {
111             dmiPluginRegistrationResponse.setRemovedCmHandles(
112                     parseAndProcessDeletedCmHandlesInRegistration(dmiPluginRegistration.getRemovedCmHandles()));
113         }
114
115         if (!dmiPluginRegistration.getCreatedCmHandles().isEmpty()) {
116             populateTrustLevelPerCmHandleCache(dmiPluginRegistration);
117             dmiPluginRegistrationResponse.setCreatedCmHandles(
118                     parseAndProcessCreatedCmHandlesInRegistration(dmiPluginRegistration));
119         }
120         if (!dmiPluginRegistration.getUpdatedCmHandles().isEmpty()) {
121             dmiPluginRegistrationResponse.setUpdatedCmHandles(
122                     networkCmProxyDataServicePropertyHandler
123                             .updateCmHandleProperties(dmiPluginRegistration.getUpdatedCmHandles()));
124         }
125         if (dmiPluginRegistration.getUpgradedCmHandles() != null
126                 && !dmiPluginRegistration.getUpgradedCmHandles().getCmHandles().isEmpty()) {
127             dmiPluginRegistrationResponse.setUpgradedCmHandles(
128                     parseAndProcessUpgradedCmHandlesInRegistration(dmiPluginRegistration));
129         }
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 UpgradedCmHandles upgradedCmHandles = dmiPluginRegistration.getUpgradedCmHandles();
369         final String moduleSetTag = dmiPluginRegistration.getUpgradedCmHandles().getModuleSetTag();
370         final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle =
371                 new HashMap<>(upgradedCmHandles.getCmHandles().size());
372         final Collection<String> notReadyCmHandles = new ArrayList<>(upgradedCmHandles.getCmHandles().size());
373         final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle();
374         final String formattedModuleSetTag = MessageFormat.format("new moduleSetTag: {0}", moduleSetTag);
375
376         upgradedCmHandles.getCmHandles().forEach(cmHandleId -> {
377             if (cmHandleQueries.cmHandleHasState(cmHandleId, CmHandleState.READY)) {
378                 ncmpServiceCmHandle.setCmHandleId(cmHandleId);
379                 ncmpServiceCmHandle.setCompositeState(new CompositeStateBuilder()
380                         .withCmHandleState(CmHandleState.READY)
381                         .withLockReason(MODULE_UPGRADE, formattedModuleSetTag).build());
382                 final YangModelCmHandle yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle(
383                         dmiPluginRegistration.getDmiPlugin(),
384                         dmiPluginRegistration.getDmiDataPlugin(),
385                         dmiPluginRegistration.getDmiModelPlugin(),
386                         ncmpServiceCmHandle,
387                         moduleSetTag);
388                 cmHandleStatePerCmHandle.put(yangModelCmHandle, CmHandleState.LOCKED);
389             } else {
390                 notReadyCmHandles.add(cmHandleId);
391             }
392         });
393
394         return prepareAndGetCmHandleUpgradeResponses(cmHandleStatePerCmHandle, notReadyCmHandles);
395     }
396
397     private CmHandleRegistrationResponse deleteCmHandleAndGetCmHandleRegistrationResponse(final String cmHandleId) {
398         try {
399             deleteCmHandleFromDbAndModuleSyncMap(cmHandleId);
400             return CmHandleRegistrationResponse.createSuccessResponse(cmHandleId);
401         } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
402             log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
403                 cmHandleId, dataNodeNotFoundException.getMessage());
404             return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND);
405         } catch (final DataValidationException dataValidationException) {
406             log.error("Unable to de-register cm-handle id: {}, caused by: {}",
407                 cmHandleId, dataValidationException.getMessage());
408             return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID);
409         } catch (final Exception exception) {
410             log.error("Unable to de-register cm-handle id : {} , caused by : {}", cmHandleId, exception.getMessage());
411             return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception);
412         }
413     }
414
415     private void updateCmHandleStateBatch(final Collection<YangModelCmHandle> yangModelCmHandles,
416                                           final CmHandleState cmHandleState) {
417         final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>(yangModelCmHandles.size());
418         yangModelCmHandles.forEach(yangModelCmHandle -> cmHandleStatePerCmHandle.put(yangModelCmHandle, cmHandleState));
419         lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
420     }
421
422     private void deleteCmHandleFromDbAndModuleSyncMap(final String cmHandleId) {
423         inventoryPersistence.deleteSchemaSetWithCascade(cmHandleId);
424         inventoryPersistence.deleteDataNode(NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId
425                 + "']");
426         removeDeletedCmHandleFromModuleSyncMap(cmHandleId);
427     }
428
429     private void batchDeleteCmHandlesFromDbAndModuleSyncMap(final Collection<String> tobeRemovedCmHandles) {
430         inventoryPersistence.deleteSchemaSetsWithCascade(tobeRemovedCmHandles);
431         inventoryPersistence.deleteDataNodes(mapCmHandleIdsToXpaths(tobeRemovedCmHandles));
432         tobeRemovedCmHandles.forEach(this::removeDeletedCmHandleFromModuleSyncMap);
433     }
434
435     private Collection<String> mapCmHandleIdsToXpaths(final Collection<String> cmHandles) {
436         return cmHandles.stream()
437                 .map(cmHandleId -> NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId + "']")
438                 .collect(Collectors.toSet());
439     }
440
441     // CPS-1239 Robustness cleaning of in progress cache
442     private void removeDeletedCmHandleFromModuleSyncMap(final String deletedCmHandleId) {
443         if (moduleSyncStartedOnCmHandles.remove(deletedCmHandleId) != null) {
444             log.debug("{} removed from in progress map", deletedCmHandleId);
445         }
446     }
447
448     private List<CmHandleRegistrationResponse> registerNewCmHandles(final Map<YangModelCmHandle, CmHandleState>
449                                                                             cmHandleStatePerCmHandle) {
450         final List<String> cmHandleIds = getCmHandleIds(cmHandleStatePerCmHandle);
451         try {
452             lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
453             return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
454         } catch (final AlreadyDefinedException alreadyDefinedException) {
455             return CmHandleRegistrationResponse.createFailureResponses(
456                     alreadyDefinedException.getAlreadyDefinedObjectNames(),
457                     CM_HANDLE_ALREADY_EXIST);
458         } catch (final Exception exception) {
459             return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, exception);
460         }
461     }
462
463     private List<CmHandleRegistrationResponse> prepareAndGetCmHandleUpgradeResponses(final Map<YangModelCmHandle,
464             CmHandleState> cmHandleStatePerCmHandle, final Collection<String> notReadyCmHandles) {
465         final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses
466                 = upgradeCmHandles(cmHandleStatePerCmHandle);
467         final List<CmHandleRegistrationResponse> failedCmHandleRegistrationResponses
468                 = CmHandleRegistrationResponse.createFailureResponses(notReadyCmHandles, CM_HANDLES_NOT_READY);
469         failedCmHandleRegistrationResponses.forEach(cmHandleRegistrationResponses::add);
470         return cmHandleRegistrationResponses;
471     }
472
473     private List<CmHandleRegistrationResponse> upgradeCmHandles(final Map<YangModelCmHandle, CmHandleState>
474                                                                         cmHandleStatePerCmHandle) {
475         final List<String> cmHandleIds = getCmHandleIds(cmHandleStatePerCmHandle);
476         log.info("Moving cm handles : {} into locked (for upgrade) state.", cmHandleIds);
477         try {
478             lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
479             return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
480         } catch (final Exception exception) {
481             return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, exception);
482         }
483     }
484
485     private static List<String> getCmHandleIds(final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle) {
486         return cmHandleStatePerCmHandle.keySet().stream().map(YangModelCmHandle::getId).toList();
487     }
488
489     private void populateTrustLevelPerCmHandleCache(final DmiPluginRegistration dmiPluginRegistration) {
490         for (final NcmpServiceCmHandle cmHandle: dmiPluginRegistration.getCreatedCmHandles()) {
491             if (cmHandle.getRegistrationTrustLevel() == null) {
492                 if (trustLevelPerCmHandle.containsKey(cmHandle.getCmHandleId())) {
493                     log.warn("CmHandle : {}, Already exists, Initial trustLevel ignored.", cmHandle.getCmHandleId());
494                 } else {
495                     trustLevelPerCmHandle.put(cmHandle.getCmHandleId(), TrustLevel.COMPLETE);
496                 }
497             } else {
498                 trustLevelPerCmHandle.put(cmHandle.getCmHandleId(), cmHandle.getRegistrationTrustLevel());
499             }
500         }
501     }
502
503 }