<?php

namespace app\models\mikrotik\routing\bgp;

use yii\base\Model;
use Yii;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use PEAR2\Net\RouterOS;
use app\models\mikrotik\MikrotikConnect;

class Route extends Model {

    public $id;
    public $dst_address;
    public $gateway;
    public $in_label;
    public $interface;
    public $out_label;
    public $route_distinguisher;

    public function rules() {
        return [
            ['id', 'string'],
            ['dst_address', 'string'],
            ['gateway', 'string'],
            ['in_label', 'string'],
            ['interface', 'string'],
            ['out_label', 'string'],
            ['route_distinguisher', 'string'],
        ];
    }

    public function getAll() {
        $connect = MikrotikConnect::Instance();
        $getRequest = new RouterOS\Request('/routing/bgp/vpnv4-route/getall');
        $responses = $connect->getConnection()->sendSync($getRequest);
        $return = array();
        foreach ($responses as $response) {
            if ($response->getProperty('.id') != null) {
                $c = new Route();
                $c->id = $response->getProperty('.id');
                $c->dst_address = $response->getProperty('dst-address');
                $c->gateway = $response->getProperty('gateway');
                $c->in_label = $response->getProperty('in-label');
                $c->interface = $response->getProperty('interface');
                $c->out_label = $response->getProperty('out-label');
                $c->route_distinguisher = $response->getProperty('route-distinguisher');
                $return[] = $c;
            }
        }
        return $return;
    }

}
