Redesign statistics context and manager
[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.collect.Lists;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.List;
14 import java.util.Objects;
15 import java.util.concurrent.CopyOnWriteArrayList;
16 import java.util.concurrent.ExecutorService;
17 import java.util.concurrent.atomic.AtomicBoolean;
18 import java.util.concurrent.atomic.AtomicReference;
19 import java.util.stream.Collectors;
20 import javax.annotation.Nonnull;
21 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
22 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
23 import org.opendaylight.openflowplugin.api.openflow.OFPContext;
24 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
26 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceRemovedHandler;
27 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChain;
28 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipState;
29 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipWatcher;
30 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainState;
31 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainStateListener;
32 import org.opendaylight.openflowplugin.api.openflow.lifecycle.GuardedContext;
33 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ReconciliationFrameworkStep;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class ContextChainImpl implements ContextChain {
38     private static final Logger LOG = LoggerFactory.getLogger(ContextChainImpl.class);
39
40     private final AtomicBoolean masterStateOnDevice = new AtomicBoolean(false);
41     private final AtomicBoolean initialGathering = new AtomicBoolean(false);
42     private final AtomicBoolean initialSubmitting = new AtomicBoolean(false);
43     private final AtomicBoolean registryFilling = new AtomicBoolean(false);
44     private final AtomicBoolean rpcRegistration = new AtomicBoolean(false);
45     private final List<DeviceRemovedHandler> deviceRemovedHandlers = new CopyOnWriteArrayList<>();
46     private final List<GuardedContext> contexts = new CopyOnWriteArrayList<>();
47     private final List<ConnectionContext> auxiliaryConnections = new CopyOnWriteArrayList<>();
48     private final ExecutorService executorService;
49     private final ContextChainMastershipWatcher contextChainMastershipWatcher;
50     private final DeviceInfo deviceInfo;
51     private final ConnectionContext primaryConnection;
52     private final AtomicReference<ContextChainState> contextChainState =
53             new AtomicReference<>(ContextChainState.UNDEFINED);
54     private AutoCloseable registration;
55
56     ContextChainImpl(@Nonnull final ContextChainMastershipWatcher contextChainMastershipWatcher,
57                      @Nonnull final ConnectionContext connectionContext,
58                      @Nonnull final ExecutorService executorService) {
59         this.contextChainMastershipWatcher = contextChainMastershipWatcher;
60         this.primaryConnection = connectionContext;
61         this.deviceInfo = connectionContext.getDeviceInfo();
62         this.executorService = executorService;
63     }
64
65     @Override
66     public <T extends OFPContext> void addContext(@Nonnull final T context) {
67         contexts.add(new GuardedContextImpl(context));
68     }
69
70     @Override
71     @SuppressWarnings("checkstyle:IllegalCatch")
72     public void instantiateServiceInstance() {
73
74         try {
75             contexts.forEach(OFPContext::instantiateServiceInstance);
76             LOG.info("Started clustering services for node {}", deviceInfo);
77         } catch (final Exception ex) {
78             LOG.warn("Not able to start clustering services for node {}", deviceInfo);
79             executorService.submit(() -> contextChainMastershipWatcher
80                     .onNotAbleToStartMastershipMandatory(deviceInfo, ex.toString()));
81         }
82     }
83
84     @Override
85     public ListenableFuture<Void> closeServiceInstance() {
86
87         contextChainMastershipWatcher.onSlaveRoleAcquired(deviceInfo);
88
89         final ListenableFuture<List<Void>> servicesToBeClosed = Futures
90                 .allAsList(Lists.reverse(contexts)
91                         .stream()
92                         .map(OFPContext::closeServiceInstance)
93                         .collect(Collectors.toList()));
94
95         return Futures.transform(servicesToBeClosed, (input) -> {
96             LOG.info("Closed clustering services for node {}", deviceInfo);
97             return null;
98         }, executorService);
99     }
100
101     @Nonnull
102     @Override
103     public ServiceGroupIdentifier getIdentifier() {
104         return deviceInfo.getServiceIdentifier();
105     }
106
107     @Override
108     @SuppressWarnings("checkstyle:IllegalCatch")
109     public void close() {
110         if (ContextChainState.CLOSED.equals(contextChainState.get())) {
111             LOG.debug("ContextChain for node {} is already in TERMINATION state.", deviceInfo);
112             return;
113         }
114
115         contextChainState.set(ContextChainState.CLOSED);
116         unMasterMe();
117
118         // Close all connections to devices
119         auxiliaryConnections.forEach(connectionContext -> connectionContext.closeConnection(false));
120         auxiliaryConnections.clear();
121
122         // If we are still registered and we are not already closing, then close the registration
123         if (Objects.nonNull(registration)) {
124             try {
125                 registration.close();
126                 registration = null;
127                 LOG.info("Closed clustering services registration for node {}", deviceInfo);
128             } catch (final Exception e) {
129                 LOG.warn("Failed to close clustering services registration for node {} with exception: ",
130                         deviceInfo, e);
131             }
132         }
133
134
135         // Close all contexts (device, statistics, rpc)
136         contexts.forEach(OFPContext::close);
137         contexts.clear();
138
139         // We are closing, so cleanup all managers now
140         deviceRemovedHandlers.forEach(h -> h.onDeviceRemoved(deviceInfo));
141         deviceRemovedHandlers.clear();
142
143         primaryConnection.closeConnection(false);
144
145     }
146
147     @Override
148     public void makeContextChainStateSlave() {
149         unMasterMe();
150         changeMastershipState(ContextChainState.WORKING_SLAVE);
151     }
152
153     @Override
154     public void registerServices(final ClusterSingletonServiceProvider clusterSingletonServiceProvider) {
155         registration = Objects.requireNonNull(clusterSingletonServiceProvider
156                 .registerClusterSingletonService(this));
157         LOG.debug("Registered clustering services for node {}", deviceInfo);
158     }
159
160     @Override
161     public boolean isMastered(@Nonnull ContextChainMastershipState mastershipState,
162                               boolean inReconciliationFrameworkStep) {
163         switch (mastershipState) {
164             case INITIAL_SUBMIT:
165                 LOG.debug("Device {}, initial submit OK.", deviceInfo);
166                 this.initialSubmitting.set(true);
167                 break;
168             case MASTER_ON_DEVICE:
169                 LOG.debug("Device {}, master state OK.", deviceInfo);
170                 this.masterStateOnDevice.set(true);
171                 break;
172             case INITIAL_GATHERING:
173                 LOG.debug("Device {}, initial gathering OK.", deviceInfo);
174                 this.initialGathering.set(true);
175                 break;
176             case RPC_REGISTRATION:
177                 LOG.debug("Device {}, RPC registration OK.", deviceInfo);
178                 this.rpcRegistration.set(true);
179                 break;
180             case INITIAL_FLOW_REGISTRY_FILL:
181                 // Flow registry fill is not mandatory to work as a master
182                 LOG.debug("Device {}, initial registry filling OK.", deviceInfo);
183                 this.registryFilling.set(true);
184                 break;
185             case CHECK:
186                 // no operation
187                 break;
188             default:
189                 // no operation
190                 break;
191         }
192
193         final boolean result = initialGathering.get() &&
194                 masterStateOnDevice.get() &&
195                 rpcRegistration.get() &&
196                 inReconciliationFrameworkStep || initialSubmitting.get();
197
198         if (!inReconciliationFrameworkStep &&
199                 result &&
200                 mastershipState != ContextChainMastershipState.CHECK) {
201             LOG.info("Device {} is able to work as master{}",
202                     deviceInfo,
203                     registryFilling.get() ? "." : " WITHOUT flow registry !!!");
204             changeMastershipState(ContextChainState.WORKING_MASTER);
205         }
206
207         return result;
208     }
209
210     @Override
211     public boolean isClosing() {
212         return ContextChainState.CLOSED.equals(contextChainState.get());
213     }
214
215     @Override
216     public void continueInitializationAfterReconciliation() {
217         contexts.forEach(context -> {
218             if (context.map(ReconciliationFrameworkStep.class::isInstance)) {
219                 context.map(ReconciliationFrameworkStep.class::cast).continueInitializationAfterReconciliation();
220             }
221         });
222     }
223
224     @Override
225     public boolean addAuxiliaryConnection(@Nonnull ConnectionContext connectionContext) {
226         return (connectionContext.getFeatures().getAuxiliaryId() != 0)
227                 && (!ConnectionContext.CONNECTION_STATE.RIP.equals(primaryConnection.getConnectionState()))
228                 && auxiliaryConnections.add(connectionContext);
229     }
230
231     @Override
232     public boolean auxiliaryConnectionDropped(@Nonnull ConnectionContext connectionContext) {
233         return auxiliaryConnections.remove(connectionContext);
234     }
235
236     @Override
237     public void registerDeviceRemovedHandler(@Nonnull final DeviceRemovedHandler deviceRemovedHandler) {
238         deviceRemovedHandlers.add(deviceRemovedHandler);
239     }
240
241     private void changeMastershipState(final ContextChainState contextChainState) {
242         if (ContextChainState.CLOSED.equals(this.contextChainState.get())) {
243             return;
244         }
245
246         boolean propagate = ContextChainState.UNDEFINED.equals(this.contextChainState.get());
247         this.contextChainState.set(contextChainState);
248
249         if (propagate) {
250             contexts.forEach(context -> {
251                 if (context.map(ContextChainStateListener.class::isInstance)) {
252                     context.map(ContextChainStateListener.class::cast).onStateAcquired(contextChainState);
253                 }
254             });
255         }
256     }
257
258     private void unMasterMe() {
259         registryFilling.set(false);
260         initialSubmitting.set(false);
261         initialGathering.set(false);
262         masterStateOnDevice.set(false);
263         rpcRegistration.set(false);
264     }
265 }