<commit_before>/*
 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.netvirt.natservice.internal;

import java.math.BigInteger;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.genius.mdsalutil.FlowEntity;
import org.opendaylight.genius.mdsalutil.MetaDataUtil;
import org.opendaylight.genius.mdsalutil.NwConstants;
import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdated;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeExperimenterErrorNotification;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.TcpMatchFields;
import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.UdpMatchFields;
import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer4Match;
import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.ip.port.mapping.intext.ip.protocol.type.ip.port.map.IpPortExternal;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NaptFlowRemovedEventHandler implements SalFlowListener {
    private static final Logger LOG = LoggerFactory.getLogger(NaptFlowRemovedEventHandler.class);
    private final DataBroker dataBroker;
    private final IMdsalApiManager mdsalManager;
    private final EventDispatcher naptEventdispatcher;
    private final NaptPacketInHandler naptPacketInHandler;
    private final NaptManager naptManager;

    public NaptFlowRemovedEventHandler(final DataBroker dataBroker,
                                       final IMdsalApiManager mdsalManager,
                                       final EventDispatcher eventDispatcher,
                                       final NaptPacketInHandler handler,
                                       final NaptManager naptManager) {
        this.dataBroker = dataBroker;
        this.mdsalManager = mdsalManager;
        this.naptEventdispatcher = eventDispatcher;
        this.naptPacketInHandler = handler;
        this.naptManager = naptManager;
    }

    @Override
    public void onSwitchFlowRemoved(SwitchFlowRemoved flowRemoved) {

    }

    private BigInteger getDpnId(String node) {
        //openflow:1]
        String[] temp = node.split(":");
        return new BigInteger(temp[1]);

    }

    @Override
    public void onFlowAdded(FlowAdded arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onFlowRemoved(FlowRemoved flowRemoved) {
        /*
        If the removed flow is from the OUTBOUND NAPT table :
        1) Get the ActionInfo of the flow.
        2) From the ActionInfo of the flow get the internal IP address, port and the protocol.
        3) Get the Metadata matching info of the flow.
        4) From the Metadata matching info of the flow get router ID.
        5) Querry the container intext-ip-port-map using the router ID
           and the internal IP address, port to get the external IP address, port
        6) Instantiate an NaptEntry event and populate the external IP address, port and the router ID.
        7) Place the NaptEntry event to the queue.
*/

        short tableId = flowRemoved.getTableId();

        if (tableId == NwConstants.OUTBOUND_NAPT_TABLE) {
            LOG.info("NaptFlowRemovedEventHandler : onSwitchFlowRemoved() entry");

            //Get the internal internal IP address and the port number from the IPv4 match.
            Ipv4Prefix internalIpv4Address = null;
            Layer3Match layer3Match = flowRemoved.getMatch().getLayer3Match();
            if (layer3Match instanceof Ipv4Match) {
                Ipv4Match internalIpv4Match = (Ipv4Match) layer3Match;
                internalIpv4Address = internalIpv4Match.getIpv4Source();
            }
            if (internalIpv4Address == null) {
                LOG.error("NaptFlowRemovedEventHandler : Matching internal IP is null while retrieving the "
                    + "value from the Outbound NAPT flow");
                return;
            }
            //Get the internal IP as a string
            String internalIpv4AddressAsString = internalIpv4Address.getValue();
            String[] internalIpv4AddressParts = internalIpv4AddressAsString.split("/");
            String internalIpv4HostAddress = null;
            if (internalIpv4AddressParts.length >= 1) {
                internalIpv4HostAddress = internalIpv4AddressParts[0];<commit_msg>Bug 7984: IDLE_TIMEOUT check required in onFlowRemoved.<commit_after>/*
 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.netvirt.natservice.internal;

import java.math.BigInteger;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.genius.mdsalutil.FlowEntity;
import org.opendaylight.genius.mdsalutil.MetaDataUtil;
import org.opendaylight.genius.mdsalutil.NwConstants;
import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdated;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeExperimenterErrorNotification;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.RemovedFlowReason;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.TcpMatchFields;
import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.UdpMatchFields;
import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer4Match;
import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.ip.port.mapping.intext.ip.protocol.type.ip.port.map.IpPortExternal;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NaptFlowRemovedEventHandler implements SalFlowListener {
    private static final Logger LOG = LoggerFactory.getLogger(NaptFlowRemovedEventHandler.class);
    private final DataBroker dataBroker;
    private final IMdsalApiManager mdsalManager;
    private final EventDispatcher naptEventdispatcher;
    private final NaptPacketInHandler naptPacketInHandler;
    private final NaptManager naptManager;

    public NaptFlowRemovedEventHandler(final DataBroker dataBroker,
                                       final IMdsalApiManager mdsalManager,
                                       final EventDispatcher eventDispatcher,
                                       final NaptPacketInHandler handler,
                                       final NaptManager naptManager) {
        this.dataBroker = dataBroker;
        this.mdsalManager = mdsalManager;
        this.naptEventdispatcher = eventDispatcher;
        this.naptPacketInHandler = handler;
        this.naptManager = naptManager;
    }

    @Override
    public void onSwitchFlowRemoved(SwitchFlowRemoved flowRemoved) {

    }

    private BigInteger getDpnId(String node) {
        //openflow:1]
        String[] temp = node.split(":");
        return new BigInteger(temp[1]);

    }

    @Override
    public void onFlowAdded(FlowAdded arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onFlowRemoved(FlowRemoved flowRemoved) {
        /*
        If the removed flow is from the OUTBOUND NAPT table :
        1) Get the ActionInfo of the flow.
        2) From the ActionInfo of the flow get the internal IP address, port and the protocol.
        3) Get the Metadata matching info of the flow.
        4) From the Metadata matching info of the flow get router ID.
        5) Querry the container intext-ip-port-map using the router ID
           and the internal IP address, port to get the external IP address, port
        6) Instantiate an NaptEntry event and populate the external IP address, port and the router ID.
        7) Place the NaptEntry event to the queue.
*/

        short tableId = flowRemoved.getTableId();
        RemovedFlowReason removedReasonFlag = flowRemoved.getReason();
        if (tableId == NwConstants.OUTBOUND_NAPT_TABLE
                && RemovedFlowReason.OFPRRIDLETIMEOUT.equals(removedReasonFlag)) {
            LOG.info("NaptFlowRemovedEventHandler : onSwitchFlowRemoved() entry");

            //Get the internal internal IP address and the port number from the IPv4 match.
            Ipv4Prefix internalIpv4Address = null;
            Layer3Match layer3Match = flowRemoved.getMatch().getLayer3Match();
            if (layer3Match instanceof Ipv4Match) {
                Ipv4Match internalIpv4Match = (Ipv4Match) layer3Match;
                internalIpv4Address = internalIpv4Match.getIpv4Source();
            }
            if (internalIpv4Address == null) {
                LOG.error("NaptFlowRemovedEventHandler : Matching internal IP is null while retrieving the "
                    + "value from the Outbound NAPT flow");
                return;
            }
            //Get the internal IP as a string
            String internalIpv4AddressAsString = internalIpv4Address.getValue();
            String[] internalIpv4AddressParts = internalIpv4AddressAsString.split("/");
            String internalIpv4HostAddress = null;
            if (internalIpv4AddressParts.length >= 1) {
                internalIpv4HostAddress = internalIpv4AddressParts[0];
{'fraction_non_alphanumeric': 0.08312198314652704, 'fraction_numerical': 0.02904360631596171, 'mean_word_length': 3.3224893917963225, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4447883', 'n_tokens_mistral': 3927, 'n_tokens_neox': 3723, 'n_words': 735}