diff options
33 files changed, 1112 insertions, 18 deletions
diff --git a/.gitignore b/.gitignore index 3ab10191f4..3c5994fa22 100755 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ *.zip *.log +cps-application/archunit_store target/ log/ @@ -1,9 +1,29 @@ +<!-- + ============LICENSE_START======================================================= + Copyright (C) 2021 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========================================================= +--> + # Configuration Persistence Service ## General Information * [CPS Project Wiki](https://wiki.onap.org/display/DW/Configuration+Persistence+Service+Project) - -## For Developers + +## For Developers * [Developer Wiki](https://wiki.onap.org/display/DW/Configuration+Persistence+Service+Developer%27s+Landing+Page) * [Building and running CPS locally](docker-compose/README.md) diff --git a/cps-application/pom.xml b/cps-application/pom.xml index 2ae4883752..f656215c06 100755 --- a/cps-application/pom.xml +++ b/cps-application/pom.xml @@ -106,6 +106,10 @@ </exclusion> </exclusions> </dependency> + <dependency> + <groupId>com.tngtech.archunit</groupId> + <artifactId>archunit-junit5</artifactId> + </dependency> </dependencies> <build> diff --git a/cps-application/src/main/resources/application.yml b/cps-application/src/main/resources/application.yml index 3edb20cd1f..4f3072eb91 100644 --- a/cps-application/src/main/resources/application.yml +++ b/cps-application/src/main/resources/application.yml @@ -1,5 +1,6 @@ # ============LICENSE_START=======================================================
# Modification (C) 2021 Nordix Foundation
+# Modification Copyright (C) 2021 Bell Canada.
# ================================================================================
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -57,6 +58,19 @@ spring: max-file-size: 100MB
max-request-size: 100MB
+ kafka:
+ bootstrap-servers: ${KAFKA_BOOTSTRAP_SERVER:localhost:9092}
+ security:
+ protocol: PLAINTEXT
+ producer:
+ value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
+ client-id: cps-core
+
+notification:
+ data-updated:
+ enabled: false
+ topic: ${CPS_CHANGE_EVENT_TOPIC:cps.cfg-state-events}
+
security:
# comma-separated uri patterns which do not require authorization
permit-uri: /manage/health/**,/manage/info,/swagger-ui/**,/swagger-resources/**,/v3/api-docs
diff --git a/cps-application/src/test/java/org/onap/cps/architecture/LayeredArchitectureTest.java b/cps-application/src/test/java/org/onap/cps/architecture/LayeredArchitectureTest.java new file mode 100644 index 0000000000..24454615a3 --- /dev/null +++ b/cps-application/src/test/java/org/onap/cps/architecture/LayeredArchitectureTest.java @@ -0,0 +1,61 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 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.architecture; + + +import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes; +import static com.tngtech.archunit.library.freeze.FreezingArchRule.freeze; + +import com.tngtech.archunit.core.importer.ImportOption; +import com.tngtech.archunit.junit.AnalyzeClasses; +import com.tngtech.archunit.junit.ArchTest; +import com.tngtech.archunit.lang.ArchRule; + +/** + * Test class responsible for layered architecture. + */ +@AnalyzeClasses(packages = "org.onap.cps", importOptions = {ImportOption.DoNotIncludeTests.class}) +public class LayeredArchitectureTest { + + private static final String REST_CONTROLLER_PACKAGE = "org.onap.cps.rest.."; + private static final String NCMP_REST_PACKAGE = "org.onap.cps.ncmp.rest.."; + private static final String API_SERVICE_PACKAGE = "org.onap.cps.api.."; + private static final String SPI_SERVICE_PACKAGE = "org.onap.cps.spi.."; + private static final String NCMP_SERVICE_PACKAGE = "org.onap.cps.ncmp.api.."; + private static final String SPI_REPOSITORY_PACKAGE = "org.onap.cps.spi.repository.."; + private static final String YANG_SCHEMA_PACKAGE = "org.onap.cps.yang.."; + + @ArchTest + static final ArchRule restControllerShouldOnlyDependOnRestController = + classes().that().resideInAPackage(REST_CONTROLLER_PACKAGE).should().onlyHaveDependentClassesThat() + .resideInAPackage(REST_CONTROLLER_PACKAGE); + + @ArchTest + static final ArchRule apiOrSpiServiceShouldOnlyBeDependedOnByControllerAndServices = + freeze(classes().that().resideInAPackage(API_SERVICE_PACKAGE) + .or().resideInAPackage(SPI_SERVICE_PACKAGE).should().onlyHaveDependentClassesThat() + .resideInAnyPackage(REST_CONTROLLER_PACKAGE, API_SERVICE_PACKAGE, SPI_SERVICE_PACKAGE, NCMP_REST_PACKAGE, + NCMP_SERVICE_PACKAGE, YANG_SCHEMA_PACKAGE)); + + @ArchTest + static final ArchRule repositoryShouldOnlyBeDependedOnByServicesAndRepository = + classes().that().resideInAPackage(SPI_REPOSITORY_PACKAGE).should().onlyHaveDependentClassesThat() + .resideInAnyPackage(API_SERVICE_PACKAGE, SPI_SERVICE_PACKAGE, SPI_REPOSITORY_PACKAGE); +} diff --git a/cps-application/src/test/resources/archunit.properties b/cps-application/src/test/resources/archunit.properties new file mode 100644 index 0000000000..d68f266695 --- /dev/null +++ b/cps-application/src/test/resources/archunit.properties @@ -0,0 +1,18 @@ +# ============LICENSE_START======================================================= +# Copyright (C) 2021 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========================================================= + +freeze.store.default.allowStoreCreation=true
\ No newline at end of file diff --git a/cps-bom/pom.xml b/cps-bom/pom.xml index 12961b8fd2..f947eaecf6 100644 --- a/cps-bom/pom.xml +++ b/cps-bom/pom.xml @@ -2,6 +2,7 @@ <!-- ============LICENSE_START======================================================= Modification Copyright (C) 2021 Nordix Foundation + Modification Copyright (C) 2021 Bell Canada. ================================================================================ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -82,6 +83,11 @@ </dependency> <dependency> <groupId>${project.groupId}</groupId> + <artifactId>cps-events</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> <artifactId>checkstyle</artifactId> <version>${project.version}</version> </dependency> diff --git a/cps-dependencies/pom.xml b/cps-dependencies/pom.xml index afe4de0597..9fd2f6b305 100755 --- a/cps-dependencies/pom.xml +++ b/cps-dependencies/pom.xml @@ -31,6 +31,7 @@ <properties> <antlr4-runtime.version>4.9.2</antlr4-runtime.version> + <archunit.version>0.18.0</archunit.version> <cglib-nodep.version>3.1</cglib-nodep.version> <commons-codec.version>1.15</commons-codec.version> <commons-lang3.version>3.11</commons-lang3.version> @@ -152,6 +153,11 @@ </dependency> <dependency> <groupId>org.testcontainers</groupId> + <artifactId>kafka</artifactId> + <version>${testcontainers.version}</version> + </dependency> + <dependency> + <groupId>org.testcontainers</groupId> <artifactId>postgresql</artifactId> <version>${testcontainers.version}</version> </dependency> @@ -170,6 +176,12 @@ <artifactId>liquibase-core</artifactId> <version>${liquibase.version}</version> </dependency> + <dependency> + <groupId>com.tngtech.archunit</groupId> + <artifactId>archunit-junit5</artifactId> + <version>${archunit.version}</version> + <scope>test</scope> + </dependency> </dependencies> </dependencyManagement> </project> diff --git a/cps-parent/pom.xml b/cps-parent/pom.xml index a341ea9af7..bd857720c1 100755 --- a/cps-parent/pom.xml +++ b/cps-parent/pom.xml @@ -327,7 +327,7 @@ see https://www.testcontainers.org/features/configuration/#disabling-ryuk --> <TESTCONTAINERS_RYUK_DISABLED>true</TESTCONTAINERS_RYUK_DISABLED> - <TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX>${docker.pull.registry}/library/</TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX> + <TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX>${docker.pull.registry}/</TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX> </environmentVariables> </configuration> </plugin> diff --git a/cps-ri/pom.xml b/cps-ri/pom.xml index 320c7edae0..b2cf6917e6 100644 --- a/cps-ri/pom.xml +++ b/cps-ri/pom.xml @@ -2,6 +2,7 @@ <!--
============LICENSE_START=======================================================
Modification Copyright (C) 2021 Nordix Foundation
+ Modifications Copyright (C) 2021 Bell Canada.
================================================================================
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -116,4 +117,18 @@ <scope>test</scope>
</dependency>
</dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <environmentVariables>
+ <TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX>${docker.pull.registry}/library/</TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX>
+ </environmentVariables>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
</project>
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java index af1eca33e6..42364ead69 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java @@ -160,7 +160,7 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName); final var anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName); if (isRootXpath(xpath)) { - return fragmentRepository.getFirstByDataspaceAndAnchor(dataspaceEntity, anchorEntity); + return fragmentRepository.findFirstRootByDataspaceAndAnchor(dataspaceEntity, anchorEntity); } else { return fragmentRepository.getByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath); diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java index c484ae9e87..ee77b73c9f 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java @@ -48,12 +48,15 @@ public interface FragmentRepository extends JpaRepository<FragmentEntity, Long> .orElseThrow(() -> new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName(), xpath));
}
- Optional<FragmentEntity> findFirstByDataspaceAndAnchor(@NonNull DataspaceEntity dataspaceEntity,
- @NonNull AnchorEntity anchorEntity);
+ @Query(
+ value = "SELECT * FROM FRAGMENT WHERE anchor_id = :anchor AND dataspace_id = :dataspace AND parent_id is NULL",
+ nativeQuery = true)
+ List<FragmentEntity> findRootsByDataspaceAndAnchor(
+ @Param("dataspace") int dataspaceId, @Param("anchor") int anchorId);
- default FragmentEntity getFirstByDataspaceAndAnchor(@NonNull DataspaceEntity dataspaceEntity,
+ default FragmentEntity findFirstRootByDataspaceAndAnchor(@NonNull DataspaceEntity dataspaceEntity,
@NonNull AnchorEntity anchorEntity) {
- return findFirstByDataspaceAndAnchor(dataspaceEntity, anchorEntity)
+ return findRootsByDataspaceAndAnchor(dataspaceEntity.getId(), anchorEntity.getId()).stream().findFirst()
.orElseThrow(() -> new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName()));
}
diff --git a/cps-service/pom.xml b/cps-service/pom.xml index 878c4434d3..dcfb1a70f1 100644 --- a/cps-service/pom.xml +++ b/cps-service/pom.xml @@ -2,6 +2,7 @@ <!--
============LICENSE_START=======================================================
Copyright Copyright (C) 2021 Nordix Foundation
+ Modifications Copyright (C) 2021 Bell Canada.
================================================================================
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -32,6 +33,10 @@ <dependencies>
<dependency>
+ <groupId>org.onap.cps</groupId>
+ <artifactId>cps-events</artifactId>
+ </dependency>
+ <dependency>
<groupId>org.opendaylight.yangtools</groupId>
<artifactId>yang-model-api</artifactId>
</dependency>
@@ -65,6 +70,14 @@ <artifactId>caffeine</artifactId>
</dependency>
<dependency>
+ <groupId>org.springframework.kafka</groupId>
+ <artifactId>spring-kafka</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-messaging</artifactId>
+ </dependency>
+ <dependency>
<!-- For logging -->
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
@@ -105,5 +118,15 @@ <artifactId>cglib-nodep</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.testcontainers</groupId>
+ <artifactId>kafka</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.kafka</groupId>
+ <artifactId>spring-kafka-test</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/cps-service/src/main/java/org/onap/cps/api/impl/CpsDataServiceImpl.java b/cps-service/src/main/java/org/onap/cps/api/impl/CpsDataServiceImpl.java index 23bf4f2ee9..c822c68ae9 100755 --- a/cps-service/src/main/java/org/onap/cps/api/impl/CpsDataServiceImpl.java +++ b/cps-service/src/main/java/org/onap/cps/api/impl/CpsDataServiceImpl.java @@ -3,6 +3,7 @@ * Copyright (C) 2021 Nordix Foundation * Modifications Copyright (C) 2020 Bell Canada. All rights reserved. * Modifications Copyright (C) 2021 Pantheon.tech + * Modifications Copyright (C) 2021 Bell Canada. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +26,7 @@ import java.util.Collection; import org.onap.cps.api.CpsAdminService; import org.onap.cps.api.CpsDataService; import org.onap.cps.api.CpsModuleService; +import org.onap.cps.notification.NotificationService; import org.onap.cps.spi.CpsDataPersistenceService; import org.onap.cps.spi.FetchDescendantsOption; import org.onap.cps.spi.exceptions.DataValidationException; @@ -53,10 +55,14 @@ public class CpsDataServiceImpl implements CpsDataService { @Autowired private YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache; + @Autowired + private NotificationService notificationService; + @Override public void saveData(final String dataspaceName, final String anchorName, final String jsonData) { final var dataNode = buildDataNodeFromJson(dataspaceName, anchorName, ROOT_NODE_XPATH, jsonData); cpsDataPersistenceService.storeDataNode(dataspaceName, anchorName, dataNode); + notificationService.processDataUpdatedEvent(dataspaceName, anchorName); } @Override @@ -64,6 +70,7 @@ public class CpsDataServiceImpl implements CpsDataService { final String jsonData) { final var dataNode = buildDataNodeFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData); cpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, parentNodeXpath, dataNode); + notificationService.processDataUpdatedEvent(dataspaceName, anchorName); } @Override @@ -72,6 +79,7 @@ public class CpsDataServiceImpl implements CpsDataService { final Collection<DataNode> dataNodesCollection = buildDataNodeCollectionFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData); cpsDataPersistenceService.addListDataNodes(dataspaceName, anchorName, parentNodeXpath, dataNodesCollection); + notificationService.processDataUpdatedEvent(dataspaceName, anchorName); } @Override @@ -86,6 +94,7 @@ public class CpsDataServiceImpl implements CpsDataService { final var dataNode = buildDataNodeFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData); cpsDataPersistenceService .updateDataLeaves(dataspaceName, anchorName, dataNode.getXpath(), dataNode.getLeaves()); + notificationService.processDataUpdatedEvent(dataspaceName, anchorName); } @Override @@ -93,6 +102,7 @@ public class CpsDataServiceImpl implements CpsDataService { final String jsonData) { final var dataNode = buildDataNodeFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData); cpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName, dataNode); + notificationService.processDataUpdatedEvent(dataspaceName, anchorName); } @Override @@ -101,6 +111,7 @@ public class CpsDataServiceImpl implements CpsDataService { final Collection<DataNode> dataNodes = buildDataNodeCollectionFromJson(dataspaceName, anchorName, parentNodeXpath, jsonData); cpsDataPersistenceService.replaceListDataNodes(dataspaceName, anchorName, parentNodeXpath, dataNodes); + notificationService.processDataUpdatedEvent(dataspaceName, anchorName); } private DataNode buildDataNodeFromJson(final String dataspaceName, final String anchorName, diff --git a/cps-service/src/main/java/org/onap/cps/notification/CpsDataUpdatedEventFactory.java b/cps-service/src/main/java/org/onap/cps/notification/CpsDataUpdatedEventFactory.java new file mode 100644 index 0000000000..ae5e9ba290 --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/notification/CpsDataUpdatedEventFactory.java @@ -0,0 +1,96 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Bell Canada. All rights reserved. + * ================================================================================ + * 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.notification; + +import java.net.URI; +import java.net.URISyntaxException; +import java.time.OffsetDateTime; +import java.time.format.DateTimeFormatter; +import java.util.UUID; +import org.onap.cps.api.CpsAdminService; +import org.onap.cps.api.CpsDataService; +import org.onap.cps.event.model.Content; +import org.onap.cps.event.model.CpsDataUpdatedEvent; +import org.onap.cps.event.model.CpsDataUpdatedEvent.Schema; +import org.onap.cps.event.model.Data; +import org.onap.cps.spi.FetchDescendantsOption; +import org.onap.cps.spi.model.Anchor; +import org.onap.cps.spi.model.DataNode; +import org.onap.cps.utils.DataMapUtils; +import org.springframework.stereotype.Component; + +@Component +class CpsDataUpdatedEventFactory { + + private static final URI EVENT_SOURCE; + private static final String EVENT_TYPE = "org.onap.cps.data-updated-event"; + private static final DateTimeFormatter dateTimeFormatter = + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); + + static { + try { + EVENT_SOURCE = new URI("urn:cps:org.onap.cps"); + } catch (final URISyntaxException e) { + // As it is fixed string, I don't expect to see this error + throw new IllegalArgumentException(e); + } + } + + private CpsDataService cpsDataService; + private CpsAdminService cpsAdminService; + + public CpsDataUpdatedEventFactory(final CpsDataService cpsDataService, final CpsAdminService cpsAdminService) { + this.cpsDataService = cpsDataService; + this.cpsAdminService = cpsAdminService; + } + + CpsDataUpdatedEvent createCpsDataUpdatedEvent(final String dataspaceName, final String anchorName) { + final var dataNode = cpsDataService + .getDataNode(dataspaceName, anchorName, "/", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS); + final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName); + return toCpsDataUpdatedEvent(anchor, dataNode); + } + + private CpsDataUpdatedEvent toCpsDataUpdatedEvent(final Anchor anchor, final DataNode dataNode) { + final var cpsDataUpdatedEvent = new CpsDataUpdatedEvent(); + cpsDataUpdatedEvent.withContent(createContent(anchor, dataNode)); + cpsDataUpdatedEvent.withId(UUID.randomUUID().toString()); + cpsDataUpdatedEvent.withSchema(Schema.URN_CPS_ORG_ONAP_CPS_DATA_UPDATED_EVENT_SCHEMA_1_1_0_SNAPSHOT); + cpsDataUpdatedEvent.withSource(EVENT_SOURCE); + cpsDataUpdatedEvent.withType(EVENT_TYPE); + return cpsDataUpdatedEvent; + } + + private Data createData(final DataNode dataNode) { + final var data = new Data(); + DataMapUtils.toDataMap(dataNode).forEach((key, value) -> data.setAdditionalProperty(key, value)); + return data; + } + + private Content createContent(final Anchor anchor, final DataNode dataNode) { + final var content = new Content(); + content.withAnchorName(anchor.getName()); + content.withDataspaceName(anchor.getDataspaceName()); + content.withSchemaSetName(anchor.getSchemaSetName()); + content.withData(createData(dataNode)); + content.withObservedTimestamp(dateTimeFormatter.format(OffsetDateTime.now())); + return content; + } +} diff --git a/cps-service/src/main/java/org/onap/cps/notification/KafkaProducerListener.java b/cps-service/src/main/java/org/onap/cps/notification/KafkaProducerListener.java new file mode 100644 index 0000000000..f4b68c0699 --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/notification/KafkaProducerListener.java @@ -0,0 +1,52 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Bell Canada. All rights reserved. + * ================================================================================ + * 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.notification; + +import lombok.extern.slf4j.Slf4j; +import org.apache.kafka.clients.producer.ProducerRecord; +import org.apache.kafka.clients.producer.RecordMetadata; +import org.springframework.kafka.support.ProducerListener; +import org.springframework.stereotype.Component; + +@Slf4j +@Component +public class KafkaProducerListener<K, V> implements ProducerListener<K, V> { + + private NotificationErrorHandler notificationErrorHandler; + + public KafkaProducerListener(final NotificationErrorHandler notificationErrorHandler) { + this.notificationErrorHandler = notificationErrorHandler; + } + + @Override + public void onSuccess(final ProducerRecord<K, V> producerRecord, final RecordMetadata recordMetadata) { + log.debug("Message sent to event-bus topic :'{}' with body : {} ", producerRecord.topic(), + producerRecord.value()); + } + + @Override + public void onError(final ProducerRecord<K, V> producerRecord, + final RecordMetadata recordMetadata, + final Exception exception) { + notificationErrorHandler.onException("Failed to send message to message bus", + exception, producerRecord, recordMetadata); + } + +} diff --git a/cps-service/src/main/java/org/onap/cps/notification/NotificationErrorHandler.java b/cps-service/src/main/java/org/onap/cps/notification/NotificationErrorHandler.java new file mode 100644 index 0000000000..eef028d5f3 --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/notification/NotificationErrorHandler.java @@ -0,0 +1,40 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Bell Canada. All rights reserved. + * ================================================================================ + * 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.notification; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +@Component +@Slf4j +public class NotificationErrorHandler { + + void onException(final Exception exception, final Object... context) { + onException("Failed to process", exception, context); + } + + void onException(final String message, final Exception exception, final Object... context) { + log.error("{} \n Error cause: {} \n Error context: {}", + message, + exception.getCause() != null ? exception.getCause().toString() : exception.getMessage(), + context, + exception); + } +} diff --git a/cps-service/src/main/java/org/onap/cps/notification/NotificationPublisher.java b/cps-service/src/main/java/org/onap/cps/notification/NotificationPublisher.java new file mode 100644 index 0000000000..1ab032b57c --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/notification/NotificationPublisher.java @@ -0,0 +1,65 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Bell Canada. All rights reserved. + * ================================================================================ + * 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.notification; + +import lombok.extern.slf4j.Slf4j; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.onap.cps.event.model.CpsDataUpdatedEvent; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.kafka.core.KafkaTemplate; +import org.springframework.stereotype.Component; + +@Component +@Slf4j +public class NotificationPublisher { + + private KafkaTemplate<String, CpsDataUpdatedEvent> kafkaTemplate; + private String topicName; + + /** + * Create an instance of Notification Publisher. + * + * @param kafkaTemplate kafkaTemplate is send event using kafka + * @param topicName topic, to which cpsDataUpdatedEvent is sent, is provided by setting + * 'notification.data-updated.topic' in the application properties + */ + @Autowired + public NotificationPublisher( + final KafkaTemplate<String, CpsDataUpdatedEvent> kafkaTemplate, + final @Value("${notification.data-updated.topic}") String topicName) { + this.kafkaTemplate = kafkaTemplate; + this.topicName = topicName; + } + + /** + * Send event to Kafka with correct message key. + * + * @param cpsDataUpdatedEvent event to be sent to kafka + */ + void sendNotification(@NonNull final CpsDataUpdatedEvent cpsDataUpdatedEvent) { + final var messageKey = cpsDataUpdatedEvent.getContent().getDataspaceName() + "," + + cpsDataUpdatedEvent.getContent().getAnchorName(); + log.debug("Data Updated event is being sent with messageKey: '{}' & body : {} ", + messageKey, cpsDataUpdatedEvent); + kafkaTemplate.send(topicName, messageKey, cpsDataUpdatedEvent); + } + +} diff --git a/cps-service/src/main/java/org/onap/cps/notification/NotificationService.java b/cps-service/src/main/java/org/onap/cps/notification/NotificationService.java new file mode 100644 index 0000000000..e97e8a3d8c --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/notification/NotificationService.java @@ -0,0 +1,89 @@ + +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Bell Canada. All rights reserved. + * ================================================================================ + * 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.notification; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +@Service +@Slf4j +public class NotificationService { + + private boolean dataUpdatedEventNotificationEnabled; + private NotificationPublisher notificationPublisher; + private CpsDataUpdatedEventFactory cpsDataUpdatedEventFactory; + private NotificationErrorHandler notificationErrorHandler; + + /** + * Create an instance of Notification Subscriber. + * + * @param dataUpdatedEventNotificationEnabled notification can be enabled by setting + * 'notification.data-updated.enabled=true' in application properties + * @param notificationPublisher notification Publisher + * @param cpsDataUpdatedEventFactory to create CPSDataUpdatedEvent + * @param notificationErrorHandler error handler + */ + @Autowired + public NotificationService( + @Value("${notification.data-updated.enabled}") final boolean dataUpdatedEventNotificationEnabled, + final NotificationPublisher notificationPublisher, + final CpsDataUpdatedEventFactory cpsDataUpdatedEventFactory, + final NotificationErrorHandler notificationErrorHandler) { + this.dataUpdatedEventNotificationEnabled = dataUpdatedEventNotificationEnabled; + this.notificationPublisher = notificationPublisher; + this.cpsDataUpdatedEventFactory = cpsDataUpdatedEventFactory; + this.notificationErrorHandler = notificationErrorHandler; + } + + /** + * Process Data Updated Event and publishes the notification. + * + * @param dataspaceName dataspace name + * @param anchorName anchor name + */ + public void processDataUpdatedEvent(final String dataspaceName, final String anchorName) { + log.debug("process data updated event for dataspace '{}' & anchor '{}'", dataspaceName, anchorName); + try { + if (shouldSendNotification()) { + final var cpsDataUpdatedEvent = + cpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(dataspaceName, anchorName); + log.debug("data updated event to be published {}", cpsDataUpdatedEvent); + notificationPublisher.sendNotification(cpsDataUpdatedEvent); + } + } catch (final Exception exception) { + /* All the exceptions are handled to not to propagate it to caller. + CPS operation should not fail if sending event fails for any reason. + */ + notificationErrorHandler.onException("Failed to process cps-data-updated-event.", + exception, dataspaceName, anchorName); + } + } + + /* + Add more complex rules based on dataspace and anchor later + */ + private boolean shouldSendNotification() { + return dataUpdatedEventNotificationEnabled; + } + +} diff --git a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDataServiceImplSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDataServiceImplSpec.groovy index 8001d6a9fe..bf94401c08 100644 --- a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDataServiceImplSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDataServiceImplSpec.groovy @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2021 Nordix Foundation * Modifications Copyright (C) 2021 Pantheon.tech + * Modifications Copyright (C) 2021 Bell Canada. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +24,7 @@ package org.onap.cps.api.impl import org.onap.cps.TestUtils import org.onap.cps.api.CpsAdminService import org.onap.cps.api.CpsModuleService +import org.onap.cps.notification.NotificationService import org.onap.cps.spi.CpsDataPersistenceService import org.onap.cps.spi.FetchDescendantsOption import org.onap.cps.spi.exceptions.DataValidationException @@ -37,6 +39,7 @@ class CpsDataServiceImplSpec extends Specification { def mockCpsAdminService = Mock(CpsAdminService) def mockCpsModuleService = Mock(CpsModuleService) def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache) + def mockNotificationService = Mock(NotificationService) def objectUnderTest = new CpsDataServiceImpl() @@ -45,6 +48,7 @@ class CpsDataServiceImplSpec extends Specification { objectUnderTest.cpsAdminService = mockCpsAdminService objectUnderTest.cpsModuleService = mockCpsModuleService objectUnderTest.yangTextSchemaSourceSetCache = mockYangTextSchemaSourceSetCache + objectUnderTest.notificationService = mockNotificationService } def dataspaceName = 'some dataspace' @@ -60,6 +64,8 @@ class CpsDataServiceImplSpec extends Specification { then: 'the persistence service method is invoked with correct parameters' 1 * mockCpsDataPersistenceService.storeDataNode(dataspaceName, anchorName, { dataNode -> dataNode.xpath == '/test-tree' }) + and: 'data updated event is sent to notification service' + 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName) } def 'Saving child data fragment under existing node.'() { @@ -71,6 +77,8 @@ class CpsDataServiceImplSpec extends Specification { then: 'the persistence service method is invoked with correct parameters' 1 * mockCpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, '/test-tree', { dataNode -> dataNode.xpath == '/test-tree/branch[@name=\'New\']' }) + and: 'data updated event is sent to notification service' + 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName) } def 'Saving list-node data fragment under existing node.'() { @@ -89,6 +97,8 @@ class CpsDataServiceImplSpec extends Specification { } } ) + and: 'data updated event is sent to notification service' + 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName) } def 'Saving empty list-node data fragment.'() { @@ -119,6 +129,8 @@ class CpsDataServiceImplSpec extends Specification { objectUnderTest.updateNodeLeaves(dataspaceName, anchorName, parentNodeXpath, jsonData) then: 'the persistence service method is invoked with correct parameters' 1 * mockCpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName, expectedNodeXpath, leaves) + and: 'data updated event is sent to notification service' + 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName) where: 'following parameters were used' scenario | parentNodeXpath | jsonData || expectedNodeXpath | leaves 'top level node' | '/' | '{"test-tree": {"branch": []}}' || '/test-tree' | Collections.emptyMap() @@ -146,6 +158,8 @@ class CpsDataServiceImplSpec extends Specification { then: 'the persistence service method is invoked with correct parameters' 1 * mockCpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName, { dataNode -> dataNode.xpath == expectedNodeXpath }) + and: 'data updated event is sent to notification service' + 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName) where: 'following parameters were used' scenario | parentNodeXpath | jsonData || expectedNodeXpath 'top level node' | '/' | '{"test-tree": {"branch": []}}' || '/test-tree' @@ -168,6 +182,8 @@ class CpsDataServiceImplSpec extends Specification { } } ) + and: 'data updated event is sent to notification service' + 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName) } def 'Replace with empty list-node data fragment.'() { diff --git a/cps-service/src/test/groovy/org/onap/cps/api/impl/E2ENetworkSliceSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/api/impl/E2ENetworkSliceSpec.groovy index aa54a9991e..b5ad42df01 100755 --- a/cps-service/src/test/groovy/org/onap/cps/api/impl/E2ENetworkSliceSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/api/impl/E2ENetworkSliceSpec.groovy @@ -23,6 +23,7 @@ package org.onap.cps.api.impl import org.onap.cps.TestUtils
import org.onap.cps.api.CpsAdminService
+import org.onap.cps.notification.NotificationService
import org.onap.cps.spi.CpsDataPersistenceService
import org.onap.cps.spi.CpsModulePersistenceService
import org.onap.cps.spi.model.Anchor
@@ -34,8 +35,9 @@ class E2ENetworkSliceSpec extends Specification { def mockModuleStoreService = Mock(CpsModulePersistenceService)
def mockDataStoreService = Mock(CpsDataPersistenceService)
def mockCpsAdminService = Mock(CpsAdminService)
+ def mockNotificationService = Mock(NotificationService)
def cpsModuleServiceImpl = new CpsModuleServiceImpl()
- def cpsDataServiceImple = new CpsDataServiceImpl()
+ def cpsDataServiceImpl = new CpsDataServiceImpl()
def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache)
def dataspaceName = 'someDataspace'
@@ -43,9 +45,10 @@ class E2ENetworkSliceSpec extends Specification { def schemaSetName = 'someSchemaSet'
def setup() {
- cpsDataServiceImple.cpsDataPersistenceService = mockDataStoreService
- cpsDataServiceImple.cpsAdminService = mockCpsAdminService
- cpsDataServiceImple.yangTextSchemaSourceSetCache = mockYangTextSchemaSourceSetCache
+ cpsDataServiceImpl.cpsDataPersistenceService = mockDataStoreService
+ cpsDataServiceImpl.cpsAdminService = mockCpsAdminService
+ cpsDataServiceImpl.yangTextSchemaSourceSetCache = mockYangTextSchemaSourceSetCache
+ cpsDataServiceImpl.notificationService = mockNotificationService
cpsModuleServiceImpl.yangTextSchemaSourceSetCache = mockYangTextSchemaSourceSetCache
cpsModuleServiceImpl.cpsModulePersistenceService = mockModuleStoreService
}
@@ -88,7 +91,7 @@ class E2ENetworkSliceSpec extends Specification { YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap)
mockModuleStoreService.getYangSchemaResources(dataspaceName, schemaSetName) >> schemaContext
when: 'saveData method is invoked'
- cpsDataServiceImple.saveData(dataspaceName, anchorName, jsonData)
+ cpsDataServiceImpl.saveData(dataspaceName, anchorName, jsonData)
then: 'Parameters are validated and processing is delegated to persistence service'
1 * mockDataStoreService.storeDataNode('someDataspace', 'someAnchor', _) >>
{ args -> dataNodeStored = args[2]}
@@ -120,7 +123,7 @@ class E2ENetworkSliceSpec extends Specification { mockYangTextSchemaSourceSetCache.get('someDataspace', 'someSchemaSet') >> YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap)
mockModuleStoreService.getYangSchemaResources('someDataspace', 'someSchemaSet') >> schemaContext
when: 'saveData method is invoked'
- cpsDataServiceImple.saveData('someDataspace', 'someAnchor', jsonData)
+ cpsDataServiceImpl.saveData('someDataspace', 'someAnchor', jsonData)
then: 'parameters are validated and processing is delegated to persistence service'
1 * mockDataStoreService.storeDataNode('someDataspace', 'someAnchor', _) >>
{ args -> dataNodeStored = args[2]}
diff --git a/cps-service/src/test/groovy/org/onap/cps/notification/CpsDataUpdateEventFactorySpec.groovy b/cps-service/src/test/groovy/org/onap/cps/notification/CpsDataUpdateEventFactorySpec.groovy new file mode 100644 index 0000000000..aecc3f7ee0 --- /dev/null +++ b/cps-service/src/test/groovy/org/onap/cps/notification/CpsDataUpdateEventFactorySpec.groovy @@ -0,0 +1,86 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Bell Canada. All rights reserved. + * ================================================================================ + * 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.notification + +import org.onap.cps.api.CpsAdminService +import org.onap.cps.api.CpsDataService +import org.onap.cps.event.model.CpsDataUpdatedEvent +import org.onap.cps.event.model.Data +import org.onap.cps.spi.FetchDescendantsOption +import org.onap.cps.spi.model.Anchor +import org.onap.cps.spi.model.DataNodeBuilder +import org.springframework.util.StringUtils +import spock.lang.Specification + +import java.time.format.DateTimeFormatter + +class CpsDataUpdateEventFactorySpec extends Specification { + + def mockCpsDataService = Mock(CpsDataService) + def mockCpsAdminService = Mock(CpsAdminService) + + def objectUnderTest = new CpsDataUpdatedEventFactory(mockCpsDataService, mockCpsAdminService) + + def myDataspaceName = 'my-dataspace' + def myAnchorName = 'my-anchorname' + def mySchemasetName = 'my-schemaset-name' + def dateTimeFormat = 'yyyy-MM-dd\'T\'HH:mm:ss.SSSZ' + + def 'Create a CPS data updated event successfully.'() { + + given: 'cps admin service is able to return anchor details' + mockCpsAdminService.getAnchor(myDataspaceName, myAnchorName) >> + new Anchor(myAnchorName, myDataspaceName, mySchemasetName) + and: 'cps data service returns the data node details' + def xpath = '/' + def dataNode = new DataNodeBuilder().withXpath(xpath).withLeaves(['leafName': 'leafValue']).build() + mockCpsDataService.getDataNode( + myDataspaceName, myAnchorName, xpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode + + when: 'CPS data updated event is created' + def cpsDataUpdatedEvent = objectUnderTest.createCpsDataUpdatedEvent(myDataspaceName, myAnchorName) + + then: 'CPS data updated event is created with expected values' + with(cpsDataUpdatedEvent) { + type == 'org.onap.cps.data-updated-event' + source == new URI('urn:cps:org.onap.cps') + schema == CpsDataUpdatedEvent.Schema.URN_CPS_ORG_ONAP_CPS_DATA_UPDATED_EVENT_SCHEMA_1_1_0_SNAPSHOT + StringUtils.hasText(id) + content != null + } + with(cpsDataUpdatedEvent.content) { + assert isExpectedDateTimeFormat(observedTimestamp): "$observedTimestamp is not in $dateTimeFormat format" + anchorName == myAnchorName + dataspaceName == myDataspaceName + schemaSetName == mySchemasetName + data == new Data().withAdditionalProperty('leafName', 'leafValue') + } + } + + def isExpectedDateTimeFormat(String observedTimestamp) { + try { + DateTimeFormatter.ofPattern(dateTimeFormat).parse(observedTimestamp) + } catch (DateTimeParseException) { + return false + } + return true + } + +} diff --git a/cps-service/src/test/groovy/org/onap/cps/notification/KafkaPublisherSpecBase.groovy b/cps-service/src/test/groovy/org/onap/cps/notification/KafkaPublisherSpecBase.groovy new file mode 100644 index 0000000000..b60b38f054 --- /dev/null +++ b/cps-service/src/test/groovy/org/onap/cps/notification/KafkaPublisherSpecBase.groovy @@ -0,0 +1,93 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Bell Canada. All rights reserved. + * ================================================================================ + * 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.notification + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.kafka.config.TopicBuilder +import org.springframework.kafka.core.ConsumerFactory +import org.springframework.kafka.core.KafkaAdmin +import org.springframework.kafka.core.KafkaTemplate +import org.springframework.kafka.listener.ConcurrentMessageListenerContainer +import org.springframework.kafka.listener.ContainerProperties +import org.springframework.kafka.listener.MessageListener +import org.springframework.kafka.test.utils.ContainerTestUtils +import org.springframework.test.context.ContextConfiguration +import org.springframework.test.context.DynamicPropertyRegistry +import org.springframework.test.context.DynamicPropertySource +import spock.lang.Shared +import spock.lang.Specification + +@ContextConfiguration(classes = [KafkaAutoConfiguration, KafkaProducerListener, NotificationErrorHandler]) +@SpringBootTest +class KafkaPublisherSpecBase extends Specification { + + @Autowired + KafkaTemplate kafkaTemplate + + @Autowired + KafkaAdmin kafkaAdmin + + @Autowired + ConsumerFactory consumerFactory + + @Shared volatile topicCreated = false + @Shared consumedMessages = new ArrayList<>() + + def cpsEventTopic = 'cps-events' + + @DynamicPropertySource + static void registerKafkaProperties(DynamicPropertyRegistry registry) { + registry.add("spring.kafka.bootstrap-servers", KafkaTestContainerConfig::getBootstrapServers) + } + + def setup() { + // Kafka listener and topic should be created only once for a test-suite. + // We are also dependent on sprint context to achieve it, and can not execute it in setupSpec + if (!topicCreated) { + kafkaAdmin.createOrModifyTopics(TopicBuilder.name(cpsEventTopic).partitions(1).replicas(1).build()) + startListeningToTopic() + topicCreated = true + } + /* kafka message listener stores the messages to consumedMessages. + It is important to clear the list before each test case so that test cases can fetch the message from index '0'. + */ + consumedMessages.clear() + } + + def startListeningToTopic() { + ContainerProperties containerProperties = new ContainerProperties(cpsEventTopic) + containerProperties.setMessageListener([ + onMessage: { + record -> + consumedMessages.add(record.value()) + }] as MessageListener) + + ConcurrentMessageListenerContainer container = + new ConcurrentMessageListenerContainer<>( + consumerFactory, + containerProperties) + + container.start() + ContainerTestUtils.waitForAssignment(container, 1) + } + +} diff --git a/cps-service/src/test/groovy/org/onap/cps/notification/KafkaTestContainerConfig.groovy b/cps-service/src/test/groovy/org/onap/cps/notification/KafkaTestContainerConfig.groovy new file mode 100644 index 0000000000..5124a519a7 --- /dev/null +++ b/cps-service/src/test/groovy/org/onap/cps/notification/KafkaTestContainerConfig.groovy @@ -0,0 +1,49 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Bell Canada. All rights reserved. + * ================================================================================ + * 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.notification + +import org.testcontainers.containers.KafkaContainer +import org.testcontainers.utility.DockerImageName + +class KafkaTestContainerConfig { + + private static KafkaContainer kafkaContainer + + static { + getKafkaContainer() + } + + // Not the best performance but it is good enough for test case + private static synchronized KafkaContainer getKafkaContainer() { + if (kafkaContainer == null) { + kafkaContainer = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:6.1.1")) + .withEnv("KAFKA_AUTO_CREATE_TOPICS_ENABLE", "false") + kafkaContainer.start() + Runtime.getRuntime().addShutdownHook(new Thread(kafkaContainer::stop)) + } + return kafkaContainer + } + + static String getBootstrapServers() { + getKafkaContainer() + return kafkaContainer.getBootstrapServers() + } + +} diff --git a/cps-service/src/test/groovy/org/onap/cps/notification/NotificationPublisherSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/notification/NotificationPublisherSpec.groovy new file mode 100644 index 0000000000..f215c6dc09 --- /dev/null +++ b/cps-service/src/test/groovy/org/onap/cps/notification/NotificationPublisherSpec.groovy @@ -0,0 +1,91 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Bell Canada. All rights reserved. + * ================================================================================ + * 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.notification + +import org.apache.kafka.clients.producer.ProducerRecord +import org.apache.kafka.clients.producer.RecordMetadata +import org.onap.cps.event.model.Content +import org.onap.cps.event.model.CpsDataUpdatedEvent +import org.spockframework.spring.SpringBean +import org.springframework.kafka.KafkaException +import org.springframework.kafka.core.KafkaTemplate +import spock.util.concurrent.PollingConditions + +class NotificationPublisherSpec extends KafkaPublisherSpecBase { + + @SpringBean + NotificationErrorHandler spyNotificationErrorHandler = Spy(new NotificationErrorHandler()) + + @SpringBean + KafkaProducerListener spyKafkaProducerListener = Spy(new KafkaProducerListener<>(spyNotificationErrorHandler)) + + KafkaTemplate spyKafkaTemplate + NotificationPublisher objectUnderTest + + def myAnchorName = 'my-anchor' + def myDataspaceName = 'my-dataspace' + + def cpsDataUpdatedEvent = new CpsDataUpdatedEvent() + .withContent(new Content() + .withDataspaceName(myDataspaceName) + .withAnchorName(myAnchorName)) + + def setup() { + spyKafkaTemplate = Spy(kafkaTemplate) + objectUnderTest = new NotificationPublisher(spyKafkaTemplate, cpsEventTopic); + } + + def 'Sending event to message bus with correct message Key.'() { + + when: 'event is sent to publisher' + objectUnderTest.sendNotification(cpsDataUpdatedEvent) + kafkaTemplate.flush() + + then: 'event is sent to correct topic with the expected messageKey' + interaction { + def messageKey = myDataspaceName + "," + myAnchorName + 1 * spyKafkaTemplate.send(cpsEventTopic, messageKey, cpsDataUpdatedEvent) + } + and: 'received a successful response' + 1 * spyKafkaProducerListener.onSuccess(_ as ProducerRecord, _) + and: 'kafka consumer returns expected message' + def conditions = new PollingConditions(timeout: 60, initialDelay: 0, factor: 1) + conditions.eventually { + assert cpsDataUpdatedEvent == consumedMessages.get(0) + } + } + + def 'Handling of async errors from message bus.'() { + given: 'topic does not exist' + objectUnderTest.topicName = 'non-existing-topic' + + when: 'message to sent to a non-existing topic' + objectUnderTest.sendNotification(cpsDataUpdatedEvent) + kafkaTemplate.flush() + + then: 'error is thrown' + thrown KafkaException + and: 'error handler is called with exception details' + 1 * spyKafkaProducerListener.onError(_ as ProducerRecord, _, _ as Exception) + 1 * spyNotificationErrorHandler.onException(_ as String, _ as Exception, + _ as ProducerRecord, _) + } + +} diff --git a/cps-service/src/test/groovy/org/onap/cps/notification/NotificationServiceSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/notification/NotificationServiceSpec.groovy new file mode 100644 index 0000000000..a74279548e --- /dev/null +++ b/cps-service/src/test/groovy/org/onap/cps/notification/NotificationServiceSpec.groovy @@ -0,0 +1,78 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Bell Canada. All rights reserved. + * ================================================================================ + * 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.notification + +import org.onap.cps.event.model.CpsDataUpdatedEvent +import spock.lang.Specification + +class NotificationServiceSpec extends Specification { + + def mockNotificationPublisher = Mock(NotificationPublisher) + def spyNotificationErrorHandler = Spy(new NotificationErrorHandler()) + def mockCpsDataUpdatedEventFactory = Mock(CpsDataUpdatedEventFactory) + + def objectUnderTest = new NotificationService(true, mockNotificationPublisher, + mockCpsDataUpdatedEventFactory, spyNotificationErrorHandler) + + def myDataspaceName = 'my-dataspace' + def myAnchorName = 'my-anchorname' + + def 'Skip sending notification when disabled.'() { + + given: 'notification is disabled' + objectUnderTest.dataUpdatedEventNotificationEnabled = false + + when: 'dataUpdatedEvent is received' + objectUnderTest.processDataUpdatedEvent(myDataspaceName, myAnchorName) + + then: 'the notification is not sent' + 0 * mockNotificationPublisher.sendNotification(_) + } + + def 'Send notification when enabled.'() { + + given: 'notification is enabled' + objectUnderTest.dataUpdatedEventNotificationEnabled = true + and: 'event factory can create event successfully' + def cpsDataUpdatedEvent = new CpsDataUpdatedEvent() + mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(myDataspaceName, myAnchorName) >> cpsDataUpdatedEvent + + when: 'dataUpdatedEvent is received' + objectUnderTest.processDataUpdatedEvent(myDataspaceName, myAnchorName) + + then: 'notification is sent with correct event' + 1 * mockNotificationPublisher.sendNotification(cpsDataUpdatedEvent) + } + + def 'Error handling in notification service.'(){ + given: 'event factory can not create event successfully' + mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(myDataspaceName, myAnchorName) >> + { throw new Exception("Could not create event") } + + when: 'event is sent for processing' + objectUnderTest.processDataUpdatedEvent(myDataspaceName, myAnchorName) + + then: 'error is handled and not thrown to caller' + notThrown Exception + 1 * spyNotificationErrorHandler.onException(_,_,_,_) + + } + +} diff --git a/cps-service/src/test/resources/application.yml b/cps-service/src/test/resources/application.yml new file mode 100644 index 0000000000..c934486fc9 --- /dev/null +++ b/cps-service/src/test/resources/application.yml @@ -0,0 +1,41 @@ +# ============LICENSE_START======================================================= +# Copyright (C) 2021 Bell Canada. All rights reserved. +# ================================================================================ +# 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========================================================= + +notification: + data-updated: + topic: cps-event + enabled: true + +spring: + kafka: + properties: + request.timeout.ms: 5000 + retries: 1 + max.block.ms: 10000 + producer: + value-serializer: org.springframework.kafka.support.serializer.JsonSerializer + cliend-id: cps + consumer: + group-id: cps-test + auto-offset-reset: earliest + value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer + properties: + spring.json.value.default.type: org.onap.cps.event.model.CpsDataUpdatedEvent + +logging: + level: + org.apache.kafka: ERROR diff --git a/csit/README.md b/csit/README.md index 2e8b5a4286..baa636bab4 100644 --- a/csit/README.md +++ b/csit/README.md @@ -1,3 +1,23 @@ +<!-- + ============LICENSE_START======================================================= + Copyright (C) 2021 Pantheon.tech. + ================================================================================ + 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========================================================= +--> + ## Continuous System and Integration Testing (CSIT) for CPS The directory structure: diff --git a/csit/tests/actuator/actuator.robot b/csit/tests/actuator/actuator.robot index 1253dffbf3..3a0493d686 100644 --- a/csit/tests/actuator/actuator.robot +++ b/csit/tests/actuator/actuator.robot @@ -1,3 +1,21 @@ +# ============LICENSE_START======================================================= +# Copyright (c) 2021 Pantheon.tech. +# ================================================================================ +# 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========================================================= + *** Settings *** Documentation CPS - Actuator endpoints @@ -16,4 +34,4 @@ Test Liveness Probe Endpoint Test Readiness Probe Endpoint ${response}= GET On Session CPS_HOST /manage/health/readiness expected_status=200 - Should Be Equal As Strings ${response.json()['status']} UP
\ No newline at end of file + Should Be Equal As Strings ${response.json()['status']} UP diff --git a/csit/tests/cps-admin/cps-admin.robot b/csit/tests/cps-admin/cps-admin.robot index 93818070ba..7e944ec6f7 100644 --- a/csit/tests/cps-admin/cps-admin.robot +++ b/csit/tests/cps-admin/cps-admin.robot @@ -1,3 +1,21 @@ +# ============LICENSE_START======================================================= +# Copyright (c) 2021 Pantheon.tech. +# ================================================================================ +# 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========================================================= + *** Settings *** Documentation CPS Core - Admin REST API diff --git a/csit/tests/cps-data/cps-data.robot b/csit/tests/cps-data/cps-data.robot index ff1e8d0480..9fd8451d28 100644 --- a/csit/tests/cps-data/cps-data.robot +++ b/csit/tests/cps-data/cps-data.robot @@ -1,3 +1,21 @@ +# ============LICENSE_START======================================================= +# Copyright (c) 2021 Pantheon.tech. +# ================================================================================ +# 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========================================================= + *** Settings *** Documentation CPS Core - Data REST API diff --git a/docker-compose/README.md b/docker-compose/README.md index c172276ebe..3fe7f9940e 100644 --- a/docker-compose/README.md +++ b/docker-compose/README.md @@ -50,8 +50,13 @@ mvn clean install -P<docker-profile> `docker-compose/docker-compose.yml` file is provided to be run with `docker-compose` tool and images previously built. It starts both Postgres database and CPS services. -1. Edit `docker-compose.yml` and uncomment desired service to be deployed, by default `cps-and-ncmp` - is enabled. You can comment it and uncomment `cps-standalone` or `ncmp-standalone`. +1. Edit `docker-compose.yml` + 1. uncomment desired service to be deployed, by default `cps-and-ncmp` is enabled. You can comment it and uncomment `cps-standalone` or `ncmp-standalone`. + 2. To send data-updated events to kafka, + * uncomment the `zookeeper` and `kafka` services. + * uncomment environment variables + * `notification.data-updated.enabled: 'true'` + * `KAFKA_BOOTSTRAP_SERVER: kafka:9092` 2. Execute following command from `docker-compose` folder: Use one of the below version type that has been generated in the local system's docker image list after the build. @@ -59,7 +64,7 @@ Use one of the below version type that has been generated in the local system's VERSION=latest DB_USERNAME=cps DB_PASSWORD=cps docker-compose up -d or VERSION=<version> DB_USERNAME=cps DB_PASSWORD=cps docker-compose up -d -``` +``` ## Running or debugging Java built code diff --git a/docker-compose/docker-compose.yml b/docker-compose/docker-compose.yml index 2878462a36..51bb162b20 100755 --- a/docker-compose/docker-compose.yml +++ b/docker-compose/docker-compose.yml @@ -30,6 +30,8 @@ services: # DB_HOST: dbpostgresql # DB_USERNAME: ${DB_USERNAME:-cps} # DB_PASSWORD: ${DB_PASSWORD:-cps} + # #KAFKA_BOOTSTRAP_SERVER: kafka:9092 + # #notification.data-updated.enabled: 'true' # restart: unless-stopped # depends_on: # - dbpostgresql @@ -45,6 +47,8 @@ services: # DB_HOST: dbpostgresql # DB_USERNAME: ${DB_USERNAME:-cps} # DB_PASSWORD: ${DB_PASSWORD:-cps} + # #KAFKA_BOOTSTRAP_SERVER: kafka:9092 + # #notification.data-updated.enabled: 'true' # restart: unless-stopped # depends_on: # - dbpostgresql @@ -60,6 +64,8 @@ services: DB_HOST: dbpostgresql DB_USERNAME: ${DB_USERNAME:-cps} DB_PASSWORD: ${DB_PASSWORD:-cps} + #KAFKA_BOOTSTRAP_SERVER: kafka:9092 + #notification.data-updated.enabled: 'true' restart: unless-stopped depends_on: - dbpostgresql @@ -73,3 +79,25 @@ services: POSTGRES_DB: cpsdb POSTGRES_USER: ${DB_USERNAME:-cps} POSTGRES_PASSWORD: ${DB_PASSWORD:-cps} + +# zookeeper: +# image: confluentinc/cp-zookeeper:6.1.1 +# environment: +# ZOOKEEPER_CLIENT_PORT: 2181 +# ZOOKEEPER_TICK_TIME: 2000 +# ports: +# - 22181:2181 +# +# kafka: +# image: confluentinc/cp-kafka:6.1.1 +# depends_on: +# - zookeeper +# ports: +# - 29092:29092 +# environment: +# KAFKA_BROKER_ID: 1 +# KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181 +# KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092,PLAINTEXT_HOST://localhost:29092 +# KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT +# KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT +# KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1 |