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