Add collaboration feature
[sdc.git] / openecomp-be / lib / openecomp-sdc-notification-lib / openecomp-sdc-notification-core / src / main / java / org / openecomp / sdc / notification / dao / impl / SubscribersDaoCassandraImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.notification.dao.impl;
22
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;
33
34 import java.util.Collection;
35 import java.util.Collections;
36 import java.util.Objects;
37 import java.util.Set;
38
39 import static java.util.Objects.isNull;
40
41 public class SubscribersDaoCassandraImpl extends CassandraBaseDao<SubscribersEntity> implements
42         SubscribersDao {
43
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);
49
50
51     @Override
52     protected Object[] getKeys(SubscribersEntity entity) {
53         return new Object[]{entity.getEntityId()};
54     }
55
56     @Override
57     protected Mapper<SubscribersEntity> getMapper() {
58         return mapper;
59     }
60
61     @Override
62     public void subscribe(String ownerId, String entityId) {
63         Objects.requireNonNull(ownerId);
64         Objects.requireNonNull(entityId);
65         accessor.subscribe(Sets.newHashSet(ownerId), entityId);
66     }
67
68     @Override
69     @Deprecated
70     public Collection<SubscribersEntity> list(SubscribersEntity entity) {
71         throw new UnsupportedOperationException();
72     }
73
74     @Override
75     public void unsubscribe(String ownerId, String entityId) {
76         Objects.requireNonNull(ownerId);
77         Objects.requireNonNull(entityId);
78         accessor.unsubscribe(Sets.newHashSet(ownerId), entityId);
79     }
80
81     @Override
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();
87         }
88         return subscribersEntity.getSubscribers();
89     }
90
91     @Accessor
92     interface SubscribersAccessor {
93
94         @Query("select * from notification_subscribers where entity_id=?")
95         Result<SubscribersEntity> getSubscribers(String entityId);
96
97         @Query("update notification_subscribers set subscribers=subscribers+? WHERE entity_id=?")
98         void subscribe(Set<String> ownerId, String entityId);
99
100         @Query("update notification_subscribers set subscribers=subscribers-? WHERE entity_id=?")
101         void unsubscribe(Set<String> ownerId, String entityId);
102     }
103
104 }