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