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.annotations.VisibleForTesting;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.util.ArrayList;
13 import java.util.List;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
16 import org.opendaylight.openflowplugin.api.openflow.lifecycle.MasterChecker;
17 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeException;
18 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeRegistration;
19 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeService;
20 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
21 import org.opendaylight.openflowplugin.api.openflow.mastership.ReconciliationFrameworkEvent;
22 import org.opendaylight.openflowplugin.api.openflow.mastership.ReconciliationFrameworkRegistration;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.rf.state.rev170713.ResultState;
25 public final class MastershipChangeServiceManagerImpl implements MastershipChangeServiceManager {
27 private final List<MastershipChangeService> serviceGroup = new ArrayList<>();
28 private ReconciliationFrameworkEvent rfService = null;
29 private MasterChecker masterChecker;
33 public MastershipChangeRegistration register(@Nonnull MastershipChangeService service) {
34 final MastershipServiceDelegate registration =
35 new MastershipServiceDelegate(service, () -> serviceGroup.remove(service));
36 serviceGroup.add(service);
37 if (masterChecker!= null && masterChecker.isAnyDeviceMastered()) {
38 fireBecomeOwnerAfterRegistration(service);
44 public ReconciliationFrameworkRegistration reconciliationFrameworkRegistration(
45 @Nonnull ReconciliationFrameworkEvent reconciliationFrameworkEvent) throws MastershipChangeException {
46 if (rfService != null) {
47 throw new MastershipChangeException("Reconciliation framework already registered.");
49 rfService = reconciliationFrameworkEvent;
50 return new ReconciliationFrameworkServiceDelegate(reconciliationFrameworkEvent, () -> rfService = null);
60 public void becomeMaster(@Nonnull final DeviceInfo deviceInfo) {
61 serviceGroup.forEach(mastershipChangeService -> mastershipChangeService.onBecomeOwner(deviceInfo));
65 public void becomeSlaveOrDisconnect(@Nonnull final DeviceInfo deviceInfo) {
66 if (rfService != null) {
67 rfService.onDeviceDisconnected(deviceInfo);
69 serviceGroup.forEach(mastershipChangeService -> mastershipChangeService.onLoseOwnership(deviceInfo));
73 public ListenableFuture<ResultState> becomeMasterBeforeSubmittedDS(@Nonnull DeviceInfo deviceInfo) {
74 return rfService == null ? null : rfService.onDevicePrepared(deviceInfo);
78 public void setMasterChecker(@Nonnull final MasterChecker masterChecker) {
79 this.masterChecker = masterChecker;
83 public boolean isReconciliationFrameworkRegistered() {
84 return (rfService != null);
88 int serviceGroupListSize() {
89 return serviceGroup.size();
92 private void fireBecomeOwnerAfterRegistration(@Nonnull final MastershipChangeService service) {
93 masterChecker.listOfMasteredDevices().forEach(service::onBecomeOwner);