Fix several issues with disconecting device
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / lifecycle / ContextChainImpl.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.Function;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.ArrayList;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Set;
17 import javax.annotation.Nullable;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
20 import org.opendaylight.openflowplugin.api.openflow.OFPContext;
21 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
22 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
23 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChain;
24 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
25 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
26 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.ContextChainState;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class ContextChainImpl implements ContextChain {
32
33     private static final Logger LOG = LoggerFactory.getLogger(ContextChainImpl.class);
34
35     private Set<OFPContext> contexts = new HashSet<>();
36     private StatisticsContext statisticsContext;
37     private DeviceContext deviceContext;
38     private RpcContext rpcContext;
39     private volatile ContextChainState contextChainState;
40     private LifecycleService lifecycleService;
41     private ConnectionContext primaryConnectionContext;
42
43     public ContextChainImpl() {
44         this.contextChainState = ContextChainState.INITIALIZED;
45     }
46
47     @Override
48     public <T extends OFPContext> void addContext(final T context) {
49         if (context instanceof StatisticsContext) {
50             this.statisticsContext = (StatisticsContext) context;
51         } else {
52             if (context instanceof DeviceContext) {
53                 this.deviceContext = (DeviceContext) context;
54             } else {
55                 if (context instanceof RpcContext) {
56                     this.rpcContext = (RpcContext) context;
57                 }
58             }
59         }
60         contexts.add(context);
61     }
62
63     @Override
64     public void addLifecycleService(final LifecycleService lifecycleService) {
65         this.lifecycleService = lifecycleService;
66     }
67
68     @Override
69     public ListenableFuture<Void> stopChain(boolean connectionDropped) {
70         //TODO: stopClusterServices change parameter
71         final List<ListenableFuture<Void>> futureList = new ArrayList<>();
72         futureList.add(statisticsContext.stopClusterServices());
73         futureList.add(rpcContext.stopClusterServices());
74         futureList.add(deviceContext.stopClusterServices(connectionDropped));
75
76         return Futures.transform(Futures.successfulAsList(futureList), new Function<List<Void>, Void>() {
77             @Nullable
78             @Override
79             public Void apply(@Nullable List<Void> input) {
80                 LOG.debug("Closed clustering MASTER services for node {}", deviceContext.getDeviceInfo().getLOGValue());
81                 contextChainState = ContextChainState.WORKINGSLAVE;
82                 return null;
83             }
84         });
85     }
86
87     @Override
88     public ListenableFuture<Void> startChain() {
89         if (ContextChainState.INITIALIZED.equals(this.contextChainState)) {
90             return Futures.transform(this.statisticsContext.initialGatherDynamicData(), new Function<Boolean, Void>() {
91                 @Nullable
92                 @Override
93                 public Void apply(@Nullable Boolean aBoolean) {
94                     contextChainState = ContextChainState.WORKINGMASTER;
95                     return null;
96                 }
97             });
98         } else {
99             this.contextChainState = ContextChainState.WORKINGMASTER;
100         }
101         return Futures.immediateFuture(null);
102     }
103
104     @Override
105     public void close() {
106
107     }
108
109     @Override
110     public void changePrimaryConnection(final ConnectionContext connectionContext) {
111         this.primaryConnectionContext = connectionContext;
112         this.contextChainState = ContextChainState.INITIALIZED;
113         for (OFPContext context : contexts) {
114             context.replaceConnection(connectionContext);
115         }
116     }
117
118     @Override
119     public ContextChainState getContextChainState() {
120         return contextChainState;
121     }
122
123     @Override
124     public ListenableFuture<Void> connectionDropped() {
125         ContextChainState oldState = this.contextChainState;
126         this.contextChainState = ContextChainState.SLEEPING;
127         if (oldState.equals(ContextChainState.WORKINGMASTER)) {
128             return this.stopChain(true);
129         }
130         return Futures.immediateFuture(null);
131     }
132
133     @Override
134     public void sleepTheChainAndDropConnection() {
135         this.contextChainState = ContextChainState.SLEEPING;
136         this.primaryConnectionContext.closeConnection(true);
137     }
138
139     @Override
140     public void registerServices(@NonNull final ClusterSingletonServiceProvider clusterSingletonServiceProvider) {
141         this.contextChainState = ContextChainState.WORKINGSLAVE;
142         this.lifecycleService.registerService(
143                 clusterSingletonServiceProvider,
144                 this.deviceContext,
145                 this.deviceContext.getServiceIdentifier(),
146                 this.deviceContext.getDeviceInfo());
147     }
148
149     @Override
150     public void makeDeviceSlave() {
151         this.lifecycleService.makeDeviceSlave(this.deviceContext);
152     }
153
154     @Override
155     public void closePrimaryConnection() {
156         this.primaryConnectionContext.closeConnection(true);
157     }
158
159     @Override
160     public DeviceContext provideDeviceContext() {
161         return this.deviceContext;
162     }
163
164     @Override
165     public void sleepTheChain() {
166         this.contextChainState = ContextChainState.SLEEPING;
167     }
168 }