Merge "Bug 5596 Cleaning lifecycle conductor"
[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.concurrent.ExecutionException;
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.device.DeviceContext;
22 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
23 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
24 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
25 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class LifecycleServiceImpl implements LifecycleService {
31
32     private static final Logger LOG = LoggerFactory.getLogger(LifecycleServiceImpl.class);
33
34     private DeviceContext deviceContext;
35     private RpcContext rpcContext;
36     private RoleContext roleContext;
37     private StatisticsContext statContext;
38     private ClusterSingletonServiceRegistration registration;
39
40
41     @Override
42     public void instantiateServiceInstance() {
43         try {
44
45             LOG.info("Starting device context cluster services for node {}", getIdentifier());
46             this.deviceContext.startupClusterServices();
47
48             LOG.info("Starting statistics context cluster services for node {}", getIdentifier());
49             this.statContext.startupClusterServices();
50
51             LOG.info("Statistics initial gathering OK, submitting data for node {}", getIdentifier());
52             this.deviceContext.initialSubmitTransaction();
53
54             LOG.info("Starting rpc context cluster services for node {}", getIdentifier());
55             this.rpcContext.startupClusterServices();
56
57             LOG.info("Starting role context cluster services for node {}", getIdentifier());
58             this.roleContext.startupClusterServices();
59
60             LOG.info("Caching flows IDs ...");
61             fillDeviceFlowRegistry();
62
63         } catch (ExecutionException | InterruptedException e) {
64             LOG.warn("Cluster service {} was unable to start.", this.getIdentifier());
65         }
66     }
67
68     @Override
69     public ListenableFuture<Void> closeServiceInstance() {
70         statContext.stopClusterServices();
71         rpcContext.stopClusterServices();
72         return deviceContext.stopClusterServices();
73     }
74
75     @Override
76     public ServiceGroupIdentifier getIdentifier() {
77         return deviceContext.getServiceIdentifier();
78     }
79
80
81     @Override
82     public void close() throws Exception {
83         if (registration != null) {
84             registration.close();
85             registration = null;
86         }
87     }
88
89     @Override
90     public void registerService(final ClusterSingletonServiceProvider singletonServiceProvider) {
91         this.registration = singletonServiceProvider.registerClusterSingletonService(this);
92     }
93
94     @Override
95     public void setDeviceContext(final DeviceContext deviceContext) {
96         this.deviceContext = deviceContext;
97     }
98
99     @Override
100     public void setRpcContext(final RpcContext rpcContext) {
101         this.rpcContext = rpcContext;
102     }
103
104     @Override
105     public void setRoleContext(final RoleContext roleContext) {
106         this.roleContext = roleContext;
107     }
108
109     @Override
110     public void setStatContext(final StatisticsContext statContext) {
111         this.statContext = statContext;
112     }
113
114     @Override
115     public DeviceContext getDeviceContext() {
116         return this.deviceContext;
117     }
118
119     @Override
120     public void closeConnection() {
121         this.deviceContext.shutdownConnection();
122     }
123
124     private void fillDeviceFlowRegistry() {
125         // Fill device flow registry with flows from datastore
126         final ListenableFuture<List<Optional<FlowCapableNode>>> deviceFlowRegistryFill = deviceContext.getDeviceFlowRegistry().fill();
127
128         // Start statistics scheduling only after we finished initializing device flow registry
129         Futures.addCallback(deviceFlowRegistryFill, new FutureCallback<List<Optional<FlowCapableNode>>>() {
130             @Override
131             public void onSuccess(@Nullable List<Optional<FlowCapableNode>> result) {
132                 if (LOG.isDebugEnabled()) {
133                     // Count all flows we read from datastore for debugging purposes.
134                     // This number do not always represent how many flows were actually added
135                     // to DeviceFlowRegistry, because of possible duplicates.
136                     long flowCount = Optional.fromNullable(result).asSet().stream()
137                             .flatMap(Collection::stream)
138                             .flatMap(flowCapableNodeOptional -> flowCapableNodeOptional.asSet().stream())
139                             .flatMap(flowCapableNode -> flowCapableNode.getTable().stream())
140                             .flatMap(table -> table.getFlow().stream())
141                             .count();
142
143                     LOG.debug("Finished filling flow registry with {} flows for node: {}", flowCount, getIdentifier());
144                 }
145             }
146
147             @Override
148             public void onFailure(Throwable t) {
149                 if (deviceFlowRegistryFill.isCancelled()) {
150                     LOG.debug("Cancelled filling flow registry with flows for node: {}", getIdentifier());
151                 } else {
152                     LOG.warn("Failed filling flow registry with flows for node: {} with exception: {}", getIdentifier(), t);
153                 }
154             }
155         });
156     }
157
158 }