<commit_before>/*
 * Copyright (c) 2015 <NAME>, <NAME>, <NAME>
 * All rights reserved.
 */

package org.rpsl4j.emitters.rpsldocument;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.tuple.Pair;

import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;

import net.ripe.db.whois.common.rpsl.AttributeType;
import net.ripe.db.whois.common.rpsl.ObjectType;
import net.ripe.db.whois.common.rpsl.RpslAttribute;
import net.ripe.db.whois.common.rpsl.RpslObject;
import net.ripe.db.whois.common.rpsl.attrs.AttributeParseException;
import net.ripe.db.whois.common.rpsl.attrs.AutNum;

/**
 * BGPAutNum corresponds to the aut-num class in RPSL. This class is responsible for parsing
 * the aut-num class and generating the tables of routes it's policy dictates to export.
 * @author <NAME>
 */
public class BGPAutNum {
	private RpslObject rpslObject;
	private AutNum autNumObject;
	protected String name;
	protected long autNum;

	/**
	 * Maps of (AS-Peer, IP) to [Routes].
	 * Used to store the list of routes included for export to BGP peers
	 */
	protected Multimap<Pair<Long, String>, BGPRoute> includedRouteMap;

	/**
	 * Initialise a new BGPAutNum instance and generate its route maps
	 * @param object aut-num RPSL object
	 */
	public BGPAutNum(RpslObject object) {
		//Sanity check the provided object
		if(object.getType() != ObjectType.AUT_NUM) throw new IllegalArgumentException("Requires AUT_NUM object, got " + object.getType());
		rpslObject = object;

		//This parse method can throw an error. We can't really recover so we won't catch it
		autNumObject = AutNum.parse(rpslObject.getTypeAttribute().getCleanValue());
		autNum = autNumObject.getValue();

		//Again, this is a mandatory attribute, assume it exists
		name = rpslObject.getValueForAttribute(AttributeType.AS_NAME).toString();

		//Jump into building the route maps
		generateRouteMaps();
	}

	/**
	 * Generate the peer route maps by parsing the AutNum RPSL object's export attributes.
	 */
	private void generateRouteMaps() {
		Multimap<Pair<Long, String>, BGPRoute> includedRouteMap = LinkedHashMultimap.create();
		for(RpslAttribute attr : rpslObject.getAttributes()) {
			//Skip non export attributes
			//TODO do we need to include EXPORT_VIA?
			if(attr.getType() != AttributeType.EXPORT)
				continue;

			//Get the Tuples of ((PeerAS, PeerIP), LocalRouter) from the export attribute
			Set<Pair<Pair<Long, String>, String>> exportPeers = getExportPeers(attr);

			//Resolve the routes for the provided peer and corresponding local router, and add to the route map
			for(Pair<Pair<Long, String>, String> exportPeer : exportPeers) {
				//TODO ignores actions, need to for each peer
				includedRouteMap.putAll(exportPeer.getLeft(), BGPRoute.resolveRoutes(attr, exportPeer.getRight()));
			}
		}

		//Update the object's maps
		this.includedRouteMap = includedRouteMap;
	}

	/**
	 * Returns a set of triples, each containing an AS/IP pair, and the source of the exported route.
	 * Example: to AS1 1.1.1.1 2.2.2.2 at 3.3.3.3 would return [((AS1, 1.1.1.1), 3.3.3.3), ((AS2, 2.2.2.2), 3.3.3.3)]
	 * @param attr export attribute to retrieve peers from
	 * @return Set of tuples of the form [((AS, PeerIP), LocalRouter)]
	 */
	static Set<Pair<Pair<Long, String>, String>> getExportPeers(RpslAttribute attr) {
		//Sanity check parameter
		if(attr.getType() != AttributeType.EXPORT) throw new IllegalArgumentException("Requires EXPORT attribtue, got " + attr.getType());
		Set<Pair<Pair<Long, String>, String>> exportPeers = new HashSet<Pair<Pair<Long, String>, String>>();
		List<Pair<String, List<String>>> attrAST = attr.getTokenList();

		//Iterate through peering specifications and add peers to set
		for(int i = 0; i < attrAST.size(); i++) {
			Pair<String, List<String>> currentASTPair = attrAST.get(i);
			List<String> pairRight = currentASTPair.getRight();
			String localRouter;
			long autNum;
			
			//Filter entries that aren't peering specifications
			if(!currentASTPair.getLeft().equals("to"))<commit_msg>Correct error in example text<commit_after>/*
 * Copyright (c) 2015 <NAME>, <NAME>, <NAME>
 * All rights reserved.
 */

package org.rpsl4j.emitters.rpsldocument;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.tuple.Pair;

import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;

import net.ripe.db.whois.common.rpsl.AttributeType;
import net.ripe.db.whois.common.rpsl.ObjectType;
import net.ripe.db.whois.common.rpsl.RpslAttribute;
import net.ripe.db.whois.common.rpsl.RpslObject;
import net.ripe.db.whois.common.rpsl.attrs.AttributeParseException;
import net.ripe.db.whois.common.rpsl.attrs.AutNum;

/**
 * BGPAutNum corresponds to the aut-num class in RPSL. This class is responsible for parsing
 * the aut-num class and generating the tables of routes it's policy dictates to export.
 * @author <NAME>
 */
public class BGPAutNum {
	private RpslObject rpslObject;
	private AutNum autNumObject;
	protected String name;
	protected long autNum;

	/**
	 * Maps of (AS-Peer, IP) to [Routes].
	 * Used to store the list of routes included for export to BGP peers
	 */
	protected Multimap<Pair<Long, String>, BGPRoute> includedRouteMap;

	/**
	 * Initialise a new BGPAutNum instance and generate its route maps
	 * @param object aut-num RPSL object
	 */
	public BGPAutNum(RpslObject object) {
		//Sanity check the provided object
		if(object.getType() != ObjectType.AUT_NUM) throw new IllegalArgumentException("Requires AUT_NUM object, got " + object.getType());
		rpslObject = object;

		//This parse method can throw an error. We can't really recover so we won't catch it
		autNumObject = AutNum.parse(rpslObject.getTypeAttribute().getCleanValue());
		autNum = autNumObject.getValue();

		//Again, this is a mandatory attribute, assume it exists
		name = rpslObject.getValueForAttribute(AttributeType.AS_NAME).toString();

		//Jump into building the route maps
		generateRouteMaps();
	}

	/**
	 * Generate the peer route maps by parsing the AutNum RPSL object's export attributes.
	 */
	private void generateRouteMaps() {
		Multimap<Pair<Long, String>, BGPRoute> includedRouteMap = LinkedHashMultimap.create();
		for(RpslAttribute attr : rpslObject.getAttributes()) {
			//Skip non export attributes
			//TODO do we need to include EXPORT_VIA?
			if(attr.getType() != AttributeType.EXPORT)
				continue;

			//Get the Tuples of ((PeerAS, PeerIP), LocalRouter) from the export attribute
			Set<Pair<Pair<Long, String>, String>> exportPeers = getExportPeers(attr);

			//Resolve the routes for the provided peer and corresponding local router, and add to the route map
			for(Pair<Pair<Long, String>, String> exportPeer : exportPeers) {
				//TODO ignores actions, need to for each peer
				includedRouteMap.putAll(exportPeer.getLeft(), BGPRoute.resolveRoutes(attr, exportPeer.getRight()));
			}
		}

		//Update the object's maps
		this.includedRouteMap = includedRouteMap;
	}

	/**
	 * Returns a set of triples, each containing an AS/IP pair, and the source of the exported route.
	 * Example: to AS1 1.1.1.1 2.2.2.2 at 3.3.3.3 would return [((AS1, 1.1.1.1), 3.3.3.3), ((AS1, 2.2.2.2), 3.3.3.3)]
	 * @param attr export attribute to retrieve peers from
	 * @return Set of tuples of the form [((AS, PeerIP), LocalRouter)]
	 */
	static Set<Pair<Pair<Long, String>, String>> getExportPeers(RpslAttribute attr) {
		//Sanity check parameter
		if(attr.getType() != AttributeType.EXPORT) throw new IllegalArgumentException("Requires EXPORT attribtue, got " + attr.getType());
		Set<Pair<Pair<Long, String>, String>> exportPeers = new HashSet<Pair<Pair<Long, String>, String>>();
		List<Pair<String, List<String>>> attrAST = attr.getTokenList();

		//Iterate through peering specifications and add peers to set
		for(int i = 0; i < attrAST.size(); i++) {
			Pair<String, List<String>> currentASTPair = attrAST.get(i);
			List<String> pairRight = currentASTPair.getRight();
			String localRouter;
			long autNum;
			
			//Filter entries that aren't peering specifications
			if(!currentASTPair.getLeft().equals("to"))
{'fraction_non_alphanumeric': 0.11776203273882238, 'fraction_numerical': 0.009284143659907158, 'mean_word_length': 4.347485303723057, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 14, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5450247', 'n_tokens_mistral': 2808, 'n_tokens_neox': 2582, 'n_words': 769}