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