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.List;
36 import java.util.stream.Collectors;
37 import lombok.RequiredArgsConstructor;
38 import lombok.extern.slf4j.Slf4j;
39 import org.onap.cps.api.CpsDataService;
40 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandlerQueryService;
41 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
42 import org.onap.cps.ncmp.api.impl.event.lcm.LcmEventsCmHandleStateHandler;
43 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations;
44 import org.onap.cps.ncmp.api.impl.operations.DmiOperations;
45 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
46 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
47 import org.onap.cps.ncmp.api.inventory.CmHandleState;
48 import org.onap.cps.ncmp.api.inventory.CompositeState;
49 import org.onap.cps.ncmp.api.inventory.CompositeStateUtils;
50 import org.onap.cps.ncmp.api.inventory.DataStoreSyncState;
51 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
52 import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters;
53 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse;
54 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError;
55 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
56 import org.onap.cps.ncmp.api.models.DmiPluginRegistrationResponse;
57 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
58 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
59 import org.onap.cps.spi.exceptions.CpsException;
60 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
61 import org.onap.cps.spi.exceptions.DataValidationException;
62 import org.onap.cps.spi.model.CmHandleQueryServiceParameters;
63 import org.onap.cps.spi.model.ModuleDefinition;
64 import org.onap.cps.spi.model.ModuleReference;
65 import org.onap.cps.utils.CpsValidator;
66 import org.onap.cps.utils.JsonObjectMapper;
67 import org.springframework.http.ResponseEntity;
68 import org.springframework.stereotype.Service;
72 @RequiredArgsConstructor
73 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
75 private final JsonObjectMapper jsonObjectMapper;
77 private final DmiDataOperations dmiDataOperations;
79 private final NetworkCmProxyDataServicePropertyHandler networkCmProxyDataServicePropertyHandler;
81 private final InventoryPersistence inventoryPersistence;
83 private final NetworkCmProxyCmHandlerQueryService networkCmProxyCmHandlerQueryService;
85 private final LcmEventsCmHandleStateHandler lcmEventsCmHandleStateHandler;
87 private final CpsDataService cpsDataService;
90 public DmiPluginRegistrationResponse updateDmiRegistrationAndSyncModule(
91 final DmiPluginRegistration dmiPluginRegistration) {
92 dmiPluginRegistration.validateDmiPluginRegistration();
93 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse = new DmiPluginRegistrationResponse();
94 dmiPluginRegistrationResponse.setRemovedCmHandles(
95 parseAndRemoveCmHandlesInDmiRegistration(dmiPluginRegistration.getRemovedCmHandles()));
96 if (!dmiPluginRegistration.getCreatedCmHandles().isEmpty()) {
97 dmiPluginRegistrationResponse.setCreatedCmHandles(
98 parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration));
100 if (!dmiPluginRegistration.getUpdatedCmHandles().isEmpty()) {
101 dmiPluginRegistrationResponse.setUpdatedCmHandles(
102 networkCmProxyDataServicePropertyHandler
103 .updateCmHandleProperties(dmiPluginRegistration.getUpdatedCmHandles()));
105 return dmiPluginRegistrationResponse;
109 public Object getResourceDataOperationalForCmHandle(final String cmHandleId,
110 final String resourceIdentifier,
111 final String optionsParamInQuery,
112 final String topicParamInQuery,
113 final String requestId) {
114 final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(cmHandleId,
117 DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL,
118 requestId, topicParamInQuery);
119 return responseEntity.getBody();
123 public Object getResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
124 final String resourceIdentifier,
125 final String optionsParamInQuery,
126 final String topicParamInQuery,
127 final String requestId) {
128 final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(cmHandleId,
131 DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING,
132 requestId, topicParamInQuery);
133 return responseEntity.getBody();
137 public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
138 final String resourceIdentifier,
139 final OperationEnum operation,
140 final String requestData,
141 final String dataType) {
142 CpsValidator.validateNameCharacters(cmHandleId);
143 return dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier, operation,
144 requestData, dataType);
148 public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
149 CpsValidator.validateNameCharacters(cmHandleId);
150 return inventoryPersistence.getYangResourcesModuleReferences(cmHandleId);
154 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
155 CpsValidator.validateNameCharacters(cmHandleId);
156 return inventoryPersistence.getModuleDefinitionsByCmHandleId(cmHandleId);
160 * Retrieve cm handles with details for the given query parameters.
162 * @param cmHandleQueryApiParameters cm handle query parameters
163 * @return cm handles with details
166 public Set<NcmpServiceCmHandle> executeCmHandleSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
167 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
168 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
170 validateCmHandleQueryParameters(cmHandleQueryServiceParameters);
172 return networkCmProxyCmHandlerQueryService.queryCmHandles(cmHandleQueryServiceParameters);
176 * Retrieve cm handle ids for the given query parameters.
178 * @param cmHandleQueryApiParameters cm handle query parameters
179 * @return cm handle ids
182 public Set<String> executeCmHandleIdSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
183 final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType(
184 cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class);
186 validateCmHandleQueryParameters(cmHandleQueryServiceParameters);
188 return networkCmProxyCmHandlerQueryService.queryCmHandleIds(cmHandleQueryServiceParameters);
192 * Set the data sync enabled flag, along with the data sync state
193 * based on the data sync enabled boolean for the cm handle id provided.
195 * @param cmHandleId cm handle id
196 * @param dataSyncEnabled data sync enabled flag
199 public void setDataSyncEnabled(final String cmHandleId, final boolean dataSyncEnabled) {
200 CpsValidator.validateNameCharacters(cmHandleId);
201 final CompositeState compositeState = inventoryPersistence
202 .getCmHandleState(cmHandleId);
203 if (compositeState.getDataSyncEnabled().equals(dataSyncEnabled)) {
204 log.info("Data-Sync Enabled flag is already: {} ", dataSyncEnabled);
205 } else if (compositeState.getCmHandleState() != CmHandleState.READY) {
206 throw new CpsException("State mismatch exception.", "Cm-Handle not in READY state. Cm handle state is: "
207 + compositeState.getCmHandleState());
209 final DataStoreSyncState dataStoreSyncState = compositeState.getDataStores()
210 .getOperationalDataStore().getDataStoreSyncState();
211 if (!dataSyncEnabled && dataStoreSyncState == DataStoreSyncState.SYNCHRONIZED) {
212 cpsDataService.deleteDataNode("NFP-Operational", cmHandleId,
213 "/netconf-state", OffsetDateTime.now());
215 CompositeStateUtils.setDataSyncEnabledFlagWithDataSyncState(dataSyncEnabled, compositeState);
216 inventoryPersistence.saveCmHandleState(cmHandleId,
222 * Retrieve cm handle details for a given cm handle.
224 * @param cmHandleId cm handle identifier
225 * @return cm handle details
228 public NcmpServiceCmHandle getNcmpServiceCmHandle(final String cmHandleId) {
229 CpsValidator.validateNameCharacters(cmHandleId);
230 return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(
231 inventoryPersistence.getYangModelCmHandle(cmHandleId));
235 * Get cm handle public properties for a given cm handle id.
237 * @param cmHandleId cm handle identifier
238 * @return cm handle public properties
241 public Map<String, String> getCmHandlePublicProperties(final String cmHandleId) {
242 CpsValidator.validateNameCharacters(cmHandleId);
243 final YangModelCmHandle yangModelCmHandle =
244 inventoryPersistence.getYangModelCmHandle(cmHandleId);
245 final List<YangModelCmHandle.Property> yangModelPublicProperties = yangModelCmHandle.getPublicProperties();
246 final Map<String, String> cmHandlePublicProperties = new HashMap<>();
247 YangDataConverter.asPropertiesMap(yangModelPublicProperties, cmHandlePublicProperties);
248 return cmHandlePublicProperties;
252 * Get cm handle composite state for a given cm handle id.
254 * @param cmHandleId cm handle identifier
255 * @return cm handle state
258 public CompositeState getCmHandleCompositeState(final String cmHandleId) {
259 CpsValidator.validateNameCharacters(cmHandleId);
260 return inventoryPersistence.getYangModelCmHandle(cmHandleId).getCompositeState();
264 * THis method registers a cm handle and initiates modules sync.
266 * @param dmiPluginRegistration dmi plugin registration information.
267 * @return cm-handle registration response for create cm-handle requests.
269 public List<CmHandleRegistrationResponse> parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(
270 final DmiPluginRegistration dmiPluginRegistration) {
271 List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>();
273 cmHandleRegistrationResponses = dmiPluginRegistration.getCreatedCmHandles().stream()
275 YangModelCmHandle.toYangModelCmHandle(
276 dmiPluginRegistration.getDmiPlugin(),
277 dmiPluginRegistration.getDmiDataPlugin(),
278 dmiPluginRegistration.getDmiModelPlugin(),
279 cmHandle)).map(this::registerNewCmHandle).collect(Collectors.toList());
280 } catch (final DataValidationException dataValidationException) {
281 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createFailureResponse(dmiPluginRegistration
282 .getCreatedCmHandles().stream()
283 .map(NcmpServiceCmHandle::getCmHandleId).findFirst().orElse(null),
284 RegistrationError.CM_HANDLE_INVALID_ID));
286 return cmHandleRegistrationResponses;
289 protected List<CmHandleRegistrationResponse> parseAndRemoveCmHandlesInDmiRegistration(
290 final List<String> tobeRemovedCmHandles) {
291 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
292 new ArrayList<>(tobeRemovedCmHandles.size());
293 for (final String cmHandleId : tobeRemovedCmHandles) {
295 CpsValidator.validateNameCharacters(cmHandleId);
296 final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
297 lcmEventsCmHandleStateHandler.updateCmHandleState(yangModelCmHandle,
298 CmHandleState.DELETING);
299 deleteCmHandleByCmHandleId(cmHandleId);
300 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandleId));
301 lcmEventsCmHandleStateHandler.updateCmHandleState(yangModelCmHandle,
302 CmHandleState.DELETED);
303 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
304 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
305 cmHandleId, dataNodeNotFoundException.getMessage());
306 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse
307 .createFailureResponse(cmHandleId, RegistrationError.CM_HANDLE_DOES_NOT_EXIST));
308 } catch (final DataValidationException dataValidationException) {
309 log.error("Unable to de-register cm-handle id: {}, caused by: {}",
310 cmHandleId, dataValidationException.getMessage());
311 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse
312 .createFailureResponse(cmHandleId, RegistrationError.CM_HANDLE_INVALID_ID));
313 } catch (final Exception exception) {
314 log.error("Unable to de-register cm-handle id : {} , caused by : {}",
315 cmHandleId, exception.getMessage());
316 cmHandleRegistrationResponses.add(
317 CmHandleRegistrationResponse.createFailureResponse(cmHandleId, exception));
320 return cmHandleRegistrationResponses;
323 private void deleteCmHandleByCmHandleId(final String cmHandleId) {
324 inventoryPersistence.deleteSchemaSetWithCascade(cmHandleId);
325 inventoryPersistence.deleteListOrListElement("/dmi-registry/cm-handles[@id='" + cmHandleId + "']");
328 private CmHandleRegistrationResponse registerNewCmHandle(final YangModelCmHandle yangModelCmHandle) {
330 lcmEventsCmHandleStateHandler.updateCmHandleState(yangModelCmHandle, CmHandleState.ADVISED);
331 return CmHandleRegistrationResponse.createSuccessResponse(yangModelCmHandle.getId());
332 } catch (final AlreadyDefinedException alreadyDefinedException) {
333 return CmHandleRegistrationResponse.createFailureResponse(
334 yangModelCmHandle.getId(), RegistrationError.CM_HANDLE_ALREADY_EXIST);
335 } catch (final Exception exception) {
336 return CmHandleRegistrationResponse.createFailureResponse(yangModelCmHandle.getId(), exception);