2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 highstreet technologies GmbH
4 * Modifications Copyright (C) 2021-2023 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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * SPDX-License-Identifier: Apache-2.0
22 * ============LICENSE_END=========================================================
25 package org.onap.cps.ncmp.api.impl;
27 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NCMP_DMI_REGISTRY_PARENT;
28 import static org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME;
29 import static org.onap.cps.ncmp.api.impl.utils.RestQueryParametersValidator.validateCmHandleQueryParameters;
31 import com.google.common.collect.Lists;
32 import com.hazelcast.map.IMap;
33 import java.time.OffsetDateTime;
34 import java.util.ArrayList;
35 import java.util.Collection;
36 import java.util.HashMap;
37 import java.util.HashSet;
38 import java.util.List;
41 import java.util.stream.Collectors;
42 import lombok.RequiredArgsConstructor;
43 import lombok.extern.slf4j.Slf4j;
44 import org.onap.cps.api.CpsDataService;
45 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandleQueryService;
46 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
47 import org.onap.cps.ncmp.api.impl.events.lcm.LcmEventsCmHandleStateHandler;
48 import org.onap.cps.ncmp.api.impl.inventory.CmHandleQueries;
49 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState;
50 import org.onap.cps.ncmp.api.impl.inventory.CompositeState;
51 import org.onap.cps.ncmp.api.impl.inventory.CompositeStateUtils;
52 import org.onap.cps.ncmp.api.impl.inventory.DataStoreSyncState;
53 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence;
54 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations;
55 import org.onap.cps.ncmp.api.impl.operations.OperationType;
56 import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevel;
57 import org.onap.cps.ncmp.api.impl.utils.CmHandleQueryConditions;
58 import org.onap.cps.ncmp.api.impl.utils.InventoryQueryConditions;
59 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
60 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
61 import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters;
62 import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters;
63 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse;
64 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError;
65 import org.onap.cps.ncmp.api.models.DataOperationRequest;
66 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
67 import org.onap.cps.ncmp.api.models.DmiPluginRegistrationResponse;
68 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
69 import org.onap.cps.spi.FetchDescendantsOption;
70 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
71 import org.onap.cps.spi.exceptions.CpsException;
72 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
73 import org.onap.cps.spi.exceptions.DataValidationException;
74 import org.onap.cps.spi.model.ModuleDefinition;
75 import org.onap.cps.spi.model.ModuleReference;
76 import org.onap.cps.utils.JsonObjectMapper;
77 import org.springframework.http.ResponseEntity;
78 import org.springframework.stereotype.Service;
82 @RequiredArgsConstructor
83 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
85 private static final int DELETE_BATCH_SIZE = 100;
86 private final JsonObjectMapper jsonObjectMapper;
87 private final DmiDataOperations dmiDataOperations;
88 private final NetworkCmProxyDataServicePropertyHandler networkCmProxyDataServicePropertyHandler;
89 private final InventoryPersistence inventoryPersistence;
90 private final CmHandleQueries cmHandleQueries;
91 private final NetworkCmProxyCmHandleQueryService networkCmProxyCmHandleQueryService;
92 private final LcmEventsCmHandleStateHandler lcmEventsCmHandleStateHandler;
93 private final CpsDataService cpsDataService;
94 private final IMap<String, Object> moduleSyncStartedOnCmHandles;
95 private final IMap<String, TrustLevel> trustLevelPerDmiPlugin;
98 public DmiPluginRegistrationResponse updateDmiRegistrationAndSyncModule(
99 final DmiPluginRegistration dmiPluginRegistration) {
100 dmiPluginRegistration.validateDmiPluginRegistration();
101 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse = new DmiPluginRegistrationResponse();
103 if (!dmiPluginRegistration.getRemovedCmHandles().isEmpty()) {
104 dmiPluginRegistrationResponse.setRemovedCmHandles(
105 parseAndRemoveCmHandlesInDmiRegistration(dmiPluginRegistration.getRemovedCmHandles()));
108 if (!dmiPluginRegistration.getCreatedCmHandles().isEmpty()) {
109 dmiPluginRegistrationResponse.setCreatedCmHandles(
110 parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration));
112 if (!dmiPluginRegistration.getUpdatedCmHandles().isEmpty()) {
113 dmiPluginRegistrationResponse.setUpdatedCmHandles(
114 networkCmProxyDataServicePropertyHandler
115 .updateCmHandleProperties(dmiPluginRegistration.getUpdatedCmHandles()));
118 setTrustLevelPerDmiPlugin(dmiPluginRegistration);
120 return dmiPluginRegistrationResponse;
124 public Object getResourceDataForCmHandle(final String datastoreName,
125 final String cmHandleId,
126 final String resourceIdentifier,
127 final String optionsParamInQuery,
128 final String topicParamInQuery,
129 final String requestId) {
130 final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(datastoreName, cmHandleId,
135 return responseEntity.getBody();
139 public Object getResourceDataForCmHandle(final String datastoreName,
140 final String cmHandleId,
141 final String resourceIdentifier,
142 final FetchDescendantsOption fetchDescendantsOption) {
143 return cpsDataService.getDataNodes(datastoreName, cmHandleId, resourceIdentifier,
144 fetchDescendantsOption).iterator().next();
148 public void executeDataOperationForCmHandles(final String topicParamInQuery,
149 final DataOperationRequest
150 dataOperationRequest,
151 final String requestId) {
152 dmiDataOperations.requestResourceDataFromDmi(topicParamInQuery, dataOperationRequest, requestId);
156 public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
157 final String resourceIdentifier,
158 final OperationType operationType,
159 final String requestData,
160 final String dataType) {
161 return dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier,
162 operationType, requestData, dataType);
166 public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
167 return inventoryPersistence.getYangResourcesModuleReferences(cmHandleId);
171 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
172 return inventoryPersistence.getModuleDefinitionsByCmHandleId(cmHandleId);
176 * Retrieve cm handles with details for the given query parameters.
178 * @param cmHandleQueryApiParameters cm handle query parameters
179 * @return cm handles with details
182 public Collection<NcmpServiceCmHandle> executeCmHandleSearch(
183 final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
184 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
185 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
186 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
187 return networkCmProxyCmHandleQueryService.queryCmHandles(cmHandleQueryServiceParameters);
191 * Retrieve cm handle ids for the given query parameters.
193 * @param cmHandleQueryApiParameters cm handle query parameters
194 * @return cm handle ids
197 public Collection<String> executeCmHandleIdSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
198 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
199 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
200 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
201 return networkCmProxyCmHandleQueryService.queryCmHandleIds(cmHandleQueryServiceParameters);
205 * Set the data sync enabled flag, along with the data sync state
206 * based on the data sync enabled boolean for the cm handle id provided.
208 * @param cmHandleId cm handle id
209 * @param dataSyncEnabled data sync enabled flag
212 public void setDataSyncEnabled(final String cmHandleId, final boolean dataSyncEnabled) {
213 final CompositeState compositeState = inventoryPersistence
214 .getCmHandleState(cmHandleId);
215 if (compositeState.getDataSyncEnabled().equals(dataSyncEnabled)) {
216 log.info("Data-Sync Enabled flag is already: {} ", dataSyncEnabled);
217 } else if (compositeState.getCmHandleState() != CmHandleState.READY) {
218 throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: "
219 + compositeState.getCmHandleState());
221 final DataStoreSyncState dataStoreSyncState = compositeState.getDataStores()
222 .getOperationalDataStore().getDataStoreSyncState();
223 if (!dataSyncEnabled && dataStoreSyncState == DataStoreSyncState.SYNCHRONIZED) {
224 cpsDataService.deleteDataNode(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId,
225 "/netconf-state", OffsetDateTime.now());
227 CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabled, compositeState);
228 inventoryPersistence.saveCmHandleState(cmHandleId,
234 * Get all cm handle IDs by DMI plugin identifier.
236 * @param dmiPluginIdentifier DMI plugin identifier
237 * @return set of cm handle IDs
240 public Collection<String> getAllCmHandleIdsByDmiPluginIdentifier(final String dmiPluginIdentifier) {
241 return cmHandleQueries.getCmHandleIdsByDmiPluginIdentifier(dmiPluginIdentifier);
245 * Get all cm handle IDs by various properties.
247 * @param cmHandleQueryServiceParameters cm handle query parameters
248 * @return set of cm handle IDs
251 public Collection<String> executeCmHandleIdSearchForInventory(
252 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
253 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, InventoryQueryConditions.ALL_CONDITION_NAMES);
254 return networkCmProxyCmHandleQueryService.queryCmHandleIdsForInventory(cmHandleQueryServiceParameters);
258 * Retrieve cm handle details for a given cm handle.
260 * @param cmHandleId cm handle identifier
261 * @return cm handle details
264 public NcmpServiceCmHandle getNcmpServiceCmHandle(final String cmHandleId) {
265 return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(
266 inventoryPersistence.getYangModelCmHandle(cmHandleId));
270 * Get cm handle public properties for a given cm handle id.
272 * @param cmHandleId cm handle identifier
273 * @return cm handle public properties
276 public Map<String, String> getCmHandlePublicProperties(final String cmHandleId) {
277 final YangModelCmHandle yangModelCmHandle =
278 inventoryPersistence.getYangModelCmHandle(cmHandleId);
279 final List<YangModelCmHandle.Property> yangModelPublicProperties = yangModelCmHandle.getPublicProperties();
280 final Map<String, String> cmHandlePublicProperties = new HashMap<>();
281 YangDataConverter.asPropertiesMap(yangModelPublicProperties, cmHandlePublicProperties);
282 return cmHandlePublicProperties;
286 * Get cm handle composite state for a given cm handle id.
288 * @param cmHandleId cm handle identifier
289 * @return cm handle state
292 public CompositeState getCmHandleCompositeState(final String cmHandleId) {
293 return inventoryPersistence.getYangModelCmHandle(cmHandleId).getCompositeState();
297 * THis method registers a cm handle and initiates modules sync.
299 * @param dmiPluginRegistration dmi plugin registration information.
300 * @return cm-handle registration response for create cm-handle requests.
302 public List<CmHandleRegistrationResponse> parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(
303 final DmiPluginRegistration dmiPluginRegistration) {
304 final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>();
305 dmiPluginRegistration.getCreatedCmHandles()
306 .forEach(cmHandle -> {
307 final YangModelCmHandle yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle(
308 dmiPluginRegistration.getDmiPlugin(),
309 dmiPluginRegistration.getDmiDataPlugin(),
310 dmiPluginRegistration.getDmiModelPlugin(),
312 cmHandleStatePerCmHandle.put(yangModelCmHandle, CmHandleState.ADVISED);
314 return registerNewCmHandles(cmHandleStatePerCmHandle);
317 protected List<CmHandleRegistrationResponse> parseAndRemoveCmHandlesInDmiRegistration(
318 final List<String> tobeRemovedCmHandles) {
319 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
320 new ArrayList<>(tobeRemovedCmHandles.size());
321 final Collection<YangModelCmHandle> yangModelCmHandles =
322 inventoryPersistence.getYangModelCmHandles(tobeRemovedCmHandles);
324 updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETING);
326 final Set<String> notDeletedCmHandles = new HashSet<>();
327 for (final List<String> tobeRemovedCmHandleBatch : Lists.partition(tobeRemovedCmHandles, DELETE_BATCH_SIZE)) {
329 batchDeleteCmHandlesFromDbAndModuleSyncMap(tobeRemovedCmHandleBatch);
330 tobeRemovedCmHandleBatch.forEach(cmHandleId ->
331 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId)));
333 } catch (final RuntimeException batchException) {
334 log.error("Unable to de-register cm-handle batch, retrying on each cm handle");
335 for (final String cmHandleId : tobeRemovedCmHandleBatch) {
336 final CmHandleRegistrationResponse cmHandleRegistrationResponse =
337 deleteCmHandleAndGetCmHandleRegistrationResponse(cmHandleId);
338 cmHandleRegistrationResponses.add(cmHandleRegistrationResponse);
339 if (cmHandleRegistrationResponse.getStatus() != CmHandleRegistrationResponse.Status.SUCCESS) {
340 notDeletedCmHandles.add(cmHandleId);
346 yangModelCmHandles.removeIf(yangModelCmHandle -> notDeletedCmHandles.contains(yangModelCmHandle.getId()));
347 updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETED);
349 return cmHandleRegistrationResponses;
352 private CmHandleRegistrationResponse deleteCmHandleAndGetCmHandleRegistrationResponse(final String cmHandleId) {
354 deleteCmHandleFromDbAndModuleSyncMap(cmHandleId);
355 return CmHandleRegistrationResponse.createSuccessResponse(cmHandleId);
356 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
357 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
358 cmHandleId, dataNodeNotFoundException.getMessage());
359 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId,
360 RegistrationError.CM_HANDLE_DOES_NOT_EXIST);
361 } catch (final DataValidationException dataValidationException) {
362 log.error("Unable to de-register cm-handle id: {}, caused by: {}",
363 cmHandleId, dataValidationException.getMessage());
364 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId,
365 RegistrationError.CM_HANDLE_INVALID_ID);
366 } catch (final Exception exception) {
367 log.error("Unable to de-register cm-handle id : {} , caused by : {}", cmHandleId, exception.getMessage());
368 return CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception);
372 private void updateCmHandleStateBatch(final Collection<YangModelCmHandle> yangModelCmHandles,
373 final CmHandleState cmHandleState) {
374 final Map<YangModelCmHandle, CmHandleState> cmHandleStatePerCmHandle = new HashMap<>(yangModelCmHandles.size());
375 yangModelCmHandles.forEach(yangModelCmHandle -> cmHandleStatePerCmHandle.put(yangModelCmHandle, cmHandleState));
376 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
379 private void deleteCmHandleFromDbAndModuleSyncMap(final String cmHandleId) {
380 inventoryPersistence.deleteSchemaSetWithCascade(cmHandleId);
381 inventoryPersistence.deleteDataNode(NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId
383 removeDeletedCmHandleFromModuleSyncMap(cmHandleId);
386 private void batchDeleteCmHandlesFromDbAndModuleSyncMap(final Collection<String> tobeRemovedCmHandles) {
387 inventoryPersistence.deleteSchemaSetsWithCascade(tobeRemovedCmHandles);
388 inventoryPersistence.deleteDataNodes(mapCmHandleIdsToXpaths(tobeRemovedCmHandles));
389 tobeRemovedCmHandles.forEach(this::removeDeletedCmHandleFromModuleSyncMap);
392 private Collection<String> mapCmHandleIdsToXpaths(final Collection<String> cmHandles) {
393 return cmHandles.stream()
394 .map(cmHandleId -> NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId + "']")
395 .collect(Collectors.toSet());
398 // CPS-1239 Robustness cleaning of in progress cache
399 private void removeDeletedCmHandleFromModuleSyncMap(final String deletedCmHandleId) {
400 if (moduleSyncStartedOnCmHandles.remove(deletedCmHandleId) != null) {
401 log.debug("{} removed from in progress map", deletedCmHandleId);
405 private List<CmHandleRegistrationResponse> registerNewCmHandles(final Map<YangModelCmHandle, CmHandleState>
406 cmHandleStatePerCmHandle) {
407 final List<String> cmHandleIds = cmHandleStatePerCmHandle.keySet().stream().map(YangModelCmHandle::getId)
408 .collect(Collectors.toList());
410 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
411 return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
412 } catch (final AlreadyDefinedException alreadyDefinedException) {
413 return CmHandleRegistrationResponse.createFailureResponses(
414 alreadyDefinedException.getAlreadyDefinedObjectNames(),
415 RegistrationError.CM_HANDLE_ALREADY_EXIST);
416 } catch (final Exception exception) {
417 return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, exception);
421 private void setTrustLevelPerDmiPlugin(final DmiPluginRegistration dmiPluginRegistration) {
422 if (DmiPluginRegistration.isNullEmptyOrBlank(dmiPluginRegistration.getDmiDataPlugin())) {
423 trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiPlugin(), TrustLevel.COMPLETE);
425 trustLevelPerDmiPlugin.put(dmiPluginRegistration.getDmiDataPlugin(), TrustLevel.COMPLETE);