Bug 6745 Improve compression queue locking and handle InterruptedException
[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.BindingAwareBroker;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
24 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
25 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
26 import org.opendaylight.openflowplugin.applications.frsync.NodeListener;
27 import org.opendaylight.openflowplugin.applications.frsync.SyncPlanPushStrategy;
28 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
29 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeCachedDao;
30 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeDao;
31 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeOdlDao;
32 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeSnapshotDao;
33 import org.opendaylight.openflowplugin.applications.frsync.impl.clustering.DeviceMastershipManager;
34 import org.opendaylight.openflowplugin.applications.frsync.impl.strategy.SyncPlanPushStrategyFlatBatchImpl;
35 import org.opendaylight.openflowplugin.applications.frsync.impl.strategy.TableForwarder;
36 import org.opendaylight.openflowplugin.applications.frsync.util.ReconciliationRegistry;
37 import org.opendaylight.openflowplugin.applications.frsync.util.SemaphoreKeeperGuavaImpl;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.SalFlatBatchService;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
43 import org.opendaylight.yangtools.concepts.ListenerRegistration;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * Top provider of forwarding rules synchronization functionality.
50  */
51 public class ForwardingRulesSyncProvider implements AutoCloseable, BindingAwareProvider {
52
53     private static final Logger LOG = LoggerFactory.getLogger(ForwardingRulesSyncProvider.class);
54     private static final String FRS_EXECUTOR_PREFIX = "FRS-executor-";
55
56     private final DataBroker dataService;
57     private final ClusterSingletonServiceProvider clusterSingletonService;
58     private final SalTableService salTableService;
59     private final SalFlatBatchService flatBatchService;
60
61     /** Wildcard path to flow-capable-node augmentation of inventory node. */
62     private static final InstanceIdentifier<FlowCapableNode> FLOW_CAPABLE_NODE_WC_PATH =
63             InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class);
64     /** Wildcard path to node (not flow-capable-node augmentation) of inventory node. */
65     private static final InstanceIdentifier<Node> NODE_WC_PATH =
66             InstanceIdentifier.create(Nodes.class).child(Node.class);
67
68     private final DataTreeIdentifier<FlowCapableNode> nodeConfigDataTreePath;
69     private final DataTreeIdentifier<Node> nodeOperationalDataTreePath;
70
71     private ListenerRegistration<NodeListener> dataTreeConfigChangeListener;
72     private ListenerRegistration<NodeListener> dataTreeOperationalChangeListener;
73
74     private final ListeningExecutorService syncThreadPool;
75
76     public ForwardingRulesSyncProvider(final BindingAwareBroker broker,
77                                        final DataBroker dataBroker,
78                                        final RpcConsumerRegistry rpcRegistry,
79                                        final ClusterSingletonServiceProvider clusterSingletonService) {
80         Preconditions.checkNotNull(rpcRegistry, "RpcConsumerRegistry can not be null!");
81         this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
82         this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService,
83                 "ClusterSingletonServiceProvider can not be null!");
84         this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
85                 "RPC SalTableService not found.");
86         this.flatBatchService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlatBatchService.class),
87                 "RPC SalFlatBatchService not found.");
88
89         nodeConfigDataTreePath = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, FLOW_CAPABLE_NODE_WC_PATH);
90         nodeOperationalDataTreePath = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, NODE_WC_PATH);
91
92         final ExecutorService executorService= Executors.newCachedThreadPool(new ThreadFactoryBuilder()
93                 .setNameFormat(FRS_EXECUTOR_PREFIX + "%d")
94                 .setDaemon(false)
95                 .setUncaughtExceptionHandler((thread, e) -> LOG.error("Uncaught exception {}", thread, e))
96                 .build());
97         syncThreadPool = MoreExecutors.listeningDecorator(executorService);
98         broker.registerProvider(this);
99     }
100
101     @Override
102     public void onSessionInitiated(final ProviderContext providerContext) {
103         final TableForwarder tableForwarder = new TableForwarder(salTableService);
104
105         final SyncPlanPushStrategy syncPlanPushStrategy = new SyncPlanPushStrategyFlatBatchImpl()
106                 .setFlatBatchService(flatBatchService)
107                 .setTableForwarder(tableForwarder);
108
109         final ReconciliationRegistry reconciliationRegistry = new ReconciliationRegistry();
110         final DeviceMastershipManager deviceMastershipManager =
111                 new DeviceMastershipManager(clusterSingletonService, reconciliationRegistry);
112
113         final SyncReactor syncReactorImpl = new SyncReactorImpl(syncPlanPushStrategy);
114         final SyncReactor syncReactorRetry = new SyncReactorRetryDecorator(syncReactorImpl, reconciliationRegistry);
115         final SyncReactor syncReactorGuard = new SyncReactorGuardDecorator(syncReactorRetry,
116                 new SemaphoreKeeperGuavaImpl<>(1, true));
117         final SyncReactor syncReactorFutureZip = new SyncReactorFutureZipDecorator(syncReactorGuard, syncThreadPool,
118                 new SemaphoreKeeperGuavaImpl<>(1, true));
119
120         final SyncReactor reactor = new SyncReactorClusterDecorator(syncReactorFutureZip, deviceMastershipManager);
121
122         final FlowCapableNodeSnapshotDao configSnapshot = new FlowCapableNodeSnapshotDao();
123         final FlowCapableNodeSnapshotDao operationalSnapshot = new FlowCapableNodeSnapshotDao();
124         final FlowCapableNodeDao configDao = new FlowCapableNodeCachedDao(configSnapshot,
125                 new FlowCapableNodeOdlDao(dataService, LogicalDatastoreType.CONFIGURATION));
126         final FlowCapableNodeDao operationalDao = new FlowCapableNodeCachedDao(operationalSnapshot,
127                 new FlowCapableNodeOdlDao(dataService, LogicalDatastoreType.OPERATIONAL));
128
129         final NodeListener<FlowCapableNode> nodeListenerConfig =
130                 new SimplifiedConfigListener(reactor, configSnapshot, operationalDao);
131         final NodeListener<Node> nodeListenerOperational =
132                 new SimplifiedOperationalListener(reactor, operationalSnapshot, configDao, reconciliationRegistry, deviceMastershipManager);
133
134         dataTreeConfigChangeListener =
135                 dataService.registerDataTreeChangeListener(nodeConfigDataTreePath, nodeListenerConfig);
136         dataTreeOperationalChangeListener =
137                 dataService.registerDataTreeChangeListener(nodeOperationalDataTreePath, nodeListenerOperational);
138
139         LOG.info("ForwardingRulesSync has started.");
140     }
141
142     public void close() {
143         if (Objects.nonNull(dataTreeConfigChangeListener)) {
144             dataTreeConfigChangeListener.close();
145             dataTreeConfigChangeListener = null;
146         }
147
148         if (Objects.nonNull(dataTreeOperationalChangeListener)) {
149             dataTreeOperationalChangeListener.close();
150             dataTreeOperationalChangeListener = null;
151         }
152
153         syncThreadPool.shutdown();
154     }
155
156 }