Bump upstreams for Silicon
[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 static java.util.Objects.requireNonNull;
11
12 import javax.annotation.PostConstruct;
13 import javax.annotation.PreDestroy;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.apache.aries.blueprint.annotation.service.Reference;
17 import org.opendaylight.genius.fcapsapp.performancecounter.PacketInCounterHandler;
18 import org.opendaylight.genius.fcapsapp.portinfo.PortNameMapping;
19 import org.opendaylight.mdsal.binding.api.DataBroker;
20 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
21 import org.opendaylight.mdsal.binding.api.NotificationService;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 @Singleton
31 public class FcapsProvider implements AutoCloseable {
32     private final DataBroker dataBroker;
33     private final NotificationService notificationService;
34     private final PacketInCounterHandler packetInCounterHandler;
35     private final NodeEventListener<FlowCapableNode> nodeEventListener;
36
37     private static final Logger LOG = LoggerFactory.getLogger(FcapsProvider.class);
38
39     /**
40      * Constructor sets the services.
41      *
42      * @param dataBroker
43      *            instance of databroker
44      * @param notificationService
45      *            instance of notificationservice
46      * @param packetInCounterHandler
47      *            instance of PacketInCounterHandler
48      * @param nodeEventListener
49      *            instance of NodeEventListener
50      */
51     @Inject
52     public FcapsProvider(@Reference final DataBroker dataBroker,
53                          @Reference final NotificationService notificationService,
54                          final PacketInCounterHandler packetInCounterHandler,
55                          final NodeEventListener nodeEventListener) {
56         this.dataBroker = requireNonNull(dataBroker, "DataBroker can not be null!");
57         LOG.info("FcapsProvider dataBroker is set");
58
59         this.notificationService = requireNonNull(notificationService, "notificationService can not be null!");
60         LOG.info("FcapsProvider notificationProviderService is set");
61
62         this.packetInCounterHandler = packetInCounterHandler;
63         this.nodeEventListener = nodeEventListener;
64     }
65
66     @PostConstruct
67     public void start() {
68         PortNameMapping.registerPortMappingBean();
69         registerListener();
70         notificationService.registerNotificationListener(packetInCounterHandler);
71         LOG.info("FcapsProvider started");
72     }
73
74     @PreDestroy
75     @Override
76     public void close() {
77         LOG.info("FcapsProvider closed");
78     }
79
80     private void registerListener() {
81         final DataTreeIdentifier<FlowCapableNode> treeId = DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
82                 getWildCardPath());
83         dataBroker.registerDataTreeChangeListener(treeId, nodeEventListener);
84     }
85
86     private InstanceIdentifier<FlowCapableNode> getWildCardPath() {
87         return InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class);
88     }
89 }