Merge "Sonar - technical debt of FRS app" into stable/boron
[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.concurrent.ExecutorService;
16 import java.util.concurrent.Executors;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
23 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
24 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
25 import org.opendaylight.openflowplugin.applications.frsync.NodeListener;
26 import org.opendaylight.openflowplugin.applications.frsync.SyncPlanPushStrategy;
27 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
28 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeCachedDao;
29 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeDao;
30 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeOdlDao;
31 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeSnapshotDao;
32 import org.opendaylight.openflowplugin.applications.frsync.impl.clustering.DeviceMastershipManager;
33 import org.opendaylight.openflowplugin.applications.frsync.impl.strategy.SyncPlanPushStrategyFlatBatchImpl;
34 import org.opendaylight.openflowplugin.applications.frsync.impl.strategy.TableForwarder;
35 import org.opendaylight.openflowplugin.applications.frsync.util.ReconciliationRegistry;
36 import org.opendaylight.openflowplugin.applications.frsync.util.SemaphoreKeeperGuavaImpl;
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
54     private final DataBroker dataService;
55     private final ClusterSingletonServiceProvider clusterSingletonService;
56     private final SalTableService salTableService;
57     private final SalFlatBatchService flatBatchService;
58
59     /** Wildcard path to flow-capable-node augmentation of inventory node. */
60     private static final InstanceIdentifier<FlowCapableNode> FLOW_CAPABLE_NODE_WC_PATH =
61             InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class);
62     /** Wildcard path to node (not flow-capable-node augmentation) of inventory node. */
63     private static final InstanceIdentifier<Node> NODE_WC_PATH =
64             InstanceIdentifier.create(Nodes.class).child(Node.class);
65
66     private final DataTreeIdentifier<FlowCapableNode> nodeConfigDataTreePath;
67     private final DataTreeIdentifier<Node> nodeOperationalDataTreePath;
68
69     private ListenerRegistration<NodeListener> dataTreeConfigChangeListener;
70     private ListenerRegistration<NodeListener> dataTreeOperationalChangeListener;
71
72     private final ListeningExecutorService syncThreadPool;
73
74     public ForwardingRulesSyncProvider(final BindingAwareBroker broker,
75                                        final DataBroker dataBroker,
76                                        final RpcConsumerRegistry rpcRegistry,
77                                        final ClusterSingletonServiceProvider clusterSingletonService) {
78         Preconditions.checkArgument(rpcRegistry != null, "RpcConsumerRegistry can not be null!");
79         this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
80         this.clusterSingletonService = Preconditions.checkNotNull(clusterSingletonService,
81                 "ClusterSingletonServiceProvider can not be null!");
82         this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
83                 "RPC SalTableService not found.");
84         this.flatBatchService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlatBatchService.class),
85                 "RPC SalFlatBatchService not found.");
86
87         nodeConfigDataTreePath = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, FLOW_CAPABLE_NODE_WC_PATH);
88         nodeOperationalDataTreePath = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, NODE_WC_PATH);
89
90         final ExecutorService executorService= Executors.newCachedThreadPool(new ThreadFactoryBuilder()
91                 .setNameFormat(SyncReactorFutureDecorator.FRM_RPC_CLIENT_PREFIX + "%d")
92                 .setDaemon(false)
93                 .setUncaughtExceptionHandler((thread, e) -> LOG.error("Uncaught exception {}", thread, e))
94                 .build());
95         syncThreadPool = MoreExecutors.listeningDecorator(executorService);
96         broker.registerProvider(this);
97     }
98
99     @Override
100     public void onSessionInitiated(final ProviderContext providerContext) {
101         final TableForwarder tableForwarder = new TableForwarder(salTableService);
102
103         final SyncPlanPushStrategy syncPlanPushStrategy = new SyncPlanPushStrategyFlatBatchImpl()
104                 .setFlatBatchService(flatBatchService)
105                 .setTableForwarder(tableForwarder);
106
107         final ReconciliationRegistry reconciliationRegistry = new ReconciliationRegistry();
108         final DeviceMastershipManager deviceMastershipManager =
109                 new DeviceMastershipManager(clusterSingletonService, reconciliationRegistry);
110
111         final SyncReactor syncReactorImpl = new SyncReactorImpl(syncPlanPushStrategy);
112         final SyncReactor syncReactorRetry = new SyncReactorRetryDecorator(syncReactorImpl, reconciliationRegistry);
113         final SyncReactor syncReactorGuard = new SyncReactorGuardDecorator(syncReactorRetry,
114                 new SemaphoreKeeperGuavaImpl<>(1, true));
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() throws InterruptedException {
140         if (dataTreeConfigChangeListener != null) {
141             dataTreeConfigChangeListener.close();
142             dataTreeConfigChangeListener = null;
143         }
144
145         if (dataTreeOperationalChangeListener != null) {
146             dataTreeOperationalChangeListener.close();
147             dataTreeOperationalChangeListener = null;
148         }
149
150         syncThreadPool.shutdown();
151     }
152
153 }