#! /usr/bin/perl

# Copyright (c) 2016, Brocade Communications Systems, Inc.
# All rights reserved.
#
# SPDX-License-Identifier: LGPL-2.1-only

use strict;
use warnings;

use lib "/opt/vyatta/share/perl5/";
use Vyatta::Dataplane;
use Vyatta::Interface;
use JSON;

my $in     = do { local $/; <> };
my $input  = decode_json($in);
my $ifname = $input->{'name'};
my $action = $input->{'action'};

die "Usage: $0 interface\n"
  unless defined($ifname);

my $intf = Vyatta::Interface->new($ifname);
die "unknown interface $ifname"
  unless defined($intf);

die "not a dataplane interface $ifname"
  unless $intf->type() eq 'dataplane';

my $fabric = $intf->dpid();
my ( $dpids, $dpsocks ) = Vyatta::Dataplane::setup_fabric_conns($fabric);
die "Dataplane $fabric is not connected or does not exist\n"
  unless ( scalar(@$dpids) > 0 );

my $sock = ${$dpsocks}[$fabric];
die "Can not connect to dataplane $fabric\n"
  unless $sock;

my $resp = $sock->execute("led $ifname $action");
exit 1 unless defined($resp);

Vyatta::Dataplane::close_fabric_conns( $dpids, $dpsocks );

printf $resp;
