diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-notification-lib')
62 files changed, 3118 insertions, 0 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/pom.xml b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/pom.xml new file mode 100644 index 0000000000..8e1c293e16 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/pom.xml @@ -0,0 +1,20 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <name>openecomp-sdc-notification-api</name> + <artifactId>openecomp-sdc-notification-api</artifactId> + <parent> + <groupId>org.openecomp.sdc</groupId> + <artifactId>openecomp-sdc-lib</artifactId> + <version>1.2.0-SNAPSHOT</version> + <relativePath>../..</relativePath> + </parent> + <dependencies> + <dependency> + <groupId>org.openecomp.sdc.core</groupId> + <artifactId>openecomp-nosqldb-core</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> +</project> diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/destinationprovider/DestinationProvider.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/destinationprovider/DestinationProvider.java new file mode 100644 index 0000000000..0c6dfc8580 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/destinationprovider/DestinationProvider.java @@ -0,0 +1,11 @@ +package org.openecomp.sdc.destinationprovider; + +import java.util.List; + +/** + * @author avrahamg + * @since July 09, 2017 + */ +public interface DestinationProvider { + List<String> getSubscribers(); +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/dao/LastNotificationDao.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/dao/LastNotificationDao.java new file mode 100644 index 0000000000..f0bf7e8874 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/dao/LastNotificationDao.java @@ -0,0 +1,34 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.dao; + +import org.openecomp.core.dao.BaseDao; +import org.openecomp.sdc.notification.dao.types.LastSeenNotificationEntity; + +import java.util.UUID; + +public interface LastNotificationDao extends BaseDao<LastSeenNotificationEntity> { + + UUID getOwnerLastEventId(String ownerId); + + void persistOwnerLastEventId(String ownerId, UUID eventId); + +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/dao/NotificationsDao.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/dao/NotificationsDao.java new file mode 100644 index 0000000000..6542d70158 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/dao/NotificationsDao.java @@ -0,0 +1,49 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.dao; + +import org.openecomp.core.dao.BaseDao; +import org.openecomp.sdc.notification.dao.types.NotificationEntity; +import org.openecomp.sdc.notification.dtos.NotificationsStatus; + +import java.util.Collection; +import java.util.List; +import java.util.UUID; + + +public interface NotificationsDao extends BaseDao<NotificationEntity> { + + int DEFAULT_LIMIT_OF_RESULTS_FOR_OWNER_NOTIFICATIONS = 1000; + + List<NotificationEntity> getNotificationsByOwnerId(String ownerId, int limit); + + List<NotificationEntity> getNewNotificationsByOwnerId(String ownerId, UUID eventId); + + List<NotificationEntity> getNewNotificationsByOwnerId(String ownerId, UUID eventId, int limit); + + NotificationsStatus getNotificationsStatus(String ownerId, UUID lastSeenNotification, int numOfRecordsToReturn); + + NotificationsStatus getNotificationsStatus(String ownerId, UUID lastSeenNotification, int numOfRecordsToReturn, UUID prevLastNotification); + + void markNotificationAsRead(String ownerId, Collection<UUID> eventIds); + + void createBatch(List<NotificationEntity> notificationEntities); +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/dao/SubscribersDao.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/dao/SubscribersDao.java new file mode 100644 index 0000000000..3daf165efc --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/dao/SubscribersDao.java @@ -0,0 +1,37 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.dao; + +import org.openecomp.core.dao.BaseDao; +import org.openecomp.sdc.notification.dao.types.SubscribersEntity; + +import java.util.Set; + + +public interface SubscribersDao extends BaseDao<SubscribersEntity> { + + void subscribe(String ownerId, String entityId); + + void unsubscribe(String ownerId, String entityId); + + Set<String> getSubscribers(String entityId); + +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/dao/types/LastSeenNotificationEntity.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/dao/types/LastSeenNotificationEntity.java new file mode 100644 index 0000000000..d6e717d1b3 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/dao/types/LastSeenNotificationEntity.java @@ -0,0 +1,103 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.dao.types; + +import com.datastax.driver.mapping.annotations.Column; +import com.datastax.driver.mapping.annotations.PartitionKey; +import com.datastax.driver.mapping.annotations.Table; + +import java.util.Objects; +import java.util.UUID; + +@Table(keyspace = "dox", name = "last_notification") +public class LastSeenNotificationEntity { + public static final String ENTITY_TYPE = "Event Notification"; + + @PartitionKey + @Column(name = "owner_id") + private String ownerId; + + @Column(name = "event_id") + private UUID lastEventId; + + public LastSeenNotificationEntity() { + } + + /** + * Instantiates a new Notification entity. + * + * @param ownerId the owner id + * @param lastEventId the last event id + */ + public LastSeenNotificationEntity(String ownerId, UUID lastEventId) { + this.ownerId = ownerId; + this.lastEventId = lastEventId; + } + + public String getOwnerId() { + return ownerId; + } + + public void setOwnerId(String ownerId) { + this.ownerId = ownerId; + } + + public UUID getLastEventId() { + return lastEventId; + } + + public void setLastEventId(UUID lastEventId) { + this.lastEventId = lastEventId; + } + + @Override + public boolean equals(Object other) { + if (Objects.equals(this, other)) { + return true; + } + + if (Objects.equals(getClass(), other.getClass())) { + return false; + } + + LastSeenNotificationEntity that = (LastSeenNotificationEntity) other; + + if (Objects.equals(ownerId, that.ownerId)) { + return false; + } + + return !Objects.equals(lastEventId, that.lastEventId); + } + + @Override + public int hashCode() { + int result = ownerId != null ? ownerId.hashCode() : 0; + result = 31 * result + (lastEventId != null ? lastEventId.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "LastSeenNotificationEntity {" + + "ownerId='" + ownerId + '\'' + + '}'; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/dao/types/NotificationEntity.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/dao/types/NotificationEntity.java new file mode 100644 index 0000000000..52377077cf --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/dao/types/NotificationEntity.java @@ -0,0 +1,188 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.dao.types; + +import com.datastax.driver.mapping.annotations.ClusteringColumn; +import com.datastax.driver.mapping.annotations.Column; +import com.datastax.driver.mapping.annotations.PartitionKey; +import com.datastax.driver.mapping.annotations.Table; + +import java.util.Objects; +import java.util.UUID; + +@Table(keyspace = "dox", name = "notifications") +public class NotificationEntity { + + public static final String ENTITY_TYPE = "Event Notification"; + + @PartitionKey + @Column(name = "owner_id") + private String ownerId; + + @Column(name = "read") + private boolean read; + + @ClusteringColumn + @Column(name = "event_id") + private UUID eventId; + + @Column(name = "event_type") + private String eventType; + + @Column(name = "event_attributes") + private String eventAttributes; + + @Column(name = "originator_id") + private String originatorId; + + public NotificationEntity() { + } + + public NotificationEntity(String ownerId) { + this.ownerId = ownerId; + } + + /** + * Instantiates a new Notification entity. + * + * @param ownerId the owner id + * @param eventId the event id + * @param eventType the event type + * @param originatorId the originator id + */ + public NotificationEntity(String ownerId, UUID eventId, String eventType, String originatorId, boolean read, String eventAttributes) { + this.ownerId = ownerId; + this.read = read; + this.eventId = eventId; + this.eventType = eventType; + this.originatorId = originatorId; + this.eventAttributes = eventAttributes; + } + + public NotificationEntity(String ownerId, UUID eventId, String eventType, String originatorId) { + this(ownerId, eventId, eventType, originatorId, false, null); + } + + public NotificationEntity(String ownerId, UUID eventId) { + this(ownerId, eventId, null, null); + } + + public String getOwnerId() { + return ownerId; + } + + public void setOwnerId(String ownerId) { + this.ownerId = ownerId; + } + + public boolean isRead() { + return read; + } + + public void setRead(boolean read) { + this.read = read; + } + + public UUID getEventId() { + return eventId; + } + + public void setEventId(UUID eventId) { + this.eventId = eventId; + } + + public String getEventType() { + return eventType; + } + + public void setEventType(String eventType) { + this.eventType = eventType; + } + + public String getEventAttributes() { + return eventAttributes; + } + + public void setEventAttributes(String eventAttributes) { + this.eventAttributes = eventAttributes; + } + + public String getOriginatorId() { + return originatorId; + } + + public void setOriginatorId(String originatorId) { + this.originatorId = originatorId; + } + + @Override + public boolean equals(Object other) { + if (Objects.equals(this, other)) { + return true; + } + + if (Objects.equals(getClass(), other.getClass())) { + return false; + } + + NotificationEntity that = (NotificationEntity) other; + + if (Objects.equals(ownerId, that.ownerId)) { + return false; + } + if (read != that.read) { + return false; + } + if (Objects.equals(eventId, that.eventId)) { + return false; + } + if (Objects.equals(eventType, that.eventType)) { + return false; + } + if (Objects.equals(eventAttributes, that.eventAttributes)) { + return false; + } + if (Objects.equals(originatorId, that.originatorId)) { + return false; + } + + return true; + } + + @Override + public int hashCode() { + int result = ownerId != null ? ownerId.hashCode() : 0; + result = 31 * result + (eventId != null ? eventId.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "NotificationEntity {" + + "ownerId='" + ownerId + '\'' + + ", state='" + (read ? "Read" : "Noread") + '\'' + + ", originatorId='" + originatorId + '\'' + + ", eventId='" + eventId + '\'' + + ", eventType='" + eventType + '\'' + + ", eventAttributes='" + eventAttributes + '\'' + + '}'; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/dao/types/SubscribersEntity.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/dao/types/SubscribersEntity.java new file mode 100644 index 0000000000..d858cbd9eb --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/dao/types/SubscribersEntity.java @@ -0,0 +1,68 @@ +package org.openecomp.sdc.notification.dao.types; + +import com.datastax.driver.mapping.annotations.Column; +import com.datastax.driver.mapping.annotations.PartitionKey; +import com.datastax.driver.mapping.annotations.Table; + +import java.util.Objects; +import java.util.Set; + +import static java.util.Objects.hash; + +@Table(keyspace = "dox", name = "notification_subscribers") +public class SubscribersEntity { + + @PartitionKey + @Column(name = "entity_id") + private String entityId; + + @Column(name = "subscribers") + private Set<String> subscribers; + + + public SubscribersEntity() { + } + + public SubscribersEntity(String entityId, Set<String> subscribers) { + this.entityId = entityId; + this.subscribers = subscribers; + } + + public String getEntityId() { + return entityId; + } + + public void setEntityId(String entityId) { + this.entityId = entityId; + } + + public Set<String> getSubscribers() { + return subscribers; + } + + public void setSubscribers(Set<String> subscribers) { + this.subscribers = subscribers; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + SubscribersEntity that = (SubscribersEntity) o; + return Objects.equals(entityId, that.entityId) && + Objects.equals(subscribers, that.subscribers); + } + + @Override + public int hashCode() { + return hash(entityId, subscribers); + } + + @Override + public String toString() { + return "SubscribersEntity{" + + "entityId='" + entityId + '\'' + + ", subscribers=" + subscribers + + '}'; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/dtos/Event.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/dtos/Event.java new file mode 100644 index 0000000000..f7a181e5e6 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/dtos/Event.java @@ -0,0 +1,14 @@ +package org.openecomp.sdc.notification.dtos; + +import java.util.Map; + +public interface Event { + + String getEventType(); + + String getOriginatorId(); + + Map<String, Object> getAttributes(); + + String getEntityId(); +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/dtos/NotificationsStatus.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/dtos/NotificationsStatus.java new file mode 100644 index 0000000000..1620569ec5 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/dtos/NotificationsStatus.java @@ -0,0 +1,24 @@ +package org.openecomp.sdc.notification.dtos; + +import org.openecomp.sdc.notification.dao.types.NotificationEntity; + +import java.util.List; +import java.util.UUID; + +/** + * @author Avrahamg + * @since June 26, 2017 + */ +public interface NotificationsStatus { + + List<NotificationEntity> getNotifications(); + + List<UUID> getNewEntries(); + + UUID getLastScanned(); + + UUID getEndOfPage(); + + long getNumOfNotSeenNotifications(); + +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/exceptons/NotificationNotExistException.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/exceptons/NotificationNotExistException.java new file mode 100644 index 0000000000..90e53ea69a --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/exceptons/NotificationNotExistException.java @@ -0,0 +1,18 @@ +package org.openecomp.sdc.notification.exceptons; + +/** + * @author avrahamg + * @since July 02, 2017 + */ +public class NotificationNotExistException extends Exception { + private String message; + + + public NotificationNotExistException(String Message){ + this(Message, null); + } + public NotificationNotExistException(String message, Throwable cause) { + super(cause); + this.message = message; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/factories/LastNotificationDaoFactory.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/factories/LastNotificationDaoFactory.java new file mode 100644 index 0000000000..6e6f9f063a --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/factories/LastNotificationDaoFactory.java @@ -0,0 +1,34 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.factories; + +import org.openecomp.core.factory.api.AbstractComponentFactory; +import org.openecomp.core.factory.api.AbstractFactory; +import org.openecomp.sdc.notification.dao.LastNotificationDao; + + +public abstract class LastNotificationDaoFactory extends AbstractComponentFactory<LastNotificationDao> { + + public static LastNotificationDaoFactory getInstance() { + return AbstractFactory.getInstance(LastNotificationDaoFactory.class); + } + +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/factories/NotificationPropagationManagerFactory.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/factories/NotificationPropagationManagerFactory.java new file mode 100644 index 0000000000..356055e26c --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/factories/NotificationPropagationManagerFactory.java @@ -0,0 +1,15 @@ +package org.openecomp.sdc.notification.factories; + +import org.openecomp.core.factory.api.AbstractComponentFactory; +import org.openecomp.core.factory.api.AbstractFactory; +import org.openecomp.sdc.notification.services.NotificationPropagationManager; + +public abstract class NotificationPropagationManagerFactory extends AbstractComponentFactory<NotificationPropagationManager> { + + + public static NotificationPropagationManagerFactory getInstance() { + return AbstractFactory.getInstance(NotificationPropagationManagerFactory.class); + } + + +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/factories/NotificationsDaoFactory.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/factories/NotificationsDaoFactory.java new file mode 100644 index 0000000000..341af7e296 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/factories/NotificationsDaoFactory.java @@ -0,0 +1,34 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.factories; + +import org.openecomp.core.factory.api.AbstractComponentFactory; +import org.openecomp.core.factory.api.AbstractFactory; +import org.openecomp.sdc.notification.dao.NotificationsDao; + + +public abstract class NotificationsDaoFactory extends AbstractComponentFactory<NotificationsDao> { + + public static NotificationsDaoFactory getInstance() { + return AbstractFactory.getInstance(NotificationsDaoFactory.class); + } + +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/factories/NotificationsServiceFactory.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/factories/NotificationsServiceFactory.java new file mode 100644 index 0000000000..eab26188c1 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/factories/NotificationsServiceFactory.java @@ -0,0 +1,34 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.factories; + +import org.openecomp.core.factory.api.AbstractComponentFactory; +import org.openecomp.core.factory.api.AbstractFactory; +import org.openecomp.sdc.notification.services.NotificationsService; + + +public abstract class NotificationsServiceFactory extends AbstractComponentFactory<NotificationsService> { + + public static NotificationsServiceFactory getInstance() { + return AbstractFactory.getInstance(NotificationsServiceFactory.class); + } + +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/factories/PropagationServiceFactory.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/factories/PropagationServiceFactory.java new file mode 100644 index 0000000000..e82783c40c --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/factories/PropagationServiceFactory.java @@ -0,0 +1,15 @@ +package org.openecomp.sdc.notification.factories; + +import org.openecomp.core.factory.api.AbstractComponentFactory; +import org.openecomp.core.factory.api.AbstractFactory; +import org.openecomp.sdc.notification.services.PropagationService; + +public abstract class PropagationServiceFactory extends AbstractComponentFactory<PropagationService> { + + + public static PropagationServiceFactory getInstance() { + return AbstractFactory.getInstance(PropagationServiceFactory.class); + } + + +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/factories/SubscribersDaoFactory.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/factories/SubscribersDaoFactory.java new file mode 100644 index 0000000000..33b62e2f5d --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/factories/SubscribersDaoFactory.java @@ -0,0 +1,34 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.factories; + +import org.openecomp.core.factory.api.AbstractComponentFactory; +import org.openecomp.core.factory.api.AbstractFactory; +import org.openecomp.sdc.notification.dao.SubscribersDao; + + +public abstract class SubscribersDaoFactory extends AbstractComponentFactory<SubscribersDao> { + + public static SubscribersDaoFactory getInstance() { + return AbstractFactory.getInstance(SubscribersDaoFactory.class); + } + +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/factories/SubscriptionServiceFactory.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/factories/SubscriptionServiceFactory.java new file mode 100644 index 0000000000..3e789c22a7 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/factories/SubscriptionServiceFactory.java @@ -0,0 +1,34 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.factories; + +import org.openecomp.core.factory.api.AbstractComponentFactory; +import org.openecomp.core.factory.api.AbstractFactory; +import org.openecomp.sdc.notification.services.SubscriptionService; + + +public abstract class SubscriptionServiceFactory extends AbstractComponentFactory<SubscriptionService> { + + public static SubscriptionServiceFactory getInstance() { + return AbstractFactory.getInstance(SubscriptionServiceFactory.class); + } + +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/services/NotificationPropagationManager.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/services/NotificationPropagationManager.java new file mode 100644 index 0000000000..7a1596eb95 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/services/NotificationPropagationManager.java @@ -0,0 +1,12 @@ +package org.openecomp.sdc.notification.services; + +import org.openecomp.sdc.notification.dtos.Event; + +/** + * @author avrahamg + * @since July 09, 2017 + */ +public interface NotificationPropagationManager { + void notifySubscribers(Event event, String ... excludedSubscribers); + void directNotification(Event event, String destinationId); +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/services/NotificationsService.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/services/NotificationsService.java new file mode 100644 index 0000000000..17cfac1e5d --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/services/NotificationsService.java @@ -0,0 +1,31 @@ +package org.openecomp.sdc.notification.services; + +import org.openecomp.sdc.notification.dao.types.LastSeenNotificationEntity; +import org.openecomp.sdc.notification.dao.types.NotificationEntity; +import org.openecomp.sdc.notification.dtos.NotificationsStatus; +import org.openecomp.sdc.notification.exceptons.NotificationNotExistException; + +import java.util.List; +import java.util.UUID; + +/** + * @author Avrahamg + * @since June 22, 2017 + */ +public interface NotificationsService { + + LastSeenNotificationEntity getLastNotification(String ownerId); + + NotificationsStatus getNotificationsStatus(String ownerId, UUID lastDelivered, int numOfRecordsToReturn, UUID endOfPage); + + void updateLastSeenNotification(String ownerId, UUID eventId); + + void markAsRead(String ownerId, String notificationId) throws NotificationNotExistException; + + List<NotificationEntity> getNotificationsByOwnerId(String ownerId, int limit); + + List<NotificationEntity> getNewNotificationsByOwnerId(String ownerId, UUID eventId); + + List<NotificationEntity> getNewNotificationsByOwnerId(String ownerId, UUID eventId, int limit); + +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/services/PropagationService.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/services/PropagationService.java new file mode 100644 index 0000000000..c084aa8015 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/services/PropagationService.java @@ -0,0 +1,9 @@ +package org.openecomp.sdc.notification.services; + +import org.openecomp.sdc.destinationprovider.DestinationProvider; +import org.openecomp.sdc.notification.dtos.Event; + +public interface PropagationService { + + void notify(Event event, DestinationProvider destinationProvider); +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/services/SubscriptionService.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/services/SubscriptionService.java new file mode 100644 index 0000000000..98cd458ca2 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-api/src/main/java/org/openecomp/sdc/notification/services/SubscriptionService.java @@ -0,0 +1,15 @@ +package org.openecomp.sdc.notification.services; + +import java.util.Set; + +/** + * @author avrahamg + * @since July 09, 2017 + */ +public interface SubscriptionService { + void subscribe(String ownerId, String entityId); + + void unsubscribe(String ownerId, String entityId); + + Set<String> getSubscribers(String entityId); +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/pom.xml b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/pom.xml new file mode 100644 index 0000000000..4fe33ea1c3 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/pom.xml @@ -0,0 +1,42 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <name>openecomp-sdc-notification-core</name> + <artifactId>openecomp-sdc-notification-core</artifactId> + + + <parent> + <groupId>org.openecomp.sdc</groupId> + <artifactId>openecomp-sdc-lib</artifactId> + <version>1.2.0-SNAPSHOT</version> + <relativePath>../..</relativePath> + </parent> + <dependencies> + <dependency> + <groupId>org.openecomp.sdc</groupId> + <artifactId>openecomp-sdc-notification-api</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-all</artifactId> + <scope>test</scope> + <version>1.10.19</version> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + <version>${junit.version}</version> + </dependency> + <dependency> + <groupId>org.powermock</groupId> + <artifactId>powermock-module-junit4</artifactId> + <version>1.6.5</version> + <scope>test</scope> + </dependency> + </dependencies> + +</project> diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/destinationprovider/impl/MulticastDestination.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/destinationprovider/impl/MulticastDestination.java new file mode 100644 index 0000000000..431f9c18d6 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/destinationprovider/impl/MulticastDestination.java @@ -0,0 +1,44 @@ +package org.openecomp.sdc.destinationprovider.impl; + +import org.apache.commons.lang3.ArrayUtils; +import org.openecomp.sdc.destinationprovider.DestinationProvider; +import org.openecomp.sdc.notification.services.SubscriptionService; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +/** + * @author avrahamg + * @since July 09, 2017 + */ +public class MulticastDestination implements DestinationProvider { + + private String entityId; + private SubscriptionService subscriptionService; + private String[] excludedSubscribers; + + public MulticastDestination(String entityId, SubscriptionService subscriptionService, + String... excludedSubscribers) { + this.entityId = entityId; + this.excludedSubscribers = excludedSubscribers; + this.subscriptionService = subscriptionService; + } + + public List<String> getSubscribers() { + ArrayList<String> subscribers = new ArrayList<>(subscriptionService.getSubscribers(entityId)); + if (ArrayUtils.isNotEmpty(excludedSubscribers)) { + subscribers.removeAll(Arrays.asList(excludedSubscribers)); + } + return Collections.unmodifiableList(subscribers); + } + + public String getEntityId() { + return entityId; + } + + public void setEntityId(String entityId) { + this.entityId = entityId; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/destinationprovider/impl/UnicastDestination.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/destinationprovider/impl/UnicastDestination.java new file mode 100644 index 0000000000..693c816d01 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/destinationprovider/impl/UnicastDestination.java @@ -0,0 +1,23 @@ +package org.openecomp.sdc.destinationprovider.impl; + +import org.openecomp.sdc.destinationprovider.DestinationProvider; + +import java.util.Collections; +import java.util.List; + +/** + * @author avrahamg + * @since July 09, 2017 + */ +public class UnicastDestination implements DestinationProvider { + + private String originatorId; + + public UnicastDestination(String originatorId) { + this.originatorId = originatorId; + } + + public List<String> getSubscribers() { + return Collections.unmodifiableList(Collections.singletonList(originatorId)); + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/dao/impl/LastNotificationDaoCassandraImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/dao/impl/LastNotificationDaoCassandraImpl.java new file mode 100644 index 0000000000..0bfd6a314d --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/dao/impl/LastNotificationDaoCassandraImpl.java @@ -0,0 +1,87 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.dao.impl; + +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Row; +import com.datastax.driver.mapping.Mapper; +import com.datastax.driver.mapping.Result; +import com.datastax.driver.mapping.annotations.Accessor; +import com.datastax.driver.mapping.annotations.Query; +import org.openecomp.core.dao.impl.CassandraBaseDao; +import org.openecomp.core.nosqldb.api.NoSqlDb; +import org.openecomp.core.nosqldb.factory.NoSqlDbFactory; +import org.openecomp.sdc.notification.dao.LastNotificationDao; +import org.openecomp.sdc.notification.dao.types.LastSeenNotificationEntity; + +import java.util.Collection; +import java.util.UUID; + + +public class LastNotificationDaoCassandraImpl extends CassandraBaseDao<LastSeenNotificationEntity> implements LastNotificationDao { + + private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface(); + private static final Mapper<LastSeenNotificationEntity> mapper = + noSqlDb.getMappingManager().mapper(LastSeenNotificationEntity.class); + private static final LastNotificationAccessor accessor = + noSqlDb.getMappingManager().createAccessor(LastNotificationAccessor.class); + + @Override + protected Mapper<LastSeenNotificationEntity> getMapper() { + return mapper; + } + + @Override + protected Object[] getKeys(LastSeenNotificationEntity entity) { + return new Object[]{entity.getOwnerId()}; + } + + @Override + public Collection<LastSeenNotificationEntity> list(LastSeenNotificationEntity entity) { + return accessor.list(entity.getOwnerId()).all(); + } + + @Override + public UUID getOwnerLastEventId(String ownerId) { + ResultSet ownerLastEventId = accessor.getOwnerLastEventId(ownerId); + Row one = ownerLastEventId.one(); + return one != null ? one.getUUID("event_id") : null; + } + + @Override + public void persistOwnerLastEventId(String ownerId, UUID eventId) { + accessor.updateOwnerLastEventId(eventId, ownerId); + } + + @Accessor + interface LastNotificationAccessor { + + @Query("select * from last_notification where owner_id=?") + Result<LastSeenNotificationEntity> list(String ownerId); + + @Query("select event_id from last_notification where owner_id=?") + ResultSet getOwnerLastEventId(String ownerId); + + @Query("update last_notification set event_id=? where owner_id=?") + ResultSet updateOwnerLastEventId(UUID eventId, String ownerId); + } + +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/dao/impl/NotificationsDaoCassandraImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/dao/impl/NotificationsDaoCassandraImpl.java new file mode 100644 index 0000000000..151c2c81d9 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/dao/impl/NotificationsDaoCassandraImpl.java @@ -0,0 +1,287 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.dao.impl; + +import com.datastax.driver.core.BatchStatement; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Statement; +import com.datastax.driver.core.utils.UUIDs; +import com.datastax.driver.mapping.Mapper; +import com.datastax.driver.mapping.Result; +import com.datastax.driver.mapping.annotations.Accessor; +import com.datastax.driver.mapping.annotations.Query; +import org.apache.commons.collections.CollectionUtils; +import org.openecomp.core.dao.impl.CassandraBaseDao; +import org.openecomp.core.nosqldb.api.NoSqlDb; +import org.openecomp.core.nosqldb.factory.NoSqlDbFactory; +import org.openecomp.sdc.notification.dao.NotificationsDao; +import org.openecomp.sdc.notification.dao.types.NotificationEntity; +import org.openecomp.sdc.notification.dtos.NotificationsStatus; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.UUID; +import java.util.stream.Collectors; + +import static org.openecomp.core.nosqldb.impl.cassandra.CassandraSessionFactory.getSession; + +//import org.openecomp.sdc.notification.dao.types.LastSeenNotificationEntity; +//import java.util.Optional; + +public class NotificationsDaoCassandraImpl extends CassandraBaseDao<NotificationEntity> + implements NotificationsDao { + + private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface(); + private static final Mapper<NotificationEntity> mapper = + noSqlDb.getMappingManager().mapper(NotificationEntity.class); + private static final NotificationsAccessor accessor = + noSqlDb.getMappingManager().createAccessor(NotificationsAccessor.class); + + @Override + protected Mapper<NotificationEntity> getMapper() { + return mapper; + } + + @Override + protected Object[] getKeys(NotificationEntity entity) { + return new Object[]{entity.getOwnerId(), entity.getEventId()}; + } + + @Override + public List<NotificationEntity> list(NotificationEntity entity) { + return accessor.list(entity.getOwnerId()).all(); + } + + @Override + public List<NotificationEntity> getNotificationsByOwnerId(String ownerId, int limit) { + return accessor.getNotifications(ownerId, limit).all(); + } + + @Override + public List<NotificationEntity> getNewNotificationsByOwnerId(String ownerId, UUID eventId) { + return getNewNotificationsByOwnerId(ownerId, eventId, + DEFAULT_LIMIT_OF_RESULTS_FOR_OWNER_NOTIFICATIONS); + } + + @Override + public List<NotificationEntity> getNewNotificationsByOwnerId(String ownerId, UUID eventId, int limit) { + if (Objects.isNull(eventId)) { + return getNotificationsByOwnerId(ownerId, limit); + } + return accessor.getNewNotifications(ownerId, eventId, limit).all(); + } + + @Override + public void markNotificationAsRead(String ownerId, Collection<UUID> eventIds) { + eventIds.forEach(eventId -> accessor.markAsRead(ownerId, eventId)); + } + + @Override + public NotificationsStatus getNotificationsStatus(String ownerId, UUID lastScannedEventId, int numOfRecordsToReturn) { + NotificationsStatusImpl notificationsStatus = new NotificationsStatusImpl(); + List<NotificationEntity> entities = accessor.getNotifications(ownerId, numOfRecordsToReturn).all(); + if (CollectionUtils.isNotEmpty(entities)) { + long lastSeen = UUIDs.unixTimestamp(lastScannedEventId); + populateNewNotifications(notificationsStatus, entities, lastSeen); + UUID firstScannedEventId = entities.get(0).getEventId(); + notificationsStatus.setLastScanned(firstScannedEventId); + notificationsStatus.setNumOfNotSeenNotifications(accessor.getNewNotificationsCount(ownerId, lastScannedEventId, firstScannedEventId).one().getLong(0)); + } + return notificationsStatus; + } + + private void populateNewNotifications(NotificationsStatusImpl notificationsStatus, List<NotificationEntity> entities, long lastSeen) { + for (NotificationEntity entity : entities) { + UUID eventId = entity.getEventId(); + notificationsStatus.addNotification(entity); + if (UUIDs.unixTimestamp(eventId) > lastSeen) { + notificationsStatus.addNewNotificationUUID(eventId); + } + } + } + + @Override + public NotificationsStatus getNotificationsStatus(String ownerId, UUID lastSeenNotification, int numOfRecordsToReturn, UUID prevLastScannedEventId) { + NotificationsStatusImpl notificationsStatus = new NotificationsStatusImpl(); + List<NotificationEntity> entities = accessor.getPrevNotifications(ownerId, prevLastScannedEventId, numOfRecordsToReturn).all(); + if (CollectionUtils.isNotEmpty(entities)) { + long lastSeen = UUIDs.unixTimestamp(lastSeenNotification); + populateNewNotifications(notificationsStatus, entities, lastSeen); + } + return notificationsStatus; + } + +/* + @Override + public NotificationsStatus getNotificationsStatus(String ownerId, + LastSeenNotificationEntity lastSeenNotification, + int numOfRecordsToReturn) { + + List<NotificationEntity> notificationEntities = + fetchNewNotifications(lastSeenNotification, numOfRecordsToReturn); + NotificationsStatusImpl notificationsStatus = new NotificationsStatusImpl(); + if (CollectionUtils.isEmpty(notificationEntities)) { + return notificationsStatus; + } + + notificationEntities.forEach(notification -> { + if (isNewNotification(lastSeenNotification, notification)) { + notificationsStatus.addNewNotificationUUID(notification.getEventId()); + } + notificationsStatus.addNotification(notification); + }); + + Optional<NotificationEntity> latestNotification = notificationEntities.stream().findFirst(); + latestNotification.ifPresent(e -> notificationsStatus.setLastScanned(e.getEventId())); + return notificationsStatus; + } + + private List<NotificationEntity> fetchNewNotifications( + LastSeenNotificationEntity lastSeenNotification, int numOfRecordsToReturn) { + String ownerId = lastSeenNotification.getOwnerId(); + UUID lastEventId = lastSeenNotification.getLastEventId(); + List<NotificationEntity> newNotificationsByOwnerId = + getNewNotificationsByOwnerId(ownerId, lastEventId); + newNotificationsByOwnerId = fetchMoreIfNeeded(ownerId, newNotificationsByOwnerId, + numOfRecordsToReturn, lastEventId); + return newNotificationsByOwnerId; + } + + private boolean isNewNotification(LastSeenNotificationEntity lastSeenNotification, + NotificationEntity notification) { + return Objects.isNull(lastSeenNotification.getLastEventId()) || + UUIDs.unixTimestamp(notification.getEventId()) > + UUIDs.unixTimestamp(lastSeenNotification.getLastEventId()); + } +*/ + + @Override + public void createBatch(List<NotificationEntity> notificationEntities) { + BatchStatement batch = new BatchStatement(); + List<Statement> statements = notificationEntities.stream() + .map(mapper::saveQuery) + .collect(Collectors.toList()); + batch.addAll(statements); + getSession().execute(batch); + } + + @Accessor + interface NotificationsAccessor { + + @Query("select * from notifications where owner_id=?") + Result<NotificationEntity> list(String ownerId); + + @Query("select * from notifications where owner_id=? limit ?") + Result<NotificationEntity> getNotifications(String ownerId, int limit); + + @Query("select * from notifications where owner_id=? and event_id > ? limit ?") + Result<NotificationEntity> getNewNotifications(String ownerId, UUID lastScannedEventId, int limit); + + @Query("select * from notifications where owner_id=? and event_id < ? limit ?") + Result<NotificationEntity> getPrevNotifications(String ownerId, UUID prevLastScannedEventId, int limit); + + @Query("select count(*) from notifications where owner_id=? and event_id > ? and event_id <= ?") + ResultSet getNewNotificationsCount(String ownerId, UUID lastScannedEventId, UUID firstScannedEventId); + + @Query("update notifications set read=true where owner_id=? and event_id=?") + ResultSet markAsRead(String ownerId, UUID eventId); + } + + private class NotificationsStatusImpl implements NotificationsStatus { + + private List<NotificationEntity> notifications = new ArrayList<>(); + private List<UUID> newEntries = new ArrayList<>(); + private UUID lastScanned; + private UUID endOfPage; + private long numOfNotSeenNotifications = 0; + + void addNotification(NotificationEntity notification) { + notifications.add(notification); + endOfPage = notification.getEventId(); + } + + void addNewNotificationUUID(UUID notificationUuid) { + newEntries.add(notificationUuid); + } + + @Override + public List<NotificationEntity> getNotifications() { + return Collections.unmodifiableList(notifications); + } + + @Override + public List<UUID> getNewEntries() { + return Collections.unmodifiableList(newEntries); + } + + @Override + public UUID getLastScanned() { + return lastScanned; + } + + void setLastScanned(UUID lastScanned) { + this.lastScanned = lastScanned; + } + + @Override + public UUID getEndOfPage() { + return endOfPage; + } + + @Override + public long getNumOfNotSeenNotifications() { + return numOfNotSeenNotifications; + } + + void setNumOfNotSeenNotifications(long numOfNotSeenNotifications) { + this.numOfNotSeenNotifications = numOfNotSeenNotifications; + } + } + +/* + private List<NotificationEntity> fetchMoreIfNeeded(String ownerId, + List<NotificationEntity> notificationEntities, + int numOfRecordsToReturn, UUID lastEventId) { + + if (numOfRecordsToReturn <= notificationEntities.size() || Objects.isNull(lastEventId)) { + return notificationEntities; + } + + int multiplier = 2; + while (numOfRecordsToReturn > notificationEntities.size()) { + + int bring = notificationEntities.size() + + (numOfRecordsToReturn - notificationEntities.size()) * multiplier; + notificationEntities = getNotificationsByOwnerId(ownerId, bring); + + if (notificationEntities.size() < bring) { + return notificationEntities; + } + multiplier++; + } + return notificationEntities; + } +*/ + +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/dao/impl/SubscribersDaoCassandraImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/dao/impl/SubscribersDaoCassandraImpl.java new file mode 100644 index 0000000000..8e8cf9b181 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/dao/impl/SubscribersDaoCassandraImpl.java @@ -0,0 +1,104 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.dao.impl; + +import com.datastax.driver.mapping.Mapper; +import com.datastax.driver.mapping.Result; +import com.datastax.driver.mapping.annotations.Accessor; +import com.datastax.driver.mapping.annotations.Query; +import com.google.common.collect.Sets; +import org.openecomp.core.dao.impl.CassandraBaseDao; +import org.openecomp.core.nosqldb.api.NoSqlDb; +import org.openecomp.core.nosqldb.factory.NoSqlDbFactory; +import org.openecomp.sdc.notification.dao.SubscribersDao; +import org.openecomp.sdc.notification.dao.types.SubscribersEntity; + +import java.util.Collection; +import java.util.Collections; +import java.util.Objects; +import java.util.Set; + +import static java.util.Objects.isNull; + +public class SubscribersDaoCassandraImpl extends CassandraBaseDao<SubscribersEntity> implements + SubscribersDao { + + private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface(); + private static final Mapper<SubscribersEntity> mapper = + noSqlDb.getMappingManager().mapper(SubscribersEntity.class); + private static final SubscribersAccessor accessor = + noSqlDb.getMappingManager().createAccessor(SubscribersAccessor.class); + + + @Override + protected Object[] getKeys(SubscribersEntity entity) { + return new Object[]{entity.getEntityId()}; + } + + @Override + protected Mapper<SubscribersEntity> getMapper() { + return mapper; + } + + @Override + public void subscribe(String ownerId, String entityId) { + Objects.requireNonNull(ownerId); + Objects.requireNonNull(entityId); + accessor.subscribe(Sets.newHashSet(ownerId), entityId); + } + + @Override + @Deprecated + public Collection<SubscribersEntity> list(SubscribersEntity entity) { + throw new UnsupportedOperationException(); + } + + @Override + public void unsubscribe(String ownerId, String entityId) { + Objects.requireNonNull(ownerId); + Objects.requireNonNull(entityId); + accessor.unsubscribe(Sets.newHashSet(ownerId), entityId); + } + + @Override + public Set<String> getSubscribers(String entityId) { + Objects.requireNonNull(entityId); + SubscribersEntity subscribersEntity = accessor.getSubscribers(entityId).one(); + if (isNull(subscribersEntity)) { + return Collections.emptySet(); + } + return subscribersEntity.getSubscribers(); + } + + @Accessor + interface SubscribersAccessor { + + @Query("select * from notification_subscribers where entity_id=?") + Result<SubscribersEntity> getSubscribers(String entityId); + + @Query("update notification_subscribers set subscribers=subscribers+? WHERE entity_id=?") + void subscribe(Set<String> ownerId, String entityId); + + @Query("update notification_subscribers set subscribers=subscribers-? WHERE entity_id=?") + void unsubscribe(Set<String> ownerId, String entityId); + } + +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/LastNotificationDaoFactoryImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/LastNotificationDaoFactoryImpl.java new file mode 100644 index 0000000000..7ac9e00967 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/LastNotificationDaoFactoryImpl.java @@ -0,0 +1,19 @@ +package org.openecomp.sdc.notification.factories.impl; + +import org.openecomp.sdc.notification.dao.LastNotificationDao; +import org.openecomp.sdc.notification.dao.impl.LastNotificationDaoCassandraImpl; +import org.openecomp.sdc.notification.factories.LastNotificationDaoFactory; + +/** + * @author itzikpa + * @since June 23, 2017 + */ + +public class LastNotificationDaoFactoryImpl extends LastNotificationDaoFactory { + private static final LastNotificationDao INSTANCE = new LastNotificationDaoCassandraImpl(); + + @Override + public LastNotificationDao createInterface() { + return INSTANCE; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/NotificationPropagationManagerFactoryImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/NotificationPropagationManagerFactoryImpl.java new file mode 100644 index 0000000000..7b9e51e8c9 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/NotificationPropagationManagerFactoryImpl.java @@ -0,0 +1,19 @@ +package org.openecomp.sdc.notification.factories.impl; + +import org.openecomp.sdc.notification.factories.NotificationPropagationManagerFactory; +import org.openecomp.sdc.notification.factories.PropagationServiceFactory; +import org.openecomp.sdc.notification.factories.SubscriptionServiceFactory; +import org.openecomp.sdc.notification.services.NotificationPropagationManager; +import org.openecomp.sdc.notification.services.impl.NotificationPropagationManagerImpl; + +public class NotificationPropagationManagerFactoryImpl extends + NotificationPropagationManagerFactory { + private static final NotificationPropagationManager INSTANCE = new NotificationPropagationManagerImpl( + PropagationServiceFactory.getInstance().createInterface(), SubscriptionServiceFactory + .getInstance().createInterface()); + + @Override + public NotificationPropagationManager createInterface() { + return INSTANCE; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/NotificationsDaoFactoryImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/NotificationsDaoFactoryImpl.java new file mode 100644 index 0000000000..edb020637b --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/NotificationsDaoFactoryImpl.java @@ -0,0 +1,18 @@ +package org.openecomp.sdc.notification.factories.impl; + +import org.openecomp.sdc.notification.dao.NotificationsDao; +import org.openecomp.sdc.notification.dao.impl.NotificationsDaoCassandraImpl; +import org.openecomp.sdc.notification.factories.NotificationsDaoFactory; + +/** + * @author Avrahamg + * @since June 20, 2017 + */ +public class NotificationsDaoFactoryImpl extends NotificationsDaoFactory { + private static final NotificationsDao INSTANCE = new NotificationsDaoCassandraImpl(); + + @Override + public NotificationsDao createInterface() { + return INSTANCE; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/NotificationsServiceFactoryImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/NotificationsServiceFactoryImpl.java new file mode 100644 index 0000000000..ac265e3c8c --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/NotificationsServiceFactoryImpl.java @@ -0,0 +1,22 @@ +package org.openecomp.sdc.notification.factories.impl; + +import org.openecomp.sdc.notification.factories.LastNotificationDaoFactory; +import org.openecomp.sdc.notification.factories.NotificationsDaoFactory; +import org.openecomp.sdc.notification.factories.NotificationsServiceFactory; +import org.openecomp.sdc.notification.services.NotificationsService; +import org.openecomp.sdc.notification.services.impl.NotificationsServiceImpl; + +/** + * @author Avrahamg + * @since June 20, 2017 + */ +public class NotificationsServiceFactoryImpl extends NotificationsServiceFactory { + private static final NotificationsService INSTANCE = new NotificationsServiceImpl( + LastNotificationDaoFactory.getInstance().createInterface(), NotificationsDaoFactory + .getInstance().createInterface()); + + @Override + public NotificationsService createInterface() { + return INSTANCE; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/PropagationServiceFactoryImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/PropagationServiceFactoryImpl.java new file mode 100644 index 0000000000..e5987fe266 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/PropagationServiceFactoryImpl.java @@ -0,0 +1,16 @@ +package org.openecomp.sdc.notification.factories.impl; + +import org.openecomp.sdc.notification.factories.NotificationsDaoFactory; +import org.openecomp.sdc.notification.factories.PropagationServiceFactory; +import org.openecomp.sdc.notification.services.PropagationService; +import org.openecomp.sdc.notification.services.impl.PropagationServiceImpl; + +public class PropagationServiceFactoryImpl extends PropagationServiceFactory { + private static final PropagationService INSTANCE = new PropagationServiceImpl( + NotificationsDaoFactory.getInstance().createInterface()); + + @Override + public PropagationService createInterface() { + return INSTANCE; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/SubscribersDaoFactoryImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/SubscribersDaoFactoryImpl.java new file mode 100644 index 0000000000..fb87b944f9 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/SubscribersDaoFactoryImpl.java @@ -0,0 +1,15 @@ +package org.openecomp.sdc.notification.factories.impl; + +import org.openecomp.sdc.notification.dao.SubscribersDao; +import org.openecomp.sdc.notification.dao.impl.SubscribersDaoCassandraImpl; +import org.openecomp.sdc.notification.factories.SubscribersDaoFactory; + + +public class SubscribersDaoFactoryImpl extends SubscribersDaoFactory { + private static final SubscribersDao INSTANCE = new SubscribersDaoCassandraImpl(); + + @Override + public SubscribersDao createInterface() { + return INSTANCE; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/SubscriptionServiceFactoryImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/SubscriptionServiceFactoryImpl.java new file mode 100644 index 0000000000..604cff0b5e --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/factories/impl/SubscriptionServiceFactoryImpl.java @@ -0,0 +1,17 @@ +package org.openecomp.sdc.notification.factories.impl; + +import org.openecomp.sdc.notification.factories.SubscribersDaoFactory; +import org.openecomp.sdc.notification.factories.SubscriptionServiceFactory; +import org.openecomp.sdc.notification.services.SubscriptionService; +import org.openecomp.sdc.notification.services.impl.SubscriptionServiceImpl; + + +public class SubscriptionServiceFactoryImpl extends SubscriptionServiceFactory { + private static final SubscriptionService INSTANCE = new SubscriptionServiceImpl + (SubscribersDaoFactory.getInstance().createInterface()); + + @Override + public SubscriptionService createInterface() { + return INSTANCE; + } +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/services/impl/NotificationPropagationManagerImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/services/impl/NotificationPropagationManagerImpl.java new file mode 100644 index 0000000000..d381268b3f --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/services/impl/NotificationPropagationManagerImpl.java @@ -0,0 +1,35 @@ +package org.openecomp.sdc.notification.services.impl; + +import org.openecomp.sdc.destinationprovider.impl.MulticastDestination; +import org.openecomp.sdc.destinationprovider.impl.UnicastDestination; +import org.openecomp.sdc.notification.dtos.Event; +import org.openecomp.sdc.notification.services.NotificationPropagationManager; +import org.openecomp.sdc.notification.services.PropagationService; +import org.openecomp.sdc.notification.services.SubscriptionService; + +/** + * @author avrahamg + * @since July 10, 2017 + */ +public class NotificationPropagationManagerImpl implements NotificationPropagationManager { + + private PropagationService propagationService; + private SubscriptionService subscriptionService; + + public NotificationPropagationManagerImpl(PropagationService propagationService, + SubscriptionService subscriptionService) { + this.propagationService = propagationService; + this.subscriptionService = subscriptionService; + } + + @Override + public void notifySubscribers(Event event, String ... excludedSubscribers) { + propagationService.notify(event, new MulticastDestination(event.getEntityId(), + subscriptionService, excludedSubscribers)); + } + + @Override + public void directNotification(Event event, String destinationId) { + propagationService.notify(event, new UnicastDestination(destinationId)); + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/services/impl/NotificationsServiceImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/services/impl/NotificationsServiceImpl.java new file mode 100644 index 0000000000..8a7ab8bdae --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/services/impl/NotificationsServiceImpl.java @@ -0,0 +1,92 @@ +package org.openecomp.sdc.notification.services.impl; + +import org.openecomp.sdc.notification.dao.LastNotificationDao; +import org.openecomp.sdc.notification.dao.NotificationsDao; +import org.openecomp.sdc.notification.dao.types.LastSeenNotificationEntity; +import org.openecomp.sdc.notification.dao.types.NotificationEntity; +import org.openecomp.sdc.notification.dtos.NotificationsStatus; +import org.openecomp.sdc.notification.exceptons.NotificationNotExistException; +import org.openecomp.sdc.notification.services.NotificationsService; + +import java.util.List; +import java.util.Objects; +import java.util.UUID; + +/** + * @author Avrahamg + * @since June 26, 2017 + */ +public class NotificationsServiceImpl implements NotificationsService { + + private LastNotificationDao lastNotificationDao; + private NotificationsDao notificationsDao; + + public NotificationsServiceImpl(LastNotificationDao lastNotificationDao, + NotificationsDao notificationsDao) { + this.lastNotificationDao = lastNotificationDao; + this.notificationsDao = notificationsDao; + } + + @Override + public LastSeenNotificationEntity getLastNotification(String ownerId) { + return new LastSeenNotificationEntity(ownerId, + lastNotificationDao.getOwnerLastEventId(ownerId)); + } + + @Override + public void updateLastSeenNotification(String ownerId, UUID eventId) + { + lastNotificationDao.persistOwnerLastEventId(ownerId, eventId); + } + + @Override + public NotificationsStatus getNotificationsStatus(String ownerId, UUID lastDelivered, int numOfRecordsToReturn, UUID endOfPage) { + if (Objects.isNull(lastDelivered)) { + LastSeenNotificationEntity entity = getLastNotification(ownerId); + if (Objects.nonNull(entity)) { + lastDelivered = entity.getLastEventId(); + } + if (Objects.isNull(lastDelivered)) { + lastDelivered = UUID.fromString("00000000-0000-1000-8080-808080808080"); // Lowest time UUID value + } + } + if (Objects.isNull(endOfPage)) { + // First page + return notificationsDao.getNotificationsStatus(ownerId, lastDelivered, numOfRecordsToReturn); + } + else { + // Next page + return notificationsDao.getNotificationsStatus(ownerId, lastDelivered, numOfRecordsToReturn, endOfPage); + } + } + + @Override + public void markAsRead(String ownerId, String notificationId) throws + NotificationNotExistException { + NotificationEntity notificationEntity = + notificationsDao.get(new NotificationEntity(ownerId, UUID.fromString(notificationId))); + if (Objects.isNull(notificationEntity)) { + throw new NotificationNotExistException( + "Notification '" + notificationId + "' is not related to ownerId" + + " '" + ownerId + "'"); + } + notificationEntity.setRead(true); + notificationsDao.update(notificationEntity); + } + + @Override + public List<NotificationEntity> getNotificationsByOwnerId(String ownerId, int limit) { + return notificationsDao.getNotificationsByOwnerId(ownerId, limit); + } + + @Override + public List<NotificationEntity> getNewNotificationsByOwnerId(String ownerId, UUID eventId) { + return notificationsDao.getNewNotificationsByOwnerId(ownerId, eventId); + } + + @Override + public List<NotificationEntity> getNewNotificationsByOwnerId(String ownerId, UUID eventId, int limit) { + return notificationsDao.getNewNotificationsByOwnerId(ownerId, eventId, limit); + } + +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/services/impl/PropagationServiceImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/services/impl/PropagationServiceImpl.java new file mode 100644 index 0000000000..99d35b3726 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/services/impl/PropagationServiceImpl.java @@ -0,0 +1,78 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.services.impl; + +import com.datastax.driver.core.utils.UUIDs; +import org.apache.commons.collections4.CollectionUtils; +import org.openecomp.core.utilities.json.JsonUtil; +import org.openecomp.sdc.destinationprovider.DestinationProvider; +import org.openecomp.sdc.notification.dao.NotificationsDao; +import org.openecomp.sdc.notification.dao.types.NotificationEntity; +import org.openecomp.sdc.notification.dtos.Event; +import org.openecomp.sdc.notification.services.PropagationService; + +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.stream.Collectors; + +import static java.util.Objects.requireNonNull; + +public class PropagationServiceImpl implements PropagationService { + + private NotificationsDao notificationsDao; + + public PropagationServiceImpl(NotificationsDao notificationsDao) { + this.notificationsDao = notificationsDao; + } + + + @Override + public void notify(Event event, DestinationProvider destinationProvider) { + requireNonNull(event.getEventType()); + requireNonNull(event.getOriginatorId()); + List<String> subscribers = destinationProvider.getSubscribers(); + if (CollectionUtils.isEmpty(subscribers)) { + return; + } + List<NotificationEntity> notificationEntities = subscribers.stream().map( + subscriber -> { + UUID eventId = UUIDs.timeBased(); + return createNotificationEntity(event.getEventType(), subscriber, + event.getOriginatorId(), event.getAttributes(), eventId); + }).collect(Collectors.toList()); + if(CollectionUtils.isNotEmpty(notificationEntities)) { + notificationsDao.createBatch(notificationEntities); + } + } + + private NotificationEntity createNotificationEntity(String eventType, String subscriber, + String originatorId, + Map<String, Object> attributes, + UUID eventId) { + NotificationEntity notificationEntity = + new NotificationEntity(subscriber, eventId, eventType, originatorId); + if (attributes != null && !attributes.isEmpty()) { + notificationEntity.setEventAttributes(JsonUtil.object2Json(attributes)); + } + return notificationEntity; + } +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/services/impl/SubscriptionServiceImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/services/impl/SubscriptionServiceImpl.java new file mode 100644 index 0000000000..2a12463fae --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/java/org/openecomp/sdc/notification/services/impl/SubscriptionServiceImpl.java @@ -0,0 +1,50 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.services.impl; + +import org.openecomp.sdc.notification.dao.SubscribersDao; +import org.openecomp.sdc.notification.services.SubscriptionService; + +import java.util.Set; + +public class SubscriptionServiceImpl implements SubscriptionService { + + private SubscribersDao subscribersDao; + + public SubscriptionServiceImpl(SubscribersDao subscribersDao) { + this.subscribersDao = subscribersDao; + } + + @Override + public void subscribe(String ownerId, String entityId) { + subscribersDao.subscribe(ownerId, entityId); + } + + @Override + public void unsubscribe(String ownerId, String entityId) { + subscribersDao.unsubscribe(ownerId, entityId); + } + + @Override + public Set<String> getSubscribers(String entityId) { + return subscribersDao.getSubscribers(entityId); + } +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/resources/factoryConfiguration.json b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/resources/factoryConfiguration.json new file mode 100644 index 0000000000..3f1bdf3656 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/main/resources/factoryConfiguration.json @@ -0,0 +1,9 @@ +{ + "org.openecomp.sdc.notification.factories.LastNotificationDaoFactory": "org.openecomp.sdc.notification.factories.impl.LastNotificationDaoFactoryImpl", + "org.openecomp.sdc.notification.factories.NotificationsDaoFactory": "org.openecomp.sdc.notification.factories.impl.NotificationsDaoFactoryImpl", + "org.openecomp.sdc.notification.factories.NotificationsServiceFactory": "org.openecomp.sdc.notification.factories.impl.NotificationsServiceFactoryImpl", + "org.openecomp.sdc.notification.factories.SubscribersDaoFactory": "org.openecomp.sdc.notification.factories.impl.SubscribersDaoFactoryImpl", + "org.openecomp.sdc.notification.factories.PropagationServiceFactory": "org.openecomp.sdc.notification.factories.impl.PropagationServiceFactoryImpl", + "org.openecomp.sdc.notification.factories.SubscriptionServiceFactory": "org.openecomp.sdc.notification.factories.impl.SubscriptionServiceFactoryImpl", + "org.openecomp.sdc.notification.factories.NotificationPropagationManagerFactory": "org.openecomp.sdc.notification.factories.impl.NotificationPropagationManagerFactoryImpl" +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/test/java/org/openecomp/sdc/destinationprovider/impl/MulticastDestinationTest.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/test/java/org/openecomp/sdc/destinationprovider/impl/MulticastDestinationTest.java new file mode 100644 index 0000000000..a0dd00632e --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/test/java/org/openecomp/sdc/destinationprovider/impl/MulticastDestinationTest.java @@ -0,0 +1,67 @@ +package org.openecomp.sdc.destinationprovider.impl; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.openecomp.sdc.notification.services.SubscriptionService; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; + +/** + * @author avrahamg + * @since July 13, 2017 + */ +public class MulticastDestinationTest { + @Mock + private SubscriptionService subscriptionServiceMock; + + private final String excludedSubscriber = "excluded"; + private Set<String> subscribers = new HashSet<>(Arrays.asList("a", "b", excludedSubscriber)); + private MulticastDestination multicastDestination; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @Test + public void shouldReturnAllSubscribersIfNoExcludedProvided() throws Exception { + doReturn(subscribers).when(subscriptionServiceMock).getSubscribers(any()); + multicastDestination = new MulticastDestination("aa", subscriptionServiceMock); + assertEquals(subscribers.size(), multicastDestination.getSubscribers().size()); + List<String> actualSubscribers = multicastDestination.getSubscribers(); + assertTrue(actualSubscribers.containsAll(subscribers)); + } + + @Test + public void shouldReturnAllSubscribersExceptExcluded() throws Exception { + doReturn(subscribers).when(subscriptionServiceMock).getSubscribers(any()); + multicastDestination = + new MulticastDestination("aa", subscriptionServiceMock, excludedSubscriber); + List<String> actualSubscribers = multicastDestination.getSubscribers(); + assertNotEquals(this.subscribers.size(), actualSubscribers.size()); + assertFalse(actualSubscribers.containsAll(subscribers)); + assertFalse(actualSubscribers.contains(excludedSubscriber)); + } + + @Test(expected = UnsupportedOperationException.class) + public void shouldThrowUnsupportedOperationExceptionWhenTryingToChangeSubscribersList() throws + Exception { + doReturn(subscribers).when(subscriptionServiceMock).getSubscribers(any()); + multicastDestination = + new MulticastDestination("aa", subscriptionServiceMock, excludedSubscriber); + List<String> actualSubscribers = multicastDestination.getSubscribers(); + actualSubscribers.add("sss"); + } +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/test/java/org/openecomp/sdc/notification/services/impl/NotificationPropagationManagerImplTest.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/test/java/org/openecomp/sdc/notification/services/impl/NotificationPropagationManagerImplTest.java new file mode 100644 index 0000000000..0eb2b6235c --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/test/java/org/openecomp/sdc/notification/services/impl/NotificationPropagationManagerImplTest.java @@ -0,0 +1,64 @@ +package org.openecomp.sdc.notification.services.impl; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.InjectMocks; +import org.mockito.Matchers; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.Spy; +import org.openecomp.sdc.destinationprovider.DestinationProvider; +import org.openecomp.sdc.destinationprovider.impl.MulticastDestination; +import org.openecomp.sdc.destinationprovider.impl.UnicastDestination; +import org.openecomp.sdc.notification.dtos.Event; +import org.openecomp.sdc.notification.services.PropagationService; +import org.openecomp.sdc.notification.services.SubscriptionService; + +import static org.mockito.Mockito.verify; + + +/** + * @author avrahamg + * @since July 13, 2017 + */ +public class NotificationPropagationManagerImplTest { + @Mock + private PropagationService propagationServiceMock; + @Mock + private SubscriptionService subscriptionServiceMock; + @Mock + private Event eventMock; + @Captor + private ArgumentCaptor<DestinationProvider> destinationProviderCaptor; + + @Spy + @InjectMocks + private NotificationPropagationManagerImpl notificationPropagationManager; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @Test + public void shouldCallPropagationServiceNotifyWithMulticastDestinationWhenNotifySubscribers() + throws Exception { + notificationPropagationManager.notifySubscribers(eventMock); + verify(propagationServiceMock).notify(Matchers.eq(eventMock), destinationProviderCaptor + .capture()); + Assert.assertTrue(destinationProviderCaptor.getValue() instanceof MulticastDestination); + + } + + @Test + public void shouldCallPropagationServiceNotifyWithUnicastDestinationWhenDirectNotification() + throws Exception { + notificationPropagationManager.directNotification(eventMock, "aaa"); + verify(propagationServiceMock).notify(Matchers.eq(eventMock), destinationProviderCaptor + .capture()); + Assert.assertTrue(destinationProviderCaptor.getValue() instanceof UnicastDestination); + } +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/test/java/org/openecomp/sdc/notification/services/impl/NotificationsServiceImplTest.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/test/java/org/openecomp/sdc/notification/services/impl/NotificationsServiceImplTest.java new file mode 100644 index 0000000000..a20d0cce45 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/test/java/org/openecomp/sdc/notification/services/impl/NotificationsServiceImplTest.java @@ -0,0 +1,50 @@ +package org.openecomp.sdc.notification.services.impl; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.Spy; +import org.openecomp.sdc.notification.dao.LastNotificationDao; +import org.openecomp.sdc.notification.dao.NotificationsDao; +import org.openecomp.sdc.notification.dao.types.NotificationEntity; +import org.openecomp.sdc.notification.exceptons.NotificationNotExistException; + +import java.util.UUID; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +/** + * @author avrahamg + * @since July 13, 2017 + */ +public class NotificationsServiceImplTest { + @Mock + private LastNotificationDao lastNotificationDao; + @Mock + private NotificationsDao notificationsDao; + @Spy + @InjectMocks + private NotificationsServiceImpl notificationsService; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + public void shouldCallNotificationsDaoIfNotificationEntityExist() throws Exception { + doReturn(new NotificationEntity()).when(notificationsDao).get(any()); + notificationsService.markAsRead("ownerId", UUID.randomUUID().toString()); + verify(notificationsDao, times(1)).update(any()); + } + + @Test(expected = NotificationNotExistException.class) + public void shouldThrowExceptionIfOwnerIdAndNotificationIdDontRelate() throws Exception { + doReturn(null).when(notificationsDao).get(any()); + notificationsService.markAsRead("ownerId", UUID.randomUUID().toString()); + } +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/test/java/org/openecomp/sdc/notification/services/impl/PropagationServiceImplTest.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/test/java/org/openecomp/sdc/notification/services/impl/PropagationServiceImplTest.java new file mode 100644 index 0000000000..866dec0d6d --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/src/test/java/org/openecomp/sdc/notification/services/impl/PropagationServiceImplTest.java @@ -0,0 +1,96 @@ +package org.openecomp.sdc.notification.services.impl; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.Spy; +import org.openecomp.sdc.destinationprovider.DestinationProvider; +import org.openecomp.sdc.notification.dao.NotificationsDao; +import org.openecomp.sdc.notification.dtos.Event; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; + +import static org.mockito.Matchers.anyList; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + +/** + * @author avrahamg + * @since July 13, 2017 + */ +public class PropagationServiceImplTest { + @Mock + private NotificationsDao notificationsDaoMock; + @Mock + private Event eventMock; + @Mock + private DestinationProvider destinationProviderMock; + @Captor + private ArgumentCaptor<List> createBatchCaptor; + + @Rule + public ExpectedException thrown= ExpectedException.none(); + + @InjectMocks + @Spy + private PropagationServiceImpl propagationService; + private List<String> subscribersList = Arrays.asList("A1, A2, A3");; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + initEventMock(); + } + + @Test + public void shouldCallToNotificationsDaoWithCreateBatchWithNotificationEntitiesAsNumberOfSubscribers() + throws Exception { + doReturn(subscribersList).when(destinationProviderMock).getSubscribers(); + propagationService.notify(eventMock, destinationProviderMock); + verify(notificationsDaoMock).createBatch(createBatchCaptor.capture()); + Assert.assertEquals(createBatchCaptor.getValue().size(), subscribersList.size()); + } + + @Test + public void shouldNotCallNotificationDaoIfSubscriberIsNull() throws Exception { + doReturn(Collections.EMPTY_LIST).when(destinationProviderMock).getSubscribers(); + verify(notificationsDaoMock,never()).createBatch(anyList()); + } + + @Test + public void shouldThrowExceptionIfEventTypeIsNull() throws Exception { + doReturn(null).when(eventMock).getEventType(); + callToNotify(); + } + + @Test + public void shouldThrowExceptionIfOriginatorIdIsNull() throws Exception { + doReturn(null).when(eventMock).getOriginatorId(); + callToNotify(); + } + + private void callToNotify() { + thrown.expect(NullPointerException.class); + propagationService.notify(eventMock, destinationProviderMock); + } + + private void initEventMock() { + doReturn("eventType").when(eventMock).getEventType(); + doReturn("originator").when(eventMock).getOriginatorId(); + doReturn("entity").when(eventMock).getEntityId(); + doReturn(new HashMap<>()).when(eventMock).getAttributes(); + } + + +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-websocket/pom.xml b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-websocket/pom.xml new file mode 100644 index 0000000000..abff87f1ad --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-websocket/pom.xml @@ -0,0 +1,34 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <name>openecomp-sdc-notification-websocket</name> + <artifactId>openecomp-sdc-notification-websocket</artifactId> + + <parent> + <groupId>org.openecomp.sdc</groupId> + <artifactId>openecomp-sdc-lib</artifactId> + <version>1.2.0-SNAPSHOT</version> + <relativePath>../..</relativePath> + </parent> + + <dependencies> + <dependency> + <groupId>org.openecomp.sdc</groupId> + <artifactId>openecomp-sdc-notification-worker</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-websocket</artifactId> + <version>4.3.4.RELEASE</version> + </dependency> + <dependency> + <groupId>com.google.code.gson</groupId> + <artifactId>gson</artifactId> + <version>2.3.1</version> + </dependency> + </dependencies> + +</project> diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-websocket/src/main/java/org/openecomp/sdc/notification/websocket/NotificationWebsocketHandler.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-websocket/src/main/java/org/openecomp/sdc/notification/websocket/NotificationWebsocketHandler.java new file mode 100644 index 0000000000..1e424631f7 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-websocket/src/main/java/org/openecomp/sdc/notification/websocket/NotificationWebsocketHandler.java @@ -0,0 +1,130 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.websocket; + +import com.google.gson.Gson; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; +import org.openecomp.sdc.notification.types.NotificationsStatusDto; +import org.openecomp.sdc.notification.workers.NotificationWorker; +import org.springframework.http.HttpHeaders; +import org.springframework.web.socket.CloseStatus; +import org.springframework.web.socket.TextMessage; +import org.springframework.web.socket.WebSocketSession; +import org.springframework.web.socket.handler.TextWebSocketHandler; + +import java.io.IOException; +import java.util.Objects; +import java.util.Optional; +import java.util.UUID; +import java.util.function.Consumer; + +public class NotificationWebsocketHandler extends TextWebSocketHandler { + + private static final String USER_ID_HEADER_PARAM = "USER_ID"; + private static final String LAST_DELIVERED_QUERY_PARAM = "LAST_DELIVERED_EVENT_ID"; + private static final String COOKIE = "Cookie"; + private static Logger LOGGER = LoggerFactory.getLogger(NotificationWebsocketHandler.class); + private NotificationWorker worker; + + public NotificationWebsocketHandler(NotificationWorker worker) { + super(); + this.worker = Objects.requireNonNull(worker, "NotificationWorker object is not initialized."); + } + + @Override + public void afterConnectionEstablished(WebSocketSession session) throws Exception { + super.afterConnectionEstablished(session); + + String ownerId = getOwnerId(session); + if (ownerId == null) { + return; + } + UUID lastDelivered = getLastEventId(session); + + Consumer<NotificationsStatusDto> notesProcessor = (notes) -> notifyReceiver(session, notes); + + worker.register(ownerId, lastDelivered, notesProcessor); + } + + @Override + public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { + String ownerId = getOwnerId(session); + if (ownerId != null) { + worker.unregister(ownerId); + } + super.afterConnectionClosed(session, status); + } + + private void notifyReceiver(WebSocketSession session, NotificationsStatusDto notificationsStatusDto) { + + try { + session.sendMessage(new TextMessage(new Gson().toJson(notificationsStatusDto))); + } catch (IOException e) { + LOGGER.error("IO Exception during Receiver notification.", e); + } + } + + private String getOwnerId(WebSocketSession session) { + + HttpHeaders handshakeHeaders = session.getHandshakeHeaders(); + if (handshakeHeaders.containsKey(COOKIE)) { + String[] cookies = handshakeHeaders.get(COOKIE).get(0).split("; "); + Optional<String> cookie = extractValue(cookies, USER_ID_HEADER_PARAM); + if (cookie.isPresent()) { + return cookie.get(); + } + } + + LOGGER.error("No " + USER_ID_HEADER_PARAM + " specified in the session cookies."); + return null; + } + + private UUID getLastEventId(WebSocketSession session) { + + String uriQuery = session.getUri().getQuery(); + if (uriQuery != null) { + + String[] queries = uriQuery.split("; "); + Optional<String> paramValue = extractValue(queries, LAST_DELIVERED_QUERY_PARAM); + if (paramValue.isPresent()) { + return UUID.fromString(paramValue.get()); + } + } + + LOGGER.warn("No " + LAST_DELIVERED_QUERY_PARAM + " specified in the request URI."); + return null; + } + + private Optional<String> extractValue(String[] pairs, String name) { + + for (String nameValuePair : pairs) { + + String[] value = nameValuePair.split("="); + if (value[0].equals(name)) { + return Optional.of(value[1]); + } + } + + return Optional.empty(); + } + +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/pom.xml b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/pom.xml new file mode 100644 index 0000000000..fb55d28017 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/pom.xml @@ -0,0 +1,73 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <name>openecomp-sdc-notification-worker</name> + <artifactId>openecomp-sdc-notification-worker</artifactId> + + <parent> + <groupId>org.openecomp.sdc</groupId> + <artifactId>openecomp-sdc-lib</artifactId> + <version>1.2.0-SNAPSHOT</version> + <relativePath>../..</relativePath> + </parent> + + <dependencies> + <dependency> + <groupId>org.openecomp.sdc.core</groupId> + <artifactId>openecomp-utilities-lib</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.codehaus.jackson</groupId> + <artifactId>jackson-jaxrs</artifactId> + <version>${org.codehaus.jackson.version}</version> + </dependency> + <dependency> + <groupId>org.codehaus.jackson</groupId> + <artifactId>jackson-core-asl</artifactId> + <version>${org.codehaus.jackson.version}</version> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-websocket</artifactId> + <version>4.3.4.RELEASE</version> + </dependency> + <dependency> + <groupId>com.google.code.gson</groupId> + <artifactId>gson</artifactId> + <version>2.3.1</version> + </dependency> + <dependency> + <groupId>com.googlecode.json-simple</groupId> + <artifactId>json-simple</artifactId> + <version>1.1</version> + </dependency> + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpclient</artifactId> + <version>4.4.1</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpcore</artifactId> + <version>4.4.1</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>com.datastax.cassandra</groupId> + <artifactId>cassandra-driver-core</artifactId> + <version>2.0.1</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + <version>${junit.version}</version> + </dependency> + </dependencies> + +</project> diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/config/ConfigurationManager.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/config/ConfigurationManager.java new file mode 100644 index 0000000000..d960b8b9dc --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/config/ConfigurationManager.java @@ -0,0 +1,113 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.config; + +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; +import org.openecomp.sdc.tosca.services.YamlUtil; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.function.BiConsumer; + +public class ConfigurationManager { + + private static final String CONFIGURATION_YAML_FILE = "onboarding_configuration.yaml"; + private static final String NOTIFICATIONS_CONFIG = "notifications"; + + private LinkedHashMap<String, Object> notificationsConfiguration; + private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationManager.class); + private static final ConfigurationManager SINGLETON = new ConfigurationManager(); + + public static ConfigurationManager getInstance() { + return SINGLETON; + } + + private ConfigurationManager() { + initConfiguration(); + } + + private void initConfiguration() { + + YamlUtil yamlUtil = new YamlUtil(); + readConfigurationFromStream(yamlUtil, (filename, stream) -> { + + if (stream == null) { + LOGGER.warn("Configuration not found: " + filename + ". Using defaults"); + return; + } + + Map<String, LinkedHashMap<String, Object>> configurationMap = yamlUtil.yamlToMap(stream); + if (configurationMap == null) { + LOGGER.warn("Configuration cannot be parsed: " + filename + ". Using defaults"); + return; + } + + notificationsConfiguration = configurationMap.get(NOTIFICATIONS_CONFIG); + if (notificationsConfiguration == null) { + LOGGER.error(NOTIFICATIONS_CONFIG + + " is missing in configuration file '" + filename + "'. Using defaults"); + } + }); + } + + private void readConfigurationFromStream(YamlUtil yamlUtil, + BiConsumer<String, InputStream> reader) { + + String configurationYamlFile = System.getProperty(CONFIGURATION_YAML_FILE); + + try { + + if (configurationYamlFile == null) { + + try (InputStream inputStream = + yamlUtil.loadYamlFileIs("/" + CONFIGURATION_YAML_FILE)) { + reader.accept(CONFIGURATION_YAML_FILE, inputStream); + } + + } else { + + try (InputStream inputStream = new FileInputStream(configurationYamlFile)) { + reader.accept(configurationYamlFile, inputStream); + } + } + + } catch (IOException e) { + LOGGER.error("Failed to read configuration", e); + } + } + + public <T> T getConfigValue(String name, T defaultValue) { + + Object value = notificationsConfiguration.get(name); + + try { + return value == null ? defaultValue : (T) value; + } catch (ClassCastException e) { + LOGGER.warn(String.format("Failed to read configuration property '%s' as requested type. Using default '%s'", + name, defaultValue), e); + return defaultValue; + } + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/types/NotificationEntityDto.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/types/NotificationEntityDto.java new file mode 100644 index 0000000000..a415d9e081 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/types/NotificationEntityDto.java @@ -0,0 +1,103 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.types; + +import java.util.Map; +import java.util.UUID; + +public class NotificationEntityDto { + private boolean read; + private UUID eventId; + private String dateTime; + private String eventType; + private Map<String, Object> eventAttributes; + + public NotificationEntityDto() { + } + public NotificationEntityDto(boolean read, UUID eventId, String eventType, + Map<String, Object> eventAttributes) { + this.read = read; + this.eventId = eventId; + this.eventType = eventType; + this.eventAttributes = eventAttributes; + } + + public NotificationEntityDto(boolean read, UUID eventId,String eventType, + Map<String, Object> eventAttributes, String dateTime) { + this.read = read; + this.eventId = eventId; + this.dateTime = dateTime; + this.eventType = eventType; + this.eventAttributes = eventAttributes; + } + + public boolean isRead() { + return read; + } + + public void setRead(boolean read) { + this.read = read; + } + + public UUID getEventId() { + return eventId; + } + + public void setEventId(UUID eventId) { + this.eventId = eventId; + } + + public String getEventType() { + return eventType; + } + + public void setEventType(String eventType) { + this.eventType = eventType; + } + + public Map<String, Object> getEventAttributes() { + return eventAttributes; + } + + public void setEventAttributes(Map<String, Object> eventAttributes) { + this.eventAttributes = eventAttributes; + } + + public String getDateTime() { + return dateTime; + } + + public void setDateTime(String dateTime) { + this.dateTime = dateTime; + } + + @Override + public String toString() { + return "NotificationEntityDto {" + + ", state='" + (read ? "Read" : "Noread") + '\'' + + ", dateTime='" + dateTime + '\'' + + ", eventId='" + eventId + '\'' + + ", eventType='" + eventType + '\'' + + ", eventAttributes='" + eventAttributes + '\'' + + '}'; + } + +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/types/NotificationsStatusDto.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/types/NotificationsStatusDto.java new file mode 100644 index 0000000000..fd8d32090e --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/types/NotificationsStatusDto.java @@ -0,0 +1,72 @@ +package org.openecomp.sdc.notification.types; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +/** + * @author avrahamg + * @since June 29, 2017 + */ +public class NotificationsStatusDto { + private List<NotificationEntityDto> notifications; + private List<UUID> newEntries = new ArrayList<>(); + private UUID lastScanned; + private UUID endOfPage; + private long numOfNotSeenNotifications; + + public NotificationsStatusDto() { + } + + public List<NotificationEntityDto> getNotifications() { + return notifications; + } + + public void setNotifications( + List<NotificationEntityDto> notifications) { + this.notifications = notifications; + } + + public List<UUID> getNewEntries() { + return newEntries; + } + + public void setNewEntries(List<UUID> newEntries) { + this.newEntries = newEntries; + } + + public UUID getLastScanned() { + return lastScanned; + } + + public void setLastScanned(UUID lastScanned) { + this.lastScanned = lastScanned; + } + + public UUID getEndOfPage() { + return endOfPage; + } + + public void setEndOfPage(UUID endOfPage) { + this.endOfPage = endOfPage; + } + + public long getNumOfNotSeenNotifications() { + return numOfNotSeenNotifications; + } + + public void setNumOfNotSeenNotifications(long numOfNotSeenNotifications) { + this.numOfNotSeenNotifications = numOfNotSeenNotifications; + } + + @Override + public String toString() { + return "NotificationsStatusDto{" + + "notifications=" + notifications + + ", newEntries=" + newEntries + + ", lastScanned=" + lastScanned + + ", endOfPage=" + endOfPage + + ", numOfNotSeenNotifications=" + numOfNotSeenNotifications + + '}'; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/workers/NewNotificationsReader.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/workers/NewNotificationsReader.java new file mode 100644 index 0000000000..75b9f5c786 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/workers/NewNotificationsReader.java @@ -0,0 +1,29 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.workers; + +import org.openecomp.sdc.notification.types.NotificationsStatusDto; + +import java.util.UUID; + +public interface NewNotificationsReader { + NotificationsStatusDto getNewNotifications(String ownerId, UUID eventId, int limit); +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/workers/NotificationReceiver.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/workers/NotificationReceiver.java new file mode 100644 index 0000000000..ee802640c4 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/workers/NotificationReceiver.java @@ -0,0 +1,69 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.workers; + +import org.openecomp.sdc.notification.types.NotificationsStatusDto; + +import java.util.UUID; +import java.util.function.Consumer; + + + +public class NotificationReceiver { + + public String ownerId = null; + public Consumer<NotificationsStatusDto> notesProcessor = null; + public UUID lastEventId = null; + + NotificationReceiver(String ownerId, UUID lastEventId, Consumer<NotificationsStatusDto> notesProcessor) { + this.ownerId = ownerId; + this.lastEventId = lastEventId; + this.notesProcessor = notesProcessor; + } + + NotificationReceiver(String ownerId, Consumer<NotificationsStatusDto> notesProcessor) { + this(ownerId, null, notesProcessor); + } + + public void setOwnerId(String ownerId) { + this.ownerId = ownerId; + } + + public String getOwnerId() { + return this.ownerId; + } + + public void setNotesProcessor(Consumer<NotificationsStatusDto> notesProcessor) { + this.notesProcessor = notesProcessor; + } + + public Consumer<NotificationsStatusDto> getNotesProcessor() { + return this.notesProcessor; + } + + public void setLastEventId(UUID lastEventId) { + this.lastEventId = lastEventId; + } + + public UUID getlastEventId() { + return this.lastEventId; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/workers/NotificationWorker.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/workers/NotificationWorker.java new file mode 100644 index 0000000000..e8c2006d5c --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/workers/NotificationWorker.java @@ -0,0 +1,117 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.workers; + +import org.apache.commons.collections4.CollectionUtils; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; +import org.openecomp.sdc.notification.config.ConfigurationManager; +import org.openecomp.sdc.notification.types.NotificationsStatusDto; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Consumer; + +public class NotificationWorker { + + private static final int DEFAULT_POLLING_INTERVAL = 2000; + private static final String POLLING_INTERVAL = "pollingIntervalMsec"; + private static final int DEFAULT_SELECTION_LIMIT = 10; + private static final String SELECTION_SIZE = "selectionSize"; + + private static boolean stopRunning = false; + + private int selectionLimit = DEFAULT_SELECTION_LIMIT; + private int pollingSleepInterval = DEFAULT_POLLING_INTERVAL; + + private static final Logger LOGGER = LoggerFactory.getLogger(NotificationWorker.class); + + private static Map<String, NotificationReceiver> activeUsers = new ConcurrentHashMap<>(); + private NewNotificationsReader news = null; + + public NotificationWorker(NewNotificationsReader news) { + ConfigurationManager cm = ConfigurationManager.getInstance(); + pollingSleepInterval = cm.getConfigValue(POLLING_INTERVAL, DEFAULT_POLLING_INTERVAL); + selectionLimit = cm.getConfigValue(SELECTION_SIZE, DEFAULT_SELECTION_LIMIT); + + Objects.requireNonNull(news, "NotificationNews object is not initialized."); + this.news = news; + + NotificationWorker.Poller p = new Poller(); + Thread thread = new Thread(p); + thread.start(); + } + + public Map<String, NotificationReceiver> getActiveUsers() { + return activeUsers; + } + + public class Poller extends Thread { + public void run() { + try { + while (!stopRunning) { + pollNotifications(); + Thread.sleep(pollingSleepInterval); + } + } + catch (InterruptedException e) { + LOGGER.error("Interrupted Exception during Notification poller launch.", e); + } + } + + private void pollNotifications() { + + Map<String, NotificationReceiver> currUsers = new HashMap<>(); + currUsers.putAll(getActiveUsers()); + + for (NotificationReceiver receiver : currUsers.values()) { + String ownerId = receiver.getOwnerId(); + UUID eventId = receiver.getlastEventId(); + NotificationsStatusDto status = news.getNewNotifications(ownerId, eventId, selectionLimit); + if(Objects.nonNull(status) && CollectionUtils.isNotEmpty(status.getNotifications())) { + receiver.setLastEventId(status.getLastScanned()); + receiver.getNotesProcessor().accept(status); + } + } + } + + } + + public void register(String ownerId, UUID lastDelivered, Consumer<NotificationsStatusDto> notesProcessor) { + NotificationReceiver receiver = new NotificationReceiver(ownerId, lastDelivered, notesProcessor); + activeUsers.put(ownerId, receiver); + LOGGER.debug("User {} is registered with eventId: {}", ownerId, receiver.getlastEventId()); + } + + public void unregister(String ownerId) { + activeUsers.remove(ownerId); + LOGGER.debug("User {} is unregistered.", ownerId); + } + + public void stopPolling() { + LOGGER.debug("Stop notification polling."); + stopRunning = true; + } + +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/workers/impl/NewNotificationsReaderRestImpl.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/workers/impl/NewNotificationsReaderRestImpl.java new file mode 100644 index 0000000000..a332efaf5a --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/java/org/openecomp/sdc/notification/workers/impl/NewNotificationsReaderRestImpl.java @@ -0,0 +1,84 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.workers.impl; + +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.HttpClientBuilder; +import org.codehaus.jackson.map.ObjectMapper; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; +import org.openecomp.sdc.notification.config.ConfigurationManager; +import org.openecomp.sdc.notification.types.NotificationsStatusDto; +import org.openecomp.sdc.notification.workers.NewNotificationsReader; + +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; +import java.io.InputStreamReader; +import java.util.UUID; + +public class NewNotificationsReaderRestImpl implements NewNotificationsReader { + + private static final String USER_ID_HEADER_PARAM = "USER_ID"; + private static final String LAST_DELIVERED_QUERY_PARAM = "LAST_DELIVERED_EVENT_ID"; + private static final String LIMIT_QUERY_PARAM = "NOTIFICATION_ROWS_LIMIT"; + private static final String BE_HOST = "beHost"; + private static final String BE_PORT = "beHttpPort"; + private static final String DEFAULT_BE_HOST = "localhost"; + private static final int DEFAULT_BE_PORT = 8080; + private static final String URL = "http://%s:%d/onboarding-api/v1.0/notifications/worker?"; + private static final ObjectMapper mapper = new ObjectMapper(); + + private static String beHost; + private static int bePort; + + private static final Logger LOGGER = LoggerFactory.getLogger(NewNotificationsReaderRestImpl.class); + + public NewNotificationsReaderRestImpl() { + ConfigurationManager cm = ConfigurationManager.getInstance(); + bePort = cm.getConfigValue(BE_PORT, DEFAULT_BE_PORT); + beHost = cm.getConfigValue(BE_HOST, DEFAULT_BE_HOST); + } + + public NotificationsStatusDto getNewNotifications(String ownerId, UUID eventId, int limit) { + HttpClient client = HttpClientBuilder.create().build(); + String url = String.format(URL, beHost, bePort); + + url = url + LIMIT_QUERY_PARAM + "=" + limit; + if (eventId != null) { + url = url + "&" + LAST_DELIVERED_QUERY_PARAM + "=" + eventId; + } + + HttpGet request = new HttpGet(url); + request.addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON); + request.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); + request.addHeader(USER_ID_HEADER_PARAM, ownerId); + + try { + HttpResponse response = client.execute(request); + return mapper.readValue(new InputStreamReader(response.getEntity().getContent()), NotificationsStatusDto.class); + } catch (Exception e) { + LOGGER.error("Failed to execute the request {}", url, e); + return null; + } + } +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/resources/factoryConfiguration.json b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/resources/factoryConfiguration.json new file mode 100644 index 0000000000..5f5693fb92 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/resources/factoryConfiguration.json @@ -0,0 +1,3 @@ +{ + "org.openecomp.config.api.ConfigurationManager": "org.openecomp.config.impl.ConfigurationImpl" +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/resources/onboarding_configuration.yaml b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/resources/onboarding_configuration.yaml new file mode 100644 index 0000000000..a1b7ba5d9e --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/main/resources/onboarding_configuration.yaml @@ -0,0 +1,5 @@ +notifications: + pollingIntervalMsec: 2000 + selectionSize: 10 + beHost: localhost + beHttpPort: 8080 diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/test/java/org/openecomp/sdc/notification/workers/NotificationWorkerTest.java b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/test/java/org/openecomp/sdc/notification/workers/NotificationWorkerTest.java new file mode 100644 index 0000000000..457ba071cd --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/test/java/org/openecomp/sdc/notification/workers/NotificationWorkerTest.java @@ -0,0 +1,134 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.notification.workers; + +import com.datastax.driver.core.utils.UUIDs; +import org.junit.BeforeClass; +import org.junit.Test; +import org.openecomp.core.utilities.json.JsonUtil; +import org.openecomp.sdc.notification.types.NotificationEntityDto; +import org.openecomp.sdc.notification.types.NotificationsStatusDto; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.text.DateFormat; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.UUID; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.stream.Collectors; + +//import org.junit.Before; + + +public class NotificationWorkerTest { + + private static final String OWNER = "owner_1"; + + private String fileName = null; + private NewNotificationsReader news = this.new SimNewNotificationsReader(); + private NotificationWorker worker = new NotificationWorker(news); + + @BeforeClass + public static void beforeClass() throws Exception { + } + + @Test + public void testBasicResourceCreation() throws IOException, InterruptedException { + + Consumer<NotificationsStatusDto> notesProcessor = this::notifyReceiver; + + fileName = "notification_1.csv"; + + worker.register(OWNER, null, notesProcessor); + worker.register("owner_2", null, notesProcessor); + worker.register("owner_3", null, notesProcessor); + + int pollInterval = 2000; + Thread.sleep(pollInterval); + + worker.unregister("owner_2"); + + fileName = "notification_2.csv"; + + Thread.sleep(pollInterval); + + worker.stopPolling(); + + } + + private void notifyReceiver(NotificationsStatusDto notes) { + if (Objects.nonNull(notes)) { + System.out.println("Received notes:"); + System.out.println(notes); + } + } + + + private class SimNewNotificationsReader implements NewNotificationsReader { + + + private String resourcesDir = "src/test/resources/"; + + public NotificationsStatusDto getNewNotifications(String ownerId, UUID eventId, int limit) { + if (fileName == null) { + return null; + } + String fn = fileName; + fileName = null; + + return getNotifications(fn); + } + + private NotificationsStatusDto getNotifications(String fn) { + NotificationsStatusDto notificationsStatusDto = new NotificationsStatusDto(); + List<NotificationEntityDto> inputList = new ArrayList<>(); + try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(resourcesDir + fn))))) { + int limit = 10; + inputList = br.lines().skip(1).limit(limit).map(mapToEntity).collect(Collectors.toCollection(ArrayList::new)); + } catch (IOException e) { + System.err.println("getNotifications(): file " + resourcesDir + fn + " open exception: " + e.getMessage()); + } + notificationsStatusDto.setNotifications(inputList); + notificationsStatusDto.setLastScanned(inputList.get(0).getEventId()); + return notificationsStatusDto; + } + + private Function<String, NotificationEntityDto> mapToEntity = (line) -> { + String[] p = line.split("\\|"); + NotificationEntityDto entity = new NotificationEntityDto(); + DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT); + entity.setRead(Boolean.parseBoolean(p[1])); + entity.setEventId(UUID.fromString(p[2])); + entity.setEventType(p[4]); + entity.setDateTime(formatter.format(UUIDs.unixTimestamp(entity.getEventId()))); + entity.setEventAttributes(JsonUtil.json2Object(p[5], Map.class)); + return entity; + }; + } + +} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/test/resources/logback.xml b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/test/resources/logback.xml new file mode 100644 index 0000000000..c1932e31e8 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/test/resources/logback.xml @@ -0,0 +1,15 @@ +<configuration> + + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> + <!-- encoders are assigned the type + ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --> + <encoder> + <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> + </encoder> + </appender> + + <root level="warn"> + <appender-ref ref="STDOUT" /> + </root> + +</configuration>
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/test/resources/notification_1.csv b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/test/resources/notification_1.csv new file mode 100644 index 0000000000..8b7de38ce5 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/test/resources/notification_1.csv @@ -0,0 +1,11 @@ +# Owner|Read|EventId|EventType|OriginatorId|EventAttributes +owner_1|false|1dc38a90-5e85-11e7-907b-a6006ad3dba0|type_1|orig_1|{attr_1: val_1, attr_2: val_2} +owner_1|false|1dc387d4-5e85-11e7-907b-a6006ad3dba0|type_1|orig_1|{attr_1: val_1, attr_2: val_2} +owner_1|false|1dc38702-5e85-11e7-907b-a6006ad3dba0|type_1|orig_1|{attr_1: val_1, attr_2: val_2} +owner_1|false|1dc385fe-5e85-11e7-907b-a6006ad3dba0|type_1|orig_1|{attr_1: val_1, attr_2: val_2} +owner_1|false|1dc384a0-5e85-11e7-907b-a6006ad3dba0|type_1|orig_1|{attr_1: val_1, attr_2: val_2} +owner_1|false|1dc383c4-5e85-11e7-907b-a6006ad3dba0|type_1|orig_1|{attr_1: val_1, attr_2: val_2} +owner_1|false|1dc3818a-5e85-11e7-907b-a6006ad3dba0|type_1|orig_1|{attr_1: val_1, attr_2: val_2} +owner_1|false|1dc380b8-5e85-11e7-907b-a6006ad3dba0|type_1|orig_1|{attr_1: val_1, attr_2: val_2} +owner_1|false|1dc37fdc-5e85-11e7-907b-a6006ad3dba0|type_1|orig_1|{attr_1: val_1, attr_2: val_2} +owner_1|false|1dc37ee2-5e85-11e7-907b-a6006ad3dba0|type_1|orig_1|{attr_1: val_1, attr_2: val_2} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/test/resources/notification_2.csv b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/test/resources/notification_2.csv new file mode 100644 index 0000000000..565e80bce8 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/test/resources/notification_2.csv @@ -0,0 +1,11 @@ +# Owner|Read|EventId|EventType|OriginatorId|EventAttributes +owner_1|false|1dc39346-5e85-11e7-907b-a6006ad3dba0|type_1|orig_1|{attr_1: val_1, attr_2: val_2} +owner_1|false|1dc39288-5e85-11e7-907b-a6006ad3dba0|type_1|orig_1|{attr_1: val_1, attr_2: val_2} +owner_1|false|1dc391c0-5e85-11e7-907b-a6006ad3dba0|type_1|orig_1|{attr_1: val_1, attr_2: val_2} +owner_1|false|1dc38f2c-5e85-11e7-907b-a6006ad3dba0|type_1|orig_1|{attr_1: val_1, attr_2: val_2} +owner_1|false|1dc38e64-5e85-11e7-907b-a6006ad3dba0|type_1|orig_1|{attr_1: val_1, attr_2: val_2} +owner_1|false|1dc38da6-5e85-11e7-907b-a6006ad3dba0|type_1|orig_1|{attr_1: val_1, attr_2: val_2} +owner_1|false|1dc38ce8-5e85-11e7-907b-a6006ad3dba0|type_1|orig_1|{attr_1: val_1, attr_2: val_2} +owner_1|false|1dc38c2a-5e85-11e7-907b-a6006ad3dba0|type_1|orig_1|{attr_1: val_1, attr_2: val_2} +owner_1|false|1dc38b62-5e85-11e7-907b-a6006ad3dba0|type_1|orig_1|{attr_1: val_1, attr_2: val_2} +owner_1|false|1dc37c08-5e85-11e7-907b-a6006ad3dba0|type_1|orig_1|{attr_1: val_1, attr_2: val_2} diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/test/resources/onboarding_configuration.yaml b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/test/resources/onboarding_configuration.yaml new file mode 100644 index 0000000000..cc04531c7c --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-worker/src/test/resources/onboarding_configuration.yaml @@ -0,0 +1,5 @@ +notifications: + pollingIntervalMsec: 3000 + selectionSize: 5 + beHost: localhost + beHttpPort: 8080 diff --git a/openecomp-be/lib/openecomp-sdc-notification-lib/pom.xml b/openecomp-be/lib/openecomp-sdc-notification-lib/pom.xml new file mode 100644 index 0000000000..9dc0dcfd3e --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-notification-lib/pom.xml @@ -0,0 +1,23 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <name>openecomp-sdc-notification-lib</name> + <artifactId>openecomp-sdc-notification-lib</artifactId> + + <packaging>pom</packaging> + <parent> + <artifactId>openecomp-sdc-lib</artifactId> + <groupId>org.openecomp.sdc</groupId> + <version>1.2.0-SNAPSHOT</version> + <relativePath>..</relativePath> + </parent> + + <modules> + <module>openecomp-sdc-notification-api</module> + <module>openecomp-sdc-notification-core</module> + <module>openecomp-sdc-notification-worker</module> + <module>openecomp-sdc-notification-websocket</module> + </modules> + +</project> |