2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowplugin.impl.mastership;
10 import com.google.common.util.concurrent.FutureCallback;
11 import java.util.LinkedList;
12 import java.util.List;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
15 import org.opendaylight.openflowplugin.api.openflow.lifecycle.MasterChecker;
16 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeRegistration;
17 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeService;
18 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.rf.state.rev170713.ResultState;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public class MastershipServiceManagerImpl implements MastershipChangeServiceManager {
25 private static final Logger LOG = LoggerFactory.getLogger(MastershipServiceManagerImpl.class);
27 private final List<MastershipChangeService> serviceGroup = new LinkedList<>();
28 private MasterChecker masterChecker;
32 public MastershipChangeRegistration register(@Nonnull MastershipChangeService service) {
33 if (LOG.isDebugEnabled()) {
34 LOG.debug("Mastership change service registered: {}", service);
36 MastershipServiceDelegate registration = new MastershipServiceDelegate(service, this);
37 serviceGroup.add(registration);
38 if (masterChecker.isAnyDeviceMastered()) {
39 fireBecomeOwnerAfterRegistration(registration);
46 public void unregister(@Nonnull MastershipChangeService service) {
47 serviceGroup.remove(service);
51 public void becomeMaster(@Nonnull final DeviceInfo deviceInfo) {
52 serviceGroup.forEach(mastershipChangeService -> mastershipChangeService.onBecomeOwner(deviceInfo));
56 public void becomeSlaveOrDisconnect(@Nonnull final DeviceInfo deviceInfo) {
57 serviceGroup.forEach(mastershipChangeService -> mastershipChangeService.onLoseOwnership(deviceInfo));
61 public void becomeMasterBeforeSubmittedDS(@Nonnull DeviceInfo deviceInfo, @Nonnull FutureCallback<ResultState> callback) {
62 serviceGroup.forEach(mastershipChangeService -> mastershipChangeService.onDevicePrepared(deviceInfo, callback));
66 public void setMasterChecker(@Nonnull final MasterChecker masterChecker) {
67 this.masterChecker = masterChecker;
70 private void fireBecomeOwnerAfterRegistration(@Nonnull final MastershipChangeService service) {
71 masterChecker.listOfMasteredDevices().forEach(service::onBecomeOwner);