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