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.openflowplugin.api.openflow.mastership.MastershipChangeServiceManagerFactory;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.rf.state.rev170713.ResultState;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 public class MastershipChangeServiceManagerFactoryImpl implements MastershipChangeServiceManagerFactory {
27 public MastershipChangeServiceManager newInstance() {
28 return new MastershipChangeServiceManagerImpl();
31 private static final class MastershipChangeServiceManagerImpl implements MastershipChangeServiceManager {
33 private static final Logger LOG = LoggerFactory.getLogger(MastershipChangeServiceManagerImpl.class);
35 private final List<MastershipChangeService> serviceGroup = new LinkedList<>();
36 private MasterChecker masterChecker;
40 public MastershipChangeRegistration register(@Nonnull MastershipChangeService service) {
41 if (LOG.isDebugEnabled()) {
42 LOG.debug("Mastership change service registered: {}", service);
44 MastershipServiceDelegate registration = new MastershipServiceDelegate(service, this);
45 serviceGroup.add(registration);
46 if (masterChecker.isAnyDeviceMastered()) {
47 fireBecomeOwnerAfterRegistration(registration);
54 public void unregister(@Nonnull MastershipChangeService service) {
55 serviceGroup.remove(service);
64 public void becomeMaster(@Nonnull final DeviceInfo deviceInfo) {
65 serviceGroup.forEach(mastershipChangeService -> mastershipChangeService.onBecomeOwner(deviceInfo));
69 public void becomeSlaveOrDisconnect(@Nonnull final DeviceInfo deviceInfo) {
70 serviceGroup.forEach(mastershipChangeService -> mastershipChangeService.onLoseOwnership(deviceInfo));
74 public void becomeMasterBeforeSubmittedDS(@Nonnull DeviceInfo deviceInfo, @Nonnull FutureCallback<ResultState> callback) {
75 serviceGroup.forEach(mastershipChangeService -> mastershipChangeService.onDevicePrepared(deviceInfo, callback));
79 public void setMasterChecker(@Nonnull final MasterChecker masterChecker) {
80 this.masterChecker = masterChecker;
83 private void fireBecomeOwnerAfterRegistration(@Nonnull final MastershipChangeService service) {
84 masterChecker.listOfMasteredDevices().forEach(service::onBecomeOwner);