added modules to features and nexthopmgr updates.
[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 java.util.ArrayList;
12
13 import com.google.common.base.Optional;
14
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
17 import org.opendaylight.yangtools.concepts.ListenerRegistration;
18 import org.opendaylight.yangtools.yang.binding.DataObject;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
21 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.Adjacency;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.Adjacencies;
26 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInterfaces;
27 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
28 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterfaceKey;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
32 import org.opendaylight.vpnservice.nexthopmgr.AbstractDataChangeListener;
33
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37
38 public class VpnInterfaceChangeListener extends AbstractDataChangeListener<VpnInterface> implements AutoCloseable {
39     private static final Logger LOG = LoggerFactory.getLogger(VpnInterfaceChangeListener.class);
40
41     private ListenerRegistration<DataChangeListener> listenerRegistration;
42     private final DataBroker broker;
43     private NexthopManager nexthopManager;
44
45     public VpnInterfaceChangeListener(final DataBroker db, NexthopManager nhm) {
46         super(VpnInterface.class);
47         broker = db;
48         nexthopManager = nhm;
49         registerListener(db);
50     }
51
52     @Override
53     public void close() throws Exception {
54         if (listenerRegistration != null) {
55             try {
56                 listenerRegistration.close();
57             } catch (final Exception e) {
58                 LOG.error("Error when cleaning up DataChangeListener.", e);
59             }
60             listenerRegistration = null;
61         }
62         LOG.info("VPN Interface Manager Closed");
63     }
64
65
66     private void registerListener(final DataBroker db) {
67         try {
68             listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
69                     getWildCardPath(), VpnInterfaceChangeListener.this, DataChangeScope.SUBTREE);
70         } catch (final Exception e) {
71             LOG.error("Nexthop Manager DataChange listener registration fail!", e);
72             throw new IllegalStateException("Nexthop Manager registration Listener failed.", e);
73         }
74     }
75
76     @Override
77     protected void add(InstanceIdentifier<VpnInterface> identifier,
78             VpnInterface vpnIf) {
79         LOG.info("key: " + identifier + ", value=" + vpnIf );
80
81         String vpnName = vpnIf.getVpnInstanceName();
82         final VpnInterfaceKey key = identifier.firstKeyOf(VpnInterface.class, VpnInterfaceKey.class);
83         String interfaceName = key.getName();
84         InstanceIdentifierBuilder<Interface> idBuilder =
85                 InstanceIdentifier.builder(Interfaces.class).child(Interface.class, new InterfaceKey(interfaceName));
86         InstanceIdentifier<Interface> id = idBuilder.build();
87         Optional<Interface> port = read(LogicalDatastoreType.CONFIGURATION, id);
88         if (port.isPresent()) {
89             //Interface interf = port.get();
90
91             //Read NextHops
92             InstanceIdentifier<Adjacencies> path = identifier.augmentation(Adjacencies.class);
93             Optional<Adjacencies> adjacencies = read(LogicalDatastoreType.CONFIGURATION, path);
94
95             if (adjacencies.isPresent()) {
96                 List<Adjacency> nextHops = adjacencies.get().getAdjacency();
97                 List<Adjacency> value = new ArrayList<>();
98
99                 if (!nextHops.isEmpty()) {
100                     LOG.info("NextHops are " + nextHops);
101                     for (Adjacency nextHop : nextHops) {
102                         nexthopManager.createLocalNextHop(interfaceName, vpnName, nextHop.getIpAddress());
103                     }
104                 }
105             }
106         }
107
108     }
109
110     private <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,
111             InstanceIdentifier<T> path) {
112
113         ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
114
115         Optional<T> result = Optional.absent();
116         try {
117             result = tx.read(datastoreType, path).get();
118         } catch (Exception e) {
119             throw new RuntimeException(e);
120         }
121
122         return result;
123     }
124
125     private InstanceIdentifier<VpnInterface> getWildCardPath() {
126         return InstanceIdentifier.create(VpnInterfaces.class).child(VpnInterface.class);
127     }
128
129     @Override
130     protected void remove(InstanceIdentifier<VpnInterface> identifier,
131             VpnInterface del) {
132             // TODO Auto-generated method stub
133     }
134
135     @Override
136     protected void update(InstanceIdentifier<VpnInterface> identifier,
137                 VpnInterface original, VpnInterface update) {
138             // TODO Auto-generated method stub
139
140     }
141
142 }