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

#  $Id: QLOGICSW.pm,v 1.1 2004/12/08 20:58:39 bkeslin Exp $

use Report;
use CIM::Instance;
use Data::Dumper;
use strict;
use Ilist;
use PDM;
use NWS;
sub revision {'$Revision: 1.1 $'}
#
#  RUNS ON QLOGIC Branded Switches
#

sub newSystem {
  my ($class, $rep) = @_;
  my(@pil, $i, $dd, $a, $j, $k, $k1, $v, $x);
  my($PORTS);
  my($wwn) = $rep->get('id.wwn');
  my $firm = $rep->get('info.version.Active_Firmware_image');
  my $type = $rep->get('id.device_type');
  my $power_cnt = 0;
  for ($x = 1; $x <= 3; $x++) {
    next if(!$rep->{"sensor.power-supply.$x.name"});
    $power_cnt++;
  }
  my($p1) = CIM::Instance->new('NWS_System',[
                    [Name               => $wwn               ],
                    [SystemName         => $rep->get('id.name')],
                    [OtherIdentifyingInfo  => "wwn=$wwn"      ],
                    [WWN                => $wwn      ],
                    [IP                 => $rep->get('id.ip')],
                    [FruCount           => $rep->get('port.count')+1 + $power_cnt],
                    [SysRevision        => $firm ],
                    [FruPowerCount      => $power_cnt],
                    [BootInfo           => $rep->get('info.UpTime')] ,
                    [Caption            => ucfirst($type)],
                ]);

  my($p2) = CIM::Instance->new('CIM_PhysicalFrame', [
                  [Tag                  => $rep->get("id.wwn")],
                  [Name                 => $rep->get("id.wwn")],
                  [Manufacturer         => "Qlogic" ],
                  [Model                => "Qlogic_". $rep->get('port.count')],
                  ]);

  my($a2) = CIM::Instance->new('CIM_ComputerSystemPackage', [
                  [Antecedent            => $p1],
                  [Dependent             => $p2],
                  ]);

  my($l1) = NWS->location($rep);
  
  my($al1) = CIM::Instance->new('NWS_PhysicalFrameLocation', [
                  [Element              => $p2],
                  [PhysicalLocation     => $l1],
                  ]);
               
  push (@pil, $p1, $p2, $a2, $l1, $al1);

  $PORTS =  $rep->get('port.count');


  for ($i = 0; $i < $PORTS; $i++) {
    $dd = $class->newPort($rep,"port.$i" , $i);
    $a = CIM::Instance->new('CIM_Container', [
                   [GroupComponent  => $p2],
                   [PartComponent => $dd->[1]],
                   [LocationWithinContainer => "port$i"],
                   ]);
    push (@pil, @$dd);
    push (@pil, $a);
  }

 my $j = 1;
 foreach my $el ('board','fan','power-supply') {
    for ($x = 1; $x <= 20; $x++) {
       my $name = $rep->{"sensor.$el.$x.name"};
       next if (!$name);
       my $comp = "sensor.$el.$x";
       $dd = $class->newFru($rep,$comp);
       next if (!$dd);
       $a = CIM::Instance->new('CIM_Container', [
                   [GroupComponent  => $p2],
                   [PartComponent => $dd->[1]],
                   [LocationWithinContainer => $comp],
                   ]);
       $j++;
       push (@pil, @$dd, $a);

    }
  }

  return Ilist->new(\@pil);
}


sub newPort {
  my($class, $rep, $cd, $no) = @_;
  my(@il, $p1, $c1, $a1);
  
  my $type       = $rep->get('id.device_type');
  my $state_info = $rep->get("${cd}.state");
  my $status     = $rep->get("${cd}.status");

  my $serial_no = $rep->get("id.wwn") .  "." . $cd;

  my $map   = PDM->getDeviceStateMap("$type.availability");
  my $state = $map->get("state.$state_info");

  $p1 = CIM::Instance->new('NWS_SwitchPort',  [
             [Admin        => $rep->get("$cd.state")],
             [Mode         => $rep->get("$cd.status")],
             [SystemName   => ucfirst($type) . "-" . $rep->get("port.count") ],
             [DeviceID     => $serial_no ],
             [Name         => $serial_no ],
             [RemoteWWN    => $rep->get("$cd.link_wwn") ],
             [StatusInfo   => "$state_info, $status"    ],
             [Status       => $state     ],
             [Speed        => $rep->get("$cd.speed") ],
             [PortType     => $rep->get("$cd.type") ],
                    ]);

  push(@il,$p1);

  $c1 = CIM::Instance->new('CIM_Card', [
                [Removable      => "No"],
                [Replaceable    => "No"],
                [Tag            => $serial_no],
                [Name           => $serial_no],
                [SerialNumber   => $serial_no],
                    ]);

  push(@il, $c1);
  $a1 = CIM::Instance->new('CIM_Realizes', [
                [Antecedent => $c1],
                [Dependent => $p1],
                    ]);
                    
  push(@il,$a1);

  return Ilist->new(\@il);

}

sub newFru {
  my($class, $rep, $cd) = @_;
  my(@il, $p1, $c1, $a1, $c2, $a2);

  my($serial_no) = $rep->get('id.wwn');

  return undef if (!$serial_no);
  $serial_no = $serial_no . ".$cd";


  my $status  = $rep->get("$cd.status") || "UNKNOWN";

  my $id = $rep->get("$cd.name") || $cd;
  $id =~ s/ /_/g;

  $p1 = CIM::Instance->new("NWS_Fru", [
                [SystemName         => $id     ],
                [DeviceID           => $serial_no],
                [Name               => $serial_no],
                [Status             => $status   ],
                [StatusInfo         => $status   ],
                [Type               => "sensor"  ],
                [FruId              => $id       ],
                [OtherIdentifyingInfo => $rep->get("$cd.message") ],
                 ]);
  push(@il,$p1);
  $c1 = CIM::Instance->new('CIM_Card', [
                [Removable     => "Yes"],
                [Tag            => $serial_no],
                [Name           => $id],
                    ]);


  push(@il, $c1);
  $a1 = CIM::Instance->new('CIM_Realizes', [
                    [Antecedent => $c1],
                    [Dependent => $p1],
                    ]);
                    
  push(@il,$a1);

  return Ilist->new(\@il);

}



1;
