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