blob: 79c9147c781c87d018f79473d430c7f16b98283f [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 com.google.common.collect.ImmutableList;
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
Jordan Haltermane74e6292018-05-15 00:52:31 -070025import org.onosproject.cluster.NodeId;
26import org.onosproject.cluster.RoleInfo;
27import org.onosproject.event.EventDeliveryService;
28import org.onosproject.event.ListenerRegistry;
29import org.onosproject.mastership.MastershipEvent;
30import org.onosproject.mastership.MastershipListener;
31import org.onosproject.mastership.MastershipService;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.DeviceId;
33import org.onosproject.store.flow.ReplicaInfo;
34import org.onosproject.store.flow.ReplicaInfoEvent;
35import org.onosproject.store.flow.ReplicaInfoEventListener;
36import org.onosproject.store.flow.ReplicaInfoService;
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070037import org.slf4j.Logger;
38
Jordan Haltermane74e6292018-05-15 00:52:31 -070039import java.util.Collections;
40import java.util.List;
Simon Huntff663742015-05-14 13:33:05 -070041import static com.google.common.base.Preconditions.checkNotNull;
42import static org.onosproject.store.flow.ReplicaInfoEvent.Type.BACKUPS_CHANGED;
43import static org.onosproject.store.flow.ReplicaInfoEvent.Type.MASTER_CHANGED;
44import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani0f6ad142015-05-13 17:10:04 -070045
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070046/**
47 * Manages replica placement information.
48 */
49@Component(immediate = true)
50@Service
Jordan Haltermane74e6292018-05-15 00:52:31 -070051public class ReplicaInfoManager implements ReplicaInfoService {
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070052
53 private final Logger log = getLogger(getClass());
54
Jordan Haltermane74e6292018-05-15 00:52:31 -070055 private final MastershipListener mastershipListener = new InternalMastershipListener();
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070056
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jordan Haltermane74e6292018-05-15 00:52:31 -070058 protected EventDeliveryService eventDispatcher;
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070059
Jordan Haltermane74e6292018-05-15 00:52:31 -070060 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
61 protected MastershipService mastershipService;
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070062
Jordan Haltermane74e6292018-05-15 00:52:31 -070063 protected final ListenerRegistry<ReplicaInfoEvent, ReplicaInfoEventListener>
64 listenerRegistry = new ListenerRegistry<>();
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070065
66 @Activate
67 public void activate() {
68 eventDispatcher.addSink(ReplicaInfoEvent.class, listenerRegistry);
Jordan Haltermane74e6292018-05-15 00:52:31 -070069 mastershipService.addListener(mastershipListener);
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070070 log.info("Started");
71 }
72
73 @Deactivate
74 public void deactivate() {
75 eventDispatcher.removeSink(ReplicaInfoEvent.class);
Jordan Haltermane74e6292018-05-15 00:52:31 -070076 mastershipService.removeListener(mastershipListener);
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070077 log.info("Stopped");
78 }
79
80 @Override
81 public ReplicaInfo getReplicaInfoFor(DeviceId deviceId) {
Jordan Haltermane74e6292018-05-15 00:52:31 -070082 return buildFromRoleInfo(mastershipService.getNodesFor(deviceId));
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070083 }
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
Jordan Haltermane74e6292018-05-15 00:52:31 -070095 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 Jampani86940d92015-05-06 11:47:57 -070099 }
100
Jordan Haltermane74e6292018-05-15 00:52:31 -0700101 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 HIGUCHIb25f4da2014-10-16 15:09:26 -0700120 }
121 }
122
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -0700123}