blob: c3fa99a7ec5dd58914629889083dbf11912bf705 [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;
Yuta HIGUCHId4ab8082014-11-03 11:09:09 -080021import static org.onlab.onos.store.flow.ReplicaInfoEvent.Type.BACKUPS_CHANGED;
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070022
23import java.util.Collections;
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070024import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Deactivate;
27import org.apache.felix.scr.annotations.Reference;
28import org.apache.felix.scr.annotations.ReferenceCardinality;
29import org.apache.felix.scr.annotations.Service;
30import org.onlab.onos.cluster.NodeId;
31import org.onlab.onos.event.AbstractListenerRegistry;
32import org.onlab.onos.event.EventDeliveryService;
33import org.onlab.onos.mastership.MastershipEvent;
34import org.onlab.onos.mastership.MastershipListener;
35import org.onlab.onos.mastership.MastershipService;
36import org.onlab.onos.net.DeviceId;
37import org.onlab.onos.store.flow.ReplicaInfo;
38import org.onlab.onos.store.flow.ReplicaInfoEvent;
39import org.onlab.onos.store.flow.ReplicaInfoEventListener;
40import org.onlab.onos.store.flow.ReplicaInfoService;
41import org.slf4j.Logger;
42
43/**
44 * Manages replica placement information.
45 */
46@Component(immediate = true)
47@Service
48public class ReplicaInfoManager implements ReplicaInfoService {
49
50 private final Logger log = getLogger(getClass());
51
52 private final MastershipListener mastershipListener = new InternalMastershipListener();
53
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected EventDeliveryService eventDispatcher;
56
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 protected MastershipService mastershipService;
59
60 protected final AbstractListenerRegistry<ReplicaInfoEvent, ReplicaInfoEventListener>
61 listenerRegistry = new AbstractListenerRegistry<>();
62
63 @Activate
64 public void activate() {
65 eventDispatcher.addSink(ReplicaInfoEvent.class, listenerRegistry);
66 mastershipService.addListener(mastershipListener);
67 log.info("Started");
68 }
69
70 @Deactivate
71 public void deactivate() {
72 eventDispatcher.removeSink(ReplicaInfoEvent.class);
73 mastershipService.removeListener(mastershipListener);
74 log.info("Stopped");
75 }
76
77 @Override
78 public ReplicaInfo getReplicaInfoFor(DeviceId deviceId) {
79 // TODO: populate backup List when we reach the point we need them.
80 return new ReplicaInfo(mastershipService.getMasterFor(deviceId),
81 Collections.<NodeId>emptyList());
82 }
83
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070084 @Override
85 public void addListener(ReplicaInfoEventListener listener) {
86 listenerRegistry.addListener(checkNotNull(listener));
87 }
88
89 @Override
90 public void removeListener(ReplicaInfoEventListener listener) {
91 listenerRegistry.removeListener(checkNotNull(listener));
92 }
93
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070094 final class InternalMastershipListener implements MastershipListener {
95
96 @Override
97 public void event(MastershipEvent event) {
Yuta HIGUCHId4ab8082014-11-03 11:09:09 -080098 final ReplicaInfo replicaInfo
99 = new ReplicaInfo(event.roleInfo().master(),
100 event.roleInfo().backups());
101
102 switch (event.type()) {
103 case MASTER_CHANGED:
104 eventDispatcher.post(new ReplicaInfoEvent(MASTER_CHANGED,
105 event.subject(),
106 replicaInfo));
107 break;
108 case BACKUPS_CHANGED:
109 eventDispatcher.post(new ReplicaInfoEvent(BACKUPS_CHANGED,
110 event.subject(),
111 replicaInfo));
112 break;
113 default:
114 break;
115 }
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -0700116 }
117 }
118
119}