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
9 package org.opendaylight.openflowplugin.applications.frsync.impl;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.ListeningExecutorService;
13 import com.google.common.util.concurrent.ThreadFactoryBuilder;
14 import java.lang.Thread.UncaughtExceptionHandler;
15 import java.util.concurrent.Callable;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
21 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
22 import org.opendaylight.openflowplugin.applications.frsync.NodeListener;
23 import org.opendaylight.openflowplugin.applications.frsync.SyncPlanPushStrategy;
24 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
25 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeCachedDao;
26 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeDao;
27 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeOdlDao;
28 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeSnapshotDao;
29 import org.opendaylight.openflowplugin.applications.frsync.impl.strategy.SyncPlanPushStrategyFlatBatchImpl;
30 import org.opendaylight.openflowplugin.applications.frsync.util.SemaphoreKeeperGuavaImpl;
31 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.SalFlatBatchService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
37 import org.opendaylight.yangtools.concepts.ListenerRegistration;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Top provider of forwarding rules synchronization functionality.
45 public class ForwardingRulesSyncProvider implements AutoCloseable, BindingAwareProvider {
47 private static final Logger LOG = LoggerFactory.getLogger(ForwardingRulesSyncProvider.class);
48 public static final int STARTUP_LOOP_TICK = 500;
49 public static final int STARTUP_LOOP_MAX_RETRIES = 8;
51 private final DataBroker dataService;
52 private final SalTableService salTableService;
53 private final SalFlatBatchService flatBatchService;
55 /** wildcard path to flow-capable-node augmentation of inventory node */
56 private static final InstanceIdentifier<FlowCapableNode> FLOW_CAPABLE_NODE_WC_PATH =
57 InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class);
58 /** wildcard path to node (not flow-capable-node augmentation) of inventory node */
59 private static final InstanceIdentifier<Node> NODE_WC_PATH =
60 InstanceIdentifier.create(Nodes.class).child(Node.class);
63 private final DataTreeIdentifier<FlowCapableNode> nodeConfigDataTreePath;
64 private final DataTreeIdentifier<Node> nodeOperationalDataTreePath;
66 private ListenerRegistration<NodeListener> dataTreeConfigChangeListener;
67 private ListenerRegistration<NodeListener> dataTreeOperationalChangeListener;
70 public ForwardingRulesSyncProvider(final BindingAwareBroker broker,
71 final DataBroker dataBroker,
72 final RpcConsumerRegistry rpcRegistry) {
73 Preconditions.checkArgument(rpcRegistry != null, "RpcConsumerRegistry can not be null !");
74 this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
75 this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
76 "RPC SalTableService not found.");
77 this.flatBatchService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlatBatchService.class),
78 "RPC SalFlatBatchService not found.");
80 nodeConfigDataTreePath = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, FLOW_CAPABLE_NODE_WC_PATH);
81 nodeOperationalDataTreePath = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, NODE_WC_PATH);
83 broker.registerProvider(this);
86 private final ListeningExecutorService syncThreadPool = FrmExecutors.instance()
87 // TODO improve log in ThreadPoolExecutor.afterExecute
88 // TODO max bloking queue size
89 // TODO core/min pool size
90 .newFixedThreadPool(6, new ThreadFactoryBuilder()
91 .setNameFormat(SyncReactorFutureDecorator.FRM_RPC_CLIENT_PREFIX + "%d")
93 .setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
95 public void uncaughtException(Thread thread, Throwable e) {
96 LOG.error("uncaught exception {}", thread, e);
102 public void onSessionInitiated(final BindingAwareBroker.ProviderContext providerContext) {
103 final TableForwarder tableForwarder = new TableForwarder(salTableService);
105 final SyncPlanPushStrategy syncPlanPushStrategy = new SyncPlanPushStrategyFlatBatchImpl()
106 .setFlatBatchService(flatBatchService)
107 .setTableForwarder(tableForwarder);
109 final SyncReactorImpl syncReactorImpl = new SyncReactorImpl(syncPlanPushStrategy);
110 final SyncReactor syncReactorGuard = new SyncReactorGuardDecorator(syncReactorImpl,
111 new SemaphoreKeeperGuavaImpl<InstanceIdentifier<FlowCapableNode>>(1, true));
113 final SyncReactor cfgReactor = new SyncReactorFutureWithCompressionDecorator(syncReactorGuard, syncThreadPool);
114 final SyncReactor operReactor = new SyncReactorFutureWithCompressionDecorator(syncReactorGuard, syncThreadPool);
116 final FlowCapableNodeSnapshotDao configSnapshot = new FlowCapableNodeSnapshotDao();
117 final FlowCapableNodeSnapshotDao operationalSnapshot = new FlowCapableNodeSnapshotDao();
118 final FlowCapableNodeDao configDao = new FlowCapableNodeCachedDao(configSnapshot,
119 new FlowCapableNodeOdlDao(dataService, LogicalDatastoreType.CONFIGURATION));
120 final FlowCapableNodeDao operationalDao = new FlowCapableNodeCachedDao(operationalSnapshot,
121 new FlowCapableNodeOdlDao(dataService, LogicalDatastoreType.OPERATIONAL));
123 final NodeListener<FlowCapableNode> nodeListenerConfig = new SimplifiedConfigListener(cfgReactor, configSnapshot, operationalDao);
124 final NodeListener<Node> nodeListenerOperational = new SimplifiedOperationalListener(operReactor, operationalSnapshot, configDao);
127 SimpleTaskRetryLooper looper1 = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
128 dataTreeConfigChangeListener = looper1.loopUntilNoException(
129 new Callable<ListenerRegistration<NodeListener>>() {
131 public ListenerRegistration<NodeListener> call() throws Exception {
132 return dataService.registerDataTreeChangeListener(
133 nodeConfigDataTreePath, nodeListenerConfig);
137 SimpleTaskRetryLooper looper2 = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
138 dataTreeOperationalChangeListener = looper2.loopUntilNoException(
139 new Callable<ListenerRegistration<NodeListener>>() {
141 public ListenerRegistration<NodeListener> call() throws Exception {
142 return dataService.registerDataTreeChangeListener(
143 nodeOperationalDataTreePath, nodeListenerOperational);
146 } catch (final Exception e) {
147 LOG.warn("FR-Sync node DataChange listener registration fail!", e);
148 throw new IllegalStateException("FR-Sync startup fail!", e);
150 LOG.info("ForwardingRulesSync has started.");
153 public void close() throws Exception {
154 if (dataTreeConfigChangeListener != null) {
155 dataTreeConfigChangeListener.close();
156 dataTreeConfigChangeListener = null;
158 if (dataTreeOperationalChangeListener != null) {
159 dataTreeOperationalChangeListener.close();
160 dataTreeOperationalChangeListener = null;
163 syncThreadPool.shutdown();