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.operations.DmiRequestBody.OperationEnum;
27 import static org.onap.cps.utils.CmHandleQueryRestParametersValidator.validateCmHandleQueryParameters;
29 import java.time.OffsetDateTime;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.HashMap;
33 import java.util.HashSet;
34 import java.util.List;
37 import java.util.stream.Collectors;
38 import lombok.RequiredArgsConstructor;
39 import lombok.extern.slf4j.Slf4j;
40 import org.onap.cps.api.CpsDataService;
41 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandlerQueryService;
42 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
43 import org.onap.cps.ncmp.api.impl.event.lcm.LcmEventsCmHandleStateHandler;
44 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations;
45 import org.onap.cps.ncmp.api.impl.operations.DmiOperations;
46 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
47 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
48 import org.onap.cps.ncmp.api.inventory.CmHandleQueries;
49 import org.onap.cps.ncmp.api.inventory.CmHandleState;
50 import org.onap.cps.ncmp.api.inventory.CompositeState;
51 import org.onap.cps.ncmp.api.inventory.CompositeStateUtils;
52 import org.onap.cps.ncmp.api.inventory.DataStoreSyncState;
53 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
54 import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters;
55 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse;
56 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError;
57 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
58 import org.onap.cps.ncmp.api.models.DmiPluginRegistrationResponse;
59 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
60 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
61 import org.onap.cps.spi.exceptions.CpsException;
62 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
63 import org.onap.cps.spi.exceptions.DataValidationException;
64 import org.onap.cps.spi.model.CmHandleQueryServiceParameters;
65 import org.onap.cps.spi.model.ModuleDefinition;
66 import org.onap.cps.spi.model.ModuleReference;
67 import org.onap.cps.utils.CpsValidator;
68 import org.onap.cps.utils.JsonObjectMapper;
69 import org.springframework.http.ResponseEntity;
70 import org.springframework.stereotype.Service;
74 @RequiredArgsConstructor
75 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
77 private final JsonObjectMapper jsonObjectMapper;
79 private final DmiDataOperations dmiDataOperations;
81 private final NetworkCmProxyDataServicePropertyHandler networkCmProxyDataServicePropertyHandler;
83 private final InventoryPersistence inventoryPersistence;
85 private final CmHandleQueries cmHandleQueries;
87 private final NetworkCmProxyCmHandlerQueryService networkCmProxyCmHandlerQueryService;
89 private final LcmEventsCmHandleStateHandler lcmEventsCmHandleStateHandler;
91 private final CpsDataService cpsDataService;
94 public DmiPluginRegistrationResponse updateDmiRegistrationAndSyncModule(
95 final DmiPluginRegistration dmiPluginRegistration) {
96 dmiPluginRegistration.validateDmiPluginRegistration();
97 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse = new DmiPluginRegistrationResponse();
98 dmiPluginRegistrationResponse.setRemovedCmHandles(
99 parseAndRemoveCmHandlesInDmiRegistration(dmiPluginRegistration.getRemovedCmHandles()));
100 if (!dmiPluginRegistration.getCreatedCmHandles().isEmpty()) {
101 dmiPluginRegistrationResponse.setCreatedCmHandles(
102 parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration));
104 if (!dmiPluginRegistration.getUpdatedCmHandles().isEmpty()) {
105 dmiPluginRegistrationResponse.setUpdatedCmHandles(
106 networkCmProxyDataServicePropertyHandler
107 .updateCmHandleProperties(dmiPluginRegistration.getUpdatedCmHandles()));
109 return dmiPluginRegistrationResponse;
113 public Object getResourceDataOperationalForCmHandle(final String cmHandleId,
114 final String resourceIdentifier,
115 final String optionsParamInQuery,
116 final String topicParamInQuery,
117 final String requestId) {
118 final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(cmHandleId,
121 DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL,
122 requestId, topicParamInQuery);
123 return responseEntity.getBody();
127 public Object getResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
128 final String resourceIdentifier,
129 final String optionsParamInQuery,
130 final String topicParamInQuery,
131 final String requestId) {
132 final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(cmHandleId,
135 DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING,
136 requestId, topicParamInQuery);
137 return responseEntity.getBody();
141 public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
142 final String resourceIdentifier,
143 final OperationEnum operation,
144 final String requestData,
145 final String dataType) {
146 CpsValidator.validateNameCharacters(cmHandleId);
147 return dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier, operation,
148 requestData, dataType);
152 public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
153 CpsValidator.validateNameCharacters(cmHandleId);
154 return inventoryPersistence.getYangResourcesModuleReferences(cmHandleId);
158 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
159 CpsValidator.validateNameCharacters(cmHandleId);
160 return inventoryPersistence.getModuleDefinitionsByCmHandleId(cmHandleId);
164 * Retrieve cm handles with details for the given query parameters.
166 * @param cmHandleQueryApiParameters cm handle query parameters
167 * @return cm handles with details
170 public Set<NcmpServiceCmHandle> executeCmHandleSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
171 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
172 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
174 validateCmHandleQueryParameters(cmHandleQueryServiceParameters);
176 return networkCmProxyCmHandlerQueryService.queryCmHandles(cmHandleQueryServiceParameters);
180 * Retrieve cm handle ids for the given query parameters.
182 * @param cmHandleQueryApiParameters cm handle query parameters
183 * @return cm handle ids
186 public Set<String> executeCmHandleIdSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
187 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
188 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
190 validateCmHandleQueryParameters(cmHandleQueryServiceParameters);
192 return networkCmProxyCmHandlerQueryService.queryCmHandleIds(cmHandleQueryServiceParameters);
196 * Set the data sync enabled flag, along with the data sync state
197 * based on the data sync enabled boolean for the cm handle id provided.
199 * @param cmHandleId cm handle id
200 * @param dataSyncEnabled data sync enabled flag
203 public void setDataSyncEnabled(final String cmHandleId, final boolean dataSyncEnabled) {
204 CpsValidator.validateNameCharacters(cmHandleId);
205 final CompositeState compositeState = inventoryPersistence
206 .getCmHandleState(cmHandleId);
207 if (compositeState.getDataSyncEnabled().equals(dataSyncEnabled)) {
208 log.info("Data-Sync Enabled flag is already: {} ", dataSyncEnabled);
209 } else if (compositeState.getCmHandleState() != CmHandleState.READY) {
210 throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: "
211 + compositeState.getCmHandleState());
213 final DataStoreSyncState dataStoreSyncState = compositeState.getDataStores()
214 .getOperationalDataStore().getDataStoreSyncState();
215 if (!dataSyncEnabled && dataStoreSyncState == DataStoreSyncState.SYNCHRONIZED) {
216 cpsDataService.deleteDataNode("NFP-Operational", cmHandleId,
217 "/netconf-state", OffsetDateTime.now());
219 CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabled, compositeState);
220 inventoryPersistence.saveCmHandleState(cmHandleId,
226 * Get all cm handle IDs by DMI plugin identifier.
228 * @param dmiPluginIdentifier DMI plugin identifier
229 * @return set of cm handle IDs
232 public Set<String> getAllCmHandleIdsByDmiPluginIdentifier(final String dmiPluginIdentifier) {
233 final Set<NcmpServiceCmHandle> ncmpServiceCmHandles =
234 cmHandleQueries.getCmHandlesByDmiPluginIdentifier(dmiPluginIdentifier);
235 final Set<String> cmHandleIds = new HashSet<>(ncmpServiceCmHandles.size());
236 ncmpServiceCmHandles.forEach(cmHandle -> {
237 cmHandleIds.add(cmHandle.getCmHandleId());
243 * Retrieve cm handle details for a given cm handle.
245 * @param cmHandleId cm handle identifier
246 * @return cm handle details
249 public NcmpServiceCmHandle getNcmpServiceCmHandle(final String cmHandleId) {
250 CpsValidator.validateNameCharacters(cmHandleId);
251 return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(
252 inventoryPersistence.getYangModelCmHandle(cmHandleId));
256 * Get cm handle public properties for a given cm handle id.
258 * @param cmHandleId cm handle identifier
259 * @return cm handle public properties
262 public Map<String, String> getCmHandlePublicProperties(final String cmHandleId) {
263 CpsValidator.validateNameCharacters(cmHandleId);
264 final YangModelCmHandle yangModelCmHandle =
265 inventoryPersistence.getYangModelCmHandle(cmHandleId);
266 final List<YangModelCmHandle.Property> yangModelPublicProperties = yangModelCmHandle.getPublicProperties();
267 final Map<String, String> cmHandlePublicProperties = new HashMap<>();
268 YangDataConverter.asPropertiesMap(yangModelPublicProperties, cmHandlePublicProperties);
269 return cmHandlePublicProperties;
273 * Get cm handle composite state for a given cm handle id.
275 * @param cmHandleId cm handle identifier
276 * @return cm handle state
279 public CompositeState getCmHandleCompositeState(final String cmHandleId) {
280 CpsValidator.validateNameCharacters(cmHandleId);
281 return inventoryPersistence.getYangModelCmHandle(cmHandleId).getCompositeState();
285 * THis method registers a cm handle and initiates modules sync.
287 * @param dmiPluginRegistration dmi plugin registration information.
288 * @return cm-handle registration response for create cm-handle requests.
290 public List<CmHandleRegistrationResponse> parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(
291 final DmiPluginRegistration dmiPluginRegistration) {
292 List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>();
294 cmHandleRegistrationResponses = dmiPluginRegistration.getCreatedCmHandles().stream()
296 YangModelCmHandle.toYangModelCmHandle(
297 dmiPluginRegistration.getDmiPlugin(),
298 dmiPluginRegistration.getDmiDataPlugin(),
299 dmiPluginRegistration.getDmiModelPlugin(),
300 cmHandle)).map(this::registerNewCmHandle).collect(Collectors.toList());
301 } catch (final DataValidationException dataValidationException) {
302 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createFailureResponse(dmiPluginRegistration
303 .getCreatedCmHandles().stream()
304 .map(NcmpServiceCmHandle::getCmHandleId).findFirst().orElse(null),
305 RegistrationError.CM_HANDLE_INVALID_ID));
307 return cmHandleRegistrationResponses;
310 protected List<CmHandleRegistrationResponse> parseAndRemoveCmHandlesInDmiRegistration(
311 final List<String> tobeRemovedCmHandles) {
312 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
313 new ArrayList<>(tobeRemovedCmHandles.size());
314 for (final String cmHandleId : tobeRemovedCmHandles) {
316 CpsValidator.validateNameCharacters(cmHandleId);
317 final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
318 lcmEventsCmHandleStateHandler.updateCmHandleState(yangModelCmHandle,
319 CmHandleState.DELETING);
320 deleteCmHandleByCmHandleId(cmHandleId);
321 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId));
322 lcmEventsCmHandleStateHandler.updateCmHandleState(yangModelCmHandle,
323 CmHandleState.DELETED);
324 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
325 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
326 cmHandleId, dataNodeNotFoundException.getMessage());
327 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse
328 .createFailureResponse(cmHandleId, RegistrationError.CM_HANDLE_DOES_NOT_EXIST));
329 } catch (final DataValidationException dataValidationException) {
330 log.error("Unable to de-register cm-handle id: {}, caused by: {}",
331 cmHandleId, dataValidationException.getMessage());
332 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse
333 .createFailureResponse(cmHandleId, RegistrationError.CM_HANDLE_INVALID_ID));
334 } catch (final Exception exception) {
335 log.error("Unable to de-register cm-handle id : {} , caused by : {}",
336 cmHandleId, exception.getMessage());
337 cmHandleRegistrationResponses.add(
338 CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception));
341 return cmHandleRegistrationResponses;
344 private void deleteCmHandleByCmHandleId(final String cmHandleId) {
345 inventoryPersistence.deleteSchemaSetWithCascade(cmHandleId);
346 inventoryPersistence.deleteListOrListElement("/dmi-registry/cm-handles[@id='" + cmHandleId + "']");
349 private CmHandleRegistrationResponse registerNewCmHandle(final YangModelCmHandle yangModelCmHandle) {
351 lcmEventsCmHandleStateHandler.updateCmHandleState(yangModelCmHandle, CmHandleState.ADVISED);
352 return CmHandleRegistrationResponse.createSuccessResponse(yangModelCmHandle.getId());
353 } catch (final AlreadyDefinedException alreadyDefinedException) {
354 return CmHandleRegistrationResponse.createFailureResponse(
355 yangModelCmHandle.getId(), RegistrationError.CM_HANDLE_ALREADY_EXIST);
356 } catch (final Exception exception) {
357 return CmHandleRegistrationResponse.createFailureResponse(yangModelCmHandle.getId(), exception);