Fix checkstyle violations
[l2switch.git] / arphandler / src / main / java / org / opendaylight / l2switch / arphandler / core / ArpHandlerProvider.java
1 /*
2  * Copyright (c) 2016 Inocybe 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.l2switch.arphandler.core;
10
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
13 import org.opendaylight.l2switch.arphandler.flow.InitialFlowWriter;
14 import org.opendaylight.l2switch.arphandler.inventory.InventoryReader;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.arp.handler.config.rev140528.ArpHandlerConfig;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
18 import org.opendaylight.yangtools.concepts.Registration;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public class ArpHandlerProvider {
23     private static final Logger LOG = LoggerFactory.getLogger(ArpHandlerProvider.class);
24     private Registration listenerRegistration;
25     private Registration floodTopoListenerReg;
26     private Registration floodInvListenerReg;
27     private Registration topoNodeListenerReg;
28
29     private final NotificationProviderService notificationService;
30     private final DataBroker dataBroker;
31     private final SalFlowService salFlowService;
32     private final PacketProcessingService packetProcessingService;
33     private final ArpHandlerConfig arpHandlerConfig;
34
35     public ArpHandlerProvider(final DataBroker dataBroker,
36             final NotificationProviderService notificationProviderService,
37             final SalFlowService salFlowService,
38             final PacketProcessingService packetProcessingService,
39             final ArpHandlerConfig config) {
40         this.notificationService = notificationProviderService;
41         this.dataBroker = dataBroker;
42         this.salFlowService = salFlowService;
43         this.packetProcessingService = packetProcessingService;
44         this.arpHandlerConfig = config;
45     }
46
47     public void init() {
48         if (arpHandlerConfig.isIsProactiveFloodMode()) {
49             //Setup proactive flow writer, which writes flood flows
50             LOG.info("ArpHandler is in Proactive Flood Mode");
51             ProactiveFloodFlowWriter floodFlowWriter = new ProactiveFloodFlowWriter(dataBroker, salFlowService);
52             floodFlowWriter.setFlowTableId(arpHandlerConfig.getFloodFlowTableId());
53             floodFlowWriter.setFlowPriority(arpHandlerConfig.getFloodFlowPriority());
54             floodFlowWriter.setFlowIdleTimeout(arpHandlerConfig.getFloodFlowIdleTimeout());
55             floodFlowWriter.setFlowHardTimeout(arpHandlerConfig.getFloodFlowHardTimeout());
56             floodFlowWriter.setFlowInstallationDelay(arpHandlerConfig.getFloodFlowInstallationDelay());
57             floodTopoListenerReg = floodFlowWriter.registerAsDataChangeListener();
58             floodInvListenerReg = notificationService.registerNotificationListener(floodFlowWriter);
59         } else {
60             //Write initial flows to send arp to controller
61             LOG.info("ArpHandler is in Reactive Mode");
62             InitialFlowWriter initialFlowWriter = new InitialFlowWriter(salFlowService);
63             initialFlowWriter.setFlowTableId(arpHandlerConfig.getArpFlowTableId());
64             initialFlowWriter.setFlowPriority(arpHandlerConfig.getArpFlowPriority());
65             initialFlowWriter.setFlowIdleTimeout(arpHandlerConfig.getArpFlowIdleTimeout());
66             initialFlowWriter.setFlowHardTimeout(arpHandlerConfig.getArpFlowHardTimeout());
67             initialFlowWriter.setIsHybridMode(arpHandlerConfig.isIsHybridMode());
68             topoNodeListenerReg = initialFlowWriter.registerAsDataChangeListener(dataBroker);
69
70             // Setup InventoryReader
71             InventoryReader inventoryReader = new InventoryReader(dataBroker);
72
73             // Setup PacketDispatcher
74             PacketDispatcher packetDispatcher = new PacketDispatcher();
75             packetDispatcher.setInventoryReader(inventoryReader);
76             packetDispatcher.setPacketProcessingService(packetProcessingService);
77
78             // Setup ArpPacketHandler
79             ArpPacketHandler arpPacketHandler = new ArpPacketHandler(packetDispatcher);
80
81             // Register ArpPacketHandler
82             this.listenerRegistration = notificationService.registerNotificationListener(arpPacketHandler);
83         }
84         LOG.info("ArpHandler initialized.");
85     }
86
87     public void close() throws Exception {
88         if (listenerRegistration != null) {
89             listenerRegistration.close();
90         }
91         if (floodTopoListenerReg != null) {
92             floodTopoListenerReg.close();
93         }
94         if (floodInvListenerReg != null) {
95             floodInvListenerReg.close();
96         }
97         if (topoNodeListenerReg != null) {
98             topoNodeListenerReg.close();
99         }
100         LOG.info("ArpHandler (instance {}) torn down.", this);
101     }
102 }