Adjust to odlparent 3 Checkstyle settings
[genius.git] / fcapsapplication / fcapsapplication-impl / src / main / java / org / opendaylight / genius / fcapsapp / FcapsProvider.java
1 /*
2  * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. 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.genius.fcapsapp;
9
10 import com.google.common.base.Preconditions;
11 import javax.annotation.PostConstruct;
12 import javax.annotation.PreDestroy;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
17 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.genius.fcapsapp.performancecounter.PacketInCounterHandler;
20 import org.opendaylight.genius.fcapsapp.portinfo.PortNameMapping;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 @Singleton
29 public class FcapsProvider implements AutoCloseable {
30     private final DataBroker dataBroker;
31     private final NotificationService notificationService;
32     private final PacketInCounterHandler packetInCounterHandler;
33     private final NodeEventListener<FlowCapableNode> nodeEventListener;
34
35     public static final Logger LOG = LoggerFactory.getLogger(FcapsProvider.class);
36
37     /**
38      * Constructor sets the services.
39      *
40      * @param dataBroker
41      *            instance of databroker
42      * @param notificationService
43      *            instance of notificationservice
44      * @param packetInCounterHandler
45      *            instance of PacketInCounterHandler
46      * @param nodeEventListener
47      *            instance of NodeEventListener
48      */
49     @Inject
50     public FcapsProvider(final DataBroker dataBroker, final NotificationService notificationService,
51             final PacketInCounterHandler packetInCounterHandler,
52             final NodeEventListener nodeEventListener) {
53         this.dataBroker = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
54         LOG.info("FcapsProvider dataBroker is set");
55
56         this.notificationService = Preconditions.checkNotNull(notificationService,
57                 "notificationService can not be null!");
58         LOG.info("FcapsProvider notificationProviderService is set");
59
60         this.packetInCounterHandler = packetInCounterHandler;
61         this.nodeEventListener = nodeEventListener;
62     }
63
64     @PostConstruct
65     public void start() throws Exception {
66         PortNameMapping.registerPortMappingBean();
67         registerListener();
68         notificationService.registerNotificationListener(packetInCounterHandler);
69         LOG.info("FcapsProvider started");
70     }
71
72     @PreDestroy
73     @Override
74     public void close() throws Exception {
75         LOG.info("FcapsProvider closed");
76     }
77
78     private void registerListener() {
79         final DataTreeIdentifier<FlowCapableNode> treeId = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL,
80                 getWildCardPath());
81         dataBroker.registerDataTreeChangeListener(treeId, nodeEventListener);
82     }
83
84     private InstanceIdentifier<FlowCapableNode> getWildCardPath() {
85         return InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class);
86     }
87 }