blob: cfa998d7c2be4e1c522f7411da28b2bd4958d32a [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
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070018import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
23import org.apache.felix.scr.annotations.Service;
Jordan Haltermane74e6292018-05-15 00:52:31 -070024import org.onosproject.event.EventDeliveryService;
25import org.onosproject.event.ListenerRegistry;
26import org.onosproject.mastership.MastershipEvent;
Jordan Halterman0a2bd452018-06-13 17:24:58 -070027import org.onosproject.mastership.MastershipInfo;
Jordan Haltermane74e6292018-05-15 00:52:31 -070028import org.onosproject.mastership.MastershipListener;
29import org.onosproject.mastership.MastershipService;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.DeviceId;
31import org.onosproject.store.flow.ReplicaInfo;
32import org.onosproject.store.flow.ReplicaInfoEvent;
33import org.onosproject.store.flow.ReplicaInfoEventListener;
34import org.onosproject.store.flow.ReplicaInfoService;
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070035import org.slf4j.Logger;
36
Simon Huntff663742015-05-14 13:33:05 -070037import static com.google.common.base.Preconditions.checkNotNull;
38import static org.onosproject.store.flow.ReplicaInfoEvent.Type.BACKUPS_CHANGED;
39import static org.onosproject.store.flow.ReplicaInfoEvent.Type.MASTER_CHANGED;
40import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani0f6ad142015-05-13 17:10:04 -070041
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070042/**
43 * Manages replica placement information.
44 */
45@Component(immediate = true)
46@Service
Jordan Haltermane74e6292018-05-15 00:52:31 -070047public class ReplicaInfoManager implements ReplicaInfoService {
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070048
49 private final Logger log = getLogger(getClass());
50
Jordan Haltermane74e6292018-05-15 00:52:31 -070051 private final MastershipListener mastershipListener = new InternalMastershipListener();
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070052
53 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jordan Haltermane74e6292018-05-15 00:52:31 -070054 protected EventDeliveryService eventDispatcher;
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070055
Jordan Haltermane74e6292018-05-15 00:52:31 -070056 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected MastershipService mastershipService;
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070058
Jordan Haltermane74e6292018-05-15 00:52:31 -070059 protected final ListenerRegistry<ReplicaInfoEvent, ReplicaInfoEventListener>
60 listenerRegistry = new ListenerRegistry<>();
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070061
62 @Activate
63 public void activate() {
64 eventDispatcher.addSink(ReplicaInfoEvent.class, listenerRegistry);
Jordan Haltermane74e6292018-05-15 00:52:31 -070065 mastershipService.addListener(mastershipListener);
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070066 log.info("Started");
67 }
68
69 @Deactivate
70 public void deactivate() {
71 eventDispatcher.removeSink(ReplicaInfoEvent.class);
Jordan Haltermane74e6292018-05-15 00:52:31 -070072 mastershipService.removeListener(mastershipListener);
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070073 log.info("Stopped");
74 }
75
76 @Override
77 public ReplicaInfo getReplicaInfoFor(DeviceId deviceId) {
Jordan Halterman0a2bd452018-06-13 17:24:58 -070078 return buildFromRoleInfo(mastershipService.getMastershipFor(deviceId));
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070079 }
80
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070081 @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 Halterman0a2bd452018-06-13 17:24:58 -070091 private static ReplicaInfo buildFromRoleInfo(MastershipInfo mastership) {
92 return new ReplicaInfo(mastership.term(), mastership.master().orElse(null), mastership.backups());
Madan Jampani86940d92015-05-06 11:47:57 -070093 }
94
Jordan Haltermane74e6292018-05-15 00:52:31 -070095 final class InternalMastershipListener implements MastershipListener {
96
97 @Override
98 public void event(MastershipEvent event) {
Jordan Halterman0a2bd452018-06-13 17:24:58 -070099 final ReplicaInfo replicaInfo = buildFromRoleInfo(event.mastershipInfo());
Jordan Haltermane74e6292018-05-15 00:52:31 -0700100 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 HIGUCHIb25f4da2014-10-16 15:09:26 -0700114 }
115 }
116
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -0700117}