<?php

namespace App\Helpers\Utility;

use Illuminate\Support\Facades\Http;

class Arista {
  
  /**
   * Ping a given IP address with all Arista's.
   */
  static function ping_full($_addr) {

    if(boolval(filter_var($_addr, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE))) {

      try {
        // Use a hashing mechanism to ensure the message is authentic
        $hmac = hash_hmac('sha256', $_addr, config('creatia.secret.bgp_forward')); 

        $return = Http::withHeaders([
            'Authorization' => "$hmac",
            'Content-Type' => 'application/json'
          ])
          ->retry(3, 100)
          ->get(config('creatia.endpoint.bgp_forward') . '/ping', [ 'addr' => $_addr ])
          ->json()['results'];

        $results = [];

        \Log::debug($return);

        foreach($return as $router_ip => $a) {
          $matches = [];

          if(preg_match("/(?: = \d+\.\d+\/)(\d+\.\d+)/", $a, $matches) != 0) {
            $results[$router_ip] = floatval($matches[1]);
          }
        }

        return $results;
      }
      catch(\Exception $x) {
        \Log::debug($x);
        return false;
      }
    }
  
    return false;
  }

  static function route_insert($_addr, $_location, $_vrf, $_router = false) {
    if(boolval(filter_var($_addr, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE))) {

      $_continue = false;

      // Perform some heavy sanitisation
      if(array_search($_vrf, ['NTT', 'OPTUS', 'COGENT', 'LUMEN', 'TELSTRA', 'ZAYO', 'SOFTBANK', 'VOYAGER', 'OMANTEL']) !== false) {
        $list = json_decode(file_get_contents(base_path() . '/app/Models/RouterList.json'), true);
        if(isset($list[$_location]) && isset($list[$_location][$_vrf])) {
          foreach($list[$_location][$_vrf] as $router) {

            if($_router === false) {
              $_router = $router;
              $_continue = true;
              break;
            }

            if($router['router'] === $_router) {
              $_continue = true;
              break;
            }

          }
        }
      }

      if($_continue) {
        try {
          // Use a hashing mechanism to ensure the message is authentic
          $hmac = hash_hmac('sha256', "$_addr $_location $_vrf $_router", config('creatia.secret.bgp_forward')); 
  
          $return = Http::withHeaders([
              'Authorization' => "$hmac",
              'Content-Type' => 'application/json'
            ])
            ->retry(3, 100)
            ->post(config('creatia.endpoint.bgp_forward') . '/reroute', [
              'addr'        => $_addr,
              'location'    => $_location,
              'vrf'         => $_vrf,
              'router'      => $_router
            ])
            ->json()['results'];
  
          return $return;
        }
        catch(\Exception $x) {
          \Log::debug($x);
          return false;
        }
      }
    }
  
    return false;
  }

  static function route_remove($_addr, $_location, $_vrf, $_router = false) {
    if(boolval(filter_var($_addr, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE))) {

      $_continue = false;

      // Perform some heavy sanitisation
      if(array_search($_vrf, ['NTT', 'OPTUS', 'COGENT', 'LUMEN', 'TELSTRA', 'ZAYO', 'SOFTBANK', 'VOYAGER', 'OMANTEL']) !== false) {
        $list = json_decode(file_get_contents(base_path() . '/app/Models/RouterList.json'), true);
        if(isset($list[$_location]) && isset($list[$_location][$_vrf])) {
          foreach($list[$_location][$_vrf] as $router) {

            if($_router === false) {
              $_router = $router;
              $_continue = true;
              break;
            }

            if($router['router'] === $_router) {
              $_continue = true;
              break;
            }

          }
        }
      }

      if($_continue) {
        try {
          // Use a hashing mechanism to ensure the message is authentic
          $hmac = hash_hmac('sha256', "$_addr $_location $_vrf $_router", config('creatia.secret.bgp_forward')); 
  
          $return = Http::withHeaders([
              'Authorization' => "$hmac",
              'Content-Type' => 'application/json'
            ])
            ->retry(3, 100)
            ->post(config('creatia.endpoint.bgp_forward') . '/unroute', [
              'addr'        => $_addr,
              'location'    => $_location,
              'vrf'         => $_vrf,
              'router'      => $_router
            ])
            ->json()['results'];

          return $return;
        }
        catch(\Exception $x) {
          \Log::debug($x);
          return false;
        }
      }
    }
  
    return false;
  }
}

?>