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;
34 import java.util.ArrayList;
35 import java.util.Collection;
36 import java.util.HashMap;
37 import java.util.LinkedHashMap;
38 import java.util.List;
40 import java.util.UUID;
41 import java.util.regex.Pattern;
42 import java.util.stream.Collectors;
43 import lombok.RequiredArgsConstructor;
44 import lombok.extern.slf4j.Slf4j;
45 import org.onap.cps.api.CpsAdminService;
46 import org.onap.cps.api.CpsDataService;
47 import org.onap.cps.api.CpsModuleService;
48 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
49 import org.onap.cps.ncmp.api.impl.exception.InvalidTopicException;
50 import org.onap.cps.ncmp.api.impl.exception.ServerNcmpException;
51 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations;
52 import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations;
53 import org.onap.cps.ncmp.api.impl.operations.DmiOperations;
54 import org.onap.cps.ncmp.api.impl.operations.YangModelCmHandleRetriever;
55 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
56 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse;
57 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError;
58 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
59 import org.onap.cps.ncmp.api.models.DmiPluginRegistrationResponse;
60 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
61 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
62 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
63 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException;
64 import org.onap.cps.spi.model.ModuleReference;
65 import org.onap.cps.utils.JsonObjectMapper;
66 import org.springframework.http.HttpStatus;
67 import org.springframework.http.ResponseEntity;
68 import org.springframework.stereotype.Service;
72 @RequiredArgsConstructor
73 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
75 private final CpsDataService cpsDataService;
77 private final JsonObjectMapper jsonObjectMapper;
79 private final DmiDataOperations dmiDataOperations;
81 private final DmiModelOperations dmiModelOperations;
83 private final CpsModuleService cpsModuleService;
85 private final CpsAdminService cpsAdminService;
87 private final NetworkCmProxyDataServicePropertyHandler networkCmProxyDataServicePropertyHandler;
89 private final YangModelCmHandleRetriever yangModelCmHandleRetriever;
91 // valid kafka topic name regex
92 private static final Pattern TOPIC_NAME_PATTERN = Pattern.compile("^[a-zA-Z0-9]([._-](?![._-])|"
93 + "[a-zA-Z0-9]){0,120}[a-zA-Z0-9]$");
94 private static final String NO_REQUEST_ID = null;
95 private static final String NO_TOPIC = null;
98 public DmiPluginRegistrationResponse updateDmiRegistrationAndSyncModule(
99 final DmiPluginRegistration dmiPluginRegistration) {
100 dmiPluginRegistration.validateDmiPluginRegistration();
101 final var 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 acceptParamInHeader,
120 final String optionsParamInQuery,
121 final String topicParamInQuery) {
123 return validateTopicNameAndGetResourceData(cmHandleId, resourceIdentifier, acceptParamInHeader,
124 DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL, optionsParamInQuery, topicParamInQuery);
128 public Object getResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
129 final String resourceIdentifier,
130 final String acceptParamInHeader,
131 final String optionsParamInQuery,
132 final String topicParamInQuery) {
133 return validateTopicNameAndGetResourceData(cmHandleId, resourceIdentifier, acceptParamInHeader,
134 DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING, optionsParamInQuery, topicParamInQuery);
138 public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
139 final String resourceIdentifier,
140 final OperationEnum operation,
141 final String requestData,
142 final String dataType) {
143 return handleResponse(
144 dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(
145 cmHandleId, resourceIdentifier, operation, requestData, dataType),
146 "Not able to " + operation + " resource data.");
151 public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
152 return cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
156 * Retrieve cm handle identifiers for the given list of module names.
158 * @param moduleNames module names.
159 * @return a collection of anchor identifiers
162 public Collection<String> executeCmHandleHasAllModulesSearch(final Collection<String> moduleNames) {
163 return cpsAdminService.queryAnchorNames(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, moduleNames);
167 * Retrieve cm handle details for a given cm handle.
169 * @param cmHandleId cm handle identifier
170 * @return cm handle details
173 public NcmpServiceCmHandle getNcmpServiceCmHandle(final String cmHandleId) {
174 final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle();
175 final YangModelCmHandle yangModelCmHandle =
176 yangModelCmHandleRetriever.getDmiServiceNamesAndProperties(cmHandleId);
177 final List<YangModelCmHandle.Property> dmiProperties = yangModelCmHandle.getDmiProperties();
178 final List<YangModelCmHandle.Property> publicProperties = yangModelCmHandle.getPublicProperties();
179 ncmpServiceCmHandle.setCmHandleID(yangModelCmHandle.getId());
180 setDmiProperties(dmiProperties, ncmpServiceCmHandle);
181 setPublicProperties(publicProperties, ncmpServiceCmHandle);
182 return ncmpServiceCmHandle;
185 private void setDmiProperties(final List<YangModelCmHandle.Property> dmiProperties,
186 final NcmpServiceCmHandle ncmpServiceCmHandle) {
187 final Map<String, String> dmiPropertiesMap = new LinkedHashMap<>(dmiProperties.size());
188 asPropertiesMap(dmiProperties, dmiPropertiesMap);
189 ncmpServiceCmHandle.setDmiProperties(dmiPropertiesMap);
192 private void setPublicProperties(final List<YangModelCmHandle.Property> publicProperties,
193 final NcmpServiceCmHandle ncmpServiceCmHandle) {
194 final Map<String, String> publicPropertiesMap = new LinkedHashMap<>();
195 asPropertiesMap(publicProperties, publicPropertiesMap);
196 ncmpServiceCmHandle.setPublicProperties(publicPropertiesMap);
199 private void asPropertiesMap(final List<YangModelCmHandle.Property> properties,
200 final Map<String, String> propertiesMap) {
201 for (final YangModelCmHandle.Property property: properties) {
202 propertiesMap.put(property.getName(), property.getValue());
207 * THis method registers a cm handle and initiates modules sync.
209 * @param dmiPluginRegistration dmi plugin registration information.
210 * @return cm-handle registration response for create cm-handle requests.
212 public List<CmHandleRegistrationResponse> parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(
213 final DmiPluginRegistration dmiPluginRegistration) {
214 return dmiPluginRegistration.getCreatedCmHandles().stream()
216 YangModelCmHandle.toYangModelCmHandle(
217 dmiPluginRegistration.getDmiPlugin(),
218 dmiPluginRegistration.getDmiDataPlugin(),
219 dmiPluginRegistration.getDmiModelPlugin(), cmHandle)
221 .map(this::registerAndSyncNewCmHandle)
222 .collect(Collectors.toList());
225 private static Object handleResponse(final ResponseEntity<?> responseEntity,
226 final String exceptionMessage) {
227 if (responseEntity.getStatusCode().is2xxSuccessful()) {
228 return responseEntity.getBody();
230 throw new ServerNcmpException(exceptionMessage,
231 "DMI status code: " + responseEntity.getStatusCodeValue()
232 + ", DMI response body: " + responseEntity.getBody());
236 private CmHandleRegistrationResponse registerAndSyncNewCmHandle(final YangModelCmHandle yangModelCmHandle) {
238 final String cmHandleJsonData = String.format("{\"cm-handles\":[%s]}",
239 jsonObjectMapper.asJsonString(yangModelCmHandle));
240 cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT,
241 cmHandleJsonData, NO_TIMESTAMP);
242 syncModulesAndCreateAnchor(yangModelCmHandle);
243 return CmHandleRegistrationResponse.createSuccessResponse(yangModelCmHandle.getId());
244 } catch (final AlreadyDefinedException alreadyDefinedException) {
245 return CmHandleRegistrationResponse.createFailureResponse(
246 yangModelCmHandle.getId(), RegistrationError.CM_HANDLE_ALREADY_EXIST);
247 } catch (final Exception exception) {
248 return CmHandleRegistrationResponse.createFailureResponse(yangModelCmHandle.getId(), exception);
252 protected void syncModulesAndCreateAnchor(final YangModelCmHandle yangModelCmHandle) {
253 syncAndCreateSchemaSet(yangModelCmHandle);
254 createAnchor(yangModelCmHandle);
257 protected List<CmHandleRegistrationResponse> parseAndRemoveCmHandlesInDmiRegistration(
258 final List<String> tobeRemovedCmHandles) {
259 final List<CmHandleRegistrationResponse> cmHandleRegistrationResponses =
260 new ArrayList<>(tobeRemovedCmHandles.size());
261 for (final String cmHandle : tobeRemovedCmHandles) {
263 deleteSchemaSetWithCascade(cmHandle);
264 cpsDataService.deleteListOrListElement(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
265 "/dmi-registry/cm-handles[@id='" + cmHandle + "']", NO_TIMESTAMP);
266 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse.createSuccessResponse(cmHandle));
267 } catch (final DataNodeNotFoundException dataNodeNotFoundException) {
268 log.error("Unable to find dataNode for cmHandleId : {} , caused by : {}",
269 cmHandle, dataNodeNotFoundException.getMessage());
270 cmHandleRegistrationResponses.add(CmHandleRegistrationResponse
271 .createFailureResponse(cmHandle, RegistrationError.CM_HANDLE_DOES_NOT_EXIST));
272 } catch (final Exception exception) {
273 log.error("Unable to de-register cm-handleIdd : {} , caused by : {}",
274 cmHandle, exception.getMessage());
275 cmHandleRegistrationResponses.add(
276 CmHandleRegistrationResponse.createFailureResponse(cmHandle, exception));
279 return cmHandleRegistrationResponses;
282 private void deleteSchemaSetWithCascade(final String schemaSetName) {
284 cpsModuleService.deleteSchemaSet(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetName,
285 CASCADE_DELETE_ALLOWED);
286 } catch (final SchemaSetNotFoundException schemaSetNotFoundException) {
287 log.warn("Schema set {} does not exist or already deleted", schemaSetName);
291 private void syncAndCreateSchemaSet(final YangModelCmHandle yangModelCmHandle) {
292 final Collection<ModuleReference> moduleReferencesFromCmHandle =
293 dmiModelOperations.getModuleReferences(yangModelCmHandle);
295 final Collection<ModuleReference> identifiedNewModuleReferencesFromCmHandle = cpsModuleService
296 .identifyNewModuleReferences(moduleReferencesFromCmHandle);
298 final Collection<ModuleReference> existingModuleReferencesFromCmHandle =
299 moduleReferencesFromCmHandle.stream().filter(moduleReferenceFromCmHandle ->
300 !identifiedNewModuleReferencesFromCmHandle.contains(moduleReferenceFromCmHandle)
301 ).collect(Collectors.toList());
303 final Map<String, String> newModuleNameToContentMap;
304 if (identifiedNewModuleReferencesFromCmHandle.isEmpty()) {
305 newModuleNameToContentMap = new HashMap<>();
307 newModuleNameToContentMap = dmiModelOperations.getNewYangResourcesFromDmi(yangModelCmHandle,
308 identifiedNewModuleReferencesFromCmHandle);
311 .createSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, yangModelCmHandle.getId(),
312 newModuleNameToContentMap, existingModuleReferencesFromCmHandle);
315 private void createAnchor(final YangModelCmHandle yangModelCmHandle) {
316 cpsAdminService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, yangModelCmHandle.getId(),
317 yangModelCmHandle.getId());
320 private static boolean hasTopicParameter(final String topicName) {
321 if (topicName == null) {
324 if (TOPIC_NAME_PATTERN.matcher(topicName).matches()) {
327 throw new InvalidTopicException("Topic name " + topicName + " is invalid", "invalid topic");
330 private Map<String, Object> buildDmiResponse(final String requestId) {
331 final Map<String, Object> dmiResponseMap = new HashMap<>();
332 dmiResponseMap.put("requestId", requestId);
333 return dmiResponseMap;
336 private Object validateTopicNameAndGetResourceData(final String cmHandleId,
337 final String resourceIdentifier,
338 final String acceptParamInHeader,
339 final DmiOperations.DataStoreEnum dataStore,
340 final String optionsParamInQuery,
341 final String topicParamInQuery) {
342 final boolean processAsynchronously = hasTopicParameter(topicParamInQuery);
343 if (processAsynchronously) {
344 final String resourceDataRequestId = UUID.randomUUID().toString();
345 return ResponseEntity.status(HttpStatus.OK)
346 .body(buildDmiResponse(resourceDataRequestId));
348 final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(
349 cmHandleId, resourceIdentifier, optionsParamInQuery, acceptParamInHeader,
350 dataStore, NO_REQUEST_ID, NO_TOPIC);
351 return handleResponse(responseEntity, "Not able to get resource data.");