blob: 5d766e63a6d3c63360e6d28fa52f806024a31b4d [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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 */
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070016package org.onlab.onos.store.flow.impl;
17
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070018import static com.google.common.base.Preconditions.checkNotNull;
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070019import static org.slf4j.LoggerFactory.getLogger;
20import static org.onlab.onos.store.flow.ReplicaInfoEvent.Type.MASTER_CHANGED;
21
22import java.util.Collections;
23import java.util.List;
24
25import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
28import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
30import org.apache.felix.scr.annotations.Service;
31import org.onlab.onos.cluster.NodeId;
32import org.onlab.onos.event.AbstractListenerRegistry;
33import org.onlab.onos.event.EventDeliveryService;
34import org.onlab.onos.mastership.MastershipEvent;
35import org.onlab.onos.mastership.MastershipListener;
36import org.onlab.onos.mastership.MastershipService;
37import org.onlab.onos.net.DeviceId;
38import org.onlab.onos.store.flow.ReplicaInfo;
39import org.onlab.onos.store.flow.ReplicaInfoEvent;
40import org.onlab.onos.store.flow.ReplicaInfoEventListener;
41import org.onlab.onos.store.flow.ReplicaInfoService;
42import org.slf4j.Logger;
43
44/**
45 * Manages replica placement information.
46 */
47@Component(immediate = true)
48@Service
49public class ReplicaInfoManager implements ReplicaInfoService {
50
51 private final Logger log = getLogger(getClass());
52
53 private final MastershipListener mastershipListener = new InternalMastershipListener();
54
55 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
56 protected EventDeliveryService eventDispatcher;
57
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 protected MastershipService mastershipService;
60
61 protected final AbstractListenerRegistry<ReplicaInfoEvent, ReplicaInfoEventListener>
62 listenerRegistry = new AbstractListenerRegistry<>();
63
64 @Activate
65 public void activate() {
66 eventDispatcher.addSink(ReplicaInfoEvent.class, listenerRegistry);
67 mastershipService.addListener(mastershipListener);
68 log.info("Started");
69 }
70
71 @Deactivate
72 public void deactivate() {
73 eventDispatcher.removeSink(ReplicaInfoEvent.class);
74 mastershipService.removeListener(mastershipListener);
75 log.info("Stopped");
76 }
77
78 @Override
79 public ReplicaInfo getReplicaInfoFor(DeviceId deviceId) {
80 // TODO: populate backup List when we reach the point we need them.
81 return new ReplicaInfo(mastershipService.getMasterFor(deviceId),
82 Collections.<NodeId>emptyList());
83 }
84
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070085 @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
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070095 final class InternalMastershipListener implements MastershipListener {
96
97 @Override
98 public void event(MastershipEvent event) {
99 // TODO: distinguish stby list update, when MastershipService,
100 // start publishing them
101 final List<NodeId> standbyList = Collections.<NodeId>emptyList();
102 eventDispatcher.post(new ReplicaInfoEvent(MASTER_CHANGED,
103 event.subject(),
Ayaka Koshibea7384a82014-10-22 18:59:06 -0700104 new ReplicaInfo(event.roleInfo().master(), standbyList)));
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -0700105 }
106 }
107
108}