Merge "Remove uses of BindingAwareProvider/Consumer"
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / impl / ForwardingRulesSyncProvider.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
9 package org.opendaylight.openflowplugin.applications.frsync.impl;
10
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;
43
44 /**
45  * Top provider of forwarding rules synchronization functionality.
46  */
47 public class ForwardingRulesSyncProvider implements AutoCloseable {
48
49     private static final Logger LOG = LoggerFactory.getLogger(ForwardingRulesSyncProvider.class);
50     private static final String FRS_EXECUTOR_PREFIX = "FRS-executor-";
51
52     private final DataBroker dataService;
53     private final ClusterSingletonServiceProvider clusterSingletonService;
54     private final SalTableService salTableService;
55     private final SalFlatBatchService flatBatchService;
56
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);
63
64     private final DataTreeIdentifier<FlowCapableNode> nodeConfigDataTreePath;
65     private final DataTreeIdentifier<Node> nodeOperationalDataTreePath;
66
67     private ListenerRegistration<NodeListener> dataTreeConfigChangeListener;
68     private ListenerRegistration<NodeListener> dataTreeOperationalChangeListener;
69
70     private final ListeningExecutorService syncThreadPool;
71
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.");
83
84         nodeConfigDataTreePath = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION,
85                 FLOW_CAPABLE_NODE_WC_PATH);
86         nodeOperationalDataTreePath = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, NODE_WC_PATH);
87
88         final ExecutorService executorService = Executors.newCachedThreadPool(new ThreadFactoryBuilder()
89                 .setNameFormat(FRS_EXECUTOR_PREFIX + "%d")
90                 .setDaemon(false)
91                 .setUncaughtExceptionHandler((thread, ex) -> LOG.error("Uncaught exception {}", thread, ex))
92                 .build());
93         syncThreadPool = MoreExecutors.listeningDecorator(executorService);
94     }
95
96     public void init() {
97         final TableForwarder tableForwarder = new TableForwarder(salTableService);
98
99         final SyncPlanPushStrategy syncPlanPushStrategy = new SyncPlanPushStrategyFlatBatchImpl()
100                 .setFlatBatchService(flatBatchService)
101                 .setTableForwarder(tableForwarder);
102
103         final ReconciliationRegistry reconciliationRegistry = new ReconciliationRegistry();
104         final DeviceMastershipManager deviceMastershipManager =
105                 new DeviceMastershipManager(clusterSingletonService, reconciliationRegistry);
106
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);
111
112         final SyncReactor reactor = new SyncReactorClusterDecorator(syncReactorFutureZip, deviceMastershipManager);
113
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));
120
121         final NodeListener<FlowCapableNode> nodeListenerConfig =
122                 new SimplifiedConfigListener(reactor, configSnapshot, operationalDao);
123         final NodeListener<Node> nodeListenerOperational = new SimplifiedOperationalListener(reactor,
124                 operationalSnapshot, configDao, reconciliationRegistry, deviceMastershipManager);
125
126         dataTreeConfigChangeListener =
127                 dataService.registerDataTreeChangeListener(nodeConfigDataTreePath, nodeListenerConfig);
128         dataTreeOperationalChangeListener =
129                 dataService.registerDataTreeChangeListener(nodeOperationalDataTreePath, nodeListenerOperational);
130
131         LOG.info("ForwardingRulesSync has started.");
132     }
133
134     @Override
135     public void close() {
136         if (Objects.nonNull(dataTreeConfigChangeListener)) {
137             dataTreeConfigChangeListener.close();
138             dataTreeConfigChangeListener = null;
139         }
140
141         if (Objects.nonNull(dataTreeOperationalChangeListener)) {
142             dataTreeOperationalChangeListener.close();
143             dataTreeOperationalChangeListener = null;
144         }
145
146         syncThreadPool.shutdown();
147     }
148
149 }