Fix checkstyle violations
[l2switch.git] / l2switch-main / src / main / java / org / opendaylight / l2switch / L2SwitchMainProvider.java
1 /*
2  * Copyright (c) 2014 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.l2switch;
9
10 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
11 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
12 import org.opendaylight.l2switch.flow.FlowWriterServiceImpl;
13 import org.opendaylight.l2switch.flow.InitialFlowWriter;
14 import org.opendaylight.l2switch.flow.ReactiveFlowWriter;
15 import org.opendaylight.l2switch.inventory.InventoryReader;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2switch.l2switch.config.rev140528.L2switchConfig;
18 import org.opendaylight.yangtools.concepts.Registration;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public class L2SwitchMainProvider {
23     private static final Logger LOG = LoggerFactory.getLogger(L2SwitchMainProvider.class);
24     private Registration topoNodeListherReg;
25     private Registration reactFlowWriterReg;
26
27     private final DataBroker dataService;
28     private final NotificationProviderService notificationService;
29     private final SalFlowService salFlowService;
30     private final L2switchConfig mainConfig;
31
32     public L2SwitchMainProvider(final DataBroker dataBroker,
33             final NotificationProviderService notificationService,
34             final SalFlowService salFlowService, final L2switchConfig config) {
35         this.dataService = dataBroker;
36         this.notificationService = notificationService;
37         this.salFlowService = salFlowService;
38         this.mainConfig = config;
39     }
40
41     public void init() {
42         // Setup FlowWrtierService
43         FlowWriterServiceImpl flowWriterService = new FlowWriterServiceImpl(salFlowService);
44         flowWriterService.setFlowTableId(mainConfig.getReactiveFlowTableId());
45         flowWriterService.setFlowPriority(mainConfig.getReactiveFlowPriority());
46         flowWriterService.setFlowIdleTimeout(mainConfig.getReactiveFlowIdleTimeout());
47         flowWriterService.setFlowHardTimeout(mainConfig.getReactiveFlowHardTimeout());
48
49         // Setup InventoryReader
50         InventoryReader inventoryReader = new InventoryReader(dataService);
51
52         // Write initial flows
53         if (mainConfig.isIsInstallDropallFlow()) {
54             LOG.info("L2Switch will install a dropall flow on each switch");
55             InitialFlowWriter initialFlowWriter = new InitialFlowWriter(salFlowService);
56             initialFlowWriter.setFlowTableId(mainConfig.getDropallFlowTableId());
57             initialFlowWriter.setFlowPriority(mainConfig.getDropallFlowPriority());
58             initialFlowWriter.setFlowIdleTimeout(mainConfig.getDropallFlowIdleTimeout());
59             initialFlowWriter.setFlowHardTimeout(mainConfig.getDropallFlowHardTimeout());
60             topoNodeListherReg = initialFlowWriter.registerAsDataChangeListener(dataService);
61         }
62         else {
63             LOG.info("Dropall flows will not be installed");
64         }
65
66         if (mainConfig.isIsLearningOnlyMode()) {
67             LOG.info("L2Switch is in Learning Only Mode");
68         }
69         else {
70             // Setup reactive flow writer
71             LOG.info("L2Switch will react to network traffic and install flows");
72             ReactiveFlowWriter reactiveFlowWriter = new ReactiveFlowWriter(inventoryReader, flowWriterService);
73             reactFlowWriterReg = notificationService.registerNotificationListener(reactiveFlowWriter);
74         }
75
76         LOG.info("L2SwitchMain initialized.");
77     }
78
79     public void close() {
80         if (reactFlowWriterReg != null) {
81             reactFlowWriterReg.close();
82         }
83
84         if (topoNodeListherReg != null) {
85             topoNodeListherReg.close();
86         }
87         LOG.info("L2SwitchMain (instance {}) torn down.", this);
88     }
89 }