<?php

class network{ 
	
	protected $app;
	
	protected $libClass;
	
	

	

	
	public function __construct($app){
		$this->app = $app;
		$this->libClass = $app->getLibClass("network");
		
	}


	public function action($action){
		
		 $this->{$action.""}();
		
	}

	
	public function create_network(array $options)
	{
		
		$network = $this->libClass->createNetworks($options);
		

		return $network;
	}

	public function create_subnet(array $options)
	{
		
		$subnet = $this->libClass->createSubnet($options);
		

		return $subnet;
	}
	
	public function list_network_ids()
	{
		
		$ln = $this->libClass->listNetworks();
		
		$list_ids = array();

		
		foreach($ln as $n)
		{
			
			$list_ids[] =  $n->id;
		

		}

	return $list_ids;
	}

	public function list_network_names()
	{
		
		$ln = $this->libClass->listNetworks();
		
		$list_names = array();

		
		foreach($ln as $n)
		{
			
			$list_names[] =  $n->name;
		

		}

	return $list_names;
	}

	public function list_cidr()
	{
		
		$ls = $this->libClass->listSubnets();
		$list_cidr = array();
		foreach ($ls as $subnet)
		 {
     		
			$list_cidr[] = $subnet->cidr;
		 }

	return $list_names;
	}
	//option array()
	public function getNetwork($idNet)
	{
		$network="";
		
		try
		{	$newtork = $networking->getNetwork($idNet);
			$network->retrieve();

			
		}

		catch (OpenStack\Common\Error\BadResponseError $e)
		{
			$network = "Id non existant ";
	        }
		catch (Exception $e)
		{
			$network = 'Exception reçue : ',  $e->getMessage(), "\n";
		}

	return $network;
	}

	public function getSubnet($idSub)
	{
		$sbnet="";
		
		try
		{	$subnet = $networking->getSubnet($idSub);
			$subnet->retrieve();

			
		}

		catch (OpenStack\Common\Error\BadResponseError $e)
		{
			$sbnet = "Id non existant ";
	        }
		catch (Exception $e)
		{
			$sbnet = 'Exception reçue : ',  $e->getMessage(), "\n";
		}

	return $sbnet;
	}	

        public function updateNetwork($idNet,$name,$shared,$adminStateUp)
     	{
		$network = getNetwork($idNet);
		if(!is_null($name))
		{
			$network->name = $name;	
		}
		if(!is_null($shared))
		{
			$network->shared = $shared;	
		}
		if(!is_null($adminStateUp))
		{
			$network->adminStateUp = $shared;	
		}
		$network->update();
	}
	
	public function updateSubnet($idSub,$name,$networkId,$ipVersion,$cidr)
     	{
		$subnet = getNetwork($idSub);
		if(!is_null($name))
		{
			$subnet->name = $name;	
		}
		if(!is_null($networkId))
		{
			$subnet->networkId = $networkId;	
		}
		if(!is_null($ipVersion))
		{
			$subnet->ipVersion = $ipVersion;	
		}
		if(!is_null($cidr))
		{
			$subnet->cidr = $cidr;	
		}
		$subnet->update();
	}

	public function deleteNetwork($idNet)
     	{
		$network = getNetwork($idNet);
		$network->delete();
	}

	public function deleteSubnet($idSub)
     	{
		$subnet = getNetwork($idSub);
		$subnet->delete();
	}