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

org.restcomm.protocols.ss7.map.MAPStackImpl Maven / Gradle / Ivy

/*
 * 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.map;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

import org.restcomm.protocols.ss7.map.api.MAPMessageType;
import org.restcomm.protocols.ss7.map.api.MAPProvider;
import org.restcomm.protocols.ss7.map.api.MAPStack;
import org.restcomm.protocols.ss7.map.api.errors.MAPErrorCode;
import org.restcomm.protocols.ss7.sccp.SccpProvider;
import org.restcomm.protocols.ss7.tcap.TCAPStackImpl;
import org.restcomm.protocols.ss7.tcap.api.TCAPProvider;
import org.restcomm.protocols.ss7.tcap.api.TCAPStack;

/**
 *
 * @author amit bhayani
 * @author yulianoifa
 *
 */
public class MAPStackImpl implements MAPStack {

	public static final String PROTOCOL_NAME="map";
	
    protected TCAPStack tcapStack = null;

    protected MAPProviderImpl mapProvider = null;

    private State state = State.IDLE;

    private final String name;
    private final MAPStackConfigurationManagement mapCfg;

    private ConcurrentHashMap messagesSentByType=new ConcurrentHashMap();
    private ConcurrentHashMap messagesReceivedByType=new ConcurrentHashMap();
    private ConcurrentHashMap errorsSentByType=new ConcurrentHashMap();
    private ConcurrentHashMap errorsReceivedByType=new ConcurrentHashMap();
    private ConcurrentHashMap dialogsSentByType=new ConcurrentHashMap();
    private ConcurrentHashMap dialogsReceivedByType=new ConcurrentHashMap();
    
    private ConcurrentHashMap> messagesSentByTypeAndNetwork=new ConcurrentHashMap>();
    private ConcurrentHashMap> messagesReceivedByTypeAndNetwork=new ConcurrentHashMap>();
    private ConcurrentHashMap> errorsSentByTypeAndNetwork=new ConcurrentHashMap>();
    private ConcurrentHashMap> errorsReceivedByTypeAndNetwork=new ConcurrentHashMap>();
    private ConcurrentHashMap> dialogsSentByTypeAndNetwork=new ConcurrentHashMap>();
    private ConcurrentHashMap> dialogsReceivedByTypeAndNetwork=new ConcurrentHashMap>();
    
    public MAPStackImpl(String name, SccpProvider sccpPprovider, int ssn) {
        this.name = name;
        this.tcapStack = new TCAPStackImpl(name, sccpPprovider, ssn, 4);
        TCAPProvider tcapProvider = tcapStack.getProvider();
        this.mapCfg = new MAPStackConfigurationManagement();
        mapProvider = new MAPProviderImpl(name, this, tcapProvider,this.mapCfg);
        this.state = State.CONFIGURED;
        initCounters();
    }

    public MAPStackImpl(String name, TCAPProvider tcapProvider) {
        this.name = name;
        this.mapCfg = new MAPStackConfigurationManagement();
        mapProvider = new MAPProviderImpl(name, this, tcapProvider,this.mapCfg);
        this.tcapStack = tcapProvider.getStack();
        this.state = State.CONFIGURED;   
        initCounters();
    }
    
    public static List allMessageTypes=new ArrayList();
    
    static
    {
    	for(MAPMessageType messageType:MAPMessageType.values())
    		allMessageTypes.add(messageType.name());
        
    	allMessageTypes.add("unknown");    	
    }
    
    private void initCounters() {
    	for(String currName:MAPServiceBaseImpl.getNames()) {
    		dialogsSentByType.put(currName,new AtomicLong(0));
    		dialogsReceivedByType.put(currName,new AtomicLong(0));
        } 
    	
    	for(MAPMessageType currType:MAPMessageType.values()) {
    		messagesSentByType.put(currType.name(),new AtomicLong(0));
    		messagesReceivedByType.put(currType.name(),new AtomicLong(0));
        }  
    	
    	messagesSentByType.put("unknown",new AtomicLong(0));
		messagesReceivedByType.put("unknown",new AtomicLong(0));
		
		for(String currName:MAPErrorCode.getAllNames()) {
    		errorsSentByType.put(currName,new AtomicLong(0));
    		errorsReceivedByType.put(currName,new AtomicLong(0));
        }
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public MAPProvider getProvider() {
        return this.mapProvider;
    }

    @Override
    public void start() throws Exception {
        if (state != State.CONFIGURED) {
            throw new IllegalStateException("Stack has not been configured or is already running!");
        }
        if (tcapStack != null) {
            // this is null in junits!
            this.tcapStack.start();
        }
        this.mapProvider.start();

        this.state = State.RUNNING;

    }

    @Override
    public void stop() {
        if (state != State.RUNNING) {
            throw new IllegalStateException("Stack is not running!");
        }
        this.mapProvider.stop();
        if (tcapStack != null) {
            this.tcapStack.stop();
        }

        this.state = State.CONFIGURED;
    }

    // // ///////////////
    // // CONF METHOD //
    // // ///////////////
    // /**
    // * @throws ConfigurationException
    // *
    // */
    // public void configure(Properties props) throws ConfigurationException {
    // if (state != State.IDLE) {
    // throw new
    // IllegalStateException("Stack already been configured or is already running!");
    // }
    // tcapStack.configure(props);
    // TCAPProvider tcapProvider = tcapStack.getProvider();
    // mapProvider = new MAPProviderImpl(tcapProvider);
    // this.state = State.CONFIGURED;
    // }

    private enum State {
        IDLE, CONFIGURED, RUNNING;
    }

    @Override
    public TCAPStack getTCAPStack() {
        return this.tcapStack;
    }

	@Override
	public Map getMessagesSentByType() {
		Map result=new HashMap();
		Iterator> iterator=messagesSentByType.entrySet().iterator();
		while(iterator.hasNext()) {
			Entry currEntry=iterator.next();
			result.put(currEntry.getKey(), currEntry.getValue().get());
		}
		
		return result;
	}

	@Override
	public Map getMessagesReceivedByType() {
		Map result=new HashMap();
		Iterator> iterator=messagesReceivedByType.entrySet().iterator();
		while(iterator.hasNext()) {
			Entry currEntry=iterator.next();
			result.put(currEntry.getKey(), currEntry.getValue().get());
		}
		
		return result;
	}

	@Override
	public Map getErrorsSentByType() {
		Map result=new HashMap();
		Iterator> iterator=errorsSentByType.entrySet().iterator();
		while(iterator.hasNext()) {
			Entry currEntry=iterator.next();
			result.put(currEntry.getKey(), currEntry.getValue().get());
		}
		
		return result;
	}

	@Override
	public Map getErrorsReceivedByType() {
		Map result=new HashMap();
		Iterator> iterator=errorsReceivedByType.entrySet().iterator();
		while(iterator.hasNext()) {
			Entry currEntry=iterator.next();
			result.put(currEntry.getKey(), currEntry.getValue().get());
		}
		
		return result;
	}

	@Override
	public Map getDialogsSentByType() {
		Map result=new HashMap();
		Iterator> iterator=dialogsSentByType.entrySet().iterator();
		while(iterator.hasNext()) {
			Entry currEntry=iterator.next();
			result.put(currEntry.getKey(), currEntry.getValue().get());
		}
		
		return result;
	}

	@Override
	public Map getDialogsReceivedByType() {
		Map result=new HashMap();
		Iterator> iterator=dialogsReceivedByType.entrySet().iterator();
		while(iterator.hasNext()) {
			Entry currEntry=iterator.next();
			result.put(currEntry.getKey(), currEntry.getValue().get());
		}
		
		return result;
	}

	@Override
	public Map getMessagesSentByTypeAndNetwork(int networkID) {
		Map result=new HashMap();
		Map messagesSentByType = messagesSentByTypeAndNetwork.get(networkID);
		if(messagesSentByType!=null) {
			Iterator> iterator=messagesSentByType.entrySet().iterator();
			while(iterator.hasNext()) {
				Entry currEntry=iterator.next();
				result.put(currEntry.getKey(), currEntry.getValue().get());
			}
		}
		
		return result;
	}

	@Override
	public Map getMessagesReceivedByTypeAndNetwork(int networkID) {
		Map result=new HashMap();
		Map messagesReceivedByType = messagesReceivedByTypeAndNetwork.get(networkID);
		if(messagesReceivedByType!=null) {
			Iterator> iterator=messagesReceivedByType.entrySet().iterator();
			while(iterator.hasNext()) {
				Entry currEntry=iterator.next();
				result.put(currEntry.getKey(), currEntry.getValue().get());
			}
		}
		
		return result;
	}

	@Override
	public Map getErrorsSentByTypeAndNetwork(int networkID) {
		Map result=new HashMap();
		Map errorsSentByType = errorsSentByTypeAndNetwork.get(networkID);
		if(errorsSentByType!=null) {
			Iterator> iterator=errorsSentByType.entrySet().iterator();
			while(iterator.hasNext()) {
				Entry currEntry=iterator.next();
				result.put(currEntry.getKey(), currEntry.getValue().get());
			}
		}
		
		return result;
	}

	@Override
	public Map getErrorsReceivedByTypeAndNetwork(int networkID) {
		Map result=new HashMap();
		Map errorsReceivedByType = errorsReceivedByTypeAndNetwork.get(networkID);
		if(errorsReceivedByType!=null) {
			Iterator> iterator=errorsReceivedByType.entrySet().iterator();
			while(iterator.hasNext()) {
				Entry currEntry=iterator.next();
				result.put(currEntry.getKey(), currEntry.getValue().get());
			}
		}
		
		return result;
	}

	@Override
	public Map getDialogsSentByTypeAndNetwork(int networkID) {
		Map result=new HashMap();
		Map dialogsSentByType = dialogsSentByTypeAndNetwork.get(networkID);
		if(dialogsSentByType!=null) {
			Iterator> iterator=dialogsSentByType.entrySet().iterator();
			while(iterator.hasNext()) {
				Entry currEntry=iterator.next();
				result.put(currEntry.getKey(), currEntry.getValue().get());
			}
		}
		
		return result;
	}

	@Override
	public Map getDialogsReceivedByTypeAndNetwork(int networkID) {
		Map result=new HashMap();
		Map dialogsReceivedByType = dialogsReceivedByTypeAndNetwork.get(networkID);
		if(dialogsReceivedByType!=null) {
			Iterator> iterator=dialogsReceivedByType.entrySet().iterator();
			while(iterator.hasNext()) {
				Entry currEntry=iterator.next();
				result.put(currEntry.getKey(), currEntry.getValue().get());
			}
		}
		
		return result;
	}
	
	public void newMessageReceived(String messageType,int networkID) {
		messagesSentByType.get(messageType).incrementAndGet();
		
		ConcurrentHashMap messagesSentByType =messagesSentByTypeAndNetwork.get(networkID);
		if(messagesSentByType==null) {
			messagesSentByType=new ConcurrentHashMap();
			for(MAPMessageType currType:MAPMessageType.values())
				messagesSentByType.put(currType.name(),new AtomicLong(0));
	        
			messagesSentByType.put("unknown",new AtomicLong(0));
			
			ConcurrentHashMap oldValue=messagesSentByTypeAndNetwork.putIfAbsent(networkID, messagesSentByType);
			if(oldValue!=null)
				messagesSentByType=oldValue;
		}
		
		messagesSentByType.get(messageType).incrementAndGet();
	}
	
	public void newMessageSent(String messageType,int networkID) {
		messagesReceivedByType.get(messageType).incrementAndGet();
		
		ConcurrentHashMap messagesReceivedByType =messagesReceivedByTypeAndNetwork.get(networkID);
		if(messagesReceivedByType==null) {
			messagesReceivedByType=new ConcurrentHashMap();
			for(MAPMessageType currType:MAPMessageType.values())
	    		messagesReceivedByType.put(currType.name(),new AtomicLong(0));
	        
	    	messagesReceivedByType.put("unknown",new AtomicLong(0));
			
			ConcurrentHashMap oldValue=messagesReceivedByTypeAndNetwork.putIfAbsent(networkID, messagesReceivedByType);
			if(oldValue!=null)
				messagesReceivedByType=oldValue;
		}
		
		messagesReceivedByType.get(messageType).incrementAndGet();
	}
	
	public void newErrorReceived(String errorType,int networkID) {
		errorsSentByType.get(errorType).incrementAndGet();
		
		ConcurrentHashMap errorsSentByType =errorsSentByTypeAndNetwork.get(networkID);
		if(errorsSentByType==null) {
			errorsSentByType=new ConcurrentHashMap();
			for(String currName:MAPErrorCode.getAllNames())
				errorsSentByType.put(currName,new AtomicLong(0));
	        
			ConcurrentHashMap oldValue=errorsSentByTypeAndNetwork.putIfAbsent(networkID, errorsSentByType);
			if(oldValue!=null)
				errorsSentByType=oldValue;
		}
		
		errorsSentByType.get(errorType).incrementAndGet();
	}
	
	public void newErrorSent(String errorType,int networkID) {
		errorsReceivedByType.get(errorType).incrementAndGet();
		
		ConcurrentHashMap errorsReceivedByType =errorsReceivedByTypeAndNetwork.get(networkID);
		if(errorsReceivedByType==null) {
			errorsReceivedByType=new ConcurrentHashMap();
			for(String currName:MAPErrorCode.getAllNames())
	    		errorsReceivedByType.put(currName,new AtomicLong(0));
	        
			ConcurrentHashMap oldValue=errorsReceivedByTypeAndNetwork.putIfAbsent(networkID, errorsReceivedByType);
			if(oldValue!=null)
				errorsReceivedByType=oldValue;
		}
		
		errorsReceivedByType.get(errorType).incrementAndGet();
	}
	
	public void newDialogReceived(String dialogType,int networkID) {
		dialogsSentByType.get(dialogType).incrementAndGet();
		
		ConcurrentHashMap dialogsSentByType =dialogsSentByTypeAndNetwork.get(networkID);
		if(dialogsSentByType==null) {
			dialogsSentByType=new ConcurrentHashMap();
			for(String currName:MAPServiceBaseImpl.getNames())
				dialogsSentByType.put(currName,new AtomicLong(0));
	        
			ConcurrentHashMap oldValue=dialogsSentByTypeAndNetwork.putIfAbsent(networkID, dialogsSentByType);
			if(oldValue!=null)
				dialogsSentByType=oldValue;
		}
		
		dialogsSentByType.get(dialogType).incrementAndGet();
	}
	
	public void newDialogSent(String dialogType,int networkID) {
		dialogsReceivedByType.get(dialogType).incrementAndGet();
		
		ConcurrentHashMap dialogsReceivedByType =dialogsReceivedByTypeAndNetwork.get(networkID);
		if(dialogsReceivedByType==null) {
			dialogsReceivedByType=new ConcurrentHashMap();
			for(String currName:MAPServiceBaseImpl.getNames())
	    		dialogsReceivedByType.put(currName,new AtomicLong(0));
	        
			ConcurrentHashMap oldValue=dialogsReceivedByTypeAndNetwork.putIfAbsent(networkID, dialogsReceivedByType);
			if(oldValue!=null)
				dialogsReceivedByType=oldValue;
		}
		
		dialogsReceivedByType.get(dialogType).incrementAndGet();		
	}

	@Override
	public String getProtocol() {
		return PROTOCOL_NAME;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy