2 * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
8 package org.opendaylight.vpnservice;
\r
11 import java.util.List;
\r
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
\r
14 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
\r
15 import org.opendaylight.yangtools.concepts.ListenerRegistration;
\r
16 import org.opendaylight.yangtools.yang.binding.DataObject;
\r
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
\r
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
\r
19 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
\r
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
\r
21 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
\r
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
\r
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
\r
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.NextHopList;
\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.next.hop.list.L3NextHops;
\r
27 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInterfaces;
\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.VpnInterface1;
\r
29 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
\r
30 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterfaceKey;
\r
31 import org.slf4j.Logger;
\r
32 import org.slf4j.LoggerFactory;
\r
34 import com.google.common.base.Optional;
\r
35 import com.google.common.collect.FluentIterable;
\r
37 public class VpnInterfaceManager extends AbstractDataChangeListener<VpnInterface> implements AutoCloseable {
\r
38 private static final Logger LOG = LoggerFactory.getLogger(VpnInterfaceManager.class);
\r
39 private ListenerRegistration<DataChangeListener> listenerRegistration;
\r
40 private final DataBroker broker;
\r
43 * Responsible for listening to data change related to VPN Interface
\r
44 * Bind VPN Service on the interface and informs the BGP service
\r
46 * @param db - dataBroker service reference
\r
48 public VpnInterfaceManager(final DataBroker db) {
\r
49 super(VpnInterface.class);
\r
51 registerListener(db);
\r
55 public void close() throws Exception {
\r
56 if (listenerRegistration != null) {
\r
58 listenerRegistration.close();
\r
59 } catch (final Exception e) {
\r
60 LOG.error("Error when cleaning up DataChangeListener.", e);
\r
62 listenerRegistration = null;
\r
64 LOG.info("VPN Interface Manager Closed");
\r
67 private void registerListener(final DataBroker db) {
\r
69 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
\r
70 getWildCardPath(), VpnInterfaceManager.this, DataChangeScope.SUBTREE);
\r
71 } catch (final Exception e) {
\r
72 LOG.error("VPN Service DataChange listener registration fail!", e);
\r
73 throw new IllegalStateException("VPN Service registration Listener failed.", e);
\r
78 protected void add(final InstanceIdentifier<VpnInterface> identifier,
\r
79 final VpnInterface vpnInterface) {
\r
80 LOG.info("key: " + identifier + ", value=" + vpnInterface );
\r
81 addInterface(identifier, vpnInterface);
\r
84 private void addInterface(final InstanceIdentifier<VpnInterface> identifier,
\r
85 final VpnInterface vpnInterface) {
\r
86 final VpnInterfaceKey key = identifier.firstKeyOf(VpnInterface.class, VpnInterfaceKey.class);
\r
87 String interfaceName = key.getName();
\r
88 InstanceIdentifierBuilder<Interface> idBuilder =
\r
89 InstanceIdentifier.builder(Interfaces.class).child(Interface.class, new InterfaceKey(interfaceName));
\r
90 InstanceIdentifier<Interface> id = idBuilder.build();
\r
91 Optional<Interface> port = read(LogicalDatastoreType.CONFIGURATION, id);
\r
92 if (port.isPresent()) {
\r
93 Interface interf = port.get();
\r
94 bindServiceOnInterface(interf);
\r
95 updateNextHops(identifier);
\r
99 private void updateNextHops(final InstanceIdentifier<VpnInterface> identifier) {
\r
101 InstanceIdentifier<VpnInterface1> path = identifier.augmentation(VpnInterface1.class);
\r
102 Optional<VpnInterface1> nextHopList = read(LogicalDatastoreType.CONFIGURATION, path);
\r
104 if (nextHopList.isPresent()) {
\r
105 List<L3NextHops> nextHops = nextHopList.get().getL3NextHops();
\r
107 if (!nextHops.isEmpty()) {
\r
108 LOG.info("NextHops are " + nextHops);
\r
109 for (L3NextHops nextHop : nextHops) {
\r
110 //TODO: Generate label for the prefix and store it in the next hop model
\r
113 updatePrefixToBGP(nextHop);
\r
119 private void bindServiceOnInterface(Interface intf) {
\r
120 //TODO: Create Ingress flow on the interface to bind the VPN service
\r
123 private void updatePrefixToBGP(L3NextHops nextHop) {
\r
124 //TODO: Update the Prefix to BGP
\r
127 private <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,
\r
128 InstanceIdentifier<T> path) {
\r
130 ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
\r
132 Optional<T> result = Optional.absent();
\r
134 result = tx.read(datastoreType, path).get();
\r
135 } catch (Exception e) {
\r
136 throw new RuntimeException(e);
\r
142 private InstanceIdentifier<VpnInterface> getWildCardPath() {
\r
143 return InstanceIdentifier.create(VpnInterfaces.class).child(VpnInterface.class);
\r
147 protected void remove( InstanceIdentifier<VpnInterface> identifier, VpnInterface del) {
\r
148 // TODO Auto-generated method stub
\r
153 protected void update(InstanceIdentifier<VpnInterface> identifier,
\r
154 VpnInterface original, VpnInterface update) {
\r
155 // TODO Auto-generated method stub
\r