blob: eaf3f8eade703b84a2e2a1c67fb7352fb4bb6edc [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.flow.impl;
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070017
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;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import static org.onosproject.store.flow.ReplicaInfoEvent.Type.MASTER_CHANGED;
21import static org.onosproject.store.flow.ReplicaInfoEvent.Type.BACKUPS_CHANGED;
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070022
23import java.util.Collections;
Madan Jampani86940d92015-05-06 11:47:57 -070024import java.util.List;
Madan Jampani0f6ad142015-05-13 17:10:04 -070025import java.util.Map;
Madan Jampani86940d92015-05-06 11:47:57 -070026
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070027import org.apache.felix.scr.annotations.Activate;
28import org.apache.felix.scr.annotations.Component;
29import org.apache.felix.scr.annotations.Deactivate;
30import org.apache.felix.scr.annotations.Reference;
31import org.apache.felix.scr.annotations.ReferenceCardinality;
32import org.apache.felix.scr.annotations.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.cluster.NodeId;
Madan Jampani86940d92015-05-06 11:47:57 -070034import org.onosproject.cluster.RoleInfo;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.event.AbstractListenerRegistry;
36import org.onosproject.event.EventDeliveryService;
37import org.onosproject.mastership.MastershipEvent;
38import org.onosproject.mastership.MastershipListener;
39import org.onosproject.mastership.MastershipService;
40import org.onosproject.net.DeviceId;
41import org.onosproject.store.flow.ReplicaInfo;
42import org.onosproject.store.flow.ReplicaInfoEvent;
43import org.onosproject.store.flow.ReplicaInfoEventListener;
44import org.onosproject.store.flow.ReplicaInfoService;
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070045import org.slf4j.Logger;
46
Madan Jampani0f6ad142015-05-13 17:10:04 -070047import com.google.common.base.Objects;
48import com.google.common.collect.ImmutableList;
49import com.google.common.collect.Maps;
50
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070051/**
52 * Manages replica placement information.
53 */
54@Component(immediate = true)
55@Service
56public class ReplicaInfoManager implements ReplicaInfoService {
57
58 private final Logger log = getLogger(getClass());
59
60 private final MastershipListener mastershipListener = new InternalMastershipListener();
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected EventDeliveryService eventDispatcher;
64
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected MastershipService mastershipService;
67
68 protected final AbstractListenerRegistry<ReplicaInfoEvent, ReplicaInfoEventListener>
69 listenerRegistry = new AbstractListenerRegistry<>();
70
Madan Jampani0f6ad142015-05-13 17:10:04 -070071 private final Map<DeviceId, ReplicaInfo> deviceReplicaInfoMap = Maps.newConcurrentMap();
72
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070073 @Activate
74 public void activate() {
75 eventDispatcher.addSink(ReplicaInfoEvent.class, listenerRegistry);
76 mastershipService.addListener(mastershipListener);
77 log.info("Started");
78 }
79
80 @Deactivate
81 public void deactivate() {
82 eventDispatcher.removeSink(ReplicaInfoEvent.class);
83 mastershipService.removeListener(mastershipListener);
84 log.info("Stopped");
85 }
86
87 @Override
88 public ReplicaInfo getReplicaInfoFor(DeviceId deviceId) {
Madan Jampani0f6ad142015-05-13 17:10:04 -070089 return deviceReplicaInfoMap.computeIfAbsent(
90 deviceId,
91 id -> buildFromRoleInfo(mastershipService.getNodesFor(deviceId)));
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -070092 }
93
Yuta HIGUCHI4d187952014-10-16 19:59:35 -070094 @Override
95 public void addListener(ReplicaInfoEventListener listener) {
96 listenerRegistry.addListener(checkNotNull(listener));
97 }
98
99 @Override
100 public void removeListener(ReplicaInfoEventListener listener) {
101 listenerRegistry.removeListener(checkNotNull(listener));
102 }
103
Madan Jampani86940d92015-05-06 11:47:57 -0700104 private static ReplicaInfo buildFromRoleInfo(RoleInfo roles) {
105 List<NodeId> backups = roles.backups() == null ?
Madan Jampani0f6ad142015-05-13 17:10:04 -0700106 Collections.emptyList() : ImmutableList.copyOf(roles.backups());
Madan Jampani86940d92015-05-06 11:47:57 -0700107 return new ReplicaInfo(roles.master(), backups);
108 }
109
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -0700110 final class InternalMastershipListener implements MastershipListener {
111
112 @Override
113 public void event(MastershipEvent event) {
Madan Jampani0f6ad142015-05-13 17:10:04 -0700114 final DeviceId deviceId = event.subject();
Madan Jampani86940d92015-05-06 11:47:57 -0700115 final ReplicaInfo replicaInfo = buildFromRoleInfo(event.roleInfo());
Madan Jampani0f6ad142015-05-13 17:10:04 -0700116 ReplicaInfo oldReplicaInfo = deviceReplicaInfoMap.put(deviceId, replicaInfo);
117 if (Objects.equal(oldReplicaInfo, replicaInfo)) {
118 return;
119 }
Yuta HIGUCHId4ab8082014-11-03 11:09:09 -0800120 switch (event.type()) {
121 case MASTER_CHANGED:
122 eventDispatcher.post(new ReplicaInfoEvent(MASTER_CHANGED,
123 event.subject(),
124 replicaInfo));
125 break;
126 case BACKUPS_CHANGED:
127 eventDispatcher.post(new ReplicaInfoEvent(BACKUPS_CHANGED,
128 event.subject(),
129 replicaInfo));
130 break;
131 default:
132 break;
133 }
Yuta HIGUCHIb25f4da2014-10-16 15:09:26 -0700134 }
135 }
136
137}