Disconnection improvements.
[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 boolean isReady() {
49         return false;
50     }
51
52     @Override
53     public <T extends OFPContext> void addContext(final T context) {
54         if (context instanceof StatisticsContext) {
55             this.statisticsContext = (StatisticsContext) context;
56         } else {
57             if (context instanceof DeviceContext) {
58                 this.deviceContext = (DeviceContext) context;
59             } else {
60                 if (context instanceof RpcContext) {
61                     this.rpcContext = (RpcContext) context;
62                 }
63             }
64         }
65         contexts.add(context);
66     }
67
68     @Override
69     public void addLifecycleService(final LifecycleService lifecycleService) {
70         this.lifecycleService = lifecycleService;
71     }
72
73     @Override
74     public ListenableFuture<Void> stopChain(boolean connectionDropped) {
75         //TODO: stopClusterServices change parameter
76         final List<ListenableFuture<Void>> futureList = new ArrayList<>();
77         futureList.add(statisticsContext.stopClusterServices());
78         futureList.add(rpcContext.stopClusterServices());
79         futureList.add(deviceContext.stopClusterServices(connectionDropped));
80
81         return Futures.transform(Futures.successfulAsList(futureList), new Function<List<Void>, Void>() {
82             @Nullable
83             @Override
84             public Void apply(@Nullable List<Void> input) {
85                 LOG.debug("Closed clustering MASTER services for node {}", deviceContext.getDeviceInfo().getLOGValue());
86                 contextChainState = ContextChainState.WORKINGSLAVE;
87                 return null;
88             }
89         });
90     }
91
92     @Override
93     public ListenableFuture<Void> startChain() {
94         if (ContextChainState.INITIALIZED.equals(this.contextChainState)) {
95             return Futures.transform(this.statisticsContext.initialGatherDynamicData(), new Function<Boolean, Void>() {
96                 @Nullable
97                 @Override
98                 public Void apply(@Nullable Boolean aBoolean) {
99                     contextChainState = ContextChainState.WORKINGMASTER;
100                     return null;
101                 }
102             });
103         } else {
104             this.contextChainState = ContextChainState.WORKINGMASTER;
105         }
106         return Futures.immediateFuture(null);
107     }
108
109     @Override
110     public void close() {
111
112     }
113
114     @Override
115     public void changePrimaryConnection(final ConnectionContext connectionContext) {
116         this.primaryConnectionContext = connectionContext;
117         this.contextChainState = ContextChainState.INITIALIZED;
118         for (OFPContext context : contexts) {
119             context.replaceConnection(connectionContext);
120         }
121     }
122
123     @Override
124     public ContextChainState getContextChainState() {
125         return contextChainState;
126     }
127
128     @Override
129     public ListenableFuture<Void> connectionDropped() {
130         ContextChainState oldState = this.contextChainState;
131         this.contextChainState = ContextChainState.SLEEPING;
132         if (oldState.equals(ContextChainState.WORKINGMASTER)) {
133             return this.stopChain(true);
134         }
135         return Futures.immediateFuture(null);
136     }
137
138     @Override
139     public ConnectionContext getPrimaryConnectionContext() {
140         return primaryConnectionContext;
141     }
142
143     @Override
144     public void sleepTheChainAndDropConnection() {
145         this.contextChainState = ContextChainState.SLEEPING;
146         this.primaryConnectionContext.closeConnection(true);
147     }
148
149     @Override
150     public void registerServices(@NonNull final ClusterSingletonServiceProvider clusterSingletonServiceProvider) {
151         this.contextChainState = ContextChainState.WORKINGSLAVE;
152         this.lifecycleService.registerService(
153                 clusterSingletonServiceProvider,
154                 this.deviceContext,
155                 this.deviceContext.getServiceIdentifier(),
156                 this.deviceContext.getDeviceInfo());
157     }
158
159     @Override
160     public void makeDeviceSlave() {
161         this.lifecycleService.makeDeviceSlave(this.deviceContext);
162     }
163
164     @Override
165     public void closePrimaryConnection() {
166         this.primaryConnectionContext.closeConnection(true);
167     }
168
169     @Override
170     public DeviceContext provideDeviceContext() {
171         return this.deviceContext;
172     }
173 }