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

package comp3500.abn.emitters.odlconfig;

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

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

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;

/**
 * Representation of an RPSL inet-rtr object and an ODL BGP Speaker.
 * Construct the peers of the inet-rtr and provide them the routes of the speakers AutNum
 * @author <NAME>
 */
public class BGPInetRtr {
	private RpslObject inetRtr;
	protected BGPAutNum autNumObject;
	protected String speakerAddress, speakerName, peerRegistry;
	protected long speakerAutNum;
	protected Set<BGPPeer> peers = new HashSet<BGPPeer>();
	
	/**
	 * Instantiate a new BGPInetRtr (BGP Speaker) using the provided inet-rtr rpsl object.
	 * Also instantiates any peers of the instance and uses them the {@link BGPAutNum} to
	 * construct their route tables.
	 * @param object RPSL inet-rtr object of the speaker
	 * @param speakerAddress address of the speaker (should be ifaddr attribtue of the object)
	 * @param autNumObject AutNum the speaker is a member of
	 */
	public BGPInetRtr(RpslObject object, String speakerAddress, BGPAutNum autNumObject) {
		if(object.getType() != ObjectType.INET_RTR) throw new IllegalArgumentException("Requires INET_RTR object, got " + object.getType());

		//Populate fields
		this.inetRtr = object;
		this.speakerName = inetRtr.getTypeAttribute().getCleanValue().toString(); //DNS name
		this.speakerAutNum = autNumObject.autNum;
		this.speakerAddress = speakerAddress;
		this.autNumObject = autNumObject;
		this.peerRegistry = this.toString() + "-registry";
		
		//Add peers of rtr
		for(RpslAttribute peerAttribute : inetRtr.findAttributes(AttributeType.PEER))
			addPeer(peerAttribute);
		
	}
	
	/**
	 * Create and add a peer to the BGPInetRtr instance.
	 * Doesn't support peer options currently
	 * @param peerAttribute attribute of the inet-rtr declaring the peer to be added
	 */
	private void addPeer(RpslAttribute peerAttribute) {
		//TODO should handle peer attribetus such as asno and port
		//Parse the attribute
		List<Pair<String, List<String>>> peerAttrAst = peerAttribute.getTokenList();
		
		//Find the peer IP address, should be in the form ("dns", "BGP4", "172.16.31.10")
		for(Pair<String, List<String>> entry : peerAttrAst) {
			if(entry.getLeft().equals("dns") && entry.getRight().size() > 1) {
				String  peerAddress = entry.getRight().get(1);
				long 	peerAS      = autNumObject.getASOfPeer(peerAddress);
				//Check of peer has associated AS in the speaker AS
				if(peerAS == -1)
					continue;
				
				//Add new peer
				peers.add(new BGPPeer(peerAS, peerAddress, this));
			}
		}
		
	}

	/**
	 * Instantiate a set of BGPInetRtr speakers declared in an RPSL inet-rtr object.
	 * One instance will be created for each if-addr attribtue of unique address.
	 * @param object inet-rtr instance of {@link RpslObject}
	 * @return Set of BGPInetRtr speakers
	 */
	public static Set<BGPInetRtr> getSpeakerInstances(RpslObject object, BGPAutNum autNumObject) {		
		//Check object type
		if(object.getType() != ObjectType.INET_RTR) throw new IllegalArgumentException("Requires INET_RTR object, got " + object.getType());
		
		Set<BGPInetRtr> speakerSet = new HashSet<BGPInetRtr>();
		
		for(RpslAttribute ifAddrAttr : object.findAttributes(AttributeType.IFADDR)) {
			//Parse the ifaddr attribute
			List<Pair<String, List<String>>> attrAst = ifAddrAttr.getTokenList();
			
			//Find the IP of the ifaddr
			for(Pair<String, List<String>> entry : attrAst) {
				//TODO ignores masklen, shouldn't matter though
				if(entry.getLeft().equals("dns") && entry.getRight().size() > 0) {
					speakerSet.add(new BGPInetRtr(object, entry.getRight().get(0), autNumObject));
					break; //Should only be one address per attr.
				}
			}
		}
		return speakerSet;<commit_msg>Preliminary asno from peer attributes BGPInetRtr.java:  Added framework for preferentially reading asno from peer attributes rather than asking autNumObject<commit_after>/*
 * Copyright (c) 2015 <NAME>, <NAME>, <NAME>
 * All rights reserved.
 */

package comp3500.abn.emitters.odlconfig;

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

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

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;

/**
 * Representation of an RPSL inet-rtr object and an ODL BGP Speaker.
 * Construct the peers of the inet-rtr and provide them the routes of the speakers AutNum
 * @author <NAME>
 */
public class BGPInetRtr {
	private RpslObject inetRtr;
	protected BGPAutNum autNumObject;
	protected String speakerAddress, speakerName, peerRegistry;
	protected long speakerAutNum;
	protected Set<BGPPeer> peers = new HashSet<BGPPeer>();
	
	/**
	 * Instantiate a new BGPInetRtr (BGP Speaker) using the provided inet-rtr rpsl object.
	 * Also instantiates any peers of the instance and uses them the {@link BGPAutNum} to
	 * construct their route tables.
	 * @param object RPSL inet-rtr object of the speaker
	 * @param speakerAddress address of the speaker (should be ifaddr attribtue of the object)
	 * @param autNumObject AutNum the speaker is a member of
	 */
	public BGPInetRtr(RpslObject object, String speakerAddress, BGPAutNum autNumObject) {
		if(object.getType() != ObjectType.INET_RTR) throw new IllegalArgumentException("Requires INET_RTR object, got " + object.getType());

		//Populate fields
		this.inetRtr = object;
		this.speakerName = inetRtr.getTypeAttribute().getCleanValue().toString(); //DNS name
		this.speakerAutNum = autNumObject.autNum;
		this.speakerAddress = speakerAddress;
		this.autNumObject = autNumObject;
		this.peerRegistry = this.toString() + "-registry";
		
		//Add peers of rtr
		for(RpslAttribute peerAttribute : inetRtr.findAttributes(AttributeType.PEER))
			addPeer(peerAttribute);
		
	}
	
	/**
	 * Create and add a peer to the BGPInetRtr instance.
	 * Doesn't support peer options currently
	 * @param peerAttribute attribute of the inet-rtr declaring the peer to be added
	 */
	private void addPeer(RpslAttribute peerAttribute) {
		//TODO should handle peer attributes such as asno and port
		//Parse the attribute
		List<Pair<String, List<String>>> peerAttrAst = peerAttribute.getTokenList();
		
		long peerAS_fromAttr = -1;
		long peerAS_fromCache = -1;
		String  peerAddress = null;
		//Find the peer IP address, should be in the form ("dns", "BGP4", "172.16.31.10")
		for(Pair<String, List<String>> entry : peerAttrAst) {
			if(entry.getLeft().equals("dns") && entry.getRight().size() > 1) {
				peerAddress		 = entry.getRight().get(1);
				peerAS_fromCache = autNumObject.getASOfPeer(peerAddress); //retrieve the peer as based on existing known routes //TODO: that impression correct?
				
//				//Check if peer has associated AS in the speaker AS
//				if(peerAS_fromCache == -1)
//					continue;
			}
			//if an asno is provided, extract it
			if(entry.getLeft().equals("asno") && entry.getRight().size() >0) {
				String tempAS = entry.getRight().get(0);
				if(tempAS.startsWith("AS")) //sanity check
					peerAS_fromAttr = Long.parseLong(tempAS.substring(2, tempAS.length()-1));
			}
		}
		if(peerAddress==null)
			//TODO: throw appropriate exception
		if(peerAS_fromCache==-1 && peerAS_fromAttr==-1)
			//TODO: Throw appropriate exception //no ASno available. Abort
			
		//Add new peer
		peers.add(new BGPPeer((peerAS_fromAttr==-1 ? peerAS_fromCache : peerAS_fromAttr), peerAddress, this));
	}

	/**
	 * Instantiate a set of BGPInetRtr speakers declared in an RPSL inet-rtr object.
	 * One instance will be created for each if-addr attribtue of unique address.
	 * @param object inet-rtr instance of {@link RpslObject}
	 * @return Set of BGPInetRtr speakers
	 */
	public static Set<BGPInetRtr> getSpeakerInstances(RpslObject object, BGPAutNum autNumObject) {		
		//Check object type
		if(object.getType() != ObjectType.INET_RTR) throw new IllegalArgumentException("Requires INET_RTR object, got " + object.getType());
		
		Set<BGPInetRtr> speakerSet = new HashSet<BGPInetRtr>();
		
		for(RpslAttribute ifAddrAttr : object.findAttributes(AttributeType.IFADDR)) {
			//Parse the ifaddr attribute
			List<Pair<String, List<String>>> attrAst = ifAddrAttr.getTokenList();
			
			//Find the IP of the ifaddr
			for(Pair<String, List<String>> entry : attrAst) {
				//TODO ignores masklen, shouldn't matter though
				if(entry.getLeft().equals("dns") && entry.getRight().size() > 0) {
					speakerSet.add(new BGPInetRtr(object, entry.getRight().get(0), autNumObject));
					break; //Should only be one address per attr.
				}
			}
		}
		return speakerSet;
{'fraction_non_alphanumeric': 0.10972067039106145, 'fraction_numerical': 0.006368715083798882, 'mean_word_length': 4.12070938215103, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2402755', 'n_tokens_mistral': 3194, 'n_tokens_neox': 2872, 'n_words': 823}