Migrate fr-sync to use Uint types
[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 package org.opendaylight.openflowplugin.applications.frsync.impl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.ListeningExecutorService;
12 import com.google.common.util.concurrent.MoreExecutors;
13 import com.google.common.util.concurrent.ThreadFactoryBuilder;
14 import java.util.concurrent.ExecutorService;
15 import java.util.concurrent.Executors;
16 import org.opendaylight.mdsal.binding.api.DataBroker;
17 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
18 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
21 import org.opendaylight.openflowplugin.applications.frsync.NodeListener;
22 import org.opendaylight.openflowplugin.applications.frsync.SyncPlanPushStrategy;
23 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
24 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeCachedDao;
25 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeDao;
26 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeOdlDao;
27 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeSnapshotDao;
28 import org.opendaylight.openflowplugin.applications.frsync.impl.clustering.DeviceMastershipManager;
29 import org.opendaylight.openflowplugin.applications.frsync.impl.strategy.SyncPlanPushStrategyFlatBatchImpl;
30 import org.opendaylight.openflowplugin.applications.frsync.impl.strategy.TableForwarder;
31 import org.opendaylight.openflowplugin.applications.frsync.util.ReconciliationRegistry;
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;
41
42 /**
43  * Top provider of forwarding rules synchronization functionality.
44  */
45 public class ForwardingRulesSyncProvider implements AutoCloseable {
46
47     private static final Logger LOG = LoggerFactory.getLogger(ForwardingRulesSyncProvider.class);
48     private static final String FRS_EXECUTOR_PREFIX = "FRS-executor-";
49
50     private final DataBroker dataService;
51     private final ClusterSingletonServiceProvider clusterSingletonService;
52     private final SalTableService salTableService;
53     private final SalFlatBatchService flatBatchService;
54
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);
61
62     private final DataTreeIdentifier<FlowCapableNode> nodeConfigDataTreePath;
63     private final DataTreeIdentifier<Node> nodeOperationalDataTreePath;
64
65     private ListenerRegistration<?> dataTreeConfigChangeListener;
66     private ListenerRegistration<?> dataTreeOperationalChangeListener;
67
68     private final ListeningExecutorService syncThreadPool;
69
70     public ForwardingRulesSyncProvider(final DataBroker dataBroker,
71                                        final RpcConsumerRegistry rpcRegistry,
72                                        final ClusterSingletonServiceProvider clusterSingletonService) {
73         Preconditions.checkNotNull(rpcRegistry, "RpcConsumerRegistry can not be null!");
74         this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
75         this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService,
76                 "ClusterSingletonServiceProvider can not be null!");
77         this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
78                 "RPC SalTableService not found.");
79         this.flatBatchService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlatBatchService.class),
80                 "RPC SalFlatBatchService not found.");
81
82         nodeConfigDataTreePath = DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION,
83                 FLOW_CAPABLE_NODE_WC_PATH);
84         nodeOperationalDataTreePath = DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, NODE_WC_PATH);
85
86         final ExecutorService executorService = Executors.newCachedThreadPool(new ThreadFactoryBuilder()
87                 .setNameFormat(FRS_EXECUTOR_PREFIX + "%d")
88                 .setDaemon(false)
89                 .setUncaughtExceptionHandler((thread, ex) -> LOG.error("Uncaught exception {}", thread, ex))
90                 .build());
91         syncThreadPool = MoreExecutors.listeningDecorator(executorService);
92     }
93
94     public void init() {
95         final TableForwarder tableForwarder = new TableForwarder(salTableService);
96
97         final SyncPlanPushStrategy syncPlanPushStrategy = new SyncPlanPushStrategyFlatBatchImpl()
98                 .setFlatBatchService(flatBatchService)
99                 .setTableForwarder(tableForwarder);
100
101         final ReconciliationRegistry reconciliationRegistry = new ReconciliationRegistry();
102         final DeviceMastershipManager deviceMastershipManager =
103                 new DeviceMastershipManager(clusterSingletonService, reconciliationRegistry);
104
105         final SyncReactor syncReactorImpl = new SyncReactorImpl(syncPlanPushStrategy);
106         final SyncReactor syncReactorRetry = new SyncReactorRetryDecorator(syncReactorImpl, reconciliationRegistry);
107         final SyncReactor syncReactorGuard = new SyncReactorGuardDecorator(syncReactorRetry);
108         final SyncReactor syncReactorFutureZip = new SyncReactorFutureZipDecorator(syncReactorGuard, syncThreadPool);
109
110         final SyncReactor reactor = new SyncReactorClusterDecorator(syncReactorFutureZip, deviceMastershipManager);
111
112         final FlowCapableNodeSnapshotDao configSnapshot = new FlowCapableNodeSnapshotDao();
113         final FlowCapableNodeSnapshotDao operationalSnapshot = new FlowCapableNodeSnapshotDao();
114         final FlowCapableNodeDao configDao = new FlowCapableNodeCachedDao(configSnapshot,
115                 new FlowCapableNodeOdlDao(dataService, LogicalDatastoreType.CONFIGURATION));
116         final FlowCapableNodeDao operationalDao = new FlowCapableNodeCachedDao(operationalSnapshot,
117                 new FlowCapableNodeOdlDao(dataService, LogicalDatastoreType.OPERATIONAL));
118
119         final NodeListener<FlowCapableNode> nodeListenerConfig =
120                 new SimplifiedConfigListener(reactor, configSnapshot, operationalDao);
121         final NodeListener<Node> nodeListenerOperational = new SimplifiedOperationalListener(reactor,
122                 operationalSnapshot, configDao, reconciliationRegistry, deviceMastershipManager);
123
124         dataTreeConfigChangeListener =
125                 dataService.registerDataTreeChangeListener(nodeConfigDataTreePath, nodeListenerConfig);
126         dataTreeOperationalChangeListener =
127                 dataService.registerDataTreeChangeListener(nodeOperationalDataTreePath, nodeListenerOperational);
128
129         LOG.info("ForwardingRulesSync has started.");
130     }
131
132     @Override
133     public void close() {
134         if (dataTreeConfigChangeListener != null) {
135             dataTreeConfigChangeListener.close();
136             dataTreeConfigChangeListener = null;
137         }
138
139         if (dataTreeOperationalChangeListener != null) {
140             dataTreeOperationalChangeListener.close();
141             dataTreeOperationalChangeListener = null;
142         }
143
144         syncThreadPool.shutdown();
145     }
146 }