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 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 com.fasterxml.jackson.core.JsonProcessingException;
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.apache.logging.log4j.util.Strings;
46 import org.onap.cps.api.CpsAdminService;
47 import org.onap.cps.api.CpsDataService;
48 import org.onap.cps.api.CpsModuleService;
49 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
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.impl.yangmodels.YangModelCmHandlesList;
57 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
58 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
59 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
60 import org.onap.cps.spi.exceptions.DataValidationException;
61 import org.onap.cps.spi.model.ModuleReference;
62 import org.onap.cps.utils.JsonObjectMapper;
63 import org.springframework.http.HttpStatus;
64 import org.springframework.http.ResponseEntity;
65 import org.springframework.stereotype.Service;
69 @RequiredArgsConstructor
70 public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService {
72 private final CpsDataService cpsDataService;
74 private final JsonObjectMapper jsonObjectMapper;
76 private final DmiDataOperations dmiDataOperations;
78 private final DmiModelOperations dmiModelOperations;
80 private final CpsModuleService cpsModuleService;
82 private final CpsAdminService cpsAdminService;
84 private final NetworkCmProxyDataServicePropertyHandler networkCmProxyDataServicePropertyHandler;
86 private final YangModelCmHandleRetriever yangModelCmHandleRetriever;
88 // valid kafka topic name regex
89 private static final Pattern TOPIC_NAME_PATTERN = Pattern.compile("^[a-zA-Z0-9]([._-](?![._-])|"
90 + "[a-zA-Z0-9]){0,120}[a-zA-Z0-9]$");
91 private static final String NO_REQUEST_ID = null;
92 private static final String NO_TOPIC = null;
95 public void updateDmiRegistrationAndSyncModule(final DmiPluginRegistration dmiPluginRegistration) {
96 dmiPluginRegistration.validateDmiPluginRegistration();
98 if (!dmiPluginRegistration.getCreatedCmHandles().isEmpty()) {
99 parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration);
101 if (!dmiPluginRegistration.getUpdatedCmHandles().isEmpty()) {
102 parseAndUpdateCmHandlesInDmiRegistration(dmiPluginRegistration);
104 parseAndRemoveCmHandlesInDmiRegistration(dmiPluginRegistration);
105 } catch (final JsonProcessingException | DataNodeNotFoundException e) {
106 final String errorMessage = String.format(
107 "Error occurred while processing the CM-handle registration request, caused by : [%s]",
109 throw new DataValidationException(errorMessage, e.getMessage(), e);
114 public Object getResourceDataOperationalForCmHandle(final String cmHandleId,
115 final String resourceIdentifier,
116 final String acceptParamInHeader,
117 final String optionsParamInQuery,
118 final String topicParamInQuery) {
120 return validateTopicNameAndGetResourceData(cmHandleId, resourceIdentifier, acceptParamInHeader,
121 DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL, optionsParamInQuery, topicParamInQuery);
125 public Object getResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
126 final String resourceIdentifier,
127 final String acceptParamInHeader,
128 final String optionsParamInQuery,
129 final String topicParamInQuery) {
130 return validateTopicNameAndGetResourceData(cmHandleId, resourceIdentifier, acceptParamInHeader,
131 DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING, optionsParamInQuery, topicParamInQuery);
135 public Object writeResourceDataPassThroughRunningForCmHandle(final String cmHandleId,
136 final String resourceIdentifier,
137 final OperationEnum operation,
138 final String requestData,
139 final String dataType) {
140 return handleResponse(
141 dmiDataOperations.writeResourceDataPassThroughRunningFromDmi(
142 cmHandleId, resourceIdentifier, operation, requestData, dataType),
143 "Not able to " + operation + " resource data.");
148 public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
149 return cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
153 * Retrieve cm handle identifiers for the given list of module names.
155 * @param moduleNames module names.
156 * @return a collection of anchor identifiers
159 public Collection<String> executeCmHandleHasAllModulesSearch(final Collection<String> moduleNames) {
160 return cpsAdminService.queryAnchorNames(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, moduleNames);
164 * Retrieve cm handle details for a given cm handle.
165 * @param cmHandleId cm handle identifier
166 * @return cm handle details
169 public NcmpServiceCmHandle getNcmpServiceCmHandle(final String cmHandleId) {
170 final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle();
171 final YangModelCmHandle yangModelCmHandle =
172 yangModelCmHandleRetriever.getDmiServiceNamesAndProperties(cmHandleId);
173 final List<YangModelCmHandle.Property> dmiProperties = yangModelCmHandle.getDmiProperties();
174 final List<YangModelCmHandle.Property> publicProperties = yangModelCmHandle.getPublicProperties();
175 ncmpServiceCmHandle.setCmHandleID(yangModelCmHandle.getId());
176 setDmiProperties(dmiProperties, ncmpServiceCmHandle);
177 setPublicProperties(publicProperties, ncmpServiceCmHandle);
178 return ncmpServiceCmHandle;
181 private void setDmiProperties(final List<YangModelCmHandle.Property> dmiProperties,
182 final NcmpServiceCmHandle ncmpServiceCmHandle) {
183 final Map<String, String> dmiPropertiesMap = new LinkedHashMap<>(dmiProperties.size());
184 asPropertiesMap(dmiProperties, dmiPropertiesMap);
185 ncmpServiceCmHandle.setDmiProperties(dmiPropertiesMap);
188 private void setPublicProperties(final List<YangModelCmHandle.Property> publicProperties,
189 final NcmpServiceCmHandle ncmpServiceCmHandle) {
190 final Map<String, String> publicPropertiesMap = new LinkedHashMap<>();
191 asPropertiesMap(publicProperties, publicPropertiesMap);
192 ncmpServiceCmHandle.setPublicProperties(publicPropertiesMap);
195 private void asPropertiesMap(final List<YangModelCmHandle.Property> properties,
196 final Map<String, String> propertiesMap) {
197 for (final YangModelCmHandle.Property property: properties) {
198 propertiesMap.put(property.getName(), property.getValue());
203 * THis method registers a cm handle and initiates modules sync.
205 * @param dmiPluginRegistration dmi plugin registration information.
206 * @throws JsonProcessingException thrown if json is malformed or missing.
208 public void parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(
209 final DmiPluginRegistration dmiPluginRegistration) throws JsonProcessingException {
210 final YangModelCmHandlesList createdYangModelCmHandlesList =
211 getUpdatedYangModelCmHandlesList(dmiPluginRegistration,
212 dmiPluginRegistration.getCreatedCmHandles());
213 registerAndSyncNewCmHandles(createdYangModelCmHandlesList);
216 private static Object handleResponse(final ResponseEntity<?> responseEntity,
217 final String exceptionMessage) {
218 if (responseEntity.getStatusCode().is2xxSuccessful()) {
219 return responseEntity.getBody();
221 throw new ServerNcmpException(exceptionMessage,
222 "DMI status code: " + responseEntity.getStatusCodeValue()
223 + ", DMI response body: " + responseEntity.getBody());
227 private void parseAndUpdateCmHandlesInDmiRegistration(final DmiPluginRegistration dmiPluginRegistration) {
228 networkCmProxyDataServicePropertyHandler.updateCmHandleProperties(dmiPluginRegistration.getUpdatedCmHandles());
231 private YangModelCmHandlesList getUpdatedYangModelCmHandlesList(
232 final DmiPluginRegistration dmiPluginRegistration,
233 final List<NcmpServiceCmHandle> updatedCmHandles) {
234 return YangModelCmHandlesList.toYangModelCmHandlesList(
235 dmiPluginRegistration.getDmiPlugin(),
236 dmiPluginRegistration.getDmiDataPlugin(),
237 dmiPluginRegistration.getDmiModelPlugin(),
241 private void registerAndSyncNewCmHandles(final YangModelCmHandlesList yangModelCmHandlesList) {
242 final String cmHandleJsonData = jsonObjectMapper.asJsonString(yangModelCmHandlesList);
243 cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT,
244 cmHandleJsonData, NO_TIMESTAMP);
246 for (final YangModelCmHandle yangModelCmHandle : yangModelCmHandlesList.getYangModelCmHandles()) {
247 syncModulesAndCreateAnchor(yangModelCmHandle);
251 protected void syncModulesAndCreateAnchor(final YangModelCmHandle yangModelCmHandle) {
252 syncAndCreateSchemaSet(yangModelCmHandle);
253 createAnchor(yangModelCmHandle);
256 private void parseAndRemoveCmHandlesInDmiRegistration(final DmiPluginRegistration dmiPluginRegistration) {
257 for (final String cmHandle : dmiPluginRegistration.getRemovedCmHandles()) {
259 attemptToDeleteSchemaSetWithCascade(cmHandle);
260 cpsDataService.deleteListOrListElement(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
261 "/dmi-registry/cm-handles[@id='" + cmHandle + "']", NO_TIMESTAMP);
262 } catch (final DataNodeNotFoundException e) {
263 log.warn("Datanode {} not deleted message {}", cmHandle, e.getMessage());
268 private void attemptToDeleteSchemaSetWithCascade(final String schemaSetName) {
270 cpsModuleService.deleteSchemaSet(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetName,
271 CASCADE_DELETE_ALLOWED);
272 } catch (final Exception e) {
273 log.warn("Schema set {} delete failed, reason {}", schemaSetName, e.getMessage());
277 private void syncAndCreateSchemaSet(final YangModelCmHandle yangModelCmHandle) {
278 final Collection<ModuleReference> moduleReferencesFromCmHandle =
279 dmiModelOperations.getModuleReferences(yangModelCmHandle);
281 final Collection<ModuleReference> identifiedNewModuleReferencesFromCmHandle = cpsModuleService
282 .identifyNewModuleReferences(moduleReferencesFromCmHandle);
284 final Collection<ModuleReference> existingModuleReferencesFromCmHandle =
285 moduleReferencesFromCmHandle.stream().filter(moduleReferenceFromCmHandle ->
286 !identifiedNewModuleReferencesFromCmHandle.contains(moduleReferenceFromCmHandle)
287 ).collect(Collectors.toList());
289 final Map<String, String> newModuleNameToContentMap;
290 if (identifiedNewModuleReferencesFromCmHandle.isEmpty()) {
291 newModuleNameToContentMap = new HashMap<>();
293 newModuleNameToContentMap = dmiModelOperations.getNewYangResourcesFromDmi(yangModelCmHandle,
294 identifiedNewModuleReferencesFromCmHandle);
297 .createSchemaSetFromModules(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, yangModelCmHandle.getId(),
298 newModuleNameToContentMap, existingModuleReferencesFromCmHandle);
301 private void createAnchor(final YangModelCmHandle yangModelCmHandle) {
302 cpsAdminService.createAnchor(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, yangModelCmHandle.getId(),
303 yangModelCmHandle.getId());
306 private static boolean isValidTopicName(final String topicName) {
307 return Strings.isNotEmpty(topicName) && TOPIC_NAME_PATTERN.matcher(topicName).matches();
310 private Map<String, Object> buildDmiResponse(final String requestId) {
311 final Map<String, Object> dmiResponseMap = new HashMap<>();
312 dmiResponseMap.put("requestId", requestId);
313 return dmiResponseMap;
316 private Object validateTopicNameAndGetResourceData(final String cmHandleId,
317 final String resourceIdentifier,
318 final String acceptParamInHeader,
319 final DmiOperations.DataStoreEnum dataStore,
320 final String optionsParamInQuery,
321 final String topicParamInQuery) {
322 final boolean processAsynchronously = isValidTopicName(topicParamInQuery);
323 if (processAsynchronously) {
324 final String resourceDataRequestId = UUID.randomUUID().toString();
325 return ResponseEntity.status(HttpStatus.OK)
326 .body(buildDmiResponse(resourceDataRequestId));
328 final ResponseEntity<?> responseEntity = dmiDataOperations.getResourceDataFromDmi(
329 cmHandleId, resourceIdentifier, optionsParamInQuery, acceptParamInHeader,
330 dataStore, NO_REQUEST_ID, NO_TOPIC);
331 return handleResponse(responseEntity, "Not able to get resource data.");