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