Mastershipchange service implementation.
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / mastership / MastershipServiceManagerImpl.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.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;
22
23 public class MastershipServiceManagerImpl implements MastershipChangeServiceManager {
24
25     private static final Logger LOG = LoggerFactory.getLogger(MastershipServiceManagerImpl.class);
26
27     private final List<MastershipChangeService> serviceGroup = new LinkedList<>();
28     private MasterChecker masterChecker;
29
30     @Nonnull
31     @Override
32     public MastershipChangeRegistration register(@Nonnull MastershipChangeService service) {
33         if (LOG.isDebugEnabled()) {
34             LOG.debug("Mastership change service registered: {}", service);
35         }
36         MastershipServiceDelegate registration = new MastershipServiceDelegate(service, this);
37         serviceGroup.add(registration);
38         if (masterChecker.isAnyDeviceMastered()) {
39             fireBecomeOwnerAfterRegistration(registration);
40         }
41         return registration;
42
43     }
44
45     @Override
46     public void unregister(@Nonnull MastershipChangeService service) {
47         serviceGroup.remove(service);
48     }
49
50     @Override
51     public void becomeMaster(@Nonnull final DeviceInfo deviceInfo) {
52         serviceGroup.forEach(mastershipChangeService -> mastershipChangeService.onBecomeOwner(deviceInfo));
53     }
54
55     @Override
56     public void becomeSlaveOrDisconnect(@Nonnull final DeviceInfo deviceInfo) {
57         serviceGroup.forEach(mastershipChangeService -> mastershipChangeService.onLoseOwnership(deviceInfo));
58     }
59
60     @Override
61     public void becomeMasterBeforeSubmittedDS(@Nonnull DeviceInfo deviceInfo, @Nonnull FutureCallback<ResultState> callback) {
62         serviceGroup.forEach(mastershipChangeService -> mastershipChangeService.onDevicePrepared(deviceInfo, callback));
63     }
64
65     @Override
66     public void setMasterChecker(@Nonnull final MasterChecker masterChecker) {
67         this.masterChecker = masterChecker;
68     }
69
70     private void fireBecomeOwnerAfterRegistration(@Nonnull final  MastershipChangeService service) {
71         masterChecker.listOfMasteredDevices().forEach(service::onBecomeOwner);
72     }
73 }