Add posibillity to never drop context chains.
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / lifecycle / LifecycleServiceImpl.java
1 /**
2  * Copyright (c) 2016 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.lifecycle;
9
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;
32
33 public class LifecycleServiceImpl implements LifecycleService {
34
35     private static final Logger LOG = LoggerFactory.getLogger(LifecycleServiceImpl.class);
36
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;
44
45
46     public LifecycleServiceImpl(@Nonnull final MastershipChangeListener mastershipChangeListener) {
47         this.mastershipChangeListener = mastershipChangeListener;
48     }
49
50     @Override
51     public void makeDeviceSlave(final DeviceContext deviceContext) {
52
53         final DeviceInfo deviceInf = Objects.isNull(deviceInfo) ? deviceContext.getDeviceInfo() : deviceInfo;
54
55         Futures.addCallback(deviceContext.makeDeviceSlave(), new FutureCallback<RpcResult<SetRoleOutput>>() {
56             @Override
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());
61                 }
62                 mastershipChangeListener.onSlaveRoleAcquired(deviceInf);
63             }
64
65             @Override
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);
70             }
71         });
72
73     }
74
75     @Override
76     public void instantiateServiceInstance() {
77
78         LOG.info("Starting clustering MASTER services for node {}", deviceInfo.getLOGValue());
79         if (!clusterInitializationPhaseHandler.onContextInstantiateService(null)) {
80             mastershipChangeListener.onNotAbleToStartMastership(deviceInfo);
81         } else {
82             mastershipChangeListener.onMasterRoleAcquired(deviceInfo);
83         }
84
85     }
86
87     @Override
88     public ListenableFuture<Void> closeServiceInstance() {
89         return Futures.immediateFuture(null);
90     }
91
92     @Override
93     public ServiceGroupIdentifier getIdentifier() {
94         return this.serviceGroupIdentifier;
95     }
96
97     @Override
98     public void close() {
99         if (terminationState) {
100             if (LOG.isDebugEnabled()) {
101                 LOG.debug("LifecycleService is already in TERMINATION state.");
102             }
103         } else {
104             this.terminationState = true;
105
106             // We are closing, so cleanup all managers now
107             deviceRemovedHandlers.forEach(h -> h.onDeviceRemoved(deviceInfo));
108
109             // If we are still registered and we are not already closing, then close the registration
110             if (Objects.nonNull(registration)) {
111                 try {
112                     if (LOG.isDebugEnabled()) {
113                         LOG.debug("Closing clustering services for node {}", deviceInfo.getLOGValue());
114                     }
115                     registration.close();
116                 } catch (final Exception e) {
117                     LOG.debug("Failed to close clustering services for node {} with exception: ",
118                             deviceInfo.getLOGValue(), e);
119                 }
120             }
121         }
122     }
123
124     @Override
125     public void registerService(@Nonnull final ClusterSingletonServiceProvider singletonServiceProvider,
126                                 @Nonnull final ClusterInitializationPhaseHandler initializationPhaseHandler,
127                                 @Nonnull final ServiceGroupIdentifier serviceGroupIdentifier,
128                                 @Nonnull final DeviceInfo deviceInfo) {
129
130         this.clusterInitializationPhaseHandler = initializationPhaseHandler;
131         this.serviceGroupIdentifier = serviceGroupIdentifier;
132         this.deviceInfo = deviceInfo;
133         this.registration = Verify.verifyNotNull(
134                 singletonServiceProvider.registerClusterSingletonService(this));
135
136         LOG.info("Registered clustering services for node {}", deviceInfo.getLOGValue());
137
138     }
139
140     @Override
141     public void registerDeviceRemovedHandler(@Nonnull final DeviceRemovedHandler deviceRemovedHandler) {
142         if (!deviceRemovedHandlers.contains(deviceRemovedHandler)) {
143             deviceRemovedHandlers.add(deviceRemovedHandler);
144         }
145     }
146
147 }