* ============LICENSE_END=========================================================
*/
-package org.onap.cps.ncmp.api;
+package org.onap.cps.ncmp.api.datajobs;
-import org.onap.cps.ncmp.api.models.datajob.DataJobMetadata;
-import org.onap.cps.ncmp.api.models.datajob.DataJobReadRequest;
-import org.onap.cps.ncmp.api.models.datajob.DataJobWriteRequest;
+import org.onap.cps.ncmp.api.datajobs.models.DataJobMetadata;
+import org.onap.cps.ncmp.api.datajobs.models.DataJobReadRequest;
+import org.onap.cps.ncmp.api.datajobs.models.DataJobWriteRequest;
public interface DataJobService {
* ============LICENSE_END=========================================================
*/
-package org.onap.cps.ncmp.api.models.datajob;
+package org.onap.cps.ncmp.api.datajobs.models;
/**
* Metadata of read/write data job request.
* ============LICENSE_END=========================================================
*/
-package org.onap.cps.ncmp.api.models.datajob;
+package org.onap.cps.ncmp.api.datajobs.models;
import java.util.List;
* ============LICENSE_END=========================================================
*/
-package org.onap.cps.ncmp.api.models.datajob;
+package org.onap.cps.ncmp.api.datajobs.models;
import java.util.List;
* ============LICENSE_END=========================================================
*/
-package org.onap.cps.ncmp.api.models.datajob;
+package org.onap.cps.ncmp.api.datajobs.models;
import java.util.List;
* ============LICENSE_END=========================================================
*/
-package org.onap.cps.ncmp.api.models.datajob;
+package org.onap.cps.ncmp.api.datajobs.models;
/**
* Holds information of write data job operation.
*/
DataNode getCmHandleDataNodeByAlternateId(String alternateId);
- /**
- * Get data node that matches longest alternate id by removing elements (as defined by the separator string)
- * from right to left.
- *
- * @param alternateId alternate ID
- * @param separator a string that separates each element from the next.
- * @return data node
- */
- DataNode getCmHandleDataNodeByLongestMatchAlternateId(final String alternateId, final String separator);
-
/**
* Get collection of data nodes of given cm handles.
*
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang3.StringUtils;
import org.onap.cps.api.CpsAnchorService;
import org.onap.cps.api.CpsDataService;
import org.onap.cps.api.CpsModuleService;
-import org.onap.cps.ncmp.api.impl.exception.NoAlternateIdParentFoundException;
import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
import org.onap.cps.spi.FetchDescendantsOption;
return dataNodes.iterator().next();
}
- @Override
- public DataNode getCmHandleDataNodeByLongestMatchAlternateId(final String alternateId, final String separator) {
- String bestMatch = alternateId;
- while (StringUtils.isNotEmpty(bestMatch)) {
- try {
- return getCmHandleDataNodeByAlternateId(bestMatch);
- } catch (final DataNodeNotFoundException ignored) {
- bestMatch = getParentPath(bestMatch, separator);
- }
- }
- throw new NoAlternateIdParentFoundException(alternateId);
- }
-
@Override
public Collection<DataNode> getCmHandleDataNodes(final Collection<String> cmHandleIds) {
final Collection<String> xpaths = new ArrayList<>(cmHandleIds.size());
private String createCmHandlesJsonData(final List<YangModelCmHandle> yangModelCmHandles) {
return "{\"cm-handles\":" + jsonObjectMapper.asJsonString(yangModelCmHandles) + "}";
}
-
- private static String getParentPath(final String path, final String separator) {
- final int lastSeparatorIndex = path.lastIndexOf(separator);
- return lastSeparatorIndex < 0 ? "" : path.substring(0, lastSeparatorIndex);
- }
}
* ============LICENSE_END=========================================================
*/
-package org.onap.cps.ncmp.api.impl.exception;
+package org.onap.cps.ncmp.exceptions;
import java.io.Serial;
+import org.onap.cps.ncmp.api.impl.exception.NcmpException;
-public class NoAlternateIdParentFoundException extends NcmpException {
+public class NoAlternateIdMatchFoundException extends NcmpException {
@Serial
private static final long serialVersionUID = -2412915490233422945L;
- private static final String ALTERNATE_ID_NOT_FOUND = "No matching (parent) cm handle found using alternate ids";
+ private static final String ALTERNATE_ID_NOT_FOUND = "No matching cm handle found using alternate ids";
/**
* Constructor.
*
* @param cpsPath datanode cpsPath
*/
- public NoAlternateIdParentFoundException(final String cpsPath) {
+ public NoAlternateIdMatchFoundException(final String cpsPath) {
super(ALTERNATE_ID_NOT_FOUND, String.format("cannot find a datanode with alternate id %s", cpsPath));
}
}
* ============LICENSE_END=========================================================
*/
-package org.onap.cps.ncmp.api.impl;
+package org.onap.cps.ncmp.impl.datajobs;
import lombok.extern.slf4j.Slf4j;
-import org.onap.cps.ncmp.api.DataJobService;
-import org.onap.cps.ncmp.api.models.datajob.DataJobMetadata;
-import org.onap.cps.ncmp.api.models.datajob.DataJobReadRequest;
-import org.onap.cps.ncmp.api.models.datajob.DataJobWriteRequest;
+import org.onap.cps.ncmp.api.datajobs.DataJobService;
+import org.onap.cps.ncmp.api.datajobs.models.DataJobMetadata;
+import org.onap.cps.ncmp.api.datajobs.models.DataJobReadRequest;
+import org.onap.cps.ncmp.api.datajobs.models.DataJobWriteRequest;
@Slf4j
public class DataJobServiceImpl implements DataJobService {
--- /dev/null
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2024 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.utils;
+
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence;
+import org.onap.cps.ncmp.exceptions.NoAlternateIdMatchFoundException;
+import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
+import org.onap.cps.spi.model.DataNode;
+import org.springframework.stereotype.Service;
+
+@Slf4j
+@Service
+@RequiredArgsConstructor
+public class AlternateIdMatcher {
+
+ private final InventoryPersistence inventoryPersistence;
+
+ /**
+ * Get data node that matches longest alternate id by removing elements (as defined by the separator string)
+ * from right to left.
+ *
+ * @param alternateId alternate ID
+ * @param separator a string that separates each element from the next.
+ * @return data node
+ */
+ public DataNode getCmHandleDataNodeByLongestMatchAlternateId(final String alternateId, final String separator) {
+ String bestMatch = alternateId;
+ while (StringUtils.isNotEmpty(bestMatch)) {
+ try {
+ return inventoryPersistence.getCmHandleDataNodeByAlternateId(bestMatch);
+ } catch (final DataNodeNotFoundException ignored) {
+ bestMatch = getParentPath(bestMatch, separator);
+ }
+ }
+ throw new NoAlternateIdMatchFoundException(alternateId);
+ }
+
+ private String getParentPath(final String path, final String separator) {
+ final int lastSeparatorIndex = path.lastIndexOf(separator);
+ return lastSeparatorIndex < 0 ? "" : path.substring(0, lastSeparatorIndex);
+ }
+}
import ch.qos.logback.classic.Logger
import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.core.read.ListAppender
+import org.onap.cps.ncmp.impl.datajobs.DataJobServiceImpl
import org.slf4j.LoggerFactory
-import org.onap.cps.ncmp.api.models.datajob.DataJobReadRequest
-import org.onap.cps.ncmp.api.models.datajob.DataJobWriteRequest
-import org.onap.cps.ncmp.api.models.datajob.DataJobMetadata
-import org.onap.cps.ncmp.api.models.datajob.ReadOperation
-import org.onap.cps.ncmp.api.models.datajob.WriteOperation
+import org.onap.cps.ncmp.api.datajobs.models.DataJobReadRequest
+import org.onap.cps.ncmp.api.datajobs.models.DataJobWriteRequest
+import org.onap.cps.ncmp.api.datajobs.models.DataJobMetadata
+import org.onap.cps.ncmp.api.datajobs.models.ReadOperation
+import org.onap.cps.ncmp.api.datajobs.models.WriteOperation
import spock.lang.Specification
class DataJobServiceImplSpec extends Specification{
import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
import org.onap.cps.spi.CascadeDeleteAllowed
import org.onap.cps.spi.FetchDescendantsOption
-import org.onap.cps.ncmp.api.impl.exception.NoAlternateIdParentFoundException
import org.onap.cps.spi.exceptions.DataNodeNotFoundException
import org.onap.cps.spi.model.DataNode
import org.onap.cps.spi.model.ModuleDefinition
assert objectUnderTest.getCmHandleDataNodeByAlternateId('alternate id') == new DataNode()
}
- def 'Find cm handle parent data node using alternate ids'() {
- given: 'cm handle in the registry with alternateId /a/b'
- def matchingCpsPath = "/dmi-registry/cm-handles[@alternate-id='/a/b']"
- mockCmHandleQueries.queryNcmpRegistryByCpsPath(matchingCpsPath, OMIT_DESCENDANTS) >> [new DataNode()]
- and: 'no other cm handle'
- mockCmHandleQueries.queryNcmpRegistryByCpsPath(*_) >> []
- expect: 'querying for alternate id a matching result found'
- assert objectUnderTest.getCmHandleDataNodeByLongestMatchAlternateId(alternateId, '/') != null
- where: 'the following parameters are used'
- scenario | alternateId
- 'exact match' | '/a/b'
- 'exact match with trailing separator' | '/a/b/'
- 'child match' | '/a/b/c'
- }
-
- def 'Find cm handle parent data node using alternate ids mismatches'() {
- given: 'cm handle in the registry with alternateId'
- def matchingCpsPath = "/dmi-registry/cm-handles[@alternate-id='${cpsPath}]"
- mockCmHandleQueries.queryNcmpRegistryByCpsPath(matchingCpsPath, OMIT_DESCENDANTS) >> [new DataNode()]
- and: 'no other cm handle'
- mockCmHandleQueries.queryNcmpRegistryByCpsPath(*_) >> []
- when: 'attempt to find alternateId'
- objectUnderTest.getCmHandleDataNodeByLongestMatchAlternateId(alternateId, '/')
- then: 'no alternate id found exception thrown'
- def thrown = thrown(NoAlternateIdParentFoundException)
- and: 'the exception has the relevant details from the error response'
- assert thrown.message == 'No matching (parent) cm handle found using alternate ids'
- assert thrown.details == 'cannot find a datanode with alternate id ' + alternateId
- where: 'the following parameters are used'
- scenario | alternateId | cpsPath
- 'no match for parent only' | '/a' | '/a/b'
- 'no match at all' | '/x/y/z' | '/a/b'
- 'no match with trailing separator' | '/c/d/' | '/c/d'
- }
-
def 'Attempt to get non existing cm handle data node by alternate id'() {
given: 'query service is invoked and returns empty collection of data nodes'
mockCmHandleQueries.queryNcmpRegistryByCpsPath(*_) >> []
--- /dev/null
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2024 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.utils
+
+import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence
+import org.onap.cps.ncmp.exceptions.NoAlternateIdMatchFoundException
+import org.onap.cps.spi.exceptions.DataNodeNotFoundException
+import org.onap.cps.spi.model.DataNode
+import spock.lang.Specification
+
+class AlternateIdMatcherSpec extends Specification {
+
+ def mockInventoryPersistence = Mock(InventoryPersistence)
+ def objectUnderTest = new AlternateIdMatcher(mockInventoryPersistence)
+
+ def setup() {
+ given: 'cm handle in the registry with alternate id /a/b'
+ mockInventoryPersistence.getCmHandleDataNodeByAlternateId('/a/b') >> new DataNode()
+ and: 'no other cm handle'
+ mockInventoryPersistence.getCmHandleDataNodeByAlternateId(_) >> { throw new DataNodeNotFoundException('', '') }
+ }
+
+ def 'Finding longest alternate id matches.'() {
+ expect: 'querying for alternate id a matching result found'
+ assert objectUnderTest.getCmHandleDataNodeByLongestMatchAlternateId(targetAlternateId, '/') != null
+ where: 'the following parameters are used'
+ scenario | targetAlternateId
+ 'exact match' | '/a/b'
+ 'parent match' | '/a/b/c'
+ 'grand parent match' | '/a/b/c/d'
+ 'trailing separator match' | '/a/b/'
+ }
+
+ def 'Attempt to find longest alternate id match without any matches.'() {
+ when: 'attempt to find alternateId'
+ objectUnderTest.getCmHandleDataNodeByLongestMatchAlternateId(targetAlternateId, '/')
+ then: 'no alternate id match found exception thrown'
+ def thrown = thrown(NoAlternateIdMatchFoundException)
+ and: 'the exception has the relevant details from the error response'
+ assert thrown.message == 'No matching cm handle found using alternate ids'
+ assert thrown.details == 'cannot find a datanode with alternate id ' + targetAlternateId
+ where: 'the following parameters are used'
+ scenario | targetAlternateId
+ 'no match for parent only' | '/a'
+ 'no match for other child' | '/a/c'
+ 'no match at all' | '/x/y'
+ }
+}
\ No newline at end of file
import org.onap.cps.spi.model.DataNode
import org.onap.cps.spi.repository.DataspaceRepository
import org.onap.cps.spi.utils.SessionManager
+import org.onap.cps.ncmp.utils.AlternateIdMatcher
import org.onap.cps.utils.JsonObjectMapper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
@Autowired
InventoryPersistence inventoryPersistence
+ @Autowired
+ AlternateIdMatcher alternateIdMatcher
+
MockWebServer mockDmiServer = null
DmiDispatcher dmiDispatcher = new DmiDispatcher()
import org.onap.cps.integration.ResourceMeter
import org.onap.cps.integration.performance.base.NcmpPerfTestBase
-import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence
+import org.onap.cps.ncmp.utils.AlternateIdMatcher
import java.util.stream.Collectors
class CmHandleQueryByAlternateIdPerfTest extends NcmpPerfTestBase {
- InventoryPersistence objectUnderTest
+ AlternateIdMatcher objectUnderTest
ResourceMeter resourceMeter = new ResourceMeter()
- def setup() { objectUnderTest = inventoryPersistence }
+ def setup() { objectUnderTest = alternateIdMatcher }
def 'Query cm handle by longest match alternate id'() {
when: 'an alternate id as cps path query'