Changes to NextHopManager
[vpnservice.git] / nexthopmgr / nexthopmgr-impl / src / main / java / org / opendaylight / vpnservice / nexthopmgr / VpnInterfaceChangeListener.java
1 /*
2  * Copyright (c) 2015 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.vpnservice.nexthopmgr;
9
10 import java.util.List;
11 import com.google.common.base.Optional;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
14 import org.opendaylight.yangtools.concepts.ListenerRegistration;
15 import org.opendaylight.yangtools.yang.binding.DataObject;
16 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
17 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.Adjacency;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.Adjacencies;
22 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInterfaces;
23 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
24 import org.opendaylight.vpnservice.nexthopmgr.AbstractDataChangeListener;
25
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29
30 public class VpnInterfaceChangeListener extends AbstractDataChangeListener<Adjacencies> implements AutoCloseable {
31     private static final Logger LOG = LoggerFactory.getLogger(VpnInterfaceChangeListener.class);
32
33     private ListenerRegistration<DataChangeListener> listenerRegistration;
34     private final DataBroker broker;
35     private NexthopManager nexthopManager;
36
37     public VpnInterfaceChangeListener(final DataBroker db, NexthopManager nhm) {
38         super(Adjacencies.class);
39         broker = db;
40         nexthopManager = nhm;
41         registerListener(db);
42     }
43
44     @Override
45     public void close() throws Exception {
46         if (listenerRegistration != null) {
47             try {
48                 listenerRegistration.close();
49             } catch (final Exception e) {
50                 LOG.error("Error when cleaning up DataChangeListener.", e);
51             }
52             listenerRegistration = null;
53         }
54         LOG.info("VPN Interface Manager Closed");
55     }
56
57
58     private void registerListener(final DataBroker db) {
59         try {
60             listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
61                     getWildCardPath(), VpnInterfaceChangeListener.this, DataChangeScope.SUBTREE);
62         } catch (final Exception e) {
63             LOG.error("Nexthop Manager DataChange listener registration fail!", e);
64             throw new IllegalStateException("Nexthop Manager registration Listener failed.", e);
65         }
66     }
67
68     @Override
69     protected void add(InstanceIdentifier<Adjacencies> identifier,
70             Adjacencies adjs) {
71
72         InstanceIdentifier<VpnInterface> vpnIfId = identifier.firstIdentifierOf(VpnInterface.class);
73         Optional<VpnInterface> vpnIf = read(LogicalDatastoreType.CONFIGURATION, vpnIfId);
74         VpnInterface vpnIfData = vpnIf.get();
75
76         List<Adjacency> adjList = adjs.getAdjacency();
77         for (Adjacency adjacency : adjList) {
78             nexthopManager.createLocalNextHop(
79                     vpnIfData.getName(),
80                     vpnIfData.getVpnInstanceName(),
81                     adjacency.getIpAddress(),
82                     adjacency.getMacAddress());
83         }
84     }
85
86
87     @Override
88     protected void remove(InstanceIdentifier<Adjacencies> identifier,
89             Adjacencies adjs) {
90         // nexthop group will be removed after fib entry deletion
91     }
92
93     @Override
94     protected void update(InstanceIdentifier<Adjacencies> identifier,
95             Adjacencies original, Adjacencies update) {
96         // TODO Auto-generated method stub
97     }
98
99
100     private <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,
101             InstanceIdentifier<T> path) {
102
103         ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
104
105         Optional<T> result = Optional.absent();
106         try {
107             result = tx.read(datastoreType, path).get();
108         } catch (Exception e) {
109             throw new RuntimeException(e);
110         }
111
112         return result;
113     }
114
115     private InstanceIdentifier<Adjacencies> getWildCardPath() {
116         return InstanceIdentifier.create(VpnInterfaces.class).child(VpnInterface.class).augmentation(Adjacencies.class);
117     }
118
119
120 }