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.NCMP_DATASPACE_NAME;
27 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DMI_REGISTRY_ANCHOR;
28 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NCMP_DMI_REGISTRY_PARENT;
29 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME;
30 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NO_TIMESTAMP;
31 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum;
32 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED;
33 import static org.onap.cps.utils.CmHandleQueryRestParametersValidator.validateCmHandleQueryParameters;
35 import java.util.ArrayList;
36 import java.util.Collection;
37 import java.util.HashMap;
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.CpsAdminService;
45 import org.onap.cps.api.CpsDataService;
46 import org.onap.cps.api.CpsModuleService;
47 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandlerQueryService;
48 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
49 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations;
50 import org.onap.cps.ncmp.api.impl.operations.DmiOperations;
51 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
52 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
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.InventoryPersistence;
56 import org.onap.cps.ncmp.api.inventory.sync.ModuleSyncService;
57 import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters;
58 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse;
59 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError;
60 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
61 import org.onap.cps.ncmp.api.models.DmiPluginRegistrationResponse;
62 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
63 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
64 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
65 import org.onap.cps.spi.exceptions.DataValidationException;
66 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException;
67 import org.onap.cps.spi.model.CmHandleQueryServiceParameters;
68 import org.onap.cps.spi.model.ModuleReference;
69 import org.onap.cps.utils.CpsValidator;
70 import org.onap.cps.utils.JsonObjectMapper;
71 import org.springframework.http.ResponseEntity;
72 import org.springframework.stereotype.Service;
76 @RequiredArgsConstructor
77 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
79 private final CpsDataService cpsDataService;
81 private final JsonObjectMapper jsonObjectMapper;
83 private final DmiDataOperations dmiDataOperations;
85 private final CpsModuleService cpsModuleService;
87 private final CpsAdminService cpsAdminService;
89 private final NetworkCmProxyDataServicePropertyHandler networkCmProxyDataServicePropertyHandler;
91 private final InventoryPersistence inventoryPersistence;
93 private final ModuleSyncService moduleSyncService;
95 private final NetworkCmProxyCmHandlerQueryService networkCmProxyCmHandlerQueryService;
98 public DmiPluginRegistrationResponse updateDmiRegistrationAndSyncModule(
99 final DmiPluginRegistration dmiPluginRegistration) {
100 dmiPluginRegistration.validateDmiPluginRegistration();
101 final DmiPluginRegistrationResponse dmiPluginRegistrationResponse = new DmiPluginRegistrationResponse();
102 dmiPluginRegistrationResponse.setRemovedCmHandles(
103 parseAndRemoveCmHandlesInDmiRegistration(dmiPluginRegistration.getRemovedCmHandles()));
104 if (!dmiPluginRegistration.getCreatedCmHandles().isEmpty()) {
105 dmiPluginRegistrationResponse.setCreatedCmHandles(
106 parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration));
108 if (!dmiPluginRegistration.getUpdatedCmHandles().isEmpty()) {
109 dmiPluginRegistrationResponse.setUpdatedCmHandles(
110 networkCmProxyDataServicePropertyHandler
111 .updateCmHandleProperties(dmiPluginRegistration.getUpdatedCmHandles()));
113 return dmiPluginRegistrationResponse;
117 public Object getResourceDataOperationalForCmHandle(final String cmHandleId,
118 final String resourceIdentifier,
119 final String optionsParamInQuery,
120 final String topicParamInQuery,
121 final String requestId) {
122 final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(cmHandleId,
125 DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL,
126 requestId, topicParamInQuery);
127 return responseEntity.getBody();
131 public Object getResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
132 final String resourceIdentifier,
133 final String optionsParamInQuery,
134 final String topicParamInQuery,
135 final String requestId) {
136 final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(cmHandleId,
139 DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING,
140 requestId, topicParamInQuery);
141 return responseEntity.getBody();
145 public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
146 final String resourceIdentifier,
147 final OperationEnum operation,
148 final String requestData,
149 final String dataType) {
150 CpsValidator.validateNameCharacters(cmHandleId);
151 return dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(cmHandleId, resourceIdentifier, operation,
152 requestData, dataType);
157 public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
158 CpsValidator.validateNameCharacters(cmHandleId);
159 return cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
163 * Retrieve cm handles with details for the given query parameters.
165 * @param cmHandleQueryApiParameters cm handle query parameters
166 * @return cm handles with details
169 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).stream()
177 .map(dataNode -> YangDataConverter
178 .convertCmHandleToYangModel(dataNode, dataNode.getLeaves().get("id").toString()))
179 .map(YangDataConverter::convertYangModelCmHandleToNcmpServiceCmHandle).collect(Collectors.toSet());
183 * Retrieve cm handle ids for the given query parameters.
185 * @param cmHandleQueryApiParameters cm handle query parameters
186 * @return cm handle ids
189 public Set<String> executeCmHandleIdSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) {
190 return executeCmHandleSearch(cmHandleQueryApiParameters).stream().map(NcmpServiceCmHandle::getCmHandleId)
191 .collect(Collectors.toSet());
195 * Retrieve cm handle details for a given cm handle.
197 * @param cmHandleId cm handle identifier
198 * @return cm handle details
201 public NcmpServiceCmHandle getNcmpServiceCmHandle(final String cmHandleId) {
202 CpsValidator.validateNameCharacters(cmHandleId);
203 return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(
204 inventoryPersistence.getYangModelCmHandle(cmHandleId));
208 * Get cm handle public properties for a given cm handle id.
210 * @param cmHandleId cm handle identifier
211 * @return cm handle public properties
214 public Map<String, String> getCmHandlePublicProperties(final String cmHandleId) {
215 CpsValidator.validateNameCharacters(cmHandleId);
216 final YangModelCmHandle yangModelCmHandle =
217 inventoryPersistence.getYangModelCmHandle(cmHandleId);
218 final List<YangModelCmHandle.Property> yangModelPublicProperties = yangModelCmHandle.getPublicProperties();
219 final Map<String, String> cmHandlePublicProperties = new HashMap<>();
220 YangDataConverter.asPropertiesMap(yangModelPublicProperties, cmHandlePublicProperties);
221 return cmHandlePublicProperties;
225 * Get cm handle composite state for a given cm handle id.
227 * @param cmHandleId cm handle identifier
228 * @return cm handle state
231 public CompositeState getCmHandleCompositeState(final String cmHandleId) {
232 CpsValidator.validateNameCharacters(cmHandleId);
233 return inventoryPersistence.getYangModelCmHandle(cmHandleId).getCompositeState();
237 * THis method registers a cm handle and initiates modules sync.
239 * @param dmiPluginRegistration dmi plugin registration information.
240 * @return cm-handle registration response for create cm-handle requests.
242 public List<CmHandleRegistrationResponse> parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(
243 final DmiPluginRegistration dmiPluginRegistration) {
244 List<CmHandleRegistrationResponse> cmHandleRegistrationResponses = new ArrayList<>();
246 cmHandleRegistrationResponses = dmiPluginRegistration.getCreatedCmHandles().stream()
248 setCompositeStateToAdvised(cmHandle);
249 return YangModelCmHandle.toYangModelCmHandle(
250 dmiPluginRegistration.getDmiPlugin(),
251 dmiPluginRegistration.getDmiDataPlugin(),
252 dmiPluginRegistration.getDmiModelPlugin(),
256 .map(this::registerNewCmHandle)
257 .collect(Collectors.toList());
258 } catch (final DataValidationException dataValidationException) {
259 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createFailureResponse(dmiPluginRegistration
260 .getCreatedCmHandles().stream()
261 .map(NcmpServiceCmHandle::getCmHandleId).findFirst().orElse(null),
262 RegistrationError.CM_HANDLE_INVALID_ID));
264 return cmHandleRegistrationResponses;
267 private void setCompositeStateToAdvised(final NcmpServiceCmHandle ncmpServiceCmHandle) {
268 final CompositeState compositeState = new CompositeState();
269 compositeState.setCmHandleState(CmHandleState.ADVISED);
270 compositeState.setLastUpdateTimeNow();
271 ncmpServiceCmHandle.setCompositeState(compositeState);
274 protected List<CmHandleRegistrationResponse> parseAndRemoveCmHandlesInDmiRegistration(
275 final List<String> tobeRemovedCmHandles) {
276 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
277 new ArrayList<>(tobeRemovedCmHandles.size());
278 for (final String cmHandle : tobeRemovedCmHandles) {
280 CpsValidator.validateNameCharacters(cmHandle);
281 deleteSchemaSetWithCascade(cmHandle);
282 cpsDataService.deleteListOrListElement(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
283 "/dmi-registry/cm-handles[@id='" + cmHandle + "']", NO_TIMESTAMP);
284 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandle));
285 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
286 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
287 cmHandle, dataNodeNotFoundException.getMessage());
288 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse
289 .createFailureResponse(cmHandle, RegistrationError.CM_HANDLE_DOES_NOT_EXIST));
290 } catch (final DataValidationException dataValidationException) {
291 log.error("Unable to de-register cm-handle id: {}, caused by: {}",
292 cmHandle, dataValidationException.getMessage());
293 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse
294 .createFailureResponse(cmHandle, RegistrationError.CM_HANDLE_INVALID_ID));
295 } catch (final Exception exception) {
296 log.error("Unable to de-register cm-handle id : {} , caused by : {}",
297 cmHandle, exception.getMessage());
298 cmHandleRegistrationResponses.add(
299 CmHandleRegistrationResponse.createFailureResponse(cmHandle, exception));
302 return cmHandleRegistrationResponses;
305 private void deleteSchemaSetWithCascade(final String schemaSetName) {
307 cpsModuleService.deleteSchemaSet(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetName,
308 CASCADE_DELETE_ALLOWED);
309 } catch (final SchemaSetNotFoundException schemaSetNotFoundException) {
310 log.warn("Schema set {} does not exist or already deleted", schemaSetName);
314 private CmHandleRegistrationResponse registerNewCmHandle(final YangModelCmHandle yangModelCmHandle) {
316 final String cmHandleJsonData = String.format("{\"cm-handles\":[%s]}",
317 jsonObjectMapper.asJsonString(yangModelCmHandle));
318 cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT,
319 cmHandleJsonData, NO_TIMESTAMP);
320 return CmHandleRegistrationResponse.createSuccessResponse(yangModelCmHandle.getId());
321 } catch (final AlreadyDefinedException alreadyDefinedException) {
322 return CmHandleRegistrationResponse.createFailureResponse(
323 yangModelCmHandle.getId(), RegistrationError.CM_HANDLE_ALREADY_EXIST);
324 } catch (final Exception exception) {
325 return CmHandleRegistrationResponse.createFailureResponse(yangModelCmHandle.getId(), exception);