Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.flow.impl; |
Yuta HIGUCHI | b25f4da | 2014-10-16 15:09:26 -0700 | [diff] [blame] | 17 | |
Yuta HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
Yuta HIGUCHI | b25f4da | 2014-10-16 15:09:26 -0700 | [diff] [blame] | 19 | import static org.slf4j.LoggerFactory.getLogger; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import static org.onosproject.store.flow.ReplicaInfoEvent.Type.MASTER_CHANGED; |
| 21 | import static org.onosproject.store.flow.ReplicaInfoEvent.Type.BACKUPS_CHANGED; |
Yuta HIGUCHI | b25f4da | 2014-10-16 15:09:26 -0700 | [diff] [blame] | 22 | |
| 23 | import java.util.Collections; |
Yuta HIGUCHI | b25f4da | 2014-10-16 15:09:26 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Component; |
| 26 | import org.apache.felix.scr.annotations.Deactivate; |
| 27 | import org.apache.felix.scr.annotations.Reference; |
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 29 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.cluster.NodeId; |
| 31 | import org.onosproject.event.AbstractListenerRegistry; |
| 32 | import org.onosproject.event.EventDeliveryService; |
| 33 | import org.onosproject.mastership.MastershipEvent; |
| 34 | import org.onosproject.mastership.MastershipListener; |
| 35 | import org.onosproject.mastership.MastershipService; |
| 36 | import org.onosproject.net.DeviceId; |
| 37 | import org.onosproject.store.flow.ReplicaInfo; |
| 38 | import org.onosproject.store.flow.ReplicaInfoEvent; |
| 39 | import org.onosproject.store.flow.ReplicaInfoEventListener; |
| 40 | import org.onosproject.store.flow.ReplicaInfoService; |
Yuta HIGUCHI | b25f4da | 2014-10-16 15:09:26 -0700 | [diff] [blame] | 41 | import org.slf4j.Logger; |
| 42 | |
| 43 | /** |
| 44 | * Manages replica placement information. |
| 45 | */ |
| 46 | @Component(immediate = true) |
| 47 | @Service |
| 48 | public class ReplicaInfoManager implements ReplicaInfoService { |
| 49 | |
| 50 | private final Logger log = getLogger(getClass()); |
| 51 | |
| 52 | private final MastershipListener mastershipListener = new InternalMastershipListener(); |
| 53 | |
| 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 55 | protected EventDeliveryService eventDispatcher; |
| 56 | |
| 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 58 | protected MastershipService mastershipService; |
| 59 | |
| 60 | protected final AbstractListenerRegistry<ReplicaInfoEvent, ReplicaInfoEventListener> |
| 61 | listenerRegistry = new AbstractListenerRegistry<>(); |
| 62 | |
| 63 | @Activate |
| 64 | public void activate() { |
| 65 | eventDispatcher.addSink(ReplicaInfoEvent.class, listenerRegistry); |
| 66 | mastershipService.addListener(mastershipListener); |
| 67 | log.info("Started"); |
| 68 | } |
| 69 | |
| 70 | @Deactivate |
| 71 | public void deactivate() { |
| 72 | eventDispatcher.removeSink(ReplicaInfoEvent.class); |
| 73 | mastershipService.removeListener(mastershipListener); |
| 74 | log.info("Stopped"); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public ReplicaInfo getReplicaInfoFor(DeviceId deviceId) { |
| 79 | // TODO: populate backup List when we reach the point we need them. |
| 80 | return new ReplicaInfo(mastershipService.getMasterFor(deviceId), |
| 81 | Collections.<NodeId>emptyList()); |
| 82 | } |
| 83 | |
Yuta HIGUCHI | 4d18795 | 2014-10-16 19:59:35 -0700 | [diff] [blame] | 84 | @Override |
| 85 | public void addListener(ReplicaInfoEventListener listener) { |
| 86 | listenerRegistry.addListener(checkNotNull(listener)); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void removeListener(ReplicaInfoEventListener listener) { |
| 91 | listenerRegistry.removeListener(checkNotNull(listener)); |
| 92 | } |
| 93 | |
Yuta HIGUCHI | b25f4da | 2014-10-16 15:09:26 -0700 | [diff] [blame] | 94 | final class InternalMastershipListener implements MastershipListener { |
| 95 | |
| 96 | @Override |
| 97 | public void event(MastershipEvent event) { |
Yuta HIGUCHI | d4ab808 | 2014-11-03 11:09:09 -0800 | [diff] [blame] | 98 | final ReplicaInfo replicaInfo |
| 99 | = new ReplicaInfo(event.roleInfo().master(), |
| 100 | event.roleInfo().backups()); |
| 101 | |
| 102 | switch (event.type()) { |
| 103 | case MASTER_CHANGED: |
| 104 | eventDispatcher.post(new ReplicaInfoEvent(MASTER_CHANGED, |
| 105 | event.subject(), |
| 106 | replicaInfo)); |
| 107 | break; |
| 108 | case BACKUPS_CHANGED: |
| 109 | eventDispatcher.post(new ReplicaInfoEvent(BACKUPS_CHANGED, |
| 110 | event.subject(), |
| 111 | replicaInfo)); |
| 112 | break; |
| 113 | default: |
| 114 | break; |
| 115 | } |
Yuta HIGUCHI | b25f4da | 2014-10-16 15:09:26 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | } |