4ada870f2dbca5b17de3a4271645813720232570
[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.Optional;
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.Collection;
15 import java.util.List;
16 import java.util.Objects;
17 import javax.annotation.Nullable;
18 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
19 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
20 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
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.device.handlers.ClusterInitializationPhaseHandler;
24 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
25 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
26 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
27 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class LifecycleServiceImpl implements LifecycleService {
33
34     private static final Logger LOG = LoggerFactory.getLogger(LifecycleServiceImpl.class);
35
36     private DeviceContext deviceContext;
37     private RpcContext rpcContext;
38     private RoleContext roleContext;
39     private StatisticsContext statContext;
40     private ClusterSingletonServiceRegistration registration;
41     private ClusterInitializationPhaseHandler clusterInitializationPhaseHandler;
42
43
44     @Override
45     public void instantiateServiceInstance() {
46
47         LOG.info("Starting clustering MASTER services for node {}", this.deviceContext.getDeviceInfo().getLOGValue());
48
49         if (!this.clusterInitializationPhaseHandler.onContextInstantiateService(null)) {
50             this.closeConnection();
51         }
52
53     }
54
55     @Override
56     public ListenableFuture<Void> closeServiceInstance() {
57
58         LOG.info("Stopping clustering MASTER services for node {}", this.deviceContext.getDeviceInfo().getLOGValue());
59
60         final boolean connectionInterrupted =
61                 this.deviceContext
62                         .getPrimaryConnectionContext()
63                         .getConnectionState()
64                         .equals(ConnectionContext.CONNECTION_STATE.RIP);
65
66         roleContext.stopClusterServices(connectionInterrupted);
67         statContext.stopClusterServices(connectionInterrupted);
68         rpcContext.stopClusterServices(connectionInterrupted);
69         return deviceContext.stopClusterServices(connectionInterrupted);
70
71     }
72
73     @Override
74     public ServiceGroupIdentifier getIdentifier() {
75         return deviceContext.getServiceIdentifier();
76     }
77
78
79     @Override
80     public void close() throws Exception {
81         if (registration != null) {
82             registration.close();
83             registration = null;
84         }
85     }
86
87     @Override
88     public void registerService(final ClusterSingletonServiceProvider singletonServiceProvider) {
89         //lifecycle service -> device context -> statistics context -> rpc context -> role context -> lifecycle service
90         this.clusterInitializationPhaseHandler = deviceContext;
91         this.deviceContext.setLifecycleInitializationPhaseHandler(this.statContext);
92         this.statContext.setLifecycleInitializationPhaseHandler(this.rpcContext);
93         this.rpcContext.setLifecycleInitializationPhaseHandler(this.roleContext);
94         this.roleContext.setLifecycleInitializationPhaseHandler(this);
95         //Set initial submit handler
96         this.statContext.setInitialSubmitHandler(this.deviceContext);
97         //Register cluster singleton service
98         this.registration = singletonServiceProvider.registerClusterSingletonService(this);
99     }
100
101     @Override
102     public void setDeviceContext(final DeviceContext deviceContext) {
103         this.deviceContext = deviceContext;
104     }
105
106     @Override
107     public void setRpcContext(final RpcContext rpcContext) {
108         this.rpcContext = rpcContext;
109     }
110
111     @Override
112     public void setRoleContext(final RoleContext roleContext) {
113         this.roleContext = roleContext;
114     }
115
116     @Override
117     public void setStatContext(final StatisticsContext statContext) {
118         this.statContext = statContext;
119     }
120
121     @Override
122     public DeviceContext getDeviceContext() {
123         return this.deviceContext;
124     }
125
126     @Override
127     public void closeConnection() {
128         this.deviceContext.shutdownConnection();
129     }
130
131     private void fillDeviceFlowRegistry() {
132         final ListenableFuture<List<Optional<FlowCapableNode>>> deviceFlowRegistryFill = deviceContext.getDeviceFlowRegistry().fill();
133         Futures.addCallback(deviceFlowRegistryFill, new DeviceFlowRegistryCallback(deviceFlowRegistryFill));
134     }
135
136     @Override
137     public void setLifecycleInitializationPhaseHandler(final ClusterInitializationPhaseHandler handler) {
138         this.clusterInitializationPhaseHandler = handler;
139     }
140
141     @Override
142     public boolean onContextInstantiateService(final ConnectionContext connectionContext) {
143
144         if (ConnectionContext.CONNECTION_STATE.RIP.equals(connectionContext.getConnectionState())) {
145             if (LOG.isDebugEnabled()) {
146                 LOG.debug("Connection to the device {} was interrupted.", this.deviceContext.getDeviceInfo().getLOGValue());
147             }
148             return false;
149         }
150
151         fillDeviceFlowRegistry();
152         return true;
153     }
154
155     private class DeviceFlowRegistryCallback implements FutureCallback<List<Optional<FlowCapableNode>>> {
156         private final ListenableFuture<List<Optional<FlowCapableNode>>> deviceFlowRegistryFill;
157
158         public DeviceFlowRegistryCallback(ListenableFuture<List<Optional<FlowCapableNode>>> deviceFlowRegistryFill) {
159             this.deviceFlowRegistryFill = deviceFlowRegistryFill;
160         }
161
162         @Override
163         public void onSuccess(@Nullable List<Optional<FlowCapableNode>> result) {
164             if (LOG.isDebugEnabled()) {
165                 // Count all flows we read from datastore for debugging purposes.
166                 // This number do not always represent how many flows were actually added
167                 // to DeviceFlowRegistry, because of possible duplicates.
168                 long flowCount = Optional.fromNullable(result).asSet().stream()
169                         .flatMap(Collection::stream)
170                         .filter(Objects::nonNull)
171                         .flatMap(flowCapableNodeOptional -> flowCapableNodeOptional.asSet().stream())
172                         .filter(Objects::nonNull)
173                         .filter(flowCapableNode -> Objects.nonNull(flowCapableNode.getTable()))
174                         .flatMap(flowCapableNode -> flowCapableNode.getTable().stream())
175                         .filter(Objects::nonNull)
176                         .filter(table -> Objects.nonNull(table.getFlow()))
177                         .flatMap(table -> table.getFlow().stream())
178                         .filter(Objects::nonNull)
179                         .count();
180
181                 LOG.debug("Finished filling flow registry with {} flows for node: {}", flowCount, deviceContext.getDeviceInfo().getLOGValue());
182             }
183         }
184
185         @Override
186         public void onFailure(Throwable t) {
187             if (deviceFlowRegistryFill.isCancelled()) {
188                 if (LOG.isDebugEnabled()) {
189                     LOG.debug("Cancelled filling flow registry with flows for node: {}", deviceContext.getDeviceInfo().getLOGValue());
190                 }
191             } else {
192                 LOG.warn("Failed filling flow registry with flows for node: {} with exception: {}", deviceContext.getDeviceInfo().getLOGValue(), t);
193             }
194         }
195     }
196 }