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