2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.vpnservice.natservice.internal;
11 import org.opendaylight.vpnservice.mdsalutil.MetaDataUtil;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdated;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeExperimenterErrorNotification;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.RemovedReasonFlags;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.TcpMatchFields;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.UdpMatchFields;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer4Match;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch;
28 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.natservice.rev160111.intext.ip.port.map.ip.port.mapping.intext.ip.protocol.type.ip.port.map.IpPortExternal;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.opendaylight.vpnservice.mdsalutil.FlowEntity;
33 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import java.math.BigInteger;
38 public class NaptFlowRemovedEventHandler implements SalFlowListener{
39 private DataBroker dataBroker;
40 private final EventDispatcher naptEventdispatcher;
41 private static final Logger LOG = LoggerFactory.getLogger(NaptFlowRemovedEventHandler.class);
42 private final NaptPacketInHandler naptPacketInHandler;
43 private IMdsalApiManager mdsalManager;
44 private NaptManager naptManager;
46 public NaptFlowRemovedEventHandler(EventDispatcher eventDispatcher, DataBroker dataBroker, NaptPacketInHandler handler,
47 IMdsalApiManager mdsalManager, NaptManager naptManager) {
48 this.naptEventdispatcher = eventDispatcher;
49 this.dataBroker = dataBroker;
50 this.naptPacketInHandler = handler;
51 this.mdsalManager = mdsalManager;
52 this.naptManager = naptManager;
56 public void onSwitchFlowRemoved(SwitchFlowRemoved switchFlowRemoved) {
59 If the removed flow is from the OUTBOUND NAPT table :
60 1) Get the ActionInfo of the flow.
61 2) From the ActionInfo of the flow get the internal IP address, port and the protocol.
62 3) Get the Metadata matching info of the flow.
63 4) From the Metadata matching info of the flow get router ID.
64 5) Querry the container intext-ip-port-map using the router ID
65 and the internal IP address, port to get the external IP address, port
66 6) Instantiate an NaptEntry event and populate the external IP address, port and the router ID.
67 7) Place the NaptEntry event to the queue.
70 short tableId = switchFlowRemoved.getTableId();
71 RemovedReasonFlags removedReasonFlag = switchFlowRemoved.getRemovedReason();
73 if (tableId == NatConstants.OUTBOUND_NAPT_TABLE) {
74 LOG.info("NaptFlowRemovedEventHandler : onSwitchFlowRemoved() entry");
76 //Get the internal internal IP address and the port number from the IPv4 match.
77 Ipv4Prefix internalIpv4Address = null;
78 Layer3Match layer3Match = switchFlowRemoved.getMatch().getLayer3Match();
79 if (layer3Match instanceof Ipv4Match) {
80 Ipv4Match internalIpv4Match = (Ipv4Match) layer3Match;
81 internalIpv4Address = internalIpv4Match.getIpv4Source();
83 if(internalIpv4Address == null){
84 LOG.error("NaptFlowRemovedEventHandler : Matching internal IP is null while retrieving the value from the Outbound NAPT flow");
87 //Get the internal IP as a string
88 String internalIpv4AddressAsString = internalIpv4Address.getValue();
89 String[] internalIpv4AddressParts = internalIpv4AddressAsString.split("/");
90 String internalIpv4HostAddress = null;
91 if(internalIpv4AddressParts.length >= 1){
92 internalIpv4HostAddress = internalIpv4AddressParts[0];
95 //Get the protocol from the layer4 match
96 NAPTEntryEvent.Protocol protocol = null;
97 Integer internalPortNumber = null;
98 Layer4Match layer4Match = switchFlowRemoved.getMatch().getLayer4Match();
99 if (layer4Match instanceof TcpMatch) {
100 TcpMatchFields tcpMatchFields = (TcpMatchFields)layer4Match;
101 internalPortNumber = tcpMatchFields.getTcpSourcePort().getValue();
102 protocol = NAPTEntryEvent.Protocol.TCP;
103 }else if (layer4Match instanceof UdpMatch){
104 UdpMatchFields udpMatchFields = (UdpMatchFields)layer4Match;
105 internalPortNumber = udpMatchFields.getUdpSourcePort().getValue();
106 protocol = NAPTEntryEvent.Protocol.UDP;
108 if(protocol == null){
109 LOG.error("NaptFlowRemovedEventHandler : Matching protocol is null while retrieving the value from the Outbound NAPT flow");
113 //Get the router ID from the metadata.
115 BigInteger metadata = switchFlowRemoved.getMatch().getMetadata().getMetadata();
116 if(MetaDataUtil.getNatRouterIdFromMetadata(metadata) != 0) {
117 routerId = MetaDataUtil.getNatRouterIdFromMetadata(metadata);
119 LOG.error("NaptFlowRemovedEventHandler : Null exception while retrieving routerId");
123 //Get the external IP address and the port from the model
124 IpPortExternal ipPortExternal = NatUtil.getExternalIpPortMap(dataBroker, routerId, internalIpv4HostAddress, internalPortNumber.toString(), protocol);
125 if(ipPortExternal == null){
126 LOG.error("NaptFlowRemovedEventHandler : IpPortExternal is null while querried from the model");
129 String externalIpAddress = ipPortExternal.getIpAddress();
130 int externalPortNumber = ipPortExternal.getPortNum();
132 //Create an NAPT event and place it in the queue.
133 NAPTEntryEvent naptEntryEvent = new NAPTEntryEvent(externalIpAddress, externalPortNumber, routerId, NAPTEntryEvent.Operation.DELETE, protocol);
134 naptEventdispatcher.addNaptEvent(naptEntryEvent);
136 //Get the DPN ID from the Node
137 InstanceIdentifier<Node> nodeRef = switchFlowRemoved.getNode().getValue().firstIdentifierOf(Node.class);
138 String dpn = nodeRef.firstKeyOf(Node.class).getId().getValue();
139 BigInteger dpnId = getDpnId(dpn);
141 //Inform the MDSAL manager to inform about the flow removal.
142 String switchFlowRef = NatUtil.getNaptFlowRef(dpnId, tableId, String.valueOf(metadata), internalIpv4HostAddress, internalPortNumber);
143 LOG.debug("NaptFlowRemovedEventHandler : DPN ID {}, Metadata {}, SwitchFlowRef {}, internalIpv4HostAddress{}", dpnId, metadata, switchFlowRef, internalIpv4AddressAsString);
144 FlowEntity snatFlowEntity = NatUtil.buildFlowEntity(dpnId, tableId, switchFlowRef);
145 mdsalManager.removeFlow(snatFlowEntity);
147 if(removedReasonFlag.isIDLETIMEOUT()) {
148 LOG.debug("Received flow removed notification due to idleTimeout of flow from switch for flowref {}",switchFlowRef);
149 //Remove the SourceIP:Port key from the Napt packet handler map.
150 String internalIpPortKey = internalIpv4HostAddress + ":" + internalPortNumber;
151 naptPacketInHandler.removeIncomingPacketMap(internalIpPortKey);
153 //Remove the mapping of internal fixed ip/port to external ip/port from the datastore.
154 SessionAddress internalSessionAddress = new SessionAddress(internalIpv4HostAddress, internalPortNumber);
155 naptManager.releaseIpExtPortMapping(routerId, internalSessionAddress, protocol);
157 LOG.debug("Received flow removed notification due to flowdelete from switch for flowref {}",switchFlowRef);
160 LOG.info("NaptFlowRemovedEventHandler : onSwitchFlowRemoved() exit");
164 private BigInteger getDpnId(String node) {
166 String temp[] = node.split(":");
167 BigInteger dpnId = new BigInteger(temp[1]);
173 public void onFlowAdded(FlowAdded arg0) {
174 // TODO Auto-generated method stub
179 public void onFlowRemoved(FlowRemoved arg0) {
180 // TODO Auto-generated method stub
185 public void onFlowUpdated(FlowUpdated arg0) {
186 // TODO Auto-generated method stub
191 public void onNodeErrorNotification(NodeErrorNotification arg0) {
192 // TODO Auto-generated method stub
197 public void onNodeExperimenterErrorNotification(NodeExperimenterErrorNotification arg0) {
198 // TODO Auto-generated method stub