2 * Copyright (c) 2016 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.lifecycle;
10 import com.google.common.base.Verify;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Objects;
17 import javax.annotation.Nonnull;
18 import javax.annotation.Nullable;
19 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
21 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
22 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
23 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
24 import org.opendaylight.openflowplugin.api.openflow.device.handlers.ClusterInitializationPhaseHandler;
25 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceRemovedHandler;
26 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
27 import org.opendaylight.openflowplugin.api.openflow.lifecycle.MastershipChangeListener;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
29 import org.opendaylight.yangtools.yang.common.RpcResult;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class LifecycleServiceImpl implements LifecycleService {
35 private static final Logger LOG = LoggerFactory.getLogger(LifecycleServiceImpl.class);
37 private ClusterSingletonServiceRegistration registration;
38 private ClusterInitializationPhaseHandler clusterInitializationPhaseHandler;
39 private final List<DeviceRemovedHandler> deviceRemovedHandlers = new ArrayList<>();
40 private ServiceGroupIdentifier serviceGroupIdentifier;
41 private DeviceInfo deviceInfo;
42 private boolean terminationState = false;
43 private final MastershipChangeListener mastershipChangeListener;
46 public LifecycleServiceImpl(@Nonnull final MastershipChangeListener mastershipChangeListener) {
47 this.mastershipChangeListener = mastershipChangeListener;
51 public void makeDeviceSlave(final DeviceContext deviceContext) {
53 final DeviceInfo deviceInf = Objects.isNull(deviceInfo) ? deviceContext.getDeviceInfo() : deviceInfo;
55 Futures.addCallback(deviceContext.makeDeviceSlave(), new FutureCallback<RpcResult<SetRoleOutput>>() {
57 public void onSuccess(@Nullable RpcResult<SetRoleOutput> setRoleOutputRpcResult) {
58 if (LOG.isDebugEnabled()) {
59 LOG.debug("Role SLAVE was successfully propagated on device, node {}",
60 deviceContext.getDeviceInfo().getLOGValue());
62 mastershipChangeListener.onSlaveRoleAcquired(deviceInf);
66 public void onFailure(Throwable throwable) {
67 LOG.warn("Was not able to set role SLAVE to device on node {} ",
68 deviceContext.getDeviceInfo().getLOGValue());
69 mastershipChangeListener.onSlaveRoleNotAcquired(deviceInf);
76 public void instantiateServiceInstance() {
78 LOG.info("Starting clustering MASTER services for node {}", deviceInfo.getLOGValue());
79 if (!clusterInitializationPhaseHandler.onContextInstantiateService(null)) {
80 mastershipChangeListener.onNotAbleToStartMastership(deviceInfo);
82 mastershipChangeListener.onMasterRoleAcquired(deviceInfo);
88 public ListenableFuture<Void> closeServiceInstance() {
89 return Futures.immediateFuture(null);
93 public ServiceGroupIdentifier getIdentifier() {
94 return this.serviceGroupIdentifier;
99 if (terminationState) {
100 if (LOG.isDebugEnabled()) {
101 LOG.debug("LifecycleService is already in TERMINATION state.");
104 this.terminationState = true;
106 // We are closing, so cleanup all managers now
107 deviceRemovedHandlers.forEach(h -> h.onDeviceRemoved(deviceInfo));
109 // If we are still registered and we are not already closing, then close the registration
110 if (Objects.nonNull(registration)) {
112 if (LOG.isDebugEnabled()) {
113 LOG.debug("Closing clustering services for node {}", deviceInfo.getLOGValue());
115 registration.close();
116 } catch (final Exception e) {
117 LOG.debug("Failed to close clustering services for node {} with exception: ",
118 deviceInfo.getLOGValue(), e);
125 public void registerService(@Nonnull final ClusterSingletonServiceProvider singletonServiceProvider,
126 @Nonnull final ClusterInitializationPhaseHandler initializationPhaseHandler,
127 @Nonnull final ServiceGroupIdentifier serviceGroupIdentifier,
128 @Nonnull final DeviceInfo deviceInfo) {
130 this.clusterInitializationPhaseHandler = initializationPhaseHandler;
131 this.serviceGroupIdentifier = serviceGroupIdentifier;
132 this.deviceInfo = deviceInfo;
133 this.registration = Verify.verifyNotNull(
134 singletonServiceProvider.registerClusterSingletonService(this));
136 LOG.info("Registered clustering services for node {}", deviceInfo.getLOGValue());
141 public void registerDeviceRemovedHandler(@Nonnull final DeviceRemovedHandler deviceRemovedHandler) {
142 if (!deviceRemovedHandlers.contains(deviceRemovedHandler)) {
143 deviceRemovedHandlers.add(deviceRemovedHandler);