2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.lifecycle;
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;
32 public class LifecycleServiceImpl implements LifecycleService {
34 private static final Logger LOG = LoggerFactory.getLogger(LifecycleServiceImpl.class);
36 private DeviceContext deviceContext;
37 private RpcContext rpcContext;
38 private RoleContext roleContext;
39 private StatisticsContext statContext;
40 private ClusterSingletonServiceRegistration registration;
44 public void instantiateServiceInstance() {
47 LOG.info("Starting device context cluster services for node {}", getIdentifier());
48 this.deviceContext.startupClusterServices();
50 LOG.info("Starting statistics context cluster services for node {}", getIdentifier());
51 this.statContext.startupClusterServices();
53 LOG.info("Statistics initial gathering OK, submitting data for node {}", getIdentifier());
54 this.deviceContext.initialSubmitTransaction();
56 LOG.info("Starting rpc context cluster services for node {}", getIdentifier());
57 this.rpcContext.startupClusterServices();
59 LOG.info("Starting role context cluster services for node {}", getIdentifier());
60 this.roleContext.startupClusterServices();
62 LOG.info("Caching flows IDs ...");
63 fillDeviceFlowRegistry();
65 } catch (ExecutionException | InterruptedException e) {
66 LOG.warn("Cluster service {} was unable to start.", this.getIdentifier());
71 public ListenableFuture<Void> closeServiceInstance() {
72 statContext.stopClusterServices();
73 rpcContext.stopClusterServices();
74 return deviceContext.stopClusterServices();
78 public ServiceGroupIdentifier getIdentifier() {
79 return deviceContext.getServiceIdentifier();
84 public void close() throws Exception {
85 if (registration != null) {
92 public void registerService(final ClusterSingletonServiceProvider singletonServiceProvider) {
93 this.registration = singletonServiceProvider.registerClusterSingletonService(this);
97 public void setDeviceContext(final DeviceContext deviceContext) {
98 this.deviceContext = deviceContext;
102 public void setRpcContext(final RpcContext rpcContext) {
103 this.rpcContext = rpcContext;
107 public void setRoleContext(final RoleContext roleContext) {
108 this.roleContext = roleContext;
112 public void setStatContext(final StatisticsContext statContext) {
113 this.statContext = statContext;
116 private void fillDeviceFlowRegistry() {
117 // Fill device flow registry with flows from datastore
118 final ListenableFuture<List<Optional<FlowCapableNode>>> deviceFlowRegistryFill = deviceContext.getDeviceFlowRegistry().fill();
120 // Start statistics scheduling only after we finished initializing device flow registry
121 Futures.addCallback(deviceFlowRegistryFill, new FutureCallback<List<Optional<FlowCapableNode>>>() {
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())
135 LOG.debug("Finished filling flow registry with {} flows for node: {}", flowCount, getIdentifier());
140 public void onFailure(Throwable t) {
141 if (deviceFlowRegistryFill.isCancelled()) {
142 LOG.debug("Cancelled filling flow registry with flows for node: {}", getIdentifier());
144 LOG.warn("Failed filling flow registry with flows for node: {} with exception: {}", getIdentifier(), t);