2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 highstreet technologies GmbH
4 * Modifications Copyright (C) 2021-2022 Nordix Foundation
5 * Modifications Copyright (C) 2021 Pantheon.tech
6 * Modifications Copyright (C) 2021-2022 Bell Canada
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.cps.ncmp.api.impl;
26 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME;
27 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum;
28 import static org.onap.cps.ncmp.api.impl.utils.RestQueryParametersValidator.validateCmHandleQueryParameters;
30 import com.hazelcast.map.IMap;
31 import java.time.OffsetDateTime;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.HashMap;
35 import java.util.HashSet;
36 import java.util.List;
39 import java.util.stream.Collectors;
40 import lombok.RequiredArgsConstructor;
41 import lombok.extern.slf4j.Slf4j;
42 import org.onap.cps.api.CpsDataService;
43 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandlerQueryService;
44 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
45 import org.onap.cps.ncmp.api.impl.event.lcm.LcmEventsCmHandleStateHandler;
46 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations;
47 import org.onap.cps.ncmp.api.impl.operations.DmiOperations;
48 import org.onap.cps.ncmp.api.impl.utils.CmHandleQueryConditions;
49 import org.onap.cps.ncmp.api.impl.utils.InventoryQueryConditions;
50 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
51 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
52 import org.onap.cps.ncmp.api.inventory.CmHandleQueries;
53 import org.onap.cps.ncmp.api.inventory.CmHandleState;
54 import org.onap.cps.ncmp.api.inventory.CompositeState;
55 import org.onap.cps.ncmp.api.inventory.CompositeStateUtils;
56 import org.onap.cps.ncmp.api.inventory.DataStoreSyncState;
57 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
58 import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters;
59 import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters;
60 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse;
61 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError;
62 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
63 import org.onap.cps.ncmp.api.models.DmiPluginRegistrationResponse;
64 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
65 import org.onap.cps.spi.FetchDescendantsOption;
66 import org.onap.cps.spi.exceptions.AlreadyDefinedExceptionBatch;
67 import org.onap.cps.spi.exceptions.CpsException;
68 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
69 import org.onap.cps.spi.exceptions.DataValidationException;
70 import org.onap.cps.spi.model.ModuleDefinition;
71 import org.onap.cps.spi.model.ModuleReference;
72 import org.onap.cps.utils.JsonObjectMapper;
73 import org.springframework.http.ResponseEntity;
74 import org.springframework.stereotype.Service;
78 @RequiredArgsConstructor
79 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
81 private final JsonObjectMapper jsonObjectMapper;
82 private final DmiDataOperations dmiDataOperations;
83 private final NetworkCmProxyDataServicePropertyHandler networkCmProxyDataServicePropertyHandler;
84 private final InventoryPersistence inventoryPersistence;
85 private final CmHandleQueries cmHandleQueries;
86 private final NetworkCmProxyCmHandlerQueryService networkCmProxyCmHandlerQueryService;
87 private final LcmEventsCmHandleStateHandler lcmEventsCmHandleStateHandler;
88 private final CpsDataService cpsDataService;
89 private final IMap<String, Object> moduleSyncStartedOnCmHandles;
92 public DmiPluginRegistrationResponse updateDmiRegistrationAndSyncModule(
93 final DmiPluginRegistration dmiPluginRegistration) {
94 dmiPluginRegistration.validateDmiPluginRegistration();
95 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse = new DmiPluginRegistrationResponse();
97 if (!dmiPluginRegistration.getRemovedCmHandles().isEmpty()) {
98 dmiPluginRegistrationResponse.setRemovedCmHandles(
99 parseAndRemoveCmHandlesInDmiRegistration(dmiPluginRegistration.getRemovedCmHandles()));
102 if (!dmiPluginRegistration.getCreatedCmHandles().isEmpty()) {
103 dmiPluginRegistrationResponse.setCreatedCmHandles(
104 parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration));
106 if (!dmiPluginRegistration.getUpdatedCmHandles().isEmpty()) {
107 dmiPluginRegistrationResponse.setUpdatedCmHandles(
108 networkCmProxyDataServicePropertyHandler
109 .updateCmHandleProperties(dmiPluginRegistration.getUpdatedCmHandles()));
111 return dmiPluginRegistrationResponse;
115 public Object getResourceDataOperationalForCmHandle(final String cmHandleId,
116 final String resourceIdentifier,
117 final String optionsParamInQuery,
118 final String topicParamInQuery,
119 final String requestId) {
120 final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(cmHandleId,
123 DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL,
124 requestId, topicParamInQuery);
125 return responseEntity.getBody();
129 public Object getResourceDataOperational(final String cmHandleId,
130 final String resourceIdentifier,
131 final FetchDescendantsOption fetchDescendantsOption) {
132 return cpsDataService.getDataNode(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId, resourceIdentifier,
133 fetchDescendantsOption);
137 public Object getResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
138 final String resourceIdentifier,
139 final String optionsParamInQuery,
140 final String topicParamInQuery,
141 final String requestId) {
142 final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(cmHandleId,
145 DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING,
146 requestId, topicParamInQuery);
147 return responseEntity.getBody();
151 public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
152 final String resourceIdentifier,
153 final OperationEnum operation,
154 final String requestData,
155 final String dataType) {
156 return dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier, operation,
157 requestData, dataType);
161 public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
162 return inventoryPersistence.getYangResourcesModuleReferences(cmHandleId);
166 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
167 return inventoryPersistence.getModuleDefinitionsByCmHandleId(cmHandleId);
171 * Retrieve cm handles with details for the given query parameters.
173 * @param cmHandleQueryApiParameters cm handle query parameters
174 * @return cm handles with details
177 public Set<NcmpServiceCmHandle> executeCmHandleSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
178 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
179 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
180 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
181 return networkCmProxyCmHandlerQueryService.queryCmHandles(cmHandleQueryServiceParameters);
185 * Retrieve cm handle ids for the given query parameters.
187 * @param cmHandleQueryApiParameters cm handle query parameters
188 * @return cm handle ids
191 public Set<String> executeCmHandleIdSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
192 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
193 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
194 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES);
195 return networkCmProxyCmHandlerQueryService.queryCmHandleIds(cmHandleQueryServiceParameters);
199 * Set the data sync enabled flag, along with the data sync state
200 * based on the data sync enabled boolean for the cm handle id provided.
202 * @param cmHandleId cm handle id
203 * @param dataSyncEnabled data sync enabled flag
206 public void setDataSyncEnabled(final String cmHandleId, final boolean dataSyncEnabled) {
207 final CompositeState compositeState = inventoryPersistence
208 .getCmHandleState(cmHandleId);
209 if (compositeState.getDataSyncEnabled().equals(dataSyncEnabled)) {
210 log.info("Data-Sync Enabled flag is already: {} ", dataSyncEnabled);
211 } else if (compositeState.getCmHandleState() != CmHandleState.READY) {
212 throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: "
213 + compositeState.getCmHandleState());
215 final DataStoreSyncState dataStoreSyncState = compositeState.getDataStores()
216 .getOperationalDataStore().getDataStoreSyncState();
217 if (!dataSyncEnabled && dataStoreSyncState == DataStoreSyncState.SYNCHRONIZED) {
218 cpsDataService.deleteDataNode(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId,
219 "/netconf-state", OffsetDateTime.now());
221 CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabled, compositeState);
222 inventoryPersistence.saveCmHandleState(cmHandleId,
228 * Get all cm handle IDs by DMI plugin identifier.
230 * @param dmiPluginIdentifier DMI plugin identifier
231 * @return set of cm handle IDs
234 public Set<String> getAllCmHandleIdsByDmiPluginIdentifier(final String dmiPluginIdentifier) {
235 final Set<NcmpServiceCmHandle> ncmpServiceCmHandles =
236 cmHandleQueries.getCmHandlesByDmiPluginIdentifier(dmiPluginIdentifier);
237 final Set<String> cmHandleIds = new HashSet<>(ncmpServiceCmHandles.size());
238 ncmpServiceCmHandles.forEach(cmHandle -> cmHandleIds.add(cmHandle.getCmHandleId()));
243 * Get all cm handle IDs by various properties.
245 * @param cmHandleQueryServiceParameters cm handle query parameters
246 * @return set of cm handle IDs
249 public Set<String> executeCmHandleIdSearchForInventory(
250 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) {
251 validateCmHandleQueryParameters(cmHandleQueryServiceParameters, InventoryQueryConditions.ALL_CONDITION_NAMES);
252 return networkCmProxyCmHandlerQueryService.queryCmHandleIdsForInventory(cmHandleQueryServiceParameters);
256 * Retrieve cm handle details for a given cm handle.
258 * @param cmHandleId cm handle identifier
259 * @return cm handle details
262 public NcmpServiceCmHandle getNcmpServiceCmHandle(final String cmHandleId) {
263 return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(
264 inventoryPersistence.getYangModelCmHandle(cmHandleId));
268 * Get cm handle public properties for a given cm handle id.
270 * @param cmHandleId cm handle identifier
271 * @return cm handle public properties
274 public Map<String, String> getCmHandlePublicProperties(final String cmHandleId) {
275 final YangModelCmHandle yangModelCmHandle =
276 inventoryPersistence.getYangModelCmHandle(cmHandleId);
277 final List<YangModelCmHandle.Property> yangModelPublicProperties = yangModelCmHandle.getPublicProperties();
278 final Map<String, String> cmHandlePublicProperties = new HashMap<>();
279 YangDataConverter.asPropertiesMap(yangModelPublicProperties, cmHandlePublicProperties);
280 return cmHandlePublicProperties;
284 * Get cm handle composite state for a given cm handle id.
286 * @param cmHandleId cm handle identifier
287 * @return cm handle state
290 public CompositeState getCmHandleCompositeState(final String cmHandleId) {
291 return inventoryPersistence.getYangModelCmHandle(cmHandleId).getCompositeState();
295 * THis method registers a cm handle and initiates modules sync.
297 * @param dmiPluginRegistration dmi plugin registration information.
298 * @return cm-handle registration response for create cm-handle requests.
300 public List<CmHandleRegistrationResponse> parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(
301 final DmiPluginRegistration dmiPluginRegistration) {
302 List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>();
303 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 cmHandleRegistrationResponses = registerNewCmHandles(cmHandleStatePerCmHandle);
315 } catch (final DataValidationException dataValidationException) {
316 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createFailureResponse(dmiPluginRegistration
317 .getCreatedCmHandles().stream()
318 .map(NcmpServiceCmHandle::getCmHandleId).findFirst().orElse(null),
319 RegistrationError.CM_HANDLE_INVALID_ID));
321 return cmHandleRegistrationResponses;
324 protected List<CmHandleRegistrationResponse> parseAndRemoveCmHandlesInDmiRegistration(
325 final List<String> tobeRemovedCmHandles) {
326 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
327 new ArrayList<>(tobeRemovedCmHandles.size());
328 final List<YangModelCmHandle> yangModelCmHandles =
329 tobeRemovedCmHandles.stream().map(inventoryPersistence::getYangModelCmHandle)
330 .collect(Collectors.toList());
331 updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETING);
333 for (final String cmHandleId : tobeRemovedCmHandles) {
335 deleteCmHandleFromDbAndModuleSyncMap(cmHandleId);
336 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId));
337 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
338 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
339 cmHandleId, dataNodeNotFoundException.getMessage());
340 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse
341 .createFailureResponse(cmHandleId, RegistrationError.CM_HANDLE_DOES_NOT_EXIST));
342 } catch (final DataValidationException dataValidationException) {
343 log.error("Unable to de-register cm-handle id: {}, caused by: {}",
344 cmHandleId, dataValidationException.getMessage());
345 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse
346 .createFailureResponse(cmHandleId, RegistrationError.CM_HANDLE_INVALID_ID));
347 } catch (final Exception exception) {
348 log.error("Unable to de-register cm-handle id : {} , caused by : {}",
349 cmHandleId, exception.getMessage());
350 cmHandleRegistrationResponses.add(
351 CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception));
355 updateCmHandleStateBatch(yangModelCmHandles, CmHandleState.DELETED);
356 return cmHandleRegistrationResponses;
359 private void updateCmHandleStateBatch(final List<YangModelCmHandle> yangModelCmHandles,
360 final CmHandleState cmHandleState) {
361 final Map<YangModelCmHandle, CmHandleState> cmHandleIdsToBeRemoved = new HashMap<>();
362 yangModelCmHandles.stream().forEach(yangModelCmHandle ->
363 cmHandleIdsToBeRemoved.put(yangModelCmHandle, cmHandleState));
364 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleIdsToBeRemoved);
367 private void deleteCmHandleFromDbAndModuleSyncMap(final String cmHandleId) {
368 inventoryPersistence.deleteSchemaSetWithCascade(cmHandleId);
369 inventoryPersistence.deleteListOrListElement("/dmi-registry/cm-handles[@id='" + cmHandleId + "']");
370 removeDeletedCmHandleFromModuleSyncMap(cmHandleId);
373 // CPS-1239 Robustness cleaning of in progress cache
374 private void removeDeletedCmHandleFromModuleSyncMap(final String deletedCmHandleId) {
375 if (moduleSyncStartedOnCmHandles.remove(deletedCmHandleId) != null) {
376 log.debug("{} removed from in progress map", deletedCmHandleId);
380 private List<CmHandleRegistrationResponse> registerNewCmHandles(final Map<YangModelCmHandle, CmHandleState>
381 cmHandleStatePerCmHandle) {
382 final List<String> cmHandleIds = cmHandleStatePerCmHandle.keySet().stream().map(YangModelCmHandle::getId)
383 .collect(Collectors.toList());
385 lcmEventsCmHandleStateHandler.updateCmHandleStateBatch(cmHandleStatePerCmHandle);
386 return CmHandleRegistrationResponse.createSuccessResponses(cmHandleIds);
387 } catch (final AlreadyDefinedExceptionBatch alreadyDefinedExceptionBatch) {
388 return CmHandleRegistrationResponse.createFailureResponses(
389 alreadyDefinedExceptionBatch.getAlreadyDefinedXpaths(),
390 RegistrationError.CM_HANDLE_ALREADY_EXIST);
391 } catch (final Exception exception) {
392 return CmHandleRegistrationResponse.createFailureResponses(cmHandleIds, exception);