d5e024a90586f4e1851856e6809fb9f7197dc5af
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / lifecycle / LifecycleServiceImpl.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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 java.util.concurrent.ExecutionException;
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.connection.ConnectionContext;
23 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
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
42
43     @Override
44     public void instantiateServiceInstance() {
45         try {
46
47             if (LOG.isDebugEnabled()) {
48                 LOG.debug("Starting clustering MASTER services for node {}", this.deviceContext.getDeviceInfo().getLOGValue());
49                 LOG.debug("===============================================");
50             }
51
52             if (connectionInterrupted()) {
53                 return;
54             }
55
56             LOG.info("Starting device context cluster services for node {}", getIdentifier());
57             this.deviceContext.startupClusterServices();
58
59             if (connectionInterrupted()) {
60                 return;
61             }
62
63             LOG.info("Starting statistics context cluster services for node {}", getIdentifier());
64             this.statContext.startupClusterServices();
65
66             if (connectionInterrupted()) {
67                 return;
68             }
69
70             LOG.info("Statistics initial gathering OK, submitting data for node {}", getIdentifier());
71             this.deviceContext.initialSubmitTransaction();
72
73             if (connectionInterrupted()) {
74                 return;
75             }
76
77             LOG.info("Starting rpc context cluster services for node {}", getIdentifier());
78             this.rpcContext.startupClusterServices();
79
80             if (connectionInterrupted()) {
81                 return;
82             }
83
84             LOG.info("Starting role context cluster services for node {}", getIdentifier());
85             this.roleContext.startupClusterServices();
86
87             if (connectionInterrupted()) {
88                 return;
89             }
90
91             LOG.info("Caching flows IDs ...");
92             fillDeviceFlowRegistry();
93
94         } catch (ExecutionException | InterruptedException e) {
95             LOG.warn("Cluster service {} was unable to start.", this.getIdentifier());
96             this.deviceContext.shutdownConnection();
97         }
98     }
99
100     private boolean connectionInterrupted() {
101         if (this.deviceContext.getPrimaryConnectionContext().getConnectionState().equals(ConnectionContext.CONNECTION_STATE.RIP)) {
102             LOG.warn("Node {} was disconnected, will stop starting MASTER services.", this.deviceContext.getDeviceInfo().getLOGValue());
103             return true;
104         }
105         return false;
106     }
107
108     @Override
109     public ListenableFuture<Void> closeServiceInstance() {
110         if (LOG.isDebugEnabled()) {
111             LOG.debug("Stopping clustering MASTER services for node {}", this.deviceContext.getDeviceInfo().getLOGValue());
112             LOG.debug("===============================================");
113         }
114
115         LOG.info("Stopping role context cluster services for node {}", getIdentifier());
116         roleContext.stopClusterServices();
117
118         LOG.info("Stopping statistics context cluster services for node {}", getIdentifier());
119         statContext.stopClusterServices();
120
121         LOG.info("Stopping rpc context cluster services for node {}", getIdentifier());
122         rpcContext.stopClusterServices();
123
124         LOG.info("Stopping device context cluster services for node {}", getIdentifier());
125         return deviceContext.stopClusterServices();
126     }
127
128     @Override
129     public ServiceGroupIdentifier getIdentifier() {
130         return deviceContext.getServiceIdentifier();
131     }
132
133
134     @Override
135     public void close() throws Exception {
136         if (registration != null) {
137             registration.close();
138             registration = null;
139         }
140     }
141
142     @Override
143     public void registerService(final ClusterSingletonServiceProvider singletonServiceProvider) {
144         this.registration = singletonServiceProvider.registerClusterSingletonService(this);
145     }
146
147     @Override
148     public void setDeviceContext(final DeviceContext deviceContext) {
149         this.deviceContext = deviceContext;
150     }
151
152     @Override
153     public void setRpcContext(final RpcContext rpcContext) {
154         this.rpcContext = rpcContext;
155     }
156
157     @Override
158     public void setRoleContext(final RoleContext roleContext) {
159         this.roleContext = roleContext;
160     }
161
162     @Override
163     public void setStatContext(final StatisticsContext statContext) {
164         this.statContext = statContext;
165     }
166
167     @Override
168     public DeviceContext getDeviceContext() {
169         return this.deviceContext;
170     }
171
172     @Override
173     public void closeConnection() {
174         this.deviceContext.shutdownConnection();
175     }
176
177     private void fillDeviceFlowRegistry() {
178         // Fill device flow registry with flows from datastore
179         final ListenableFuture<List<Optional<FlowCapableNode>>> deviceFlowRegistryFill = deviceContext.getDeviceFlowRegistry().fill();
180
181         // Start statistics scheduling only after we finished initializing device flow registry
182         Futures.addCallback(deviceFlowRegistryFill, new FutureCallback<List<Optional<FlowCapableNode>>>() {
183             @Override
184             public void onSuccess(@Nullable List<Optional<FlowCapableNode>> result) {
185                 if (LOG.isDebugEnabled()) {
186                     // Count all flows we read from datastore for debugging purposes.
187                     // This number do not always represent how many flows were actually added
188                     // to DeviceFlowRegistry, because of possible duplicates.
189                     long flowCount = Optional.fromNullable(result).asSet().stream()
190                             .flatMap(Collection::stream)
191                             .filter(Objects::nonNull)
192                             .flatMap(flowCapableNodeOptional -> flowCapableNodeOptional.asSet().stream())
193                             .filter(Objects::nonNull)
194                             .filter(flowCapableNode -> Objects.nonNull(flowCapableNode.getTable()))
195                             .flatMap(flowCapableNode -> flowCapableNode.getTable().stream())
196                             .filter(Objects::nonNull)
197                             .filter(table -> Objects.nonNull(table.getFlow()))
198                             .flatMap(table -> table.getFlow().stream())
199                             .filter(Objects::nonNull)
200                             .count();
201
202                     LOG.debug("Finished filling flow registry with {} flows for node: {}", flowCount, deviceContext.getDeviceInfo().getLOGValue());
203                 }
204             }
205
206             @Override
207             public void onFailure(Throwable t) {
208                 if (deviceFlowRegistryFill.isCancelled()) {
209                     LOG.debug("Cancelled filling flow registry with flows for node: {}", deviceContext.getDeviceInfo().getLOGValue());
210                 } else {
211                     LOG.warn("Failed filling flow registry with flows for node: {} with exception: {}", deviceContext.getDeviceInfo().getLOGValue(), t);
212                 }
213             }
214         });
215     }
216
217 }