Merge "Ncmp out event for REJECTED scenario"
[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.ALTERNATE_ID_ALREADY_ASSOCIATED;
28 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLES_NOT_FOUND;
29 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLES_NOT_READY;
30 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLE_ALREADY_EXIST;
31 import static org.onap.cps.ncmp.api.NcmpResponseStatus.CM_HANDLE_INVALID_ID;
32 import static org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory.MODULE_UPGRADE;
33 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DMI_REGISTRY_PARENT;
34 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME;
35 import static org.onap.cps.ncmp.api.impl.utils.RestQueryParametersValidator.validateCmHandleQueryParameters;
36
37 import com.google.common.collect.Lists;
38 import com.hazelcast.map.IMap;
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.apache.commons.lang3.StringUtils;
51 import org.onap.cps.api.CpsDataService;
52 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandleQueryService;
53 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
54 import org.onap.cps.ncmp.api.impl.config.embeddedcache.TrustLevelCacheConfig;
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.AlternateIdChecker;
69 import org.onap.cps.ncmp.api.impl.utils.CmHandleQueryConditions;
70 import org.onap.cps.ncmp.api.impl.utils.InventoryQueryConditions;
71 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
72 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
73 import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters;
74 import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters;
75 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse;
76 import org.onap.cps.ncmp.api.models.CmResourceAddress;
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.beans.factory.annotation.Qualifier;
90 import org.springframework.http.ResponseEntity;
91 import org.springframework.stereotype.Service;
92
93 @Slf4j
94 @Service
95 @RequiredArgsConstructor
96 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
97
98     private static final int DELETE_BATCH_SIZE = 100;
99     private final JsonObjectMapper jsonObjectMapper;
100     private final DmiDataOperations dmiDataOperations;
101     private final NetworkCmProxyDataServicePropertyHandler networkCmProxyDataServicePropertyHandler;
102     private final InventoryPersistence inventoryPersistence;
103     private final CmHandleQueries cmHandleQueries;
104     private final NetworkCmProxyCmHandleQueryService networkCmProxyCmHandleQueryService;
105     private final LcmEventsCmHandleStateHandler lcmEventsCmHandleStateHandler;
106     private final CpsDataService cpsDataService;
107     private final IMap<String, Object> moduleSyncStartedOnCmHandles;
108
109     @Qualifier(TrustLevelCacheConfig.TRUST_LEVEL_PER_DMI_PLUGIN)
110     private final Map<String, TrustLevel> trustLevelPerDmiPlugin;
111
112     private final TrustLevelManager trustLevelManager;
113     private final AlternateIdChecker alternateIdChecker;
114
115     @Override
116     public DmiPluginRegistrationResponse updateDmiRegistrationAndSyncModule(
117         final DmiPluginRegistration dmiPluginRegistration) {
118
119         dmiPluginRegistration.validateDmiPluginRegistration();
120         final DmiPluginRegistrationResponse dmiPluginRegistrationResponse = new DmiPluginRegistrationResponse();
121
122         setTrustLevelPerDmiPlugin(dmiPluginRegistration);
123
124         processRemovedCmHandles(dmiPluginRegistration, dmiPluginRegistrationResponse);
125
126         processCreatedCmHandles(dmiPluginRegistration, dmiPluginRegistrationResponse);
127
128         processUpdatedCmHandles(dmiPluginRegistration, dmiPluginRegistrationResponse);
129
130         processUpgradedCmHandles(dmiPluginRegistration, dmiPluginRegistrationResponse);
131
132         return dmiPluginRegistrationResponse;
133     }
134
135     @Override
136     public Object getResourceDataForCmHandle(final CmResourceAddress cmResourceAddress,
137                                              final String optionsParamInQuery,
138                                              final String topicParamInQuery,
139                                              final String requestId,
140                                              final String authorization) {
141         final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(cmResourceAddress,
142             optionsParamInQuery,
143             topicParamInQuery,
144             requestId,
145             authorization);
146         return responseEntity.getBody();
147     }
148
149     @Override
150     public Object getResourceDataForCmHandle(final CmResourceAddress cmResourceAddress,
151                                              final FetchDescendantsOption fetchDescendantsOption) {
152         return cpsDataService.getDataNodes(cmResourceAddress.datastoreName(),
153                                            cmResourceAddress.cmHandleId(),
154                                            cmResourceAddress.resourceIdentifier(),
155                                            fetchDescendantsOption).iterator().next();
156     }
157
158     @Override
159     public void executeDataOperationForCmHandles(final String topicParamInQuery,
160                                                  final DataOperationRequest dataOperationRequest,
161                                                  final String requestId,
162                                                  final String authorization) {
163         dmiDataOperations.requestResourceDataFromDmi(topicParamInQuery, dataOperationRequest, requestId,
164                 authorization);
165     }
166
167     @Override
168     public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
169                                                                  final String resourceIdentifier,
170                                                                  final OperationType operationType,
171                                                                  final String requestData,
172                                                                  final String dataType,
173                                                                  final String authorization) {
174         return dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier,
175             operationType, requestData, dataType, authorization);
176     }
177
178     @Override
179     public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
180         return inventoryPersistence.getYangResourcesModuleReferences(cmHandleId);
181     }
182
183     @Override
184     public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
185         return inventoryPersistence.getModuleDefinitionsByCmHandleId(cmHandleId);
186     }
187
188     @Override
189     public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleAndModule(final String cmHandleId,
190                                                                                 final String moduleName,
191                                                                                 final String moduleRevision) {
192         return inventoryPersistence.getModuleDefinitionsByCmHandleAndModule(cmHandleId, moduleName, moduleRevision);
193     }
194
195     /**
196      * Retrieve cm handles with details for the given query parameters.
197      *
198      * @param cmHandleQueryApiParameters cm handle query parameters
199      * @return cm handles with details
200      */
201     @Override
202     public Collection<NcmpServiceCmHandle> executeCmHandleSearch(
203         final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
204         final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
205             cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
206         validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
207         return networkCmProxyCmHandleQueryService.queryCmHandles(cmHandleQueryServiceParameters);
208     }
209
210     /**
211      * Retrieve cm handle ids for the given query parameters.
212      *
213      * @param cmHandleQueryApiParameters cm handle query parameters
214      * @return cm handle ids
215      */
216     @Override
217     public Collection<String> executeCmHandleIdSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
218         final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
219             cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
220         validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
221         return networkCmProxyCmHandleQueryService.queryCmHandleIds(cmHandleQueryServiceParameters);
222     }
223
224     /**
225      * Set the data sync enabled flag, along with the data sync state
226      * based on the data sync enabled boolean for the cm handle id provided.
227      *
228      * @param cmHandleId                 cm handle id
229      * @param dataSyncEnabledTargetValue data sync enabled flag
230      */
231     @Override
232     public void setDataSyncEnabled(final String cmHandleId, final Boolean dataSyncEnabledTargetValue) {
233         final CompositeState compositeState = inventoryPersistence.getCmHandleState(cmHandleId);
234         if (dataSyncEnabledTargetValue.equals(compositeState.getDataSyncEnabled())) {
235             log.info("Data-Sync Enabled flag is already: {} ", dataSyncEnabledTargetValue);
236             return;
237         }
238         if (CmHandleState.READY.equals(compositeState.getCmHandleState())) {
239             final DataStoreSyncState dataStoreSyncState = compositeState.getDataStores()
240                 .getOperationalDataStore().getDataStoreSyncState();
241             if (Boolean.FALSE.equals(dataSyncEnabledTargetValue)
242                 && DataStoreSyncState.SYNCHRONIZED.equals(dataStoreSyncState)) {
243                 // TODO : This is hard-coded for onap dmi that need to be addressed
244                 cpsDataService.deleteDataNode(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId,
245                     "/netconf-state", OffsetDateTime.now());
246             }
247             CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabledTargetValue, compositeState);
248             inventoryPersistence.saveCmHandleState(cmHandleId, compositeState);
249         } else {
250             throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: "
251                 + compositeState.getCmHandleState());
252         }
253     }
254
255     /**
256      * Get all cm handle IDs by DMI plugin identifier.
257      *
258      * @param dmiPluginIdentifier DMI plugin identifier
259      * @return set of cm handle IDs
260      */
261     @Override
262     public Collection<String> getAllCmHandleIdsByDmiPluginIdentifier(final String dmiPluginIdentifier) {
263         return cmHandleQueries.getCmHandleIdsByDmiPluginIdentifier(dmiPluginIdentifier);
264     }
265
266     /**
267      * Get all cm handle IDs by various properties.
268      *
269      * @param cmHandleQueryServiceParameters cm handle query parameters
270      * @return set of cm handle IDs
271      */
272     @Override
273     public Collection<String> executeCmHandleIdSearchForInventory(
274         final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
275         validateCmHandleQueryParameters(cmHandleQueryServiceParameters, InventoryQueryConditions.ALL_CONDITION_NAMES);
276         return networkCmProxyCmHandleQueryService.queryCmHandleIdsForInventory(cmHandleQueryServiceParameters);
277     }
278
279     /**
280      * Retrieve cm handle details for a given cm handle.
281      *
282      * @param cmHandleId cm handle identifier
283      * @return cm handle details
284      */
285     @Override
286     public NcmpServiceCmHandle getNcmpServiceCmHandle(final String cmHandleId) {
287         return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(
288             inventoryPersistence.getYangModelCmHandle(cmHandleId));
289     }
290
291     /**
292      * Get cm handle public properties for a given cm handle id.
293      *
294      * @param cmHandleId cm handle identifier
295      * @return cm handle public properties
296      */
297     @Override
298     public Map<String, String> getCmHandlePublicProperties(final String cmHandleId) {
299         final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
300         final List<YangModelCmHandle.Property> yangModelPublicProperties = yangModelCmHandle.getPublicProperties();
301         final Map<String, String> cmHandlePublicProperties = new HashMap<>();
302         YangDataConverter.asPropertiesMap(yangModelPublicProperties, cmHandlePublicProperties);
303         return cmHandlePublicProperties;
304     }
305
306     /**
307      * Get cm handle composite state for a given cm handle id.
308      *
309      * @param cmHandleId cm handle identifier
310      * @return cm handle state
311      */
312     @Override
313     public CompositeState getCmHandleCompositeState(final String cmHandleId) {
314         return inventoryPersistence.getYangModelCmHandle(cmHandleId).getCompositeState();
315     }
316
317     protected void processRemovedCmHandles(final DmiPluginRegistration dmiPluginRegistration,
318                                          final DmiPluginRegistrationResponse dmiPluginRegistrationResponse) {
319         final List<String> tobeRemovedCmHandleIds = dmiPluginRegistration.getRemovedCmHandles();
320         final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
321             new ArrayList<>(tobeRemovedCmHandleIds.size());
322         final Collection<YangModelCmHandle> yangModelCmHandles =
323             inventoryPersistence.getYangModelCmHandles(tobeRemovedCmHandleIds);
324         updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETING);
325
326         final Set<String> notDeletedCmHandles = new HashSet<>();
327         for (final List<String> tobeRemovedCmHandleBatch : Lists.partition(tobeRemovedCmHandleIds, DELETE_BATCH_SIZE)) {
328             try {
329                 batchDeleteCmHandlesFromDbAndModuleSyncMap(tobeRemovedCmHandleBatch);
330                 tobeRemovedCmHandleBatch.forEach(cmHandleId ->
331                     cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId)));
332
333             } catch (final RuntimeException batchException) {
334                 log.error("Unable to de-register cm-handle batch, retrying on each cm handle");
335                 for (final String cmHandleId : tobeRemovedCmHandleBatch) {
336                     final CmHandleRegistrationResponse cmHandleRegistrationResponse =
337                         deleteCmHandleAndGetCmHandleRegistrationResponse(cmHandleId);
338                     cmHandleRegistrationResponses.add(cmHandleRegistrationResponse);
339                     if (cmHandleRegistrationResponse.getStatus() != CmHandleRegistrationResponse.Status.SUCCESS) {
340                         notDeletedCmHandles.add(cmHandleId);
341                     }
342                 }
343             }
344         }
345         yangModelCmHandles.removeIf(yangModelCmHandle -> notDeletedCmHandles.contains(yangModelCmHandle.getId()));
346         updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETED);
347         dmiPluginRegistrationResponse.setRemovedCmHandles(cmHandleRegistrationResponses);
348     }
349
350     protected void processCreatedCmHandles(final DmiPluginRegistration dmiPluginRegistration,
351                                          final DmiPluginRegistrationResponse dmiPluginRegistrationResponse) {
352         final List<NcmpServiceCmHandle> ncmpServiceCmHandles = dmiPluginRegistration.getCreatedCmHandles();
353         final List<CmHandleRegistrationResponse> failedCmHandleRegistrationResponses = new ArrayList<>();
354
355         try {
356             final Collection<String> rejectedCmHandleIds
357                 = checkAlternateIds(ncmpServiceCmHandles, failedCmHandleRegistrationResponses);
358
359             final Collection<String> succeededCmHandleIds = persistCmHandlesWithState(dmiPluginRegistration,
360                 dmiPluginRegistrationResponse, ncmpServiceCmHandles, rejectedCmHandleIds);
361
362             processTrustLevels(ncmpServiceCmHandles, succeededCmHandleIds);
363
364         } catch (final AlreadyDefinedException alreadyDefinedException) {
365             failedCmHandleRegistrationResponses.addAll(CmHandleRegistrationResponse.createFailureResponsesFromXpaths(
366                 alreadyDefinedException.getAlreadyDefinedObjectNames(), CM_HANDLE_ALREADY_EXIST));
367         } catch (final Exception exception) {
368             final Collection<String> cmHandleIds =
369                 ncmpServiceCmHandles.stream().map(NcmpServiceCmHandle::getCmHandleId).collect(Collectors.toList());
370             failedCmHandleRegistrationResponses.addAll(CmHandleRegistrationResponse
371                 .createFailureResponses(cmHandleIds, exception));
372         }
373         final List<CmHandleRegistrationResponse> mergedCmHandleRegistrationResponses
374             = new ArrayList<>(failedCmHandleRegistrationResponses);
375         mergedCmHandleRegistrationResponses.addAll(dmiPluginRegistrationResponse.getCreatedCmHandles());
376
377         dmiPluginRegistrationResponse.setCreatedCmHandles(mergedCmHandleRegistrationResponses);
378     }
379
380     protected void processUpdatedCmHandles(final DmiPluginRegistration dmiPluginRegistration,
381                                          final DmiPluginRegistrationResponse dmiPluginRegistrationResponse) {
382         dmiPluginRegistrationResponse.setUpdatedCmHandles(networkCmProxyDataServicePropertyHandler
383             .updateCmHandleProperties(dmiPluginRegistration.getUpdatedCmHandles()));
384     }
385
386     protected void processUpgradedCmHandles(
387         final DmiPluginRegistration dmiPluginRegistration,
388         final DmiPluginRegistrationResponse dmiPluginRegistrationResponse) {
389
390         final List<String> cmHandleIds = dmiPluginRegistration.getUpgradedCmHandles().getCmHandles();
391         final String upgradedModuleSetTag = dmiPluginRegistration.getUpgradedCmHandles().getModuleSetTag();
392         final Map<YangModelCmHandle, CmHandleState> acceptedCmHandleStatePerCmHandle
393             = new HashMap<>(cmHandleIds.size());
394         final List<CmHandleRegistrationResponse> cmHandleUpgradeResponses = new ArrayList<>(cmHandleIds.size());
395
396         for (final String cmHandleId : cmHandleIds) {
397             try {
398                 final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
399                 if (yangModelCmHandle.getCompositeState().getCmHandleState() == CmHandleState.READY) {
400                     if (moduleUpgradeCanBeSkipped(yangModelCmHandle, upgradedModuleSetTag)) {
401                         cmHandleUpgradeResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId));
402                     } else {
403                         updateYangModelCmHandleForUpgrade(yangModelCmHandle, upgradedModuleSetTag);
404                         acceptedCmHandleStatePerCmHandle.put(yangModelCmHandle, CmHandleState.LOCKED);
405                     }
406                 } else {
407                     cmHandleUpgradeResponses.add(
408                             CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_READY));
409                 }
410             } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
411                 log.error("Unable to find data node for cm handle id : {} , caused by : {}",
412                         cmHandleId, dataNodeNotFoundException.getMessage());
413                 cmHandleUpgradeResponses.add(
414                         CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND));
415             } catch (final DataValidationException dataValidationException) {
416                 log.error("Unable to upgrade cm handle id: {}, caused by : {}",
417                         cmHandleId, dataValidationException.getMessage());
418                 cmHandleUpgradeResponses.add(
419                         CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID));
420             }
421         }
422         cmHandleUpgradeResponses.addAll(upgradeCmHandles(acceptedCmHandleStatePerCmHandle));
423         dmiPluginRegistrationResponse.setUpgradedCmHandles(cmHandleUpgradeResponses);
424     }
425
426     private Collection<String> checkAlternateIds(
427         final List<NcmpServiceCmHandle> cmHandlesToBeCreated,
428         final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses) {
429         final Collection<String> rejectedCmHandleIds = alternateIdChecker
430             .getIdsOfCmHandlesWithRejectedAlternateId(cmHandlesToBeCreated, AlternateIdChecker.Operation.CREATE);
431         cmHandleRegistrationResponses.addAll(CmHandleRegistrationResponse.createFailureResponses(
432             rejectedCmHandleIds, ALTERNATE_ID_ALREADY_ASSOCIATED));
433         return rejectedCmHandleIds;
434     }
435
436     private List<String> persistCmHandlesWithState(final DmiPluginRegistration dmiPluginRegistration,
437                                                    final DmiPluginRegistrationResponse dmiPluginRegistrationResponse,
438                                                    final List<NcmpServiceCmHandle> cmHandlesToBeCreated,
439                                                    final Collection<String> rejectedCmHandleIds) {
440         final List<String> succeededCmHandleIds = new ArrayList<>(cmHandlesToBeCreated.size());
441         final List<YangModelCmHandle> yangModelCmHandlesToRegister = new ArrayList<>(cmHandlesToBeCreated.size());
442         final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
443             new ArrayList<>(cmHandlesToBeCreated.size());
444         for (final NcmpServiceCmHandle ncmpServiceCmHandle: cmHandlesToBeCreated) {
445             if (!rejectedCmHandleIds.contains(ncmpServiceCmHandle.getCmHandleId())) {
446                 yangModelCmHandlesToRegister.add(getYangModelCmHandle(dmiPluginRegistration, ncmpServiceCmHandle));
447                 cmHandleRegistrationResponses.add(
448                     CmHandleRegistrationResponse.createSuccessResponse(ncmpServiceCmHandle.getCmHandleId()));
449                 succeededCmHandleIds.add(ncmpServiceCmHandle.getCmHandleId());
450             }
451         }
452         lcmEventsCmHandleStateHandler.initiateStateAdvised(yangModelCmHandlesToRegister);
453         dmiPluginRegistrationResponse.setCreatedCmHandles(cmHandleRegistrationResponses);
454         return succeededCmHandleIds;
455     }
456
457     private YangModelCmHandle getYangModelCmHandle(final DmiPluginRegistration dmiPluginRegistration,
458                                                    final NcmpServiceCmHandle ncmpServiceCmHandle) {
459         return YangModelCmHandle.toYangModelCmHandle(
460             dmiPluginRegistration.getDmiPlugin(),
461             dmiPluginRegistration.getDmiDataPlugin(),
462             dmiPluginRegistration.getDmiModelPlugin(),
463             ncmpServiceCmHandle,
464             ncmpServiceCmHandle.getModuleSetTag(),
465             ncmpServiceCmHandle.getAlternateId(),
466             ncmpServiceCmHandle.getDataProducerIdentifier());
467     }
468
469     private void processTrustLevels(final Collection<NcmpServiceCmHandle> cmHandlesToBeCreated,
470                                     final Collection<String> succeededCmHandleIds) {
471         final Map<String, TrustLevel> initialTrustLevelPerCmHandleId = new HashMap<>(cmHandlesToBeCreated.size());
472         for (final NcmpServiceCmHandle ncmpServiceCmHandle: cmHandlesToBeCreated) {
473             if (succeededCmHandleIds.contains(ncmpServiceCmHandle.getCmHandleId())) {
474                 initialTrustLevelPerCmHandleId.put(ncmpServiceCmHandle.getCmHandleId(),
475                     ncmpServiceCmHandle.getRegistrationTrustLevel());
476             }
477         }
478         trustLevelManager.handleInitialRegistrationOfTrustLevels(initialTrustLevelPerCmHandleId);
479     }
480
481     private static boolean moduleUpgradeCanBeSkipped(final YangModelCmHandle yangModelCmHandle,
482                                                      final String upgradedModuleSetTag) {
483         if (StringUtils.isBlank(upgradedModuleSetTag)) {
484             return false;
485         }
486         return yangModelCmHandle.getModuleSetTag().equals(upgradedModuleSetTag);
487     }
488
489     private static void updateYangModelCmHandleForUpgrade(final YangModelCmHandle yangModelCmHandle,
490                                                           final String upgradedModuleSetTag) {
491         final String lockReasonWithModuleSetTag = String.format(ModuleOperationsUtils.MODULE_SET_TAG_MESSAGE_FORMAT,
492                 upgradedModuleSetTag);
493         yangModelCmHandle.setCompositeState(new CompositeStateBuilder().withCmHandleState(CmHandleState.READY)
494                 .withLockReason(MODULE_UPGRADE, lockReasonWithModuleSetTag).build());
495     }
496
497     private CmHandleRegistrationResponse deleteCmHandleAndGetCmHandleRegistrationResponse(final String cmHandleId) {
498         try {
499             deleteCmHandleFromDbAndModuleSyncMap(cmHandleId);
500             return CmHandleRegistrationResponse.createSuccessResponse(cmHandleId);
501         } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
502             log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
503                 cmHandleId, dataNodeNotFoundException.getMessage());
504             return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLES_NOT_FOUND);
505         } catch (final DataValidationException dataValidationException) {
506             log.error("Unable to de-register cm-handle id: {}, caused by: {}",
507                 cmHandleId, dataValidationException.getMessage());
508             return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, CM_HANDLE_INVALID_ID);
509         } catch (final Exception exception) {
510             log.error("Unable to de-register cm-handle id : {} , caused by : {}", cmHandleId, exception.getMessage());
511             return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception);
512         }
513     }
514
515     private void updateCmHandleStateBatch(final Collection<YangModelCmHandle> yangModelCmHandles,
516                                           final CmHandleState cmHandleState) {
517         final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>(yangModelCmHandles.size());
518         yangModelCmHandles.forEach(yangModelCmHandle -> cmHandleStatePerCmHandle.put(yangModelCmHandle, cmHandleState));
519         lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
520     }
521
522     private void deleteCmHandleFromDbAndModuleSyncMap(final String cmHandleId) {
523         inventoryPersistence.deleteSchemaSetWithCascade(cmHandleId);
524         inventoryPersistence.deleteDataNode(NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId + "']");
525         removeDeletedCmHandleFromModuleSyncMap(cmHandleId);
526     }
527
528     private void batchDeleteCmHandlesFromDbAndModuleSyncMap(final Collection<String> cmHandleIds) {
529         inventoryPersistence.deleteSchemaSetsWithCascade(cmHandleIds);
530         inventoryPersistence.deleteDataNodes(mapCmHandleIdsToXpaths(cmHandleIds));
531         cmHandleIds.forEach(this::removeDeletedCmHandleFromModuleSyncMap);
532     }
533
534     private Collection<String> mapCmHandleIdsToXpaths(final Collection<String> cmHandles) {
535         return cmHandles.stream()
536             .map(cmHandleId -> NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId + "']")
537             .collect(Collectors.toSet());
538     }
539
540     // CPS-1239 Robustness cleaning of in progress cache
541     private void removeDeletedCmHandleFromModuleSyncMap(final String cmHandleId) {
542         if (moduleSyncStartedOnCmHandles.remove(cmHandleId) != null) {
543             log.debug("{} removed from in progress map", cmHandleId);
544         }
545     }
546
547     private List<CmHandleRegistrationResponse> upgradeCmHandles(final Map<YangModelCmHandle, CmHandleState>
548                                                                     cmHandleStatePerCmHandle) {
549         final List<String> cmHandleIds = getCmHandleIds(cmHandleStatePerCmHandle);
550         log.info("Moving cm handles : {} into locked (for upgrade) state.", cmHandleIds);
551         try {
552             lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
553             return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
554         } catch (final Exception e) {
555             log.error("Unable to update cmHandleIds : {} , caused by : {}", cmHandleIds, e.getMessage());
556             return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, e);
557         }
558     }
559
560     private static List<String> getCmHandleIds(final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle) {
561         return cmHandleStatePerCmHandle.keySet().stream().map(YangModelCmHandle::getId).toList();
562     }
563
564     private void setTrustLevelPerDmiPlugin(final DmiPluginRegistration dmiPluginRegistration) {
565         if (DmiPluginRegistration.isNullEmptyOrBlank(dmiPluginRegistration.getDmiDataPlugin())) {
566             trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiPlugin(), TrustLevel.COMPLETE);
567         } else {
568             trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiDataPlugin(), TrustLevel.COMPLETE);
569         }
570     }
571
572 }