All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.restcomm.protocols.ss7.commonapp.primitives.ExtensionFieldImpl Maven / Gradle / Ivy

There is a newer version: 10.0.37-java11
Show newest version
/*
 * Mobius Software LTD
 * Copyright 2019, Mobius Software LTD and individual contributors
 * by the @authors tag.
 *
 * This program is free software: you can redistribute it and/or modify
 * under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see 
 */

package org.restcomm.protocols.ss7.commonapp.primitives;

import java.util.List;

import org.restcomm.protocols.ss7.commonapp.api.primitives.CriticalityType;
import org.restcomm.protocols.ss7.commonapp.api.primitives.ExtensionField;

import com.mobius.software.telco.protocols.ss7.asn.ASNClass;
import com.mobius.software.telco.protocols.ss7.asn.annotations.ASNProperty;
import com.mobius.software.telco.protocols.ss7.asn.annotations.ASNTag;
import com.mobius.software.telco.protocols.ss7.asn.annotations.ASNValidate;
import com.mobius.software.telco.protocols.ss7.asn.exceptions.ASNParsingComponentException;
import com.mobius.software.telco.protocols.ss7.asn.exceptions.ASNParsingComponentExceptionReason;
import com.mobius.software.telco.protocols.ss7.asn.primitives.ASNInteger;
import com.mobius.software.telco.protocols.ss7.asn.primitives.ASNObjectIdentifier;
import com.mobius.software.telco.protocols.ss7.asn.primitives.ASNOctetString;

import io.netty.buffer.ByteBuf;

/**
 *
 * @author sergey vetyutnev
 * @author yulianoifa
 *
 */
@ASNTag(asnClass = ASNClass.UNIVERSAL,tag = 16,constructed = true,lengthIndefinite = false)
public class ExtensionFieldImpl implements ExtensionField {

	@ASNProperty(asnClass = ASNClass.UNIVERSAL,tag = 2,constructed = false,index = 0)
    private ASNInteger localCode;

	@ASNProperty(asnClass = ASNClass.UNIVERSAL,tag = 6,constructed = false,index = 0)
	private ASNObjectIdentifier globalCode;
    
	private ASNCriticalityType criticalityType;
    
    @ASNProperty(asnClass = ASNClass.CONTEXT_SPECIFIC,tag = 1,constructed = false,index = -1)
    public ASNOctetString data;

    @ASNProperty(asnClass = ASNClass.CONTEXT_SPECIFIC,tag = 1,constructed = true,index = -1)
    public ASNOctetString data1;
    
    public ExtensionFieldImpl() {
    }

    public ExtensionFieldImpl(Integer localCode, CriticalityType criticalityType, ByteBuf data, boolean isConstructred) {
    	if(localCode!=null)
    		this.localCode = new ASNInteger(localCode,"LocalCode",null,null,false);
    		
        if(criticalityType!=null && criticalityType!=CriticalityType.typeIgnore)
        	this.criticalityType = new ASNCriticalityType(criticalityType);
        	       
        if(data!=null) {
        	if(!isConstructred)
        		this.data = new ASNOctetString(data,"Data",null,null,false);        		
        	else if(isConstructred)
        		this.data1 = new ASNOctetString(data,"Data",null,null,false);        		
        }
    }

    public ExtensionFieldImpl(List globalCode, CriticalityType criticalityType, ByteBuf data, boolean isConstructred) {
    	if(globalCode!=null)
    		this.globalCode = new ASNObjectIdentifier(globalCode,"GlobalCode",true,false);
    		
        if(criticalityType!=null)
        	this.criticalityType = new ASNCriticalityType(criticalityType);
        	
        if(data!=null) {
        	if(!isConstructred)
        		this.data = new ASNOctetString(data,"Data",null,null,false);        	
        	else if(isConstructred)
        		this.data1 = new ASNOctetString(data,"Data",null,null,false);        	
        }
    }

    public Integer getLocalCode() {
    	if(localCode==null)
    		return null;
    	
        return localCode.getIntValue();
    }

    public List getGlobalCode() {
    	if(globalCode==null)
    		return null;
    	
        return globalCode.getValue();
    }

    public CriticalityType getCriticalityType() {
    	if(criticalityType==null)
    		return CriticalityType.typeIgnore;
    	
        return criticalityType.getType();
    }

    public ByteBuf getValue() {
    	if(data==null && data1==null)
    		return null;
    	
    	if(data!=null)
    		return data.getValue();
    	else
    		return data1.getValue();    	
    }

    @Override
    public String toString() {

        StringBuilder sb = new StringBuilder();
        sb.append("ExtensionField [");
        if (this.localCode != null) {
            sb.append("localCode=");
            sb.append(this.localCode.getValue());
        }
        if (this.globalCode != null && this.globalCode.getValue()!=null) {
            sb.append("globalCode=[");
            sb.append(ASNObjectIdentifier.printDataArrLong(this.globalCode.getValue()));
            sb.append("]");
        }
        if (this.criticalityType != null) {
            sb.append(", criticalityType=");
            sb.append(criticalityType);
        }
        if (this.data != null && this.data.getValue()!=null) {
            sb.append(", data=[");
            sb.append(this.data.printDataArr());
            sb.append("]");
        }
        if (this.data1 != null && this.data1.getValue()!=null) {
            sb.append(", data=[");
            sb.append(this.data1.printDataArr());
            sb.append("]");
        }
        sb.append("]");

        return sb.toString();
    }
	
	@ASNValidate
	public void validateElement() throws ASNParsingComponentException {
		if(localCode==null && globalCode==null)
			throw new ASNParsingComponentException("either local code or global code should be set for extension field", ASNParsingComponentExceptionReason.MistypedParameter);
		
		if(criticalityType==null)
			throw new ASNParsingComponentException("criticality type should be set for extension field", ASNParsingComponentExceptionReason.MistypedParameter);
		
		if(data==null && data1==null)
			throw new ASNParsingComponentException("data should be set for extension field", ASNParsingComponentExceptionReason.MistypedParameter);
		
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy