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