From e1f41c3e6591b572a75021f4b51538f8fdbfc88d Mon Sep 17 00:00:00 2001 From: sourabh_sourabh Date: Sat, 12 Mar 2022 22:41:10 +0530 Subject: Async: NCMP Rest impl. including Request ID generation - In case of invalid topic return http status 400 with error message - Unit test is modified to handle/validate InvalidTopicException Issue-ID: CPS-828 Signed-off-by: sourabh_sourabh Change-Id: I05645c92ccebb8aa422a47f6edcde7b64088a118 --- .../api/impl/NetworkCmProxyDataServiceImpl.java | 14 +++++--- .../api/impl/exception/InvalidTopicException.java | 40 ++++++++++++++++++++++ 2 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/exception/InvalidTopicException.java (limited to 'cps-ncmp-service/src/main/java/org') diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java index e923ce414..76d4cef9e 100755 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java @@ -42,11 +42,11 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.apache.logging.log4j.util.Strings; import org.onap.cps.api.CpsAdminService; import org.onap.cps.api.CpsDataService; import org.onap.cps.api.CpsModuleService; import org.onap.cps.ncmp.api.NetworkCmProxyDataService; +import org.onap.cps.ncmp.api.impl.exception.InvalidTopicException; import org.onap.cps.ncmp.api.impl.exception.ServerNcmpException; import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations; import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations; @@ -303,8 +303,14 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService yangModelCmHandle.getId()); } - private static boolean isValidTopicName(final String topicName) { - return Strings.isNotEmpty(topicName) && TOPIC_NAME_PATTERN.matcher(topicName).matches(); + private static boolean hasTopicParameter(final String topicName) { + if (topicName == null) { + return false; + } + if (TOPIC_NAME_PATTERN.matcher(topicName).matches()) { + return true; + } + throw new InvalidTopicException("Topic name " + topicName + " is invalid", "invalid topic"); } private Map buildDmiResponse(final String requestId) { @@ -319,7 +325,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService final DmiOperations.DataStoreEnum dataStore, final String optionsParamInQuery, final String topicParamInQuery) { - final boolean processAsynchronously = isValidTopicName(topicParamInQuery); + final boolean processAsynchronously = hasTopicParameter(topicParamInQuery); if (processAsynchronously) { final String resourceDataRequestId = UUID.randomUUID().toString(); return ResponseEntity.status(HttpStatus.OK) diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/exception/InvalidTopicException.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/exception/InvalidTopicException.java new file mode 100644 index 000000000..b56ca7b8c --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/exception/InvalidTopicException.java @@ -0,0 +1,40 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.ncmp.api.impl.exception; + +import lombok.Getter; + +public class InvalidTopicException extends RuntimeException { + + @Getter + final String details; + + /** + * Constructor. + * + * @param message the error message + * @param details the error details + */ + public InvalidTopicException(final String message, final String details) { + super(message); + this.details = details; + } +} -- cgit 1.2.3-korg