Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.opencord.igmpproxy.impl.store.groupmember; |
| 17 | |
| 18 | import org.onlab.util.KryoNamespace; |
| 19 | import org.onosproject.store.serializers.KryoNamespaces; |
| 20 | import org.onosproject.store.service.ConsistentMap; |
| 21 | import org.onosproject.store.service.Serializer; |
| 22 | import org.onosproject.store.service.StorageService; |
| 23 | import org.opencord.igmpproxy.GroupMemberId; |
| 24 | import org.opencord.igmpproxy.GroupMemberIdSerializer; |
| 25 | import org.osgi.service.component.annotations.Component; |
| 26 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 27 | import org.osgi.service.component.annotations.Reference; |
| 28 | import org.osgi.service.component.annotations.Activate; |
| 29 | import org.osgi.service.component.annotations.Deactivate; |
| 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
| 33 | /** |
| 34 | * Group member store based on distributed storage. |
| 35 | */ |
| 36 | @Component(service = GroupMemberStore.class) |
| 37 | public class DistributedGroupMemberStore extends AbstractGroupMemberStore { |
| 38 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 39 | private static final String GROUP_MEMBER_MAP_NAME = "onos-igmpproxy-groupmember-table"; |
| 40 | |
| 41 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 42 | protected StorageService storageService; |
| 43 | |
| 44 | private ConsistentMap<GroupMemberId, GroupMember> consistentMap; |
| 45 | |
| 46 | public DistributedGroupMemberStore() { |
| 47 | super(); |
| 48 | } |
| 49 | |
| 50 | @Activate |
| 51 | public void activate() { |
| 52 | KryoNamespace groupMemberSerializer = KryoNamespace.newBuilder() |
| 53 | .register(KryoNamespaces.API) |
| 54 | .register(new GroupMemberIdSerializer(), GroupMemberId.class) |
| 55 | .register(GroupMember.class) |
| 56 | .build(); |
| 57 | consistentMap = storageService.<GroupMemberId, GroupMember>consistentMapBuilder() |
| 58 | .withName(GROUP_MEMBER_MAP_NAME) |
| 59 | .withSerializer(Serializer.using(groupMemberSerializer)) |
| 60 | .build(); |
| 61 | groupMemberMap = consistentMap.asJavaMap(); |
| 62 | log.info("Started."); |
| 63 | } |
| 64 | |
| 65 | @Deactivate |
| 66 | public void deactivate() { |
| 67 | groupMemberMap.clear(); |
| 68 | groupMemberMap = null; |
| 69 | consistentMap.destroy(); |
| 70 | log.info("Stopped."); |
| 71 | } |
| 72 | } |