1. nexthopMgr intg with MdSalUtil & IdMgr
[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         InstanceIdentifier<VpnInterface> vpnIfId = identifier.firstIdentifierOf(VpnInterface.class);
91         Optional<VpnInterface> vpnIf = read(LogicalDatastoreType.CONFIGURATION, vpnIfId);
92         VpnInterface vpnIfData = vpnIf.get();
93
94         List<Adjacency> adjList = adjs.getAdjacency();
95         for (Adjacency adjacency : adjList) {
96             nexthopManager.removeLocalNextHop(vpnIfData.getVpnInstanceName(), adjacency.getIpAddress());
97         }
98
99     }
100
101     @Override
102     protected void update(InstanceIdentifier<Adjacencies> identifier,
103             Adjacencies original, Adjacencies update) {
104         // TODO Auto-generated method stub
105     }
106
107
108     private <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,
109             InstanceIdentifier<T> path) {
110
111         ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
112
113         Optional<T> result = Optional.absent();
114         try {
115             result = tx.read(datastoreType, path).get();
116         } catch (Exception e) {
117             throw new RuntimeException(e);
118         }
119
120         return result;
121     }
122
123     private InstanceIdentifier<Adjacencies> getWildCardPath() {
124         return InstanceIdentifier.create(VpnInterfaces.class).child(VpnInterface.class).augmentation(Adjacencies.class);
125     }
126
127
128 }