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.RetryRegistry;
31 import org.opendaylight.openflowplugin.applications.frsync.util.SemaphoreKeeperGuavaImpl;
32 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.SalFlatBatchService;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
38 import org.opendaylight.yangtools.concepts.ListenerRegistration;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * Top provider of forwarding rules synchronization functionality.
46 public class ForwardingRulesSyncProvider implements AutoCloseable, BindingAwareProvider {
48 private static final Logger LOG = LoggerFactory.getLogger(ForwardingRulesSyncProvider.class);
49 private static final int STARTUP_LOOP_TICK = 500;
50 private static final int STARTUP_LOOP_MAX_RETRIES = 8;
52 private final DataBroker dataService;
53 private final SalTableService salTableService;
54 private final SalFlatBatchService flatBatchService;
56 /** Wildcard path to flow-capable-node augmentation of inventory node. */
57 private static final InstanceIdentifier<FlowCapableNode> FLOW_CAPABLE_NODE_WC_PATH =
58 InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class);
59 /** Wildcard path to node (not flow-capable-node augmentation) of inventory node. */
60 private static final InstanceIdentifier<Node> NODE_WC_PATH =
61 InstanceIdentifier.create(Nodes.class).child(Node.class);
64 private final DataTreeIdentifier<FlowCapableNode> nodeConfigDataTreePath;
65 private final DataTreeIdentifier<Node> nodeOperationalDataTreePath;
67 private ListenerRegistration<NodeListener> dataTreeConfigChangeListener;
68 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 RetryRegistry retryRegistry = new RetryRegistry();
111 final SyncReactor syncReactorImpl = new SyncReactorImpl(syncPlanPushStrategy);
112 final SyncReactor syncReactorRetry = new SyncReactorRetryDecorator(syncReactorImpl, retryRegistry);
113 final SyncReactor syncReactorGuard = new SyncReactorGuardDecorator(syncReactorRetry,
114 new SemaphoreKeeperGuavaImpl<InstanceIdentifier<FlowCapableNode>>(1, true));
116 final SyncReactor reactor = new SyncReactorFutureZipDecorator(syncReactorGuard, syncThreadPool);
118 final FlowCapableNodeSnapshotDao configSnapshot = new FlowCapableNodeSnapshotDao();
119 final FlowCapableNodeSnapshotDao operationalSnapshot = new FlowCapableNodeSnapshotDao();
120 final FlowCapableNodeDao configDao = new FlowCapableNodeCachedDao(configSnapshot,
121 new FlowCapableNodeOdlDao(dataService, LogicalDatastoreType.CONFIGURATION));
122 final FlowCapableNodeDao operationalDao = new FlowCapableNodeCachedDao(operationalSnapshot,
123 new FlowCapableNodeOdlDao(dataService, LogicalDatastoreType.OPERATIONAL));
125 final NodeListener<FlowCapableNode> nodeListenerConfig =
126 new SimplifiedConfigListener(reactor, configSnapshot, operationalDao);
127 final NodeListener<Node> nodeListenerOperational =
128 new SimplifiedOperationalRetryListener(reactor, operationalSnapshot, configDao, retryRegistry);
131 SimpleTaskRetryLooper looper1 = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
132 dataTreeConfigChangeListener = looper1.loopUntilNoException(
133 new Callable<ListenerRegistration<NodeListener>>() {
135 public ListenerRegistration<NodeListener> call() throws Exception {
136 return dataService.registerDataTreeChangeListener(
137 nodeConfigDataTreePath, nodeListenerConfig);
141 SimpleTaskRetryLooper looper2 = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
142 dataTreeOperationalChangeListener = looper2.loopUntilNoException(
143 new Callable<ListenerRegistration<NodeListener>>() {
145 public ListenerRegistration<NodeListener> call() throws Exception {
146 return dataService.registerDataTreeChangeListener(
147 nodeOperationalDataTreePath, nodeListenerOperational);
150 } catch (final Exception e) {
151 LOG.warn("FR-Sync node DataChange listener registration fail!", e);
152 throw new IllegalStateException("FR-Sync startup fail!", e);
154 LOG.info("ForwardingRulesSync has started.");
157 public void close() throws Exception {
158 if (dataTreeConfigChangeListener != null) {
159 dataTreeConfigChangeListener.close();
160 dataTreeConfigChangeListener = null;
162 if (dataTreeOperationalChangeListener != null) {
163 dataTreeOperationalChangeListener.close();
164 dataTreeOperationalChangeListener = null;
167 syncThreadPool.shutdown();