2 * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.vpnservice.nexthopmgr;
11 import java.math.BigInteger;
13 import com.google.common.base.Optional;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.vpnservice.interfacemgr.interfaces.IInterfaceManager;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
26 import org.opendaylight.yangtools.concepts.ListenerRegistration;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 public class OdlInterfaceChangeListener extends AbstractDataChangeListener<Interface> implements AutoCloseable {
33 private static final Logger LOG = LoggerFactory.getLogger(OdlInterfaceChangeListener.class);
35 private ListenerRegistration<DataChangeListener> listenerRegistration;
36 private final DataBroker broker;
37 private NexthopManager nexthopManager;
38 private IInterfaceManager interfaceManager;
41 public OdlInterfaceChangeListener(final DataBroker db, NexthopManager nhm, IInterfaceManager ifManager) {
42 super(Interface.class);
45 interfaceManager = ifManager;
50 public void close() throws Exception {
51 if (listenerRegistration != null) {
53 listenerRegistration.close();
54 } catch (final Exception e) {
55 LOG.error("Error when cleaning up DataChangeListener.", e);
57 listenerRegistration = null;
59 LOG.info("odlInterface listener Closed");
63 private void registerListener(final DataBroker db) {
65 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
66 getWildCardPath(), OdlInterfaceChangeListener.this, DataChangeScope.SUBTREE);
67 } catch (final Exception e) {
68 LOG.error("Nexthop Manager Interfaces DataChange listener registration fail!", e);
69 throw new IllegalStateException("Nexthop Manager registration Listener failed.", e);
74 protected void add(InstanceIdentifier<Interface> identifier, Interface interfaceInfo) {
75 LOG.trace("Adding Interface : key: " + identifier + ", value=" + interfaceInfo );
76 // READ interface config information
77 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface intrf =
78 getInterfaceFromConfigDS(nexthopManager, new InterfaceKey(interfaceInfo.getName()));
80 if(intrf != null && intrf.getType().equals(Tunnel.class)){
81 IfTunnel intfData = intrf.getAugmentation(IfTunnel.class);
82 IpAddress gatewayIp = intfData.getTunnelGateway();
83 IpAddress remoteIp = (gatewayIp == null) ? intfData.getTunnelDestination() : gatewayIp;
84 nexthopManager.createRemoteNextHop(intrf.getName(), remoteIp.getIpv4Address().getValue());
88 private InstanceIdentifier<Interface> getWildCardPath() {
89 return InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
93 protected void remove(InstanceIdentifier<Interface> identifier,
94 Interface interfaceInfo) {
95 LOG.trace("Removing interface : key: " + identifier + ", value=" + interfaceInfo );
96 // READ interface config information
97 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface intrf =
98 getInterfaceFromConfigDS(nexthopManager, new InterfaceKey(interfaceInfo.getName()));
100 if (intrf != null && intrf.getType().equals(Tunnel.class)) {
101 BigInteger dpnId = interfaceManager.getDpnForInterface(intrf);
102 IfTunnel intfData = intrf.getAugmentation(IfTunnel.class);
103 IpAddress gatewayIp = intfData.getTunnelGateway();
104 IpAddress remoteIp = (gatewayIp == null) ? intfData.getTunnelDestination() : gatewayIp;
105 nexthopManager.removeRemoteNextHop(dpnId, intrf.getName(), remoteIp.getIpv4Address().getValue());
109 public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface getInterfaceFromConfigDS(NexthopManager nexthopManager, InterfaceKey interfaceKey) {
110 InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface> interfaceId =
111 getInterfaceIdentifier(interfaceKey);
112 Optional<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface> interfaceOptional =
113 nexthopManager.read(LogicalDatastoreType.CONFIGURATION, interfaceId);
114 if (!interfaceOptional.isPresent()) {
118 return interfaceOptional.get();
121 public static InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface> getInterfaceIdentifier(InterfaceKey interfaceKey) {
122 InstanceIdentifier.InstanceIdentifierBuilder<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface> interfaceInstanceIdentifierBuilder =
123 InstanceIdentifier.builder(Interfaces.class).child(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface.class, interfaceKey);
124 return interfaceInstanceIdentifierBuilder.build();
128 protected void update(InstanceIdentifier<Interface> identifier,
129 Interface original, Interface update) {
130 // TODO Auto-generated method stub