Change return type of events
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / mastership / MastershipChangeServiceManagerImpl.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.mastership;
9
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;
24
25 public final class MastershipChangeServiceManagerImpl implements MastershipChangeServiceManager {
26
27     private final List<MastershipChangeService> serviceGroup = new ArrayList<>();
28     private ReconciliationFrameworkEvent rfService = null;
29     private MasterChecker masterChecker;
30
31     @Nonnull
32     @Override
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);
39         }
40         return registration;
41     }
42
43     @Override
44     public ReconciliationFrameworkRegistration reconciliationFrameworkRegistration(
45             @Nonnull ReconciliationFrameworkEvent reconciliationFrameworkEvent) throws MastershipChangeException {
46         if (rfService != null) {
47             throw new MastershipChangeException("Reconciliation framework already registered.");
48         } else {
49             rfService = reconciliationFrameworkEvent;
50             return new ReconciliationFrameworkServiceDelegate(reconciliationFrameworkEvent, () -> rfService = null);
51         }
52     }
53
54     @Override
55     public void close() {
56         serviceGroup.clear();
57     }
58
59     @Override
60     public void becomeMaster(@Nonnull final DeviceInfo deviceInfo) {
61         serviceGroup.forEach(mastershipChangeService -> mastershipChangeService.onBecomeOwner(deviceInfo));
62     }
63
64     @Override
65     public void becomeSlaveOrDisconnect(@Nonnull final DeviceInfo deviceInfo) {
66         if (rfService != null) {
67             rfService.onDeviceDisconnected(deviceInfo);
68         }
69         serviceGroup.forEach(mastershipChangeService -> mastershipChangeService.onLoseOwnership(deviceInfo));
70     }
71
72     @Override
73     public ListenableFuture<ResultState> becomeMasterBeforeSubmittedDS(@Nonnull DeviceInfo deviceInfo) {
74         return rfService == null ? null : rfService.onDevicePrepared(deviceInfo);
75     }
76
77     @Override
78     public void setMasterChecker(@Nonnull final MasterChecker masterChecker) {
79         this.masterChecker = masterChecker;
80     }
81
82     @Override
83     public boolean isReconciliationFrameworkRegistered() {
84         return (rfService != null);
85     }
86
87     @VisibleForTesting
88     int serviceGroupListSize() {
89         return serviceGroup.size();
90     }
91
92     private void fireBecomeOwnerAfterRegistration(@Nonnull final MastershipChangeService service) {
93         masterChecker.listOfMasteredDevices().forEach(service::onBecomeOwner);
94     }
95 }