package TO::Node;
#<copyright>
# ----------------------------------------------------------
# Sun Proprietary/Confidential Code
# Copyright 2001, Sun Microsystems, Inc. All rights reserved.
# ----------------------------------------------------------
#</copyright>

use Debug;
use Data::Dumper;

sub leaf { 0 }

sub portLabel {
   my($node, $port) = @_;

   return "port $port";
}

sub revision {
  return "";
}

sub getPortId {
  my($class, $port) = @_;    #  0 / 1
  return "port.$port";
}


sub getPortCIMId {
  my($obj, $port) = @_;

  my(@xx) = split(/:/, $obj->{info}{name});
  return "$xx[1]." . $obj->getPortId($port);
}

sub zones { 
  my($h, $type) = @_; 
  if ($type) {
    if (exists($h->{zones}{$type})) {
       return $h->{zones}{$type};
    }
  } elsif (exists($h->{zones})) {
    return $h->{zones};
  }
  return undef;
}

sub port     { 
   my($h, $port) = @_;
   if (defined($port)) {
     my $p = $h->{port};
     return $p->[$port];
   } else {
     return $h->{port};
   }
}
sub portInfo { 
     my($h) = @_; return $h->{portInfo} }

sub info     { 
     my($h, $val) = @_; 
     if ($val) {
         return $h->{info}{$val}
     } else {
         return $h->{info};
     }
}
sub diskInfo { 
     my($h) = @_; return $h->{diskInfo} }
sub volInfo  { 
     my($h) = @_; return $h->{volInfo} }


sub name {
  my($obj) = @_;
  return $obj->{info}{name};
}


# last part of name (key)
sub key {
  my($obj) = @_;

  my(@xx) = split(/:/, $obj->{info}{name});
  return $xx[1];
}


sub id {
  my($obj) = @_;

  Debug->err("Error: id must be implemented for " . $obj->name());
}

sub boxName {
  my($obj) = @_;
  return $obj->{info}{BoxName};
}

sub getDevice {
  my($obj) = @_;
  my $Config = PDM::ConfigFile->read();
  return $Config->deviceByKey($obj->key());
}

sub getPortLink {
  my($sw, $port) = @_;

  my $p = $sw->port();
  return undef if (!$p);
  return $p->[$port];
}


sub class {
  my($obj) = @_;
  return $obj->info("class");
}

sub class0 {
  my($obj) = @_;
  my $cl = $obj->info("class");
  my $ix = index($cl, ".");
  $cl = substr($cl,0,$ix) if ($ix > 0);
  return $cl;
}

sub type {
  my($obj) = @_;
  return $obj->info("type");
}

sub toString {
  my($obj) = @_;

  return Dumper($obj);
}

# zone = "hd0"
sub getPortsInZone {
  my($node, $zone) = @_;
  my $z = substr($zone,0,2);
  my $n = substr($zone,2) + 0;

  if (exists($node->{zones}{$z})) {
    return $node->{zones}{$z}[$n]; 
  } else {
    return undef;
  }
}

sub getZoneList {
  my($node, $type) = @_;
  my %H;
  if (!exists($node->{zones})) {
     return undef;
  }
  my $z = $node->{zones};
  my $hd = $z->{hd};
  my $x;
  if ($hd && (!$type || $type eq "hd") ) {
   for ($x=0; $x <= $#$hd; $x++) {
     next if (!$hd->[$x]);
     $H{"hd$x"} = $hd->[$x];
   }
  }
  my $sl = $z->{sl};
  if ($sl && (!$type || $type eq "sl") ) {
   for ($x=0; $x <= $#$sl; $x++) {
     next if (!$sl->[$x]);
     my $ix = index($sl->[$x], " ");
     foreach my $k (%H) {
       if (index(" $H{$k} ", " " . substr($sl->[$x],0,$ix+1)) >= 0) {
          $H{"$k.sl$x"} = $sl->[$x];
       }
     }
   }
  }
  my $ns = $z->{ns};
  if ($ns && (!$type || $type eq "ns") ) {
   for ($x=0; $x <= $#$ns; $x++) {
     next if (!$ns->[$x]);
     my $ix = index($ns->[$x], " ");
     foreach my $k (%H) {
       if (index(" $H{$k} ", " " . substr($ns->[$x],0,$ix+1)) >= 0) {
          $H{"$k.ns$x"} = $ns->[$x];
       }
     }
   }
  }
  return \%H;
}

sub ipAddr {
  my($sw) = @_;
  return undef;
}


  



1;
