Merge "Initial draft of vpn service"
[vpnservice.git] / vpnmanager-impl / src / main / java / org / opendaylight / vpnservice / VpnInterfaceManager.java
1 /*\r
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.\r
3  *\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
7  */\r
8 package org.opendaylight.vpnservice;\r
9 \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
15 \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
37 \r
38 import com.google.common.base.Optional;\r
39 import com.google.common.collect.FluentIterable;\r
40 \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
45     \r
46     public VpnInterfaceManager(final DataBroker db) {\r
47         super(VpnInterface.class);\r
48         broker = db;\r
49         registerListener(db);\r
50     }\r
51 \r
52     @Override\r
53     public void close() throws Exception {\r
54         if (listenerRegistration != null) {\r
55             try {\r
56                 listenerRegistration.close();\r
57             } catch (final Exception e) {\r
58                 LOG.error("Error when cleaning up DataChangeListener.", e);\r
59             }\r
60             listenerRegistration = null;\r
61         }\r
62         LOG.info("VPN Interface Manager Closed");\r
63     }\r
64     \r
65     private void registerListener(final DataBroker db) {\r
66         try {\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
72         }\r
73     }\r
74 \r
75     @Override\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
80     }\r
81 \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
94         }\r
95     }\r
96 \r
97     private void updateNextHops(final InstanceIdentifier<VpnInterface> identifier) {\r
98         //Read NextHops\r
99         InstanceIdentifier<VpnInterface1> path = identifier.augmentation(VpnInterface1.class);\r
100         Optional<VpnInterface1> nextHopList = read(LogicalDatastoreType.CONFIGURATION, path);\r
101         \r
102         if(nextHopList.isPresent()) {\r
103             List<L3NextHops> nextHops = nextHopList.get().getL3NextHops();\r
104             \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
109                     \r
110                     //TODO: Update BGP\r
111                     updatePrefixToBGP(nextHop);\r
112                 }\r
113             }\r
114         }\r
115     }\r
116 \r
117     private void bindServiceOnInterface(Interface intf) {\r
118         //TODO: Create Ingress flow on the interface to bind the VPN service\r
119     }\r
120 \r
121     private void updatePrefixToBGP(L3NextHops nextHop) {\r
122         //TODO: Update the Prefix to BGP\r
123     }\r
124 \r
125     private <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,\r
126             InstanceIdentifier<T> path) {\r
127 \r
128         ReadOnlyTransaction tx = broker.newReadOnlyTransaction();\r
129 \r
130         Optional<T> result = Optional.absent();\r
131         try {\r
132             result = tx.read(datastoreType, path).get();\r
133         } catch (Exception e) {\r
134             throw new RuntimeException(e);\r
135         }\r
136 \r
137         return result;\r
138     }\r
139 \r
140     private InstanceIdentifier<VpnInterface> getWildCardPath() {\r
141         return InstanceIdentifier.create(VpnInterfaces.class).child(VpnInterface.class);\r
142     }\r
143 \r
144     @Override\r
145     protected void remove( InstanceIdentifier<VpnInterface> identifier, VpnInterface del) {\r
146         // TODO Auto-generated method stub\r
147 \r
148     }\r
149 \r
150     @Override\r
151     protected void update(InstanceIdentifier<VpnInterface> identifier, \r
152                                    VpnInterface original, VpnInterface update) {\r
153         // TODO Auto-generated method stub\r
154 \r
155     }\r
156 \r
157 }\r