2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.notification.dao.impl;
23 import com.datastax.driver.mapping.Mapper;
24 import com.datastax.driver.mapping.Result;
25 import com.datastax.driver.mapping.annotations.Accessor;
26 import com.datastax.driver.mapping.annotations.Query;
27 import com.google.common.collect.Sets;
28 import org.openecomp.core.dao.impl.CassandraBaseDao;
29 import org.openecomp.core.nosqldb.api.NoSqlDb;
30 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
31 import org.openecomp.sdc.notification.dao.SubscribersDao;
32 import org.openecomp.sdc.notification.dao.types.SubscribersEntity;
34 import java.util.Collection;
35 import java.util.Collections;
36 import java.util.Objects;
39 import static java.util.Objects.isNull;
41 public class SubscribersDaoCassandraImpl extends CassandraBaseDao<SubscribersEntity> implements
44 private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
45 private static final Mapper<SubscribersEntity> mapper =
46 noSqlDb.getMappingManager().mapper(SubscribersEntity.class);
47 private static final SubscribersAccessor accessor =
48 noSqlDb.getMappingManager().createAccessor(SubscribersAccessor.class);
52 protected Object[] getKeys(SubscribersEntity entity) {
53 return new Object[]{entity.getEntityId()};
57 protected Mapper<SubscribersEntity> getMapper() {
62 public void subscribe(String ownerId, String entityId) {
63 Objects.requireNonNull(ownerId);
64 Objects.requireNonNull(entityId);
65 accessor.subscribe(Sets.newHashSet(ownerId), entityId);
70 public Collection<SubscribersEntity> list(SubscribersEntity entity) {
71 throw new UnsupportedOperationException();
75 public void unsubscribe(String ownerId, String entityId) {
76 Objects.requireNonNull(ownerId);
77 Objects.requireNonNull(entityId);
78 accessor.unsubscribe(Sets.newHashSet(ownerId), entityId);
82 public Set<String> getSubscribers(String entityId) {
83 Objects.requireNonNull(entityId);
84 SubscribersEntity subscribersEntity = accessor.getSubscribers(entityId).one();
85 if (isNull(subscribersEntity)) {
86 return Collections.emptySet();
88 return subscribersEntity.getSubscribers();
92 interface SubscribersAccessor {
94 @Query("select * from notification_subscribers where entity_id=?")
95 Result<SubscribersEntity> getSubscribers(String entityId);
97 @Query("update notification_subscribers set subscribers=subscribers+? WHERE entity_id=?")
98 void subscribe(Set<String> ownerId, String entityId);
100 @Query("update notification_subscribers set subscribers=subscribers-? WHERE entity_id=?")
101 void unsubscribe(Set<String> ownerId, String entityId);