resolved checkstyle warnings
[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 \r
11 import java.util.List;\r
12 \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
33 \r
34 import com.google.common.base.Optional;\r
35 import com.google.common.collect.FluentIterable;\r
36 \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
41 \r
42     /**\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
45      * \r
46      * @param db - dataBroker service reference\r
47      */\r
48     public VpnInterfaceManager(final DataBroker db) {\r
49         super(VpnInterface.class);\r
50         broker = db;\r
51         registerListener(db);\r
52     }\r
53 \r
54     @Override\r
55     public void close() throws Exception {\r
56         if (listenerRegistration != null) {\r
57             try {\r
58                 listenerRegistration.close();\r
59             } catch (final Exception e) {\r
60                 LOG.error("Error when cleaning up DataChangeListener.", e);\r
61             }\r
62             listenerRegistration = null;\r
63         }\r
64         LOG.info("VPN Interface Manager Closed");\r
65     }\r
66 \r
67     private void registerListener(final DataBroker db) {\r
68         try {\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
74         }\r
75     }\r
76 \r
77     @Override\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
82     }\r
83 \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
96         }\r
97     }\r
98 \r
99     private void updateNextHops(final InstanceIdentifier<VpnInterface> identifier) {\r
100         //Read NextHops\r
101         InstanceIdentifier<VpnInterface1> path = identifier.augmentation(VpnInterface1.class);\r
102         Optional<VpnInterface1> nextHopList = read(LogicalDatastoreType.CONFIGURATION, path);\r
103 \r
104         if (nextHopList.isPresent()) {\r
105             List<L3NextHops> nextHops = nextHopList.get().getL3NextHops();\r
106 \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
111 \r
112                     //TODO: Update BGP\r
113                     updatePrefixToBGP(nextHop);\r
114                 }\r
115             }\r
116         }\r
117     }\r
118 \r
119     private void bindServiceOnInterface(Interface intf) {\r
120         //TODO: Create Ingress flow on the interface to bind the VPN service\r
121     }\r
122 \r
123     private void updatePrefixToBGP(L3NextHops nextHop) {\r
124         //TODO: Update the Prefix to BGP\r
125     }\r
126 \r
127     private <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,\r
128             InstanceIdentifier<T> path) {\r
129 \r
130         ReadOnlyTransaction tx = broker.newReadOnlyTransaction();\r
131 \r
132         Optional<T> result = Optional.absent();\r
133         try {\r
134             result = tx.read(datastoreType, path).get();\r
135         } catch (Exception e) {\r
136             throw new RuntimeException(e);\r
137         }\r
138 \r
139         return result;\r
140     }\r
141 \r
142     private InstanceIdentifier<VpnInterface> getWildCardPath() {\r
143         return InstanceIdentifier.create(VpnInterfaces.class).child(VpnInterface.class);\r
144     }\r
145 \r
146     @Override\r
147     protected void remove( InstanceIdentifier<VpnInterface> identifier, VpnInterface del) {\r
148         // TODO Auto-generated method stub\r
149 \r
150     }\r
151 \r
152     @Override\r
153     protected void update(InstanceIdentifier<VpnInterface> identifier, \r
154                                    VpnInterface original, VpnInterface update) {\r
155         // TODO Auto-generated method stub\r
156 \r
157     }\r
158 \r
159 }\r