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

use base 'TO::STORAGE';
use strict;

sub leaf { 1 }
sub type { "se" }

sub isKnownDevice {
  my($class, $dev) = @_;
  if ($dev->{ProductID} =~ /SE6920/) {
     return 1;
  }
  return 0;
}

sub newFromDev {
  my($class, $to, $dev, $arg) = @_;

  my $renv = System->get_renv();
  my $host = $renv->{hostname};

  my $k = $dev->{type} . ":" . $dev->{key};
  my $exist = $to->nodeByName($k);

  my $storages  =  $to->[2];
  my $key       = $dev->{key};
  my $se        = $dev->{type};
  my $name      = "$se:$key";

  my $obj = $exist || {};

  $obj->{info}{type}    = $se;
  $obj->{info}{class}   = "storage.$se";
  $obj->{info}{name}    = $name;
  $obj->{info}{BoxName} = $dev->{name} || $dev->{ipno};
  $class->set_host_type($obj, "oob", $host);
  $obj->{info}{VendorID}= "SUN";
  $obj->{info}{LGroup}  = $dev->{model} || $dev->{userLabel};
  $obj->{info}{PortWWN} = $dev->{wwn};
  $obj->{info}{NodeWWN} = $dev->{wwn};
  $obj->{info}{ipAddr}  = $dev->{ipno};
  $obj->{info}{ipName}  = $dev->{ipName};
  $obj->{info}{mgmtLevel}  = $dev->{mgmtLevel};
  
  $obj->{port}          = [];

  require Agent::SE_MODULE;
  my $LINKS;
  if ($arg->{report}) {
     $LINKS = $arg->{report};
  } else {
     $LINKS = Agent::SE_MODULE->getLinks($dev, $to);
  }
  $obj->{info}{slotTotal} = $LINKS->{'info.slotTotal'};
  $obj->{info}{portTotal} = $LINKS->{'info.portTotal'};
  $obj->{info}{wwnList}   = $LINKS->{'info.wwnList'};

  $class->addPorts($obj, $LINKS, $to, "$se:$key");

  bless($obj, 'TO::' . uc($se));
  $to->addSyn($key, $name);
  $storages->{$name} = $obj;
  return $obj;
}


sub addPorts {
  my($class, $obj, $LINKS, $to, $key) = @_;

  my $p;
  for ($p=0; $p < $LINKS->{'info.portTotal'} ; $p++) {
     
     next if (!$LINKS->{"fcPort.$p.PortWWN"});
     $to->addSyn( "S" . $LINKS->{"fcPort.$p.LocalPortWWN"},  "$key:$p");
     if (!$obj->{portInfo}[$p]) {
       $obj->{portInfo}[$p]  = {
               PortWWN        => $LINKS->{"fcPort.$p.PortWWN"},
               LocalPortWWN   => $LINKS->{"fcPort.$p.LocalPortWWN"},
               nodeId         => $LINKS->{"fcPort.$p.nodeId"},
               sw_PortType    => $LINKS->{"fcPort.$p.portType"},
              };
     } else {
       $obj->{portInfo}[$p]{sw_PortType} = $LINKS->{"fcPort.$p.portType"};
       $obj->{portInfo}[$p]{nodeId}      = $LINKS->{"fcPort.$p.nodeId"};
       $obj->{portInfo}[$p]{LocalPortWWN}= $LINKS->{"fcPort.$p.LocalPortWWN"};
     }
  }
}
  
     


sub diagramApplet {
  my($class, $name, $Comp, $to) = @_;

  my $Config = PDM::ConfigFile->read();
  my $renv = $Config->renv();
  my $dev = $Config->deviceByKey($name);
  my $width = 305;
  my $height = 232;
  my $gif = "t3_open";
  my $hi = $renv->{gui_bandwidth} || "L";
  if ($dev->{wwn2}) {
    $gif = "t3pg_open";
    $width = 303; $height = 437
  }

  open(O, System->get_home() . "/System/Products/$gif.htm");
  my (%M, $l);
  while ($l = <O>) {
     $l =~ s/"//g;
     if ($l =~ /coords = (\d+),(\d+),(\d+),(\d+)\s+HREF=([^>]+)/) {
        $M{$5} = "$1,$2,$3,$4";
     }
  }
  close(O);
  my $applet =<<EOF;
 <applet code="IMap.class" width=$width height=$height>
 <param name="mapimage" value="/gif/$gif$hi.gif">
 <param name="fgcolor" value="#FFFFFF">
 <param name="bgcolor" value="#666699">
 <param name="alarmFlag" value=1>
 <param name="border" value="1,#FFFFFF">
 <param name="boxSize" value=10>
 <param name="font" value="arial,normal,10">
 <param name="margins" value="5,5">
 <param name="outline" value="#FF0000">
EOF
  my $cnt = 1; my $y;
  foreach my $el (keys %M) {
    my $c = $Comp->{"$name:$el"};
    $applet .= "<param name=shape-$cnt value=\"rect,$M{$el}\">\n";

    if ($c && defined($c->[0])) {
      my $status = $c->[0];
      my $text = $c->[1];
      $applet .= "<param name=\"status-$cnt\" value=$status>\n";
      my $len = 0;
      my $cnt2 = 1;
      my @w = split(/\s+/, $text);
      my $curr = "";
      for ($y=0; $y <= $#w; $y++) {
        my $w1 = $w[$y];
        $curr .= "$w1 ";
        if (length($curr . $w[$y+1]) > 42) {
          $applet .= "<param name=\"text-$cnt-$cnt2\" value=\"$curr\">\n";
          $cnt2++;
          $curr= "";
        }
      }
      if ($curr) {
          $applet .= "<param name=\"text-$cnt-$cnt2\" value=\"$curr\">\n";
      }
    } else {
      $el = ($el eq "e") ? "Comm-OOBand" : ($el eq "ib"? "Comm-InBand": $el);
      $applet .= "<param name=\"text-$cnt-1\" value=\"$el\">\n";
    }
      
    $cnt++;
  }
  $applet .= "</applet>";
  return $applet;
}

sub statusFunc {
  my($class, $name, $State, $arg, $node) = @_;

  my $Comp    = $State->components($name);
  my $i       = $Comp->value("e");
  my $iIB     = $Comp->value("ib");
  my $enc     = TO->val($i->[0], $iIB->[0]);
  my $encInfo = TO->val2(1,$i, $iIB);
  my $renv    = System->get_renv();

  my($s, $slots, $slotsInfo, $u, $d);

  my $tot = $node->info("slotTotal") || 29;
  for ($s=0; $s <= 29; $s++) {
       if ($s < $tot) {
         $slots     .= $Comp->value("slot.$s",0) . ",";
         $slotsInfo .= $Comp->value("slot.$s",1) . ",";
       } else {
         $slots     .= "-1,";
         $slotsInfo .= ",";
       }
  }
  my ($port, $portInfo, $p);
  my $pi = $node->{portInfo};
  my $model = $node->info("Model") || "UNKNOWN";
  if($model =~ /6920/){
     for ($p=0; $p < 32 ; $p++) {
        my $bport = $p%8 + 1;
        my $blade = int($p/8) + 1;
	$port .=",";
	my $id = "m${blade}p${bport}"; 
	$portInfo .=  "[" . $id . "],"; 
     }
  }else{
     for ($p=0; $p <= $#$pi; $p++) {
        $port .= ",";
        my $id = $pi->[$p]{PortId} || $p;
        $portInfo .=  "[" . $id . "],"; 
     }
  }
  my ($lun, $oth, $lun_info, $oth_info);
  my($other, $otherInfo);
  foreach my $e (keys %$Comp) {
     my $el = $Comp->value($e);
     $Comp->summ($e, \$other, \$otherInfo);
  }

  return ($enc, $port, $slots , $other, $encInfo,
          $portInfo, $slotsInfo, $otherInfo);
    
}



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


sub iconLayout {
  my($obj) = @_;
  my ($cat, $label2, $ses);

  my $ports = $obj->portInfo();
  my $info = $obj->info();

  my $pc = $#$ports+1;
  $pc++ if ($pc % 2 == 1);

  if($info->{Model} =~ /6920/){
    $pc = 32;
  }
  my $col = $obj->info("mgmtLevel") eq "D"? "E0F0E0" : "D0D0F0"; 
  $cat = "a,SE,$pc/  ,10/slot,0/1/2,_,,$col/rack.gif";
  $label2 = $obj->{info}{LGroup} . "-Rack";
  $ses = 1;
  return ($cat, $label2, $ses);
}


sub getLogicalPaths{
  my($self) = @_;
  my $volinfo =$self->volInfo();
  my @paths;
  foreach my $path (keys %$volinfo){
    push(@paths, $path);
  }
  return @paths;
}

1;
