Fix build faliures due to OFPlugin checktyle fixes
[netvirt.git] / vpnservice / natservice / natservice-impl / src / main / java / org / opendaylight / netvirt / natservice / internal / RouterToVpnListener.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netvirt.natservice.internal;
9
10 import com.google.common.base.Optional;
11 import java.math.BigInteger;
12 import java.util.List;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.genius.mdsalutil.MDSALUtil;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ProviderTypes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.floating.ip.info.RouterPorts;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.floating.ip.info.router.ports.Ports;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.floating.ip.info.router.ports.ports.InternalToExternalPortMap;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.NeutronvpnListener;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.RouterAssociatedToVpn;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.RouterDisassociatedFromVpn;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 @Singleton
33 public class RouterToVpnListener implements NeutronvpnListener {
34     private static final Logger LOG = LoggerFactory.getLogger(RouterToVpnListener.class);
35     private final DataBroker dataBroker;
36     private final FloatingIPListener floatingIpListener;
37     private final OdlInterfaceRpcService interfaceManager;
38     private final ExternalRoutersListener externalRoutersListener;
39
40     @Inject
41     public RouterToVpnListener(final DataBroker dataBroker,
42                                final FloatingIPListener floatingIpListener,
43                                final OdlInterfaceRpcService interfaceManager,
44                                final ExternalRoutersListener externalRoutersListener) {
45         this.dataBroker = dataBroker;
46         this.floatingIpListener = floatingIpListener;
47         this.interfaceManager = interfaceManager;
48         this.externalRoutersListener = externalRoutersListener;
49     }
50
51     /**
52      * router association to vpn.
53      */
54     @Override
55     public void onRouterAssociatedToVpn(RouterAssociatedToVpn notification) {
56         String routerName = notification.getRouterId().getValue();
57         String vpnName = notification.getVpnId().getValue();
58         WriteTransaction writeFlowInvTx = dataBroker.newWriteOnlyTransaction();
59         //check router is associated to external network
60         String extNetwork = NatUtil.getAssociatedExternalNetwork(dataBroker, routerName);
61         if (extNetwork != null) {
62             LOG.debug("onRouterAssociatedToVpn : Router {} is associated with ext nw {}", routerName, extNetwork);
63             handleDNATConfigurationForRouterAssociation(routerName, vpnName, extNetwork);
64             Uuid extNetworkUuid = NatUtil.getNetworkIdFromRouterName(dataBroker, routerName);
65             if (extNetworkUuid == null) {
66                 LOG.error("onRouterAssociatedToVpn : Unable to retrieve external network Uuid for router {}",
67                         routerName);
68                 return;
69             }
70             ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker, routerName,
71                     extNetworkUuid);
72             if (extNwProvType == null) {
73                 LOG.error("onRouterAssociatedToVpn : External Network Provider Type missing");
74                 return;
75             }
76             long routerId = NatUtil.getVpnId(dataBroker, routerName);
77             externalRoutersListener.changeLocalVpnIdToBgpVpnId(routerName, routerId, vpnName, writeFlowInvTx,
78                     extNwProvType);
79         } else {
80             LOG.debug("onRouterAssociatedToVpn : Ignoring the Router {} association with VPN {} "
81                     + "since it is not external router", routerName);
82         }
83
84         NatUtil.waitForTransactionToComplete(writeFlowInvTx);
85     }
86
87     /**
88      * router disassociation from vpn.
89      */
90     @Override
91     public void onRouterDisassociatedFromVpn(RouterDisassociatedFromVpn notification) {
92         String routerName = notification.getRouterId().getValue();
93         String vpnName = notification.getVpnId().getValue();
94         WriteTransaction writeFlowInvTx = dataBroker.newWriteOnlyTransaction();
95         //check router is associated to external network
96         String extNetwork = NatUtil.getAssociatedExternalNetwork(dataBroker, routerName);
97         if (extNetwork != null) {
98             LOG.debug("onRouterDisassociatedFromVpn : Router {} is associated with ext nw {}", routerName, extNetwork);
99             handleDNATConfigurationForRouterDisassociation(routerName, vpnName, extNetwork);
100             Uuid extNetworkUuid = NatUtil.getNetworkIdFromRouterName(dataBroker, routerName);
101             if (extNetworkUuid == null) {
102                 LOG.error("onRouterDisassociatedFromVpn : Unable to retrieve external network Uuid for router {}",
103                         routerName);
104                 return;
105             }
106             ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker, routerName,
107                     extNetworkUuid);
108             if (extNwProvType == null) {
109                 LOG.error("onRouterDisassociatedFromVpn : External Network Provider Type missing");
110                 return;
111             }
112             long routerId = NatUtil.getVpnId(dataBroker, routerName);
113             externalRoutersListener.changeBgpVpnIdToLocalVpnId(routerName, routerId, vpnName, writeFlowInvTx,
114                     extNwProvType);
115         } else {
116             LOG.debug("onRouterDisassociatedFromVpn : Ignoring the Router {} association with VPN {} "
117                     + "since it is not external router", routerName);
118         }
119
120         NatUtil.waitForTransactionToComplete(writeFlowInvTx);
121     }
122
123     void handleDNATConfigurationForRouterAssociation(String routerName, String vpnName, String externalNetwork) {
124         InstanceIdentifier<RouterPorts> routerPortsId = NatUtil.getRouterPortsId(routerName);
125         Optional<RouterPorts> optRouterPorts =
126             MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, routerPortsId);
127         if (!optRouterPorts.isPresent()) {
128             LOG.debug("handleDNATConfigurationForRouterAssociation : Could not read Router Ports data "
129                     + "object with id: {} to handle associate vpn {}", routerName, vpnName);
130             return;
131         }
132         Uuid networkId = Uuid.getDefaultInstance(externalNetwork);
133         RouterPorts routerPorts = optRouterPorts.get();
134         List<Ports> interfaces = routerPorts.getPorts();
135         for (Ports port : interfaces) {
136             String portName = port.getPortName();
137             BigInteger dpnId = NatUtil.getDpnForInterface(interfaceManager, portName);
138             if (dpnId.equals(BigInteger.ZERO)) {
139                 LOG.warn("handleDNATConfigurationForRouterAssociation : DPN not found for {}, "
140                         + "skip handling of router {} association with vpn", portName, routerName, vpnName);
141                 continue;
142             }
143
144             List<InternalToExternalPortMap> intExtPortMapList = port.getInternalToExternalPortMap();
145             for (InternalToExternalPortMap intExtPortMap : intExtPortMapList) {
146                 //remove all NAT related entries with routerName
147                 //floatingIpListener.removeNATOnlyFlowEntries(dpnId, portName, routerName, null,
148                 // intExtPortMap.getInternalIp(), externalIp);
149                 //Create NAT entries with VPN Id
150                 LOG.debug("handleDNATConfigurationForRouterAssociation : Updating DNAT flows with VPN metadata {} ",
151                         vpnName);
152                 floatingIpListener.createNATOnlyFlowEntries(dpnId, routerName, vpnName, networkId, intExtPortMap);
153             }
154         }
155     }
156
157     void handleDNATConfigurationForRouterDisassociation(String routerName, String vpnName, String externalNetwork) {
158         InstanceIdentifier<RouterPorts> routerPortsId = NatUtil.getRouterPortsId(routerName);
159         Optional<RouterPorts> optRouterPorts =
160             MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, routerPortsId);
161         if (!optRouterPorts.isPresent()) {
162             LOG.error("handleDNATConfigurationForRouterDisassociation : Could not read Router Ports "
163                     + "data object with id: {} to handle disassociate vpn {}", routerName, vpnName);
164             return;
165         }
166         Uuid networkId = Uuid.getDefaultInstance(externalNetwork);
167         RouterPorts routerPorts = optRouterPorts.get();
168         List<Ports> interfaces = routerPorts.getPorts();
169         for (Ports port : interfaces) {
170             String portName = port.getPortName();
171             BigInteger dpnId = NatUtil.getDpnForInterface(interfaceManager, portName);
172             if (dpnId.equals(BigInteger.ZERO)) {
173                 LOG.debug("handleDNATConfigurationForRouterDisassociation : DPN not found for {}, "
174                         + "skip handling of router {} association with vpn", portName, routerName, vpnName);
175                 continue;
176             }
177             List<InternalToExternalPortMap> intExtPortMapList = port.getInternalToExternalPortMap();
178             for (InternalToExternalPortMap intExtPortMap : intExtPortMapList) {
179                 //remove all NAT related entries with routerName
180                 //floatingIpListener.removeNATOnlyFlowEntries(dpnId, portName, routerName, vpnName,
181                 // intExtPortMap.getInternalIp(), externalIp);
182                 //Create NAT entries with VPN Id
183                 floatingIpListener.createNATOnlyFlowEntries(dpnId, routerName, null, networkId, intExtPortMap);
184             }
185         }
186     }
187
188 }