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