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.MoreExecutors;
14 import com.google.common.util.concurrent.ThreadFactoryBuilder;
15 import java.util.Objects;
16 import java.util.concurrent.ExecutorService;
17 import java.util.concurrent.Executors;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
22 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
23 import org.opendaylight.openflowplugin.applications.frsync.NodeListener;
24 import org.opendaylight.openflowplugin.applications.frsync.SyncPlanPushStrategy;
25 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
26 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeCachedDao;
27 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeDao;
28 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeOdlDao;
29 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeSnapshotDao;
30 import org.opendaylight.openflowplugin.applications.frsync.impl.clustering.DeviceMastershipManager;
31 import org.opendaylight.openflowplugin.applications.frsync.impl.strategy.SyncPlanPushStrategyFlatBatchImpl;
32 import org.opendaylight.openflowplugin.applications.frsync.impl.strategy.TableForwarder;
33 import org.opendaylight.openflowplugin.applications.frsync.util.ReconciliationRegistry;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.SalFlatBatchService;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
39 import org.opendaylight.yangtools.concepts.ListenerRegistration;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * Top provider of forwarding rules synchronization functionality.
47 public class ForwardingRulesSyncProvider implements AutoCloseable {
49 private static final Logger LOG = LoggerFactory.getLogger(ForwardingRulesSyncProvider.class);
50 private static final String FRS_EXECUTOR_PREFIX = "FRS-executor-";
52 private final DataBroker dataService;
53 private final ClusterSingletonServiceProvider clusterSingletonService;
54 private final SalTableService salTableService;
55 private final SalFlatBatchService flatBatchService;
57 /** Wildcard path to flow-capable-node augmentation of inventory node. */
58 private static final InstanceIdentifier<FlowCapableNode> FLOW_CAPABLE_NODE_WC_PATH =
59 InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class);
60 /** Wildcard path to node (not flow-capable-node augmentation) of inventory node. */
61 private static final InstanceIdentifier<Node> NODE_WC_PATH =
62 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 private final ListeningExecutorService syncThreadPool;
72 public ForwardingRulesSyncProvider(final DataBroker dataBroker,
73 final RpcConsumerRegistry rpcRegistry,
74 final ClusterSingletonServiceProvider clusterSingletonService) {
75 Preconditions.checkNotNull(rpcRegistry, "RpcConsumerRegistry can not be null!");
76 this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
77 this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService,
78 "ClusterSingletonServiceProvider can not be null!");
79 this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
80 "RPC SalTableService not found.");
81 this.flatBatchService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlatBatchService.class),
82 "RPC SalFlatBatchService not found.");
84 nodeConfigDataTreePath = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION,
85 FLOW_CAPABLE_NODE_WC_PATH);
86 nodeOperationalDataTreePath = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, NODE_WC_PATH);
88 final ExecutorService executorService = Executors.newCachedThreadPool(new ThreadFactoryBuilder()
89 .setNameFormat(FRS_EXECUTOR_PREFIX + "%d")
91 .setUncaughtExceptionHandler((thread, ex) -> LOG.error("Uncaught exception {}", thread, ex))
93 syncThreadPool = MoreExecutors.listeningDecorator(executorService);
97 final TableForwarder tableForwarder = new TableForwarder(salTableService);
99 final SyncPlanPushStrategy syncPlanPushStrategy = new SyncPlanPushStrategyFlatBatchImpl()
100 .setFlatBatchService(flatBatchService)
101 .setTableForwarder(tableForwarder);
103 final ReconciliationRegistry reconciliationRegistry = new ReconciliationRegistry();
104 final DeviceMastershipManager deviceMastershipManager =
105 new DeviceMastershipManager(clusterSingletonService, reconciliationRegistry);
107 final SyncReactor syncReactorImpl = new SyncReactorImpl(syncPlanPushStrategy);
108 final SyncReactor syncReactorRetry = new SyncReactorRetryDecorator(syncReactorImpl, reconciliationRegistry);
109 final SyncReactor syncReactorGuard = new SyncReactorGuardDecorator(syncReactorRetry);
110 final SyncReactor syncReactorFutureZip = new SyncReactorFutureZipDecorator(syncReactorGuard, syncThreadPool);
112 final SyncReactor reactor = new SyncReactorClusterDecorator(syncReactorFutureZip, deviceMastershipManager);
114 final FlowCapableNodeSnapshotDao configSnapshot = new FlowCapableNodeSnapshotDao();
115 final FlowCapableNodeSnapshotDao operationalSnapshot = new FlowCapableNodeSnapshotDao();
116 final FlowCapableNodeDao configDao = new FlowCapableNodeCachedDao(configSnapshot,
117 new FlowCapableNodeOdlDao(dataService, LogicalDatastoreType.CONFIGURATION));
118 final FlowCapableNodeDao operationalDao = new FlowCapableNodeCachedDao(operationalSnapshot,
119 new FlowCapableNodeOdlDao(dataService, LogicalDatastoreType.OPERATIONAL));
121 final NodeListener<FlowCapableNode> nodeListenerConfig =
122 new SimplifiedConfigListener(reactor, configSnapshot, operationalDao);
123 final NodeListener<Node> nodeListenerOperational = new SimplifiedOperationalListener(reactor,
124 operationalSnapshot, configDao, reconciliationRegistry, deviceMastershipManager);
126 dataTreeConfigChangeListener =
127 dataService.registerDataTreeChangeListener(nodeConfigDataTreePath, nodeListenerConfig);
128 dataTreeOperationalChangeListener =
129 dataService.registerDataTreeChangeListener(nodeOperationalDataTreePath, nodeListenerOperational);
131 LOG.info("ForwardingRulesSync has started.");
135 public void close() {
136 if (Objects.nonNull(dataTreeConfigChangeListener)) {
137 dataTreeConfigChangeListener.close();
138 dataTreeConfigChangeListener = null;
141 if (Objects.nonNull(dataTreeOperationalChangeListener)) {
142 dataTreeOperationalChangeListener.close();
143 dataTreeOperationalChangeListener = null;
146 syncThreadPool.shutdown();