blob: 7c3bed70dff91c8f211c82cdb286a1f24991f5a1 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.flow.impl;
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070017
Jordan Haltermane74e6292018-05-15 00:52:31 -070018import org.onosproject.event.EventDeliveryService;
19import org.onosproject.event.ListenerRegistry;
20import org.onosproject.mastership.MastershipEvent;
Jordan Halterman0a2bd452018-06-13 17:24:58 -070021import org.onosproject.mastership.MastershipInfo;
Jordan Haltermane74e6292018-05-15 00:52:31 -070022import org.onosproject.mastership.MastershipListener;
23import org.onosproject.mastership.MastershipService;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.DeviceId;
25import org.onosproject.store.flow.ReplicaInfo;
26import org.onosproject.store.flow.ReplicaInfoEvent;
27import org.onosproject.store.flow.ReplicaInfoEventListener;
28import org.onosproject.store.flow.ReplicaInfoService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070029import org.osgi.service.component.annotations.Activate;
30import org.osgi.service.component.annotations.Component;
31import org.osgi.service.component.annotations.Deactivate;
32import org.osgi.service.component.annotations.Reference;
33import org.osgi.service.component.annotations.ReferenceCardinality;
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070034import org.slf4j.Logger;
35
Simon Huntff663742015-05-14 13:33:05 -070036import static com.google.common.base.Preconditions.checkNotNull;
37import static org.onosproject.store.flow.ReplicaInfoEvent.Type.BACKUPS_CHANGED;
38import static org.onosproject.store.flow.ReplicaInfoEvent.Type.MASTER_CHANGED;
39import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani0f6ad142015-05-13 17:10:04 -070040
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070041/**
42 * Manages replica placement information.
43 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070044@Component(immediate = true, service = ReplicaInfoService.class)
Jordan Haltermane74e6292018-05-15 00:52:31 -070045public class ReplicaInfoManager implements ReplicaInfoService {
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070046
47 private final Logger log = getLogger(getClass());
48
Jordan Haltermane74e6292018-05-15 00:52:31 -070049 private final MastershipListener mastershipListener = new InternalMastershipListener();
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070050
Ray Milkeyd84f89b2018-08-17 14:54:17 -070051 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jordan Haltermane74e6292018-05-15 00:52:31 -070052 protected EventDeliveryService eventDispatcher;
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070053
Ray Milkeyd84f89b2018-08-17 14:54:17 -070054 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jordan Haltermane74e6292018-05-15 00:52:31 -070055 protected MastershipService mastershipService;
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070056
Jordan Haltermane74e6292018-05-15 00:52:31 -070057 protected final ListenerRegistry<ReplicaInfoEvent, ReplicaInfoEventListener>
58 listenerRegistry = new ListenerRegistry<>();
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070059
60 @Activate
61 public void activate() {
62 eventDispatcher.addSink(ReplicaInfoEvent.class, listenerRegistry);
Jordan Haltermane74e6292018-05-15 00:52:31 -070063 mastershipService.addListener(mastershipListener);
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070064 log.info("Started");
65 }
66
67 @Deactivate
68 public void deactivate() {
69 eventDispatcher.removeSink(ReplicaInfoEvent.class);
Jordan Haltermane74e6292018-05-15 00:52:31 -070070 mastershipService.removeListener(mastershipListener);
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070071 log.info("Stopped");
72 }
73
74 @Override
75 public ReplicaInfo getReplicaInfoFor(DeviceId deviceId) {
Jordan Halterman0a2bd452018-06-13 17:24:58 -070076 return buildFromRoleInfo(mastershipService.getMastershipFor(deviceId));
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070077 }
78
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070079 @Override
80 public void addListener(ReplicaInfoEventListener listener) {
81 listenerRegistry.addListener(checkNotNull(listener));
82 }
83
84 @Override
85 public void removeListener(ReplicaInfoEventListener listener) {
86 listenerRegistry.removeListener(checkNotNull(listener));
87 }
88
Jordan Halterman0a2bd452018-06-13 17:24:58 -070089 private static ReplicaInfo buildFromRoleInfo(MastershipInfo mastership) {
90 return new ReplicaInfo(mastership.term(), mastership.master().orElse(null), mastership.backups());
Madan Jampani86940d92015-05-06 11:47:57 -070091 }
92
Jordan Haltermane74e6292018-05-15 00:52:31 -070093 final class InternalMastershipListener implements MastershipListener {
94
95 @Override
96 public void event(MastershipEvent event) {
Jordan Halterman0a2bd452018-06-13 17:24:58 -070097 final ReplicaInfo replicaInfo = buildFromRoleInfo(event.mastershipInfo());
Jordan Haltermane74e6292018-05-15 00:52:31 -070098 switch (event.type()) {
99 case MASTER_CHANGED:
100 eventDispatcher.post(new ReplicaInfoEvent(MASTER_CHANGED,
101 event.subject(),
102 replicaInfo));
103 break;
104 case BACKUPS_CHANGED:
105 eventDispatcher.post(new ReplicaInfoEvent(BACKUPS_CHANGED,
106 event.subject(),
107 replicaInfo));
108 break;
109 default:
110 break;
111 }
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -0700112 }
113 }
114
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -0700115}