package eu.teraflow.tid.bgp4Peer.models;

import java.net.Inet4Address;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import es.tid.bgp.bgp4.update.fields.pathAttributes.LinkStateAttribute;
import es.tid.bgp.bgp4.update.tlv.BGP4TLVFormat;
import es.tid.bgp.bgp4.update.tlv.linkstate_attribute_tlvs.IPv4RouterIDLocalNodeLinkAttribTLV;
import es.tid.bgp.bgp4.update.tlv.linkstate_attribute_tlvs.NodeNameNodeAttribTLV;

public class PathAttributeMsg {

	
	private NodeNameNodeAttribTLV nodeNameTLV;
	private byte[] data;
	private String nodeName;
	private Inet4Address addrLocal;
	

	public PathAttributeMsg(LinkStateAttribute att) {
		
		
		if(att.getNodeNameTLV()!=null) {

			nodeNameTLV=att.getNodeNameTLV();
			data=nodeNameTLV.getTlv_bytes();
			int b = nodeNameTLV.getTotalTLVLength();
			int c = nodeNameTLV.getTLVValueLength();
			byte [] fin= Arrays.copyOfRange(data, b - c, b); 
			nodeName = new String(fin , StandardCharsets.UTF_8);
		}
		if(att.getIPv4RouterIDLocalNodeLATLV()!=null) {
			addrLocal=att.getIPv4RouterIDLocalNodeLATLV().getIpv4Address();
		}
		
	}
	public String toString() {
		String out="";
		if(this.nodeName!=null)
			out=out+"NODE name "+nodeName;
		if(this.addrLocal!=null)
			out=out+"NODE IP "+addrLocal.toString();
		
		return out;
	}
	
	public String getNodeName() {
		return nodeName;
	}
	public void setNodeName(String nodeName) {
		this.nodeName = nodeName;
	}
	
	@Override
    public int hashCode() {
        int result = 17;
        result = 31 * result + nodeName.hashCode();
        return result;
    }
	 @Override
	    public boolean equals(Object o) {
	        if (o == this) return true;
	        if (!(o instanceof PathAttributeMsg)) {
	            return false;
	        }
	        PathAttributeMsg pathCk = (PathAttributeMsg) o;
	        return pathCk.nodeName == nodeName;
	    }
	public Inet4Address getAddrLocal() {
		return addrLocal;
	}
	 
	
}
