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