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
10 import java.util.Collections;
\r
11 import java.util.List;
\r
12 import java.util.Map;
\r
13 import java.util.Set;
\r
14 import java.util.concurrent.Future;
\r
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
\r
17 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
\r
18 import org.opendaylight.yangtools.concepts.ListenerRegistration;
\r
19 import org.opendaylight.yangtools.yang.binding.DataObject;
\r
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
\r
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
\r
22 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
\r
23 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
\r
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
\r
25 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
\r
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
\r
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
\r
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.NextHopList;
\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.next.hop.list.*;
\r
31 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInterfaces;
\r
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.VpnInterface1;
\r
33 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
\r
34 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterfaceKey;
\r
35 import org.slf4j.Logger;
\r
36 import org.slf4j.LoggerFactory;
\r
38 import com.google.common.base.Optional;
\r
39 import com.google.common.collect.FluentIterable;
\r
41 public class VpnInterfaceManager extends AbstractDataChangeListener<VpnInterface> implements AutoCloseable{
\r
42 private static final Logger LOG = LoggerFactory.getLogger(VpnInterfaceManager.class);
\r
43 private ListenerRegistration<DataChangeListener> listenerRegistration;
\r
44 private final DataBroker broker;
\r
46 public VpnInterfaceManager(final DataBroker db) {
\r
47 super(VpnInterface.class);
\r
49 registerListener(db);
\r
53 public void close() throws Exception {
\r
54 if (listenerRegistration != null) {
\r
56 listenerRegistration.close();
\r
57 } catch (final Exception e) {
\r
58 LOG.error("Error when cleaning up DataChangeListener.", e);
\r
60 listenerRegistration = null;
\r
62 LOG.info("VPN Interface Manager Closed");
\r
65 private void registerListener(final DataBroker db) {
\r
67 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
\r
68 getWildCardPath(), VpnInterfaceManager.this, DataChangeScope.SUBTREE);
\r
69 } catch (final Exception e) {
\r
70 LOG.error("VPN Service DataChange listener registration fail!", e);
\r
71 throw new IllegalStateException("VPN Service registration Listener failed.", e);
\r
76 protected void add(final InstanceIdentifier<VpnInterface> identifier,
\r
77 final VpnInterface vpnInterface) {
\r
78 LOG.info("key: " + identifier + ", value=" + vpnInterface );
\r
79 addInterface(identifier, vpnInterface);
\r
82 private void addInterface(final InstanceIdentifier<VpnInterface> identifier,
\r
83 final VpnInterface vpnInterface) {
\r
84 final VpnInterfaceKey key = identifier.firstKeyOf(VpnInterface.class, VpnInterfaceKey.class);
\r
85 String interfaceName = key.getName();
\r
86 InstanceIdentifierBuilder<Interface> idBuilder =
\r
87 InstanceIdentifier.builder(Interfaces.class).child(Interface.class, new InterfaceKey(interfaceName));
\r
88 InstanceIdentifier<Interface> id = idBuilder.build();
\r
89 Optional<Interface> port = read(LogicalDatastoreType.CONFIGURATION, id);
\r
90 if(port.isPresent()) {
\r
91 Interface interf = port.get();
\r
92 bindServiceOnInterface(interf);
\r
93 updateNextHops(identifier);
\r
97 private void updateNextHops(final InstanceIdentifier<VpnInterface> identifier) {
\r
99 InstanceIdentifier<VpnInterface1> path = identifier.augmentation(VpnInterface1.class);
\r
100 Optional<VpnInterface1> nextHopList = read(LogicalDatastoreType.CONFIGURATION, path);
\r
102 if(nextHopList.isPresent()) {
\r
103 List<L3NextHops> nextHops = nextHopList.get().getL3NextHops();
\r
105 if(!nextHops.isEmpty()) {
\r
106 LOG.info("NextHops are "+ nextHops);
\r
107 for(L3NextHops nextHop : nextHops) {
\r
108 //TODO: Generate label for the prefix and store it in the next hop model
\r
111 updatePrefixToBGP(nextHop);
\r
117 private void bindServiceOnInterface(Interface intf) {
\r
118 //TODO: Create Ingress flow on the interface to bind the VPN service
\r
121 private void updatePrefixToBGP(L3NextHops nextHop) {
\r
122 //TODO: Update the Prefix to BGP
\r
125 private <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,
\r
126 InstanceIdentifier<T> path) {
\r
128 ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
\r
130 Optional<T> result = Optional.absent();
\r
132 result = tx.read(datastoreType, path).get();
\r
133 } catch (Exception e) {
\r
134 throw new RuntimeException(e);
\r
140 private InstanceIdentifier<VpnInterface> getWildCardPath() {
\r
141 return InstanceIdentifier.create(VpnInterfaces.class).child(VpnInterface.class);
\r
145 protected void remove( InstanceIdentifier<VpnInterface> identifier, VpnInterface del) {
\r
146 // TODO Auto-generated method stub
\r
151 protected void update(InstanceIdentifier<VpnInterface> identifier,
\r
152 VpnInterface original, VpnInterface update) {
\r
153 // TODO Auto-generated method stub
\r