EMMA Coverage Report (generated Mon Aug 23 17:21:34 CEST 2010)
[all classes][diskCacheV111.poolManager]

COVERAGE SUMMARY FOR SOURCE FILE [PoolSelectionUnitV2.java]

nameclass, %method, %block, %line, %
PoolSelectionUnitV2.java82%  (9/11)27%  (55/204)15%  (1312/8762)19%  (308.1/1650)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class PoolSelectionUnitV2$LinkGroup0%   (0/1)0%   (0/23)0%   (0/335)0%   (0/75)
PoolSelectionUnitV2$LinkGroup (String): void 0%   (0/1)0%   (0/31)0%   (0/10)
add (PoolSelectionUnit$SelectionLink): void 0%   (0/1)0%   (0/6)0%   (0/2)
addTo (PoolSelectionUnitV2$LinkGroup): void 0%   (0/1)0%   (0/7)0%   (0/2)
attribute (String): Set 0%   (0/1)0%   (0/6)0%   (0/1)
attribute (String, String, boolean): void 0%   (0/1)0%   (0/33)0%   (0/9)
attributes (): Map 0%   (0/1)0%   (0/6)0%   (0/1)
contains (PoolSelectionUnit$SelectionLink): boolean 0%   (0/1)0%   (0/5)0%   (0/1)
getAllLinks (): Collection 0%   (0/1)0%   (0/3)0%   (0/1)
getName (): String 0%   (0/1)0%   (0/3)0%   (0/1)
isCustodialAllowed (): boolean 0%   (0/1)0%   (0/3)0%   (0/1)
isNearlineAllowed (): boolean 0%   (0/1)0%   (0/3)0%   (0/1)
isOnlineAllowed (): boolean 0%   (0/1)0%   (0/3)0%   (0/1)
isOutputAllowed (): boolean 0%   (0/1)0%   (0/3)0%   (0/1)
isReplicaAllowed (): boolean 0%   (0/1)0%   (0/3)0%   (0/1)
links (): Collection 0%   (0/1)0%   (0/3)0%   (0/1)
remove (PoolSelectionUnit$SelectionLink): boolean 0%   (0/1)0%   (0/5)0%   (0/1)
removeAttribute (String, String): void 0%   (0/1)0%   (0/24)0%   (0/6)
setCustodialAllowed (boolean): void 0%   (0/1)0%   (0/4)0%   (0/2)
setNearlineAllowed (boolean): void 0%   (0/1)0%   (0/4)0%   (0/2)
setOnlineAllowed (boolean): void 0%   (0/1)0%   (0/4)0%   (0/2)
setOutputAllowed (boolean): void 0%   (0/1)0%   (0/4)0%   (0/2)
setReplicaAllowed (boolean): void 0%   (0/1)0%   (0/4)0%   (0/2)
toString (): String 0%   (0/1)0%   (0/168)0%   (0/24)
     
class PoolSelectionUnitV2$Pool0%   (0/1)0%   (0/25)0%   (0/316)0%   (0/43)
PoolSelectionUnitV2$Pool (PoolSelectionUnitV2, String): void 0%   (0/1)0%   (0/39)0%   (0/11)
PoolSelectionUnitV2$Pool (PoolSelectionUnitV2, String, PoolSelectionUnitV2$1)... 0%   (0/1)0%   (0/5)0%   (0/1)
access$1200 (PoolSelectionUnitV2$Pool): Map 0%   (0/1)0%   (0/3)0%   (0/1)
access$3600 (PoolSelectionUnitV2$Pool): boolean 0%   (0/1)0%   (0/3)0%   (0/1)
access$3700 (PoolSelectionUnitV2$Pool): boolean 0%   (0/1)0%   (0/3)0%   (0/1)
canRead (): boolean 0%   (0/1)0%   (0/22)0%   (0/1)
canReadForP2P (): boolean 0%   (0/1)0%   (0/22)0%   (0/1)
canReadFromTape (): boolean 0%   (0/1)0%   (0/25)0%   (0/1)
canWrite (): boolean 0%   (0/1)0%   (0/30)0%   (0/1)
canWriteForP2P (): boolean 0%   (0/1)0%   (0/25)0%   (0/1)
getActive (): long 0%   (0/1)0%   (0/10)0%   (0/1)
getHsmInstances (): Set 0%   (0/1)0%   (0/3)0%   (0/1)
getPoolMode (): PoolV2Mode 0%   (0/1)0%   (0/3)0%   (0/1)
isActive (): boolean 0%   (0/1)0%   (0/9)0%   (0/1)
isEnabled (): boolean 0%   (0/1)0%   (0/3)0%   (0/1)
isPing (): boolean 0%   (0/1)0%   (0/3)0%   (0/1)
isReadOnly (): boolean 0%   (0/1)0%   (0/3)0%   (0/1)
setActive (boolean): void 0%   (0/1)0%   (0/8)0%   (0/2)
setEnabled (boolean): void 0%   (0/1)0%   (0/4)0%   (0/2)
setHsmInstances (Set): void 0%   (0/1)0%   (0/11)0%   (0/4)
setPing (boolean): void 0%   (0/1)0%   (0/4)0%   (0/2)
setPoolMode (PoolV2Mode): void 0%   (0/1)0%   (0/4)0%   (0/2)
setReadOnly (boolean): void 0%   (0/1)0%   (0/4)0%   (0/2)
setSerialId (long): boolean 0%   (0/1)0%   (0/12)0%   (0/4)
toString (): String 0%   (0/1)0%   (0/58)0%   (0/1)
     
class PoolSelectionUnitV2100% (1/1)17%  (14/84)10%  (734/7029)15%  (198.4/1361)
ac_psu_addto_linkGroup_$_2 (Args): String 0%   (0/1)0%   (0/102)0%   (0/18)
ac_psu_addto_pgroup_$_2 (Args): String 0%   (0/1)0%   (0/14)0%   (0/4)
ac_psu_clear_im_really_sure (Args): String 0%   (0/1)0%   (0/4)0%   (0/2)
ac_psu_create_linkGroup_$_1 (Args): String 0%   (0/1)0%   (0/58)0%   (0/11)
ac_psu_create_pool_$_1 (Args): String 0%   (0/1)0%   (0/65)0%   (0/14)
ac_psu_dump_setup (Args): String 0%   (0/1)0%   (0/13)0%   (0/3)
ac_psu_ls_linkGroup_$_0_99 (Args): String 0%   (0/1)0%   (0/134)0%   (0/26)
ac_psu_ls_link_$_0_99 (Args): String 0%   (0/1)0%   (0/214)0%   (0/35)
ac_psu_ls_netunits (Args): String 0%   (0/1)0%   (0/87)0%   (0/17)
ac_psu_ls_pgroup_$_0_99 (Args): String 0%   (0/1)0%   (0/144)0%   (0/29)
ac_psu_ls_pool_$_0_99 (Args): String 0%   (0/1)0%   (0/154)0%   (0/31)
ac_psu_ls_ugroup_$_0_99 (Args): String 0%   (0/1)0%   (0/149)0%   (0/29)
ac_psu_ls_unit_$_0_99 (Args): String 0%   (0/1)0%   (0/131)0%   (0/27)
ac_psu_match2_$_1_99 (Args): String 0%   (0/1)0%   (0/145)0%   (0/28)
ac_psu_match_$_5 (Args): String 0%   (0/1)0%   (0/129)0%   (0/17)
ac_psu_netmatch_$_1 (Args): String 0%   (0/1)0%   (0/42)0%   (0/9)
ac_psu_remove_linkGroup_$_1 (Args): String 0%   (0/1)0%   (0/59)0%   (0/12)
ac_psu_remove_linkGroup_attribute_$_2 (Args): String 0%   (0/1)0%   (0/72)0%   (0/13)
ac_psu_remove_link_$_1 (Args): String 0%   (0/1)0%   (0/99)0%   (0/17)
ac_psu_remove_pgroup_$_1 (Args): String 0%   (0/1)0%   (0/82)0%   (0/15)
ac_psu_remove_pool_$_1 (Args): String 0%   (0/1)0%   (0/85)0%   (0/15)
ac_psu_remove_ugroup_$_1 (Args): String 0%   (0/1)0%   (0/77)0%   (0/14)
ac_psu_remove_unit_$_1 (Args): String 0%   (0/1)0%   (0/103)0%   (0/19)
ac_psu_removefrom_linkGroup_$_2 (Args): String 0%   (0/1)0%   (0/87)0%   (0/16)
ac_psu_removefrom_pgroup_$_2 (Args): String 0%   (0/1)0%   (0/93)0%   (0/17)
ac_psu_removefrom_ugroup_$_2 (Args): String 0%   (0/1)0%   (0/127)0%   (0/23)
ac_psu_set_active_$_1 (Args): String 0%   (0/1)0%   (0/72)0%   (0/14)
ac_psu_set_disabled_$_1 (Args): String 0%   (0/1)0%   (0/8)0%   (0/2)
ac_psu_set_enabled_$_1 (Args): String 0%   (0/1)0%   (0/8)0%   (0/2)
ac_psu_set_linkGroup_attribute_$_2 (Args): String 0%   (0/1)0%   (0/79)0%   (0/13)
ac_psu_set_linkGroup_custodialAllowed_$_2 (Args): String 0%   (0/1)0%   (0/46)0%   (0/10)
ac_psu_set_linkGroup_nearlineAllowed_$_2 (Args): String 0%   (0/1)0%   (0/46)0%   (0/10)
ac_psu_set_linkGroup_onlineAllowed_$_2 (Args): String 0%   (0/1)0%   (0/46)0%   (0/10)
ac_psu_set_linkGroup_outputAllowed_$_2 (Args): String 0%   (0/1)0%   (0/46)0%   (0/10)
ac_psu_set_linkGroup_replicaAllowed_$_2 (Args): String 0%   (0/1)0%   (0/46)0%   (0/10)
ac_psu_set_pool_$_2 (Args): String 0%   (0/1)0%   (0/104)0%   (0/23)
ac_psu_unlink_$_2 (Args): String 0%   (0/1)0%   (0/101)0%   (0/19)
ac_psux_ls_link_$_0_1 (Args): Object 0%   (0/1)0%   (0/95)0%   (0/20)
ac_psux_ls_pgroup_$_0_1 (Args): Object 0%   (0/1)0%   (0/74)0%   (0/17)
ac_psux_ls_pool_$_0_1 (Args): Object 0%   (0/1)0%   (0/92)0%   (0/20)
ac_psux_ls_ugroup_$_0_1 (Args): Object 0%   (0/1)0%   (0/74)0%   (0/17)
ac_psux_ls_unit_$_0_1 (Args): Object 0%   (0/1)0%   (0/95)0%   (0/17)
ac_psux_match_$_5 (Args): Object 0%   (0/1)0%   (0/44)0%   (0/3)
addtoPoolGroup (String, String): void 0%   (0/1)0%   (0/69)0%   (0/13)
fillLinkProperties (PoolSelectionUnitV2$Link): Object [] 0%   (0/1)0%   (0/5)0%   (0/1)
fillLinkProperties (PoolSelectionUnitV2$Link, boolean): Object [] 0%   (0/1)0%   (0/221)0%   (0/48)
findProtocolUnit (String): PoolSelectionUnitV2$Unit 0%   (0/1)0%   (0/88)0%   (0/17)
getActive (String): long 0%   (0/1)0%   (0/29)0%   (0/9)
getActivePools (): String [] 0%   (0/1)0%   (0/49)0%   (0/9)
getDefinedPools (boolean): String [] 0%   (0/1)0%   (0/48)0%   (0/9)
getLinkByName (String): PoolSelectionUnit$SelectionLink 0%   (0/1)0%   (0/38)0%   (0/9)
getLinkGroupByName (String): PoolSelectionUnitV2$LinkGroup 0%   (0/1)0%   (0/24)0%   (0/7)
getLinkGroups (): String [] 0%   (0/1)0%   (0/26)0%   (0/6)
getLinksByGroupName (String): String [] 0%   (0/1)0%   (0/66)0%   (0/15)
getNetIdentifier (String): String 0%   (0/1)0%   (0/23)0%   (0/4)
getPool (String): PoolSelectionUnit$SelectionPool 0%   (0/1)0%   (0/24)0%   (0/7)
getPool (String, boolean): PoolSelectionUnit$SelectionPool 0%   (0/1)0%   (0/69)0%   (0/15)
getPoolsByPoolGroup (String): Collection 0%   (0/1)0%   (0/46)0%   (0/7)
getProtocolUnit (String): String 0%   (0/1)0%   (0/11)0%   (0/2)
getVersion (): String 0%   (0/1)0%   (0/2)0%   (0/1)
isEnabled (String): boolean 0%   (0/1)0%   (0/29)0%   (0/9)
match (Map, PoolSelectionUnitV2$Unit, PoolSelectionUnit$DirectionType): Map 0%   (0/1)0%   (0/39)0%   (0/8)
match (PoolSelectionUnit$DirectionType, String, String, StorageInfo, String):... 0%   (0/1)0%   (0/856)0%   (0/151)
match (PoolSelectionUnitV2$LinkMap, PoolSelectionUnitV2$Unit, PoolSelectionUn... 0%   (0/1)0%   (0/23)0%   (0/4)
match (PoolSelectionUnitV2$Unit, PoolSelectionUnitV2$LinkGroup, PoolSelection... 0%   (0/1)0%   (0/83)0%   (0/15)
poolCanStageFile (PoolSelectionUnitV2$Pool, StorageInfo): boolean 0%   (0/1)0%   (0/57)0%   (0/8)
printSetup (PrintWriter): void 0%   (0/1)0%   (0/487)0%   (0/56)
protocolConfig (): void 0%   (0/1)0%   (0/42)0%   (0/11)
setActive (String, boolean): void 0%   (0/1)0%   (0/26)0%   (0/8)
setEnabled (String, boolean): void 0%   (0/1)0%   (0/26)0%   (0/8)
ac_psu_addto_ugroup_$_2 (Args): String 100% (1/1)47%  (61/130)68%  (15.6/23)
ac_psu_set_regex_$_1 (Args): String 100% (1/1)65%  (20/31)67%  (6/9)
ac_psu_set_allpoolsactive_$_1 (Args): String 100% (1/1)65%  (32/49)73%  (8/11)
ac_psu_add_link_$_2 (Args): String 100% (1/1)67%  (58/86)86%  (14.6/17)
ac_psu_create_pgroup_$_1 (Args): String 100% (1/1)68%  (34/50)86%  (8.6/10)
ac_psu_create_ugroup_$_1 (Args): String 100% (1/1)68%  (34/50)86%  (8.6/10)
ac_psu_create_unit_$_1 (Args): String 100% (1/1)73%  (78/107)85%  (19.6/23)
ac_psu_set_link_$_1 (Args): String 100% (1/1)74%  (74/100)90%  (21.6/24)
ac_psu_create_link_$_2_99 (Args): String 100% (1/1)74%  (81/109)86%  (15.6/18)
clear (): void 100% (1/1)89%  (32/36)96%  (11.6/12)
<static initializer> 100% (1/1)100% (4/4)100% (1/1)
PoolSelectionUnitV2 (): void 100% (1/1)100% (220/220)100% (66/66)
access$800 (PoolSelectionUnitV2): PoolSelectionUnitV2$NetHandler 100% (1/1)100% (3/3)100% (1/1)
beforeSetup (): void 100% (1/1)100% (3/3)100% (2/2)
     
class PoolSelectionUnitV2$Link100% (1/1)39%  (9/23)27%  (67/246)45%  (12.6/28)
access$1700 (PoolSelectionUnitV2$Link): int 0%   (0/1)0%   (0/3)0%   (0/1)
access$1800 (PoolSelectionUnitV2$Link): String 0%   (0/1)0%   (0/3)0%   (0/1)
access$1900 (PoolSelectionUnitV2$Link): int 0%   (0/1)0%   (0/3)0%   (0/1)
access$2000 (PoolSelectionUnitV2$Link): int 0%   (0/1)0%   (0/3)0%   (0/1)
access$2100 (PoolSelectionUnitV2$Link): int 0%   (0/1)0%   (0/3)0%   (0/1)
access$2600 (PoolSelectionUnitV2$Link): String 0%   (0/1)0%   (0/3)0%   (0/1)
access$2602 (PoolSelectionUnitV2$Link, String): String 0%   (0/1)0%   (0/5)0%   (0/1)
exec (Map): boolean 0%   (0/1)0%   (0/2)0%   (0/1)
getAttraction (): String 0%   (0/1)0%   (0/40)0%   (0/1)
getLinkGroup (): PoolSelectionUnitV2$LinkGroup 0%   (0/1)0%   (0/3)0%   (0/1)
getTag (): String 0%   (0/1)0%   (0/3)0%   (0/1)
pools (): Iterator 0%   (0/1)0%   (0/50)0%   (0/8)
setLinkGroup (PoolSelectionUnitV2$LinkGroup): void 0%   (0/1)0%   (0/4)0%   (0/2)
toString (): String 0%   (0/1)0%   (0/54)0%   (0/1)
PoolSelectionUnitV2$Link (String): void 100% (1/1)100% (34/34)100% (11/11)
PoolSelectionUnitV2$Link (String, PoolSelectionUnitV2$1): void 100% (1/1)100% (4/4)100% (1/1)
access$1702 (PoolSelectionUnitV2$Link, int): int 100% (1/1)100% (5/5)100% (1/1)
access$1902 (PoolSelectionUnitV2$Link, int): int 100% (1/1)100% (5/5)100% (1/1)
access$2002 (PoolSelectionUnitV2$Link, int): int 100% (1/1)100% (5/5)100% (1/1)
access$2102 (PoolSelectionUnitV2$Link, int): int 100% (1/1)100% (5/5)100% (1/1)
access$500 (PoolSelectionUnitV2$Link): Map 100% (1/1)100% (3/3)100% (1/1)
access$600 (PoolSelectionUnitV2$Link): Map 100% (1/1)100% (3/3)100% (1/1)
getName (): String 100% (1/1)100% (3/3)100% (1/1)
     
class PoolSelectionUnitV2$PGroup100% (1/1)50%  (2/4)34%  (13/38)71%  (3.6/5)
access$100 (PoolSelectionUnitV2$PGroup): Map 0%   (0/1)0%   (0/3)0%   (0/1)
toString (): String 0%   (0/1)0%   (0/22)0%   (0/1)
PoolSelectionUnitV2$PGroup (String): void 100% (1/1)100% (9/9)100% (3/3)
PoolSelectionUnitV2$PGroup (String, PoolSelectionUnitV2$1): void 100% (1/1)100% (4/4)100% (1/1)
     
class PoolSelectionUnitV2$Unit100% (1/1)50%  (5/10)35%  (34/96)68%  (9.6/14)
access$200 (PoolSelectionUnitV2$Unit): int 0%   (0/1)0%   (0/3)0%   (0/1)
access$3800 (PoolSelectionUnitV2$Unit): String 0%   (0/1)0%   (0/3)0%   (0/1)
getType (): String 0%   (0/1)0%   (0/26)0%   (0/1)
setName (String): void 0%   (0/1)0%   (0/4)0%   (0/2)
toString (): String 0%   (0/1)0%   (0/26)0%   (0/1)
PoolSelectionUnitV2$Unit (String, int): void 100% (1/1)100% (20/20)100% (7/7)
PoolSelectionUnitV2$Unit (String, int, PoolSelectionUnitV2$1): void 100% (1/1)100% (5/5)100% (1/1)
access$2800 (PoolSelectionUnitV2$Unit): Map 100% (1/1)100% (3/3)100% (1/1)
getCanonicalName (): String 100% (1/1)100% (3/3)100% (1/1)
getName (): String 100% (1/1)100% (3/3)100% (1/1)
     
class PoolSelectionUnitV2$NetHandler100% (1/1)50%  (9/18)52%  (201/383)51%  (34.5/67)
access$2200 (PoolSelectionUnitV2$NetHandler, String): PoolSelectionUnitV2$Net... 0%   (0/1)0%   (0/4)0%   (0/1)
access$3100 (PoolSelectionUnitV2$NetHandler, PoolSelectionUnitV2$NetUnit): Po... 0%   (0/1)0%   (0/4)0%   (0/1)
access$3900 (PoolSelectionUnitV2$NetHandler): Map [] 0%   (0/1)0%   (0/3)0%   (0/1)
access$4000 (PoolSelectionUnitV2$NetHandler, PoolSelectionUnitV2$NetUnit): void 0%   (0/1)0%   (0/4)0%   (0/1)
bitsToMask (int): long 0%   (0/1)0%   (0/5)0%   (0/1)
find (PoolSelectionUnitV2$NetUnit): PoolSelectionUnitV2$NetUnit 0%   (0/1)0%   (0/29)0%   (0/5)
longAddressToString (long): String 0%   (0/1)0%   (0/45)0%   (0/5)
match (String): PoolSelectionUnitV2$NetUnit 0%   (0/1)0%   (0/56)0%   (0/13)
remove (PoolSelectionUnitV2$NetUnit): void 0%   (0/1)0%   (0/32)0%   (0/8)
PoolSelectionUnitV2$NetHandler (PoolSelectionUnitV2): void 100% (1/1)100% (98/98)100% (17/17)
PoolSelectionUnitV2$NetHandler (PoolSelectionUnitV2, PoolSelectionUnitV2$1): ... 100% (1/1)100% (4/4)100% (1/1)
access$1000 (PoolSelectionUnitV2$NetHandler): void 100% (1/1)100% (3/3)100% (1/1)
access$3500 (PoolSelectionUnitV2$NetHandler, PoolSelectionUnitV2$NetUnit): void 100% (1/1)100% (4/4)100% (1/1)
access$900 (PoolSelectionUnitV2$NetHandler, int): String 100% (1/1)100% (4/4)100% (1/1)
add (PoolSelectionUnitV2$NetUnit): void 100% (1/1)100% (35/35)100% (6/6)
bitsToString (int): String 100% (1/1)100% (5/5)100% (1/1)
clear (): void 100% (1/1)100% (20/20)100% (4/4)
inetAddressToLong (InetAddress): long 100% (1/1)100% (28/28)100% (6/6)
     
class PoolSelectionUnitV2$UGroup100% (1/1)86%  (6/7)59%  (32/54)88%  (7/8)
toString (): String 0%   (0/1)0%   (0/22)0%   (0/1)
PoolSelectionUnitV2$UGroup (String): void 100% (1/1)100% (16/16)100% (5/5)
PoolSelectionUnitV2$UGroup (String, PoolSelectionUnitV2$1): void 100% (1/1)100% (4/4)100% (1/1)
access$2900 (PoolSelectionUnitV2$UGroup): Map 100% (1/1)100% (3/3)100% (1/1)
access$300 (PoolSelectionUnitV2$UGroup): String 100% (1/1)100% (3/3)100% (1/1)
access$400 (PoolSelectionUnitV2$UGroup): Map 100% (1/1)100% (3/3)100% (1/1)
getName (): String 100% (1/1)100% (3/3)100% (1/1)
     
class PoolSelectionUnitV2$ProtocolUnit100% (1/1)100% (3/3)75%  (69/92)78%  (12.5/16)
getName (): String 100% (1/1)58%  (14/24)58%  (0.6/1)
PoolSelectionUnitV2$ProtocolUnit (PoolSelectionUnitV2, String): void 100% (1/1)79%  (50/63)78%  (10.9/14)
PoolSelectionUnitV2$ProtocolUnit (PoolSelectionUnitV2, String, PoolSelectionU... 100% (1/1)100% (5/5)100% (1/1)
     
class PoolSelectionUnitV2$NetUnit100% (1/1)100% (5/5)93%  (148/159)89%  (25/28)
PoolSelectionUnitV2$NetUnit (PoolSelectionUnitV2, String): void 100% (1/1)92%  (134/145)88%  (21/24)
PoolSelectionUnitV2$NetUnit (PoolSelectionUnitV2, String, PoolSelectionUnitV2... 100% (1/1)100% (5/5)100% (1/1)
getCanonicalName (): String 100% (1/1)100% (3/3)100% (1/1)
getHostAddress (): InetAddress 100% (1/1)100% (3/3)100% (1/1)
getHostBits (): int 100% (1/1)100% (3/3)100% (1/1)
     
class PoolSelectionUnitV2$PoolCore100% (1/1)100% (2/2)100% (14/14)100% (5/5)
PoolSelectionUnitV2$PoolCore (String): void 100% (1/1)100% (11/11)100% (4/4)
getName (): String 100% (1/1)100% (3/3)100% (1/1)

1// $Id: PoolSelectionUnitV2.java,v 1.42 2007-10-25 14:03:54 tigran Exp $
2package diskCacheV111.poolManager;
3 
4import java.net.InetAddress;
5import java.net.UnknownHostException;
6import java.net.URI;
7import java.util.ArrayList;
8import java.util.Collection;
9import java.util.Comparator;
10import java.util.Date;
11import java.util.HashMap;
12import java.util.HashSet;
13import java.util.Iterator;
14import java.util.List;
15import java.util.Map;
16import java.util.NoSuchElementException;
17import java.util.Set;
18import java.util.TreeSet;
19import java.util.concurrent.locks.Lock;
20import java.util.concurrent.locks.ReadWriteLock;
21import java.util.concurrent.locks.ReentrantReadWriteLock;
22import java.util.regex.Pattern;
23import java.io.PrintWriter;
24import java.io.StringWriter;
25 
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28 
29import dmg.util.Args;
30import dmg.util.CommandSyntaxException;
31 
32import diskCacheV111.vehicles.StorageInfo;
33import diskCacheV111.pools.PoolV2Mode;
34import diskCacheV111.vehicles.GenericStorageInfo;
35import org.dcache.cells.CellCommandListener;
36import org.dcache.cells.AbstractCellComponent;
37 
38public class PoolSelectionUnitV2
39    extends AbstractCellComponent
40    implements PoolSelectionUnit,
41               CellCommandListener
42{
43 
44    private static final String __version = "$Id: PoolSelectionUnitV2.java,v 1.42 2007-10-25 14:03:54 tigran Exp $";
45    private static final Logger _log = LoggerFactory.getLogger(PoolSelectionUnitV2.class);
46 
47 
48    public String getVersion() {
49        return __version;
50    }
51 
52    private static final int STORE = 1;
53    private static final int DCACHE = 2;
54    private static final int NET = 3;
55    private static final int PROTOCOL = 4;
56 
57    private final Map<String, PGroup> _pGroups = new HashMap<String, PGroup>();
58    private final Map<String, Pool> _pools = new HashMap<String, Pool>();
59    private final Map<String, Link> _links = new HashMap<String, Link>();
60    private final Map<String, LinkGroup> _linkGroups = new HashMap<String, LinkGroup>();
61    private final Map<String, UGroup> _uGroups = new HashMap<String, UGroup>();
62    private final Map<String, Unit> _units = new HashMap<String, Unit>();
63    private boolean _useRegex = false;
64    private boolean _allPoolsActive = false;
65 
66    /**
67     * Ok, this is the critical part of PoolManager, but (!!!) the whole select
68     * path is READ-ONLY, unless we change setup. So ReadWriteLock is what we
69     * are looking for, while is a point of serialization.
70     */
71 
72    private final ReadWriteLock _psuReadWriteLock = new ReentrantReadWriteLock();
73    private final Lock _psuReadLock = _psuReadWriteLock.readLock();
74    private final Lock _psuWriteLock = _psuReadWriteLock.writeLock();
75 
76    private final NetHandler _netHandler = new NetHandler();
77 
78    private class NetHandler {
79        private Map<Long, NetUnit>[] _netList = new HashMap[33];
80        private String[] _maskStrings = new String[33];
81        private long[] _masks = new long[33];
82 
83        private NetHandler() {
84            long mask = 0;
85            long xmask = 0;
86            long cursor = 1;
87            for (int i = 0; i < _maskStrings.length; i++) {
88 
89                _masks[i] = xmask = ~mask;
90 
91                int a = (int) ((xmask >> 24) & 0xff);
92                int b = (int) ((xmask >> 16) & 0xff);
93                int c = (int) ((xmask >> 8) & 0xff);
94                int d = (int) ((xmask) & 0xff);
95 
96                _maskStrings[i] = a + "." + b + "." + c + "." + d;
97 
98                mask |= cursor;
99                cursor <<= 1;
100            }
101        }
102 
103        private void clear() {
104            for (int i = 0; i < _netList.length; i++)
105                if (_netList[i] != null)
106                    _netList[i].clear();
107        }
108 
109        private long inetAddressToLong(InetAddress address) {
110            byte[] raw = address.getAddress();
111            long addr = 0L;
112 
113            for (int i = 0; i < raw.length; i++) {
114                addr <<= 8;
115                addr |=  raw[i] & 0xff;
116            }
117            return addr;
118        }
119 
120        private String longAddressToString(long addr) {
121            int a = (int) ((addr >> 24) & 0xff);
122            int b = (int) ((addr >> 16) & 0xff);
123            int c = (int) ((addr >> 8) & 0xff);
124            int d = (int) ((addr) & 0xff);
125 
126            return a + "." + b + "." + c + "." + d;
127        }
128 
129        private void add(NetUnit net) {
130            int bit = net.getHostBits();
131            if (_netList[bit] == null)
132                _netList[bit] = new HashMap<Long, NetUnit>();
133 
134            long addr = inetAddressToLong(net.getHostAddress());
135            _netList[bit].put(Long.valueOf(addr & _masks[bit]), net);
136        }
137 
138        private void remove(NetUnit net) {
139 
140            int bit = net.getHostBits();
141            if (_netList[bit] == null)
142                return;
143 
144            long addr = inetAddressToLong(net.getHostAddress());
145 
146            _netList[bit].remove(Long.valueOf(addr));
147            if (_netList.length == 0)
148                _netList[bit] = null;
149        }
150 
151        private NetUnit find(NetUnit net) {
152 
153            int bit = net.getHostBits();
154            if (_netList[bit] == null)
155                return null;
156 
157            long addr = inetAddressToLong(net.getHostAddress());
158 
159            return _netList[bit].get(Long.valueOf(addr & _masks[bit]));
160        }
161 
162        private NetUnit match(String inetAddress) throws UnknownHostException {
163            long addr = inetAddressToLong(InetAddress.getByName(inetAddress));
164            long mask = 0;
165            long cursor = 1;
166            NetUnit unit = null;
167            for (Map map : _netList) {
168                if (map != null) {
169                    Long l = Long.valueOf(addr & ~mask);
170                    unit = (NetUnit) map.get(l);
171                    if (unit != null)
172                        return unit;
173                }
174                mask |= cursor;
175                cursor <<= 1;
176            }
177            return null;
178        }
179 
180        private long bitsToMask(int bits) {
181            return _masks[bits];
182        }
183 
184        private String bitsToString(int bits) {
185            return _maskStrings[bits];
186        }
187    }
188 
189    protected static class PoolCore {
190 
191        protected final String _name;
192        protected final Map<String, Link> _linkList = new HashMap<String, Link>();
193 
194        protected PoolCore(String name) {
195            _name = name;
196        }
197 
198        public String getName() {
199            return _name;
200        }
201    }
202 
203    private static class PGroup extends PoolCore {
204 
205        private final Map<String, Pool> _poolList = new HashMap<String, Pool>();
206 
207        private PGroup(String name) {
208            super(name);
209        }
210 
211        @Override
212        public String toString() {
213            return _name + "  (links=" + _linkList.size() + ";pools="
214                    + _poolList.size() + ")";
215        }
216    }
217 
218    private static class LinkGroup implements SelectionLinkGroup {
219 
220        private final String _name;
221        private final Collection<SelectionLink> _links = new HashSet<SelectionLink>();
222        // no duplicates is allowed
223        private final Map<String, Set<String>> _attributes = new HashMap<String, Set<String>>();
224 
225        /*
226         * my personal view to default behavior
227         */
228        private boolean _isNearlineAllowed = true;
229        private boolean _isOnlineAllowed = false;
230        private boolean _isOutputAllowed = true;
231        private boolean _isReplicaAllowed = true;
232        private boolean _isCustodialAllowed = true;
233 
234        LinkGroup(String name) {
235            _name = name;
236        }
237 
238        public String getName() {
239            return _name;
240        }
241 
242        public void add(SelectionLink link) {
243            _links.add(link);
244        }
245 
246        public boolean remove(SelectionLink link) {
247            return _links.remove(link);
248        }
249 
250        public Collection<SelectionLink> links() {
251            return _links;
252        }
253 
254        public void attribute(String attribute, String value, boolean replace) {
255 
256            Set<String> valuesSet = null;
257            if (!_attributes.containsKey(attribute)) {
258                valuesSet = new HashSet<String>();
259                _attributes.put(attribute, valuesSet);
260            } else {
261                valuesSet = _attributes.get(attribute);
262                if (replace) {
263                    valuesSet.clear();
264                }
265            }
266 
267            valuesSet.add(value);
268 
269        }
270 
271        public Set<String> attribute(String attribute) {
272            return _attributes.get(attribute);
273        }
274 
275        /**
276         *
277         * remove a value associated with a attribute if attribute is empty,
278         * remove attribute as well.
279         *
280         * @param attribute
281         * @param value
282         */
283        public void removeAttribute(String attribute, String value) {
284 
285            if (_attributes.containsKey(attribute)) {
286 
287                Set<String> valuesSet = _attributes.get(attribute);
288                valuesSet.remove(value);
289                if (valuesSet.isEmpty()) {
290                    _attributes.remove(attribute);
291                }
292            }
293        }
294 
295        public Map<String, Set<String>> attributes() {
296            return new HashMap<String, Set<String>>(_attributes);
297        }
298 
299        @Override
300        public String toString() {
301 
302            StringBuilder sb = new StringBuilder(_name);
303            sb.append(" : ");
304 
305            if (!_links.isEmpty()) {
306                sb.append("[  ");
307                for (SelectionLink link : _links) {
308                    sb.append(link.getName());
309                    sb.append(" ");
310                }
311                sb.append("]");
312            } else {
313                sb.append("[EMPTY]");
314            }
315 
316            sb.append("\n");
317            sb.append("    Attributes:\n");
318            for (Map.Entry<String, Set<String>> aAttribute : _attributes
319                    .entrySet()) {
320                sb.append("           ").append(aAttribute.getKey()).append(
321                        " = ");
322                for (String aAttributeValue : aAttribute.getValue()) {
323                    sb.append(aAttributeValue).append(" ");
324                }
325                sb.append("\n");
326            }
327            sb.append("    AccessLatency:\n");
328            sb.append("           ").append("onlineAllowed=").append(_isOnlineAllowed).append("\n");
329            sb.append("           ").append("nearlineAllowed=").append(_isNearlineAllowed).append("\n");
330            sb.append("    RetentionPolicy:\n");
331            sb.append("           ").append("custodialAllowed=").append(_isCustodialAllowed).append("\n");
332            sb.append("           ").append("outputAllowed=").append(_isOutputAllowed).append("\n");
333            sb.append("           ").append("replicaAllowed=").append(_isReplicaAllowed).append("\n");
334            return sb.toString();
335        }
336 
337        public boolean contains(SelectionLink link) {
338            return _links.contains(link);
339        }
340 
341        public Collection<SelectionLink> getAllLinks() {
342            return _links;
343        }
344 
345        public void addTo(LinkGroup newLinks) {
346            _links.addAll(newLinks.getAllLinks());
347        }
348 
349        public boolean isCustodialAllowed() {
350            return _isCustodialAllowed;
351        }
352 
353        public boolean isNearlineAllowed() {
354            return _isNearlineAllowed;
355        }
356 
357        public boolean isOnlineAllowed() {
358            return _isOnlineAllowed;
359        }
360 
361        public boolean isOutputAllowed() {
362            return _isOutputAllowed;
363        }
364 
365        public boolean isReplicaAllowed() {
366            return _isReplicaAllowed;
367        }
368 
369        public void setCustodialAllowed(boolean isAllowed) {
370            _isCustodialAllowed = isAllowed;
371        }
372 
373        public void setNearlineAllowed(boolean isAllowed) {
374            _isNearlineAllowed = isAllowed;
375        }
376 
377        public void setOnlineAllowed(boolean isAllowed) {
378            _isOnlineAllowed = isAllowed;
379        }
380 
381        public void setOutputAllowed(boolean isAllowed) {
382            _isOutputAllowed = isAllowed;
383        }
384 
385        public void setReplicaAllowed(boolean isAllowed) {
386            _isReplicaAllowed = isAllowed;
387        }
388 
389    }
390 
391    private class Pool extends PoolCore implements SelectionPool {
392 
393        private final Map<String, PGroup> _pGroupList = new HashMap<String, PGroup>();
394        private boolean _enabled = true;
395        private long _active = 0L;
396        private boolean _ping = true;
397        private long _serialId = 0L;
398        private boolean _rdOnly = false;
399        private Set<String> _hsmInstances = new HashSet<String>(0);
400        private PoolV2Mode  _mode =
401            new PoolV2Mode(PoolV2Mode.ENABLED);
402 
403        private Pool(String name) {
404            super(name);
405        }
406 
407        public void setActive(boolean active)
408        {
409            _active =  active ? System.currentTimeMillis() : 0;
410        }
411 
412        public long getActive() {
413            return _ping ? (System.currentTimeMillis() - _active) : 0L;
414        }
415 
416        /**
417         * Returns true if pool heartbeat was received within the last
418         * 5 minutes.
419         */
420        public boolean isActive()
421        {
422            return getActive() < 5*60*1000;
423        }
424 
425        public void setReadOnly(boolean rdOnly) {
426            _rdOnly = rdOnly;
427        }
428 
429        public boolean isReadOnly() {
430            return _rdOnly;
431        }
432 
433        /**
434         * Returns true if reading from the pool is allowed.
435         */
436        public boolean canRead()
437        {
438            return isEnabled() &&
439                _mode.getMode() != PoolV2Mode.DISABLED &&
440                !_mode.isDisabled(PoolV2Mode.DISABLED_FETCH) &&
441                !_mode.isDisabled(PoolV2Mode.DISABLED_DEAD);
442        }
443 
444        /**
445         * Returns true if writing to the pool is allowed. Since we
446         * cannot distinguish between a client write and a
447         * pool-to-pool write, both operations must be enabled on the
448         * pool.
449         */
450        public boolean canWrite()
451        {
452            return isEnabled() && !isReadOnly() &&
453                _mode.getMode() != PoolV2Mode.DISABLED &&
454                !_mode.isDisabled(PoolV2Mode.DISABLED_STORE) &&
455                !_mode.isDisabled(PoolV2Mode.DISABLED_DEAD) &&
456                !_mode.isDisabled(PoolV2Mode.DISABLED_P2P_SERVER);
457        }
458 
459        /**
460         * Returns true if the pool is allowed to read from tape.
461         */
462        public boolean canReadFromTape()
463        {
464            return isEnabled() && !isReadOnly() &&
465                _mode.getMode() != PoolV2Mode.DISABLED &&
466                !_mode.isDisabled(PoolV2Mode.DISABLED_STAGE) &&
467                !_mode.isDisabled(PoolV2Mode.DISABLED_DEAD);
468        }
469 
470        /**
471         * Returns true if the pool can deliver files for P2P
472         * operations.
473         */
474        public boolean canReadForP2P()
475        {
476            return isEnabled() &&
477                _mode.getMode() != PoolV2Mode.DISABLED &&
478                !_mode.isDisabled(PoolV2Mode.DISABLED_P2P_SERVER) &&
479                !_mode.isDisabled(PoolV2Mode.DISABLED_DEAD);
480        }
481 
482        /**
483         * Returns true if the pool can receive files for P2P
484         * operations.
485         */
486        public boolean canWriteForP2P()
487        {
488            return isEnabled() && !isReadOnly() &&
489                _mode.getMode() != PoolV2Mode.DISABLED &&
490                !_mode.isDisabled(PoolV2Mode.DISABLED_P2P_CLIENT) &&
491                !_mode.isDisabled(PoolV2Mode.DISABLED_DEAD);
492        }
493 
494        public void setEnabled(boolean enabled) {
495            _enabled = enabled;
496        }
497 
498        public boolean isEnabled() {
499            return _enabled;
500        }
501 
502        public void setPing(boolean ping) {
503            _ping = ping;
504        }
505 
506        public boolean isPing() {
507            return _ping;
508        }
509 
510        @Override
511        public String toString() {
512            return _name
513                + "  (enabled=" + _enabled
514                + ";active=" +(_active > 0 ? (getActive() / 1000) : "no")
515                + ";rdOnly=" + isReadOnly()
516                + ";links=" + _linkList.size()
517                + ";pgroups=" + _pGroupList.size()
518                + ";hsm=" + _hsmInstances.toString()
519                + ";mode=" + _mode
520                + ")";
521        }
522 
523        public boolean setSerialId(long serialId) {
524            if (serialId == _serialId)
525                return false;
526            _serialId = serialId;
527            return true;
528        }
529 
530        public void setPoolMode(PoolV2Mode mode)
531        {
532            _mode = mode;
533        }
534 
535        public PoolV2Mode getPoolMode()
536        {
537            return _mode;
538        }
539 
540        public Set<String> getHsmInstances() {
541            return _hsmInstances;
542        }
543 
544        public void setHsmInstances(Set<String> hsmInstances) {
545            if (hsmInstances == null) {
546                hsmInstances = new HashSet<String>(0);
547            }
548 
549            _hsmInstances = hsmInstances;
550        }
551    }
552 
553    private static class Link implements SelectionLink {
554 
555        private final String _name;
556        private final Map<String, PoolCore> _poolList = new HashMap<String, PoolCore>();
557        private final Map<String, UGroup> _uGroupList = new HashMap<String, UGroup>();
558 
559        private int _readPref = 0;
560        private int _writePref = 0;
561        private int _cachePref = 0;
562        private int _p2pPref = -1;
563        private String _tag = null;
564        private LinkGroup _linkGroup = null;
565 
566        public String getTag() {
567            return _tag;
568        }
569 
570        public String getName() {
571            return _name;
572        }
573 
574        private Link(String name) {
575            _name = name;
576        }
577 
578        @Override
579        public String toString() {
580            return _name + "  (pref=" + _readPref + "/" + _cachePref + "/"
581                    + _p2pPref + "/" + _writePref + ";"
582                    + (_tag == null ? "" : _tag) + ";" + "ugroups="
583                    + _uGroupList.size() + ";pools=" + _poolList.size() + ")";
584        }
585 
586        public String getAttraction() {
587            return "-readpref=" + _readPref + " -writepref=" + _writePref
588                    + " -cachepref=" + _cachePref + " -p2ppref=" + _p2pPref
589                    + (_tag == null ? "" : " -section=" + _tag);
590        }
591 
592        public Iterator<SelectionPool> pools() {
593            List<SelectionPool> list = new ArrayList<SelectionPool>();
594 
595            for (Object o : _poolList.values()) {
596                if (o instanceof Pool) {
597                    list.add((Pool) o);
598                } else if (o instanceof PGroup) {
599                    for (Pool pool : ((PGroup) o)._poolList.values()) {
600                        list.add(pool);
601                    }
602                }
603            }
604            return list.iterator();
605        }
606 
607        public boolean exec(Map variableMap) {
608            return true;
609        }
610 
611        public void setLinkGroup(LinkGroup lg) {
612            _linkGroup = lg;
613        }
614 
615        public LinkGroup getLinkGroup() {
616            return _linkGroup;
617        }
618    }
619 
620    private static class UGroup {
621        private final String _name;
622        private final Map<String, Link> _linkList = new HashMap<String, Link>();
623        private final Map<String, Unit> _unitList = new HashMap<String, Unit>(); // !!!
624                                                                                    // DCache,
625                                                                                    // STore,
626                                                                                    // Net
627                                                                                    // names
628                                                                                    // must
629                                                                                    // be
630                                                                                    // different
631 
632        private UGroup(String name) {
633            _name = name;
634        }
635 
636        private String getName() {
637            return _name;
638        }
639 
640        @Override
641        public String toString() {
642            return _name + "  (links=" + _linkList.size() + ";units="
643                    + _unitList.size() + ")";
644        }
645    }
646 
647    private static class Unit {
648 
649        private String _name = null;
650        private int _type = 0;
651        private Map<String, UGroup> _uGroupList = new HashMap<String, UGroup>();
652 
653        private Unit(String name, int type) {
654            _name = name;
655            _type = type;
656        }
657 
658        public String getName() {
659            return _name;
660        }
661 
662        public String getCanonicalName() {
663            return getName();
664        }
665 
666        protected void setName(String name) {
667            _name = name;
668        }
669 
670        private String getType() {
671            return _type == STORE ? "Store" : _type == DCACHE ? "DCache"
672                    : _type == PROTOCOL ? "Protocol" : _type == NET ? "Net"
673                            : "Unknown";
674        }
675 
676        @Override
677        public String toString() {
678            return _name + "  (type=" + getType() + ";canonical="
679                    + getCanonicalName() + ";uGroups=" + _uGroupList.size()
680                    + ")";
681        }
682    }
683 
684    public SelectionLink getLinkByName(String name) throws NoSuchElementException {
685 
686        Link link = null;
687 
688        _psuReadLock.lock();
689        try {
690            link = _links.get(name);
691            if (link == null)
692                throw new NoSuchElementException("Link not found : " + name);
693        } finally {
694            _psuReadLock.unlock();
695        }
696        return link;
697    }
698 
699    public String[] getDefinedPools(boolean enabledOnly) {
700 
701        List<String> list = new ArrayList<String>();
702        _psuReadLock.lock();
703        try {
704 
705            for (Pool pool : _pools.values()) {
706                if ((!enabledOnly) || pool.isEnabled())
707                    list.add(pool.getName());
708            }
709        } finally {
710            _psuReadLock.unlock();
711        }
712        return list.toArray(new String[list.size()]);
713    }
714 
715    public String[] getActivePools() {
716        List<String> list = new ArrayList<String>();
717 
718        _psuReadLock.lock();
719        try {
720            for (Pool pool : _pools.values()) {
721                if (pool.isEnabled() && pool.isActive())
722                    list.add(pool.getName());
723            }
724        } finally {
725            _psuReadLock.unlock();
726        }
727        return list.toArray(new String[list.size()]);
728    }
729 
730    @Override
731    public void beforeSetup()
732    {
733          clear();
734    }
735 
736    @Override
737    public void printSetup(PrintWriter pw)
738    {
739        _psuReadLock.lock();
740        try {
741 
742            pw.append("#\n# Printed by ").append(getClass().getName())
743                    .append(" at ").append(new Date().toString()).append(
744                            "\n#\n#\n");
745            pw.append("psu set regex ").append(_useRegex?"on":"off").append("\n");
746            pw.append("psu set allpoolsactive ").append(_allPoolsActive?"on":"off").append("\n");
747            pw.append("#\n# The units ...\n#\n");
748            for (Unit unit : _units.values()) {
749                int type = unit._type;
750                pw.append("psu create unit ").append(
751                        type == STORE ? "-store " : type == DCACHE ? "-dcache"
752                                : type == PROTOCOL ? "-protocol" : "-net   ")
753                        .append(" ").append(unit.getName()).append("\n");
754            }
755            pw.append("#\n# The unit Groups ...\n#\n");
756            for (UGroup group : _uGroups.values()) {
757                pw.append("psu create ugroup ").append(group.getName()).append(
758                        "\n");
759                for (Unit unit : group._unitList.values()) {
760                    pw.append("psu addto ugroup ").append(group.getName())
761                            .append(" ").append(unit.getName()).append("\n");
762                }
763            }
764            pw.append("#\n# The pools ...\n#\n");
765            for (Pool pool : _pools.values()) {
766                pw.append("psu create pool ").append(pool.getName());
767 
768                if (!pool.isPing())
769                    pw.append(" -noping");
770                if (!pool.isEnabled())
771                    pw.append(" -disabled");
772 
773                pw.append("\n");
774            }
775            pw.append("#\n# The pool groups ...\n#\n");
776            for (PGroup group : _pGroups.values()) {
777                pw.append("psu create pgroup ").append(group.getName()).append(
778                        "\n");
779                for (Pool pool : group._poolList.values()) {
780                    pw.append("psu addto pgroup ").append(group.getName())
781                            .append(" ").append(pool.getName()).append("\n");
782                }
783            }
784            pw.append("#\n# The links ...\n#\n");
785            for (Link link : _links.values()) {
786                pw.append("psu create link ").append(link.getName());
787                for (UGroup group : link._uGroupList.values()) {
788                    pw.append(" ").append(group.getName());
789                }
790                pw.append("\n");
791                pw.append("psu set link ").append(link.getName()).append(" ")
792                        .println(link.getAttraction());
793                for (PoolCore poolCore : link._poolList.values()) {
794                    pw.append("psu add link ").append(link.getName()).append(
795                            " ").println(poolCore.getName());
796                }
797            }
798 
799            pw.append("#\n# The link Groups ...\n#\n");
800            for (LinkGroup linkGroup : _linkGroups.values()) {
801                pw.append("psu create linkGroup ").append(linkGroup.getName());
802                pw.append("\n");
803 
804                pw.append("psu set linkGroup custodialAllowed ").append(
805                        linkGroup.getName()).append(" ").println(
806                        linkGroup.isCustodialAllowed());
807                pw.append("psu set linkGroup replicaAllowed ").append(
808                        linkGroup.getName()).append(" ").println(
809                        linkGroup.isReplicaAllowed());
810                pw.append("psu set linkGroup nearlineAllowed ").append(
811                        linkGroup.getName()).append(" ").println(
812                        linkGroup.isNearlineAllowed());
813                pw.append("psu set linkGroup outputAllowed ").append(
814                        linkGroup.getName()).append(" ").println(
815                        linkGroup.isOutputAllowed());
816                pw.append("psu set linkGroup onlineAllowed ").append(
817                        linkGroup.getName()).append(" ").println(
818                        linkGroup.isOnlineAllowed());
819 
820                for (Map.Entry<String, Set<String>> aAttribute : linkGroup
821                        .attributes().entrySet()) {
822 
823                    String attributeName = aAttribute.getKey();
824                    for (String aAttributeValue : aAttribute.getValue()) {
825                        pw.append("psu set linkGroup attribute ").append(
826                                linkGroup.getName()).append(" ").append(
827                                attributeName).append("=").println(
828                                aAttributeValue);
829                    }
830                }
831 
832                for (SelectionLink link : linkGroup.links()) {
833                    pw.append("psu addto linkGroup ").append(
834                            linkGroup.getName()).append(" ").println(
835                            link.getName());
836                }
837            }
838 
839        } finally {
840            _psuReadLock.unlock();
841        }
842    }
843 
844    private class NetUnit extends Unit {
845        private InetAddress _address = null;
846        private long _mask = 0;
847        private int _bits = 0;
848        private String _canonicalName = null;
849 
850        private NetUnit(String name) throws UnknownHostException {
851            super(name, NET);
852 
853            int n = name.indexOf('/');
854            if (n < 0) {
855                //
856                // no netmask found (is -host)
857                //
858                _address = InetAddress.getByName(name);
859                //
860            } else {
861                if ((n == 0) || (n == (name.length() - 1)))
862                    throw new IllegalArgumentException(
863                            "host or net part missing");
864 
865                String hostPart = name.substring(0, n);
866                String netPart = name.substring(n + 1);
867 
868                //
869                // count hostbits
870                //
871                byte[] raw = InetAddress.getByName(netPart).getAddress();
872                _mask = ( (raw[0] & 0xff) << 24)
873                        | (( raw[1] & 0xff) << 16)
874                        | (( raw[2] & 0xff) << 8)
875                        | (raw[3] & 0xff);
876                long cursor = 1;
877                _bits = 0;
878                for (_bits = 0; _bits < 32; _bits++) {
879                    if ((_mask & cursor) > 0)
880                        break;
881                    cursor <<= 1;
882                }
883                _address = InetAddress.getByName(hostPart);
884            }
885            _canonicalName = _address.getHostAddress() + "/"
886                    + _netHandler.bitsToString(_bits);
887        }
888 
889        public int getHostBits() {
890            return _bits;
891        }
892 
893        public InetAddress getHostAddress() {
894            return _address;
895        }
896 
897        @Override
898        public String getCanonicalName() {
899            return _canonicalName;
900        }
901    }
902 
903    private class StoreUnit extends Unit {
904        private StoreUnit(String name) {
905            super(name, STORE);
906        }
907    }
908 
909    private class ProtocolUnit extends Unit {
910        private String _protocol = null;
911        private int _version = -1;
912 
913        private ProtocolUnit(String name) {
914            super(name, PROTOCOL);
915            int pos = name.indexOf("/");
916            if ((pos < 0) || (pos == 0) || ((name.length() - 1) == pos))
917                throw new IllegalArgumentException(
918                        "Wrong format for protocol unit <protocol>/<version>");
919 
920            _protocol = name.substring(0, pos);
921            String version = name.substring(pos + 1);
922            try {
923                _version = version.equals("*") ? -1 : Integer.parseInt(version);
924            } catch (Exception ee) {
925                throw new IllegalArgumentException(
926                        "Wrong format : Protocol version must be * or numerical");
927            }
928        }
929 
930        @Override
931        public String getName() {
932            return _protocol + (_version > -1 ? ("/" + _version) : "/*");
933        }
934    }
935 
936    public void clear() {
937 
938        _psuWriteLock.lock();
939        try {
940            _netHandler.clear();
941            _pGroups.clear();
942            _pools.clear();
943            _links.clear();
944            _uGroups.clear();
945            _units.clear();
946            _linkGroups.clear();
947        } finally {
948            _psuWriteLock.unlock();
949        }
950 
951    }
952 
953    public void setActive(String poolName, boolean active) {
954        _psuWriteLock.lock();
955        try {
956            Pool pool = _pools.get(poolName);
957            if (pool != null)
958                pool.setActive(active);
959        } finally {
960            _psuWriteLock.unlock();
961        }
962        return;
963    }
964 
965    public long getActive(String poolName) {
966 
967        long active = 100000000L;
968        _psuReadLock.lock();
969        try {
970            Pool pool = _pools.get(poolName);
971            if (pool != null) {
972                active = pool.getActive();
973            }
974        } finally {
975            _psuReadLock.unlock();
976        }
977        return active;
978    }
979 
980    public void setEnabled(String poolName, boolean enabled) {
981        _psuWriteLock.lock();
982        try {
983            Pool pool = _pools.get(poolName);
984            if (pool != null)
985                pool.setEnabled(enabled);
986        } finally {
987            _psuWriteLock.unlock();
988        }
989        return;
990    }
991 
992    public boolean isEnabled(String poolName) {
993 
994        boolean isEnabled = false;
995        _psuReadLock.lock();
996        try {
997            Pool pool = _pools.get(poolName);
998            if (pool != null) {
999                isEnabled = pool.isEnabled();
1000            }
1001        } finally {
1002            _psuReadLock.unlock();
1003        }
1004        return isEnabled;
1005    }
1006 
1007    public SelectionPool getPool(String poolName) {
1008 
1009        SelectionPool pool = null;
1010 
1011        _psuReadLock.lock();
1012        try {
1013            pool = _pools.get(poolName);
1014        } finally {
1015            _psuReadLock.unlock();
1016        }
1017 
1018        return pool;
1019    }
1020 
1021    public SelectionPool getPool(String poolName, boolean create) {
1022        Pool pool = _pools.get(poolName);
1023        if ((pool != null) || !create)
1024            return pool;
1025 
1026        pool = new Pool(poolName);
1027 
1028        _psuReadLock.lock();
1029        try {
1030            _pools.put(pool.getName(), pool);
1031            PGroup group = _pGroups.get("default");
1032            if (group == null) {
1033                throw new IllegalArgumentException("Not found : " + "default");
1034            }
1035 
1036            //
1037            // shall we disallow more than one parent group ?
1038            //
1039            // if( pool._pGroupList.size() > 0 )
1040            // throw new
1041            // IllegalArgumentException( poolName +" already member" ) ;
1042 
1043            pool._pGroupList.put(group.getName(), group);
1044            group._poolList.put(pool.getName(), pool);
1045        } finally {
1046            _psuReadLock.unlock();
1047        }
1048        return pool;
1049    }
1050 
1051    public Map<String, Link> match(Map<String, Link> map, Unit unit,
1052            DirectionType ioType) {
1053 
1054        Map<String, Link> newmap = match(unit, null, ioType);
1055        if (map == null)
1056            return newmap;
1057 
1058        Map<String, Link> resultMap = new HashMap<String, Link>();
1059        for (Link link : map.values()) {
1060            if (newmap.get(link.getName()) != null)
1061                resultMap.put(link.getName(), link);
1062        }
1063        return resultMap;
1064    }
1065 
1066    private static class LinkMap {
1067        private class LinkMapEntry {
1068            private Link _link;
1069            private int _counter = 0;
1070 
1071            private LinkMapEntry(Link link) {
1072                _link = link;
1073                _counter = link._uGroupList.size() - 1;
1074            }
1075 
1076            private void touch() {
1077                _counter--;
1078            }
1079 
1080            private boolean isTriggered() {
1081                return _counter < 1;
1082            }
1083        }
1084 
1085        private Map<String, LinkMapEntry> _linkHash = new HashMap<String, LinkMapEntry>();
1086 
1087        private Iterator<Link> iterator() {
1088            List<Link> list = new ArrayList<Link>();
1089            for (LinkMapEntry e : _linkHash.values()) {
1090                if (e._counter <= 0)
1091                    list.add(e._link);
1092            }
1093            return list.iterator();
1094        }
1095 
1096        private void addLink(Link link) {
1097            LinkMapEntry found = _linkHash.get(link.getName());
1098            if (found == null) {
1099                _linkHash.put(link.getName(), new LinkMapEntry(link));
1100            } else {
1101                found._counter--;
1102            }
1103        }
1104    }
1105 
1106    private LinkMap match(LinkMap linkMap, Unit unit, LinkGroup linkGroup,
1107            DirectionType ioType) {
1108        Map<String, Link> map = match(unit, linkGroup, ioType);
1109        for (Link link : map.values())
1110            linkMap.addLink(link);
1111        return linkMap;
1112    }
1113 
1114    private static class LinkComparator implements Comparator<Link> {
1115        private final DirectionType _type;
1116 
1117        private LinkComparator(DirectionType type) {
1118            _type = type;
1119        }
1120 
1121        public int compare(Link link1, Link link2) {
1122 
1123            switch (_type) {
1124                case READ : // read
1125                    return link1._readPref == link2._readPref ? link1._name
1126                            .compareTo(link2._name)
1127                            : link1._readPref > link2._readPref ? -1 : 1;
1128                case CACHE: // cache
1129                    return link1._cachePref == link2._cachePref ? link1._name
1130                            .compareTo(link2._name)
1131                            : link1._cachePref > link2._cachePref ? -1 : 1;
1132                case WRITE: // write
1133                    return link1._writePref == link2._writePref ? link1._name
1134                            .compareTo(link2._name)
1135                            : link1._writePref > link2._writePref ? -1 : 1;
1136                case P2P: // p2p
1137                    int pref1 = link1._p2pPref < 0 ? link1._readPref
1138                            : link1._p2pPref;
1139                    int pref2 = link2._p2pPref < 0 ? link2._readPref
1140                            : link2._p2pPref;
1141                    return pref1 == pref2 ? link1._name.compareTo(link2._name)
1142                            : pref1 > pref2 ? -1 : 1;
1143            }
1144            throw new IllegalArgumentException("Wrong comparator mode");
1145        }
1146    }
1147 
1148    /**
1149     * @Guarded by _psuReadLock
1150     */
1151    @Override
1152    public PoolPreferenceLevel[] match(DirectionType type,  String netUnitName, String protocolUnitName,
1153            StorageInfo storageInfo, String linkGroupName) {
1154 
1155        String storeUnitName = storageInfo.getStorageClass()+"@"+storageInfo.getHsm();
1156        String dCacheUnitName = storageInfo.getCacheClass();
1157 
1158        _log.debug("running match: type={} store={} dCacheUnit={} net={} protocol={} SI={} linkGoup={}",
1159                new Object[]{
1160                    type,
1161                    storeUnitName,
1162                    dCacheUnitName,
1163                    netUnitName,
1164                    protocolUnitName,
1165                    storageInfo,
1166                    linkGroupName
1167                });
1168 
1169        Map<String, String> variableMap = (storageInfo == null ? null : storageInfo.getMap());
1170 
1171        PoolPreferenceLevel[] result = null;
1172        _psuReadLock.lock();
1173        try {
1174            //
1175            // resolve the unit from the unitname (or net unit mask)
1176            //
1177            // regexp code added by rw2 12/5/02
1178            // original code is in the else
1179            //
1180            List<Unit> list = new ArrayList<Unit>();
1181            if (storeUnitName != null) {
1182                if (_useRegex) {
1183                    Unit universalCoverage = null;
1184                    Unit classCoverage = null;
1185 
1186                    for (Unit unit : _units.values()) {
1187                        if (unit._type != STORE)
1188                            continue;
1189 
1190                        if (unit.getName().equals("*@*")) {
1191                            universalCoverage = unit;
1192                        } else if (unit.getName().equals("*@" + storeUnitName)) {
1193                            classCoverage = unit;
1194                        } else {
1195                            if (Pattern.matches(unit.getName(), storeUnitName)) {
1196                                list.add(unit);
1197                                break;
1198                            }
1199                        }
1200                    }
1201                    //
1202                    // If a pattern matches then use it, fail over to a class,
1203                    // then universal. If nothing, throw exception
1204                    //
1205                    if (list.size() == 0) {
1206                        if (classCoverage != null) {
1207                            list.add(classCoverage);
1208                        } else if (universalCoverage != null) {
1209                            list.add(universalCoverage);
1210                        } else {
1211                            throw new IllegalArgumentException(
1212                                    "Unit not found : " + storeUnitName);
1213                        }
1214                    }
1215 
1216                } else {
1217                    Unit unit = _units.get(storeUnitName);
1218                    if (unit == null) {
1219                        int ind = storeUnitName.lastIndexOf("@");
1220                        if ((ind > 0) && (ind < (storeUnitName.length() - 1))) {
1221                            String template = "*@"
1222                                    + storeUnitName.substring(ind + 1);
1223                            if ((unit = _units.get(template)) == null) {
1224 
1225                                if ((unit = _units.get("*@*")) == null) {
1226                                    _log.debug("no matching storage unit found for: {}", storeUnitName);
1227                                    throw new IllegalArgumentException(
1228                                            "Unit not found : " + storeUnitName);
1229                                }
1230                            }
1231                        } else {
1232                            throw new IllegalArgumentException(
1233                                    "IllegalUnitFormat : " + storeUnitName);
1234                        }
1235                    }
1236                    _log.debug("matching storage unit found for: {}", storeUnitName);
1237                    list.add(unit);
1238                }
1239            }
1240            if (protocolUnitName != null) {
1241 
1242                Unit unit = findProtocolUnit(protocolUnitName);
1243                //
1244                if (unit == null){
1245                    _log.debug("no matching protocol unit found for: {}", protocolUnitName);
1246                    throw new IllegalArgumentException("Unit not found : "
1247                            + protocolUnitName);
1248                }
1249                _log.debug("matching protocol unit found: {}", unit);
1250                list.add(unit);
1251            }
1252            if (dCacheUnitName != null) {
1253                Unit unit = _units.get(dCacheUnitName);
1254                if (unit == null) {
1255                    _log.debug("no matching dCache unit found for: {}", dCacheUnitName);
1256                    throw new IllegalArgumentException("Unit not found : "
1257                            + dCacheUnitName);
1258                }
1259                _log.debug("matching dCache unit found: {}", unit);
1260                list.add(unit);
1261            }
1262            if (netUnitName != null) {
1263                try {
1264                    Unit unit = _netHandler.match(netUnitName);
1265                    if (unit == null) {
1266                        _log.debug("no matching net unit found for: {}", netUnitName);
1267                        throw new IllegalArgumentException(
1268                                "Unit not matched : " + netUnitName);
1269                    }
1270                    _log.debug("matching net unit found: {}" + unit);
1271                    list.add(unit);
1272                } catch (UnknownHostException uhe) {
1273                    throw new IllegalArgumentException(
1274                            "NetUnit not resolved : " + netUnitName);
1275                }
1276            }
1277            // System.out.println("PSUDEBUG : list of units : "+list );
1278            //
1279            // match the requests ( logical AND )
1280            //
1281            //
1282            // Map map = null ;
1283            // while( units.hasNext() )map = match( map , (Unit)units.next() ) ;
1284            // Iterator links = map.values().iterator() ;
1285            //
1286 
1287            //
1288            // i) sort according to the type (read,write,cache)
1289            // ii) the and is only OK if we have at least as many
1290            // units (from the arguments) as required by the
1291            // number of uGroupList(s).
1292            // iii) check for the hashtable if required.
1293            //
1294            int fitCount = list.size();
1295            Set<Link> sortedSet = new TreeSet<Link>(new LinkComparator(type));
1296            // System.out.println("match: number of units to check="+fitCount);
1297 
1298            //
1299            // use subset on links if it's defined
1300            //
1301 
1302            LinkGroup linkGroup = null;
1303            if (linkGroupName != null) {
1304                linkGroup = _linkGroups.get(linkGroupName);
1305                if (linkGroup == null) {
1306                    _log.debug("LinkGroup not found : {}", linkGroupName );
1307                    throw new IllegalArgumentException("LinkGroup not found : "
1308                            + linkGroupName);
1309                }
1310            }
1311 
1312            //
1313            // find all links that matches the specified list of units
1314            //
1315 
1316            LinkMap matchingLinks = new LinkMap();
1317            for (Unit unit : list) {
1318                matchingLinks = match(matchingLinks, unit, linkGroup, type);
1319            }
1320 
1321            Iterator<Link> linkIterator = matchingLinks.iterator();
1322            while (linkIterator.hasNext()) {
1323 
1324                Link link = linkIterator.next();
1325                // System.out.println( "PSUDEBUG link : "+link.toString() ) ;
1326                if ((link._uGroupList.size() <= fitCount)
1327                        && ((variableMap == null) || link.exec(variableMap))) {
1328 
1329                    sortedSet.add(link);
1330                    // System.out.println( "PSUDEBUG added : "+link);
1331                }
1332            }
1333            int pref = -1;
1334            List<List<Link>> listList = new ArrayList<List<Link>>();
1335            List<Link> current = null;
1336 
1337            switch (type) {
1338 
1339                case READ:
1340                    for (Link link : sortedSet) {
1341                        if (link._readPref < 1) {
1342                            continue;
1343                        }
1344                        if (link._readPref != pref) {
1345                            listList.add(current = new ArrayList<Link>());
1346                            pref = link._readPref;
1347                        }
1348                        current.add(link);
1349                    }
1350                    break;
1351                case CACHE:
1352                    for (Link link : sortedSet) {
1353                        if (link._cachePref < 1) {
1354                            continue;
1355                        }
1356                        if (link._cachePref != pref) {
1357                            listList.add(current = new ArrayList<Link>());
1358                            pref = link._cachePref;
1359                        }
1360                        current.add(link);
1361                    }
1362                    break;
1363                case P2P:
1364                    for (Link link : sortedSet) {
1365                        int tmpPref = link._p2pPref < 0 ? link._readPref
1366                                : link._p2pPref;
1367                        if (tmpPref < 1) {
1368                            continue;
1369                        }
1370                        if (tmpPref != pref) {
1371                            listList.add(current = new ArrayList<Link>());
1372                            pref = tmpPref;
1373                        }
1374                        current.add(link);
1375                    }
1376                    break;
1377                case WRITE:
1378                    for (Link link : sortedSet) {
1379                        if (link._writePref < 1) {
1380                            continue;
1381                        }
1382                        if (link._writePref != pref) {
1383                            listList.add(current = new ArrayList<Link>());
1384                            pref = link._writePref;
1385                        }
1386                        current.add(link);
1387                    }
1388            }
1389            // System.out.println("PSUDEBUG : result list : "+listList);
1390            List<Link>[] x = listList.toArray(new List[listList.size()]);
1391            result = new PoolPreferenceLevel[x.length];
1392            //
1393            // resolve the links to the pools
1394            //
1395            for (int i = 0; i < x.length; i++) {
1396 
1397                List<Link> linkList = x[i];
1398                List<String> resultList = new ArrayList<String>();
1399                String tag = null;
1400 
1401                for (Link link : linkList) {
1402                    //
1403                    // get the link if available
1404                    //
1405                    if ((tag == null) && (link._tag != null))
1406                        tag = link._tag;
1407 
1408                    for (PoolCore poolCore : link._poolList.values()) {
1409                        if (poolCore instanceof Pool) {
1410                            Pool pool = (Pool) poolCore;
1411                            _log.debug("Pool: {} can read from tape? : {}", pool, pool.canReadFromTape());
1412                            if (((type == DirectionType.READ && pool.canRead())
1413                                 || (type == DirectionType.CACHE && pool.canReadFromTape()
1414                                     && poolCanStageFile(pool, storageInfo))
1415                                 || (type == DirectionType.WRITE && pool.canWrite())
1416                                 || (type == DirectionType.P2P && pool.canWriteForP2P()))
1417                                && (_allPoolsActive || pool.isActive())) {
1418                                resultList.add(pool.getName());
1419                            }
1420                        } else {
1421                            for (Pool pool : ((PGroup)poolCore)._poolList.values()) {
1422                                _log.debug("Pool: {} can read from tape? : {}", pool, pool.canReadFromTape());
1423                                if (((type == DirectionType.READ && pool.canRead())
1424                                     || (type == DirectionType.CACHE && pool.canReadFromTape()
1425                                         && poolCanStageFile(pool, storageInfo))
1426                                     || (type == DirectionType.WRITE && pool.canWrite())
1427                                     || (type == DirectionType.P2P && pool.canWriteForP2P()))
1428                                    && (_allPoolsActive || pool.isActive())) {
1429                                    resultList.add(pool.getName());
1430                                }
1431                            }
1432                        }
1433                    }
1434                }
1435                result[i] = new PoolPreferenceLevel(resultList, tag);
1436            }
1437 
1438        } finally {
1439            _psuReadLock.unlock();
1440        }
1441 
1442        if( _log.isDebugEnabled() ) {
1443 
1444            StringBuilder sb = new StringBuilder("match done: ");
1445 
1446            for( int i = 0; i < result.length; i++) {
1447                sb.append("[").append(i).append("] :");
1448                for(String poolName: result[i].getPoolList()) {
1449                    sb.append(" ").append(poolName);
1450                }
1451            }
1452            _log.debug(sb.toString());
1453        }
1454        return result;
1455    }
1456 
1457    public String getProtocolUnit(String protocolUnitName) {
1458        Unit unit = findProtocolUnit(protocolUnitName);
1459        return unit == null ? null : unit.getName();
1460    }
1461 
1462    //
1463    // Legal formats : <protocol>/<version>
1464    //
1465    private boolean _protocolsChecked = false;
1466 
1467    private void protocolConfig() {
1468        if (_protocolsChecked)
1469            return;
1470        _protocolsChecked = true;
1471        boolean found = false;
1472        for (Object o : _units.values()) {
1473            if (o instanceof ProtocolUnit) {
1474                found = true;
1475                break;
1476            }
1477        }
1478        if (!found)
1479            _units.put("*/*", new ProtocolUnit("*/*"));
1480    }
1481 
1482    public Unit findProtocolUnit(String protocolUnitName) {
1483        //
1484        if ((protocolUnitName == null) || (protocolUnitName.length() == 0))
1485            return null;
1486        //
1487        int position = protocolUnitName.indexOf('/');
1488        //
1489        //
1490        if ((position < 0) || (position == 0)
1491                || (position == (protocolUnitName.length() - 1))) {
1492 
1493            throw new IllegalArgumentException(
1494                    "Not a valid protocol specification : " + protocolUnitName);
1495        }
1496        //
1497        // we try :
1498        // <protocol>/<majorVersion>
1499        // <protocol>/*
1500        // */*
1501        //
1502        Unit unit = null;
1503        _psuReadLock.lock();
1504        try {
1505            unit = _units.get(protocolUnitName);
1506            if (unit != null) {
1507                return unit;
1508            }
1509 
1510            //
1511            //
1512            unit = _units.get(protocolUnitName.substring(0, position) + "/*");
1513            //
1514            if (unit == null) {
1515                unit = _units.get("*/*");
1516            }
1517 
1518        } finally {
1519            _psuReadLock.unlock();
1520        }
1521        //
1522        return unit;
1523 
1524    }
1525 
1526    public String getNetIdentifier(String address) throws UnknownHostException {
1527 
1528        _psuReadLock.lock();
1529        try {
1530            NetUnit unit = _netHandler.match(address);
1531            return unit.getCanonicalName();
1532        } finally {
1533            _psuReadLock.unlock();
1534        }
1535    }
1536 
1537    /**
1538     * Picks links associated with a unit (elementary rule).
1539     *
1540     * @param unit
1541     *            The unit as the matching criteria
1542     * @param linkGroup
1543     *            Use only subset of links if defined, or all associated links
1544     *            if not defined (null)
1545     * @return the matching links
1546     */
1547    public Map<String, Link> match(Unit unit, LinkGroup linkGroup, DirectionType iotype) {
1548 
1549        Map<String, Link> map = new HashMap<String, Link>();
1550 
1551        _psuReadLock.lock();
1552        try {
1553            for (UGroup uGroup : unit._uGroupList.values()) {
1554                for (Link link : uGroup._linkList.values()) {
1555 
1556                    if (linkGroup == null) {
1557                        if (iotype == DirectionType.READ
1558                                || link.getLinkGroup() == null) {
1559                            //
1560                            // no link group specified
1561                            // only consider link if it isn't in any link group
1562                            // ( "default link group" )
1563                            //
1564                            _log.debug("link {} matching to unit {}", link.getName(), unit);
1565                            map.put(link.getName(), link);
1566                        }
1567                    } else if (linkGroup.contains(link)) {
1568                        //
1569                        // only take link if it is in the specified link group
1570                        //
1571                        _log.debug("link {} matching to unit {}", link.getName(), unit);
1572                        map.put(link.getName(), link);
1573                    }
1574                }
1575            }
1576        } finally {
1577            _psuReadLock.unlock();
1578        }
1579        return map;
1580    }
1581 
1582    public String hh_psu_set_allpoolsactive = "on|off";
1583 
1584    public String ac_psu_set_allpoolsactive_$_1(Args args) throws CommandSyntaxException {
1585 
1586        String mode = args.argv(0);
1587 
1588        _psuWriteLock.lock();
1589        try {
1590            if (mode.equals("on") || mode.equals("true")) {
1591                _allPoolsActive = true;
1592            } else if (mode.equals("off") || mode.equals("false")) {
1593                _allPoolsActive = false;
1594            } else {
1595                throw new CommandSyntaxException("Syntax error");
1596            }
1597        } finally {
1598            _psuWriteLock.unlock();
1599        }
1600 
1601        return "";
1602    }
1603 
1604    public String hh_psu_netmatch = "<hostAddress>";
1605 
1606    public String ac_psu_netmatch_$_1(Args args) throws UnknownHostException {
1607 
1608        NetUnit unit = null;
1609 
1610        _psuReadLock.lock();
1611        try {
1612            unit = _netHandler.match(args.argv(0));
1613        } finally {
1614            _psuReadLock.unlock();
1615        }
1616        if (unit == null)
1617            throw new IllegalArgumentException("Host not a unit : "
1618                    + args.argv(0));
1619        return unit.toString();
1620    }
1621 
1622    public String hh_psu_match = "[-linkGroup=<linkGroup>] read|cache|write|p2p <storeUnit>|* <dCacheUnit>|* <netUnit>|* <protocolUnit>|* ";
1623 
1624    public String ac_psu_match_$_5(Args args) throws Exception {
1625 
1626        try {
1627            long start = System.currentTimeMillis();
1628            StorageInfo si = GenericStorageInfo.valueOf(args.argv(1), args.argv(2));
1629 
1630            PoolPreferenceLevel[] list = match(args.argv(0).equals("*") ? DirectionType.ANY
1631                    : DirectionType.valueOf(args.argv(0).toUpperCase()),
1632                    args.argv(3).equals("*") ? null : args.argv(3), args
1633                            .argv(4).equals("*") ? null : args.argv(4), si,
1634                    args.getOpt("linkGroup"));
1635            start = System.currentTimeMillis() - start;
1636 
1637            StringBuilder sb = new StringBuilder();
1638            for (int i = 0; i < list.length; i++) {
1639                String tag = list[i].getTag();
1640                sb.append("Preference : ").append(i).append("\n");
1641                sb.append("       Tag : ").append(tag == null ? "NONE" : tag)
1642                        .append("\n");
1643                for (Iterator links = list[i].getPoolList().iterator(); links
1644                        .hasNext();) {
1645                    sb.append("  ").append(links.next().toString())
1646                            .append("\n");
1647                }
1648            }
1649            sb.append("(time used : ").append(start).append(" millis)\n");
1650            return sb.toString();
1651        } catch (Exception ee) {
1652            ee.printStackTrace();
1653            throw ee;
1654        }
1655    }
1656 
1657    public String hh_psu_match2 = "<unit> [...] [-net=<netUnit>}";
1658 
1659    public String ac_psu_match2_$_1_99(Args args) throws Exception {
1660        StringBuffer sb = new StringBuffer();
1661        Map<String, Link> map = null;
1662        int required = args.argc();
1663 
1664        _psuReadLock.lock();
1665        try {
1666            for (int i = 0; i < args.argc(); i++) {
1667                String unitName = args.argv(i);
1668                Unit unit = _units.get(unitName);
1669                if (unit == null)
1670                    throw new IllegalArgumentException("Unit not found : "
1671                            + unitName);
1672                // TODO:
1673                map = match(map, unit, DirectionType.READ);
1674            }
1675            String netUnitName = args.getOpt("net");
1676            if (netUnitName != null) {
1677                Unit unit = _netHandler.find(new NetUnit(netUnitName));
1678                if (unit == null)
1679                    throw new IllegalArgumentException(
1680                            "Unit not found in netList : " + netUnitName);
1681                // TODO:
1682                map = match(map, unit, DirectionType.READ);
1683            }
1684            for (Link link : map.values()) {
1685                if (link._uGroupList.size() != required)
1686                    continue;
1687                sb.append("Link : ").append(link.toString()).append("\n");
1688                Iterator<SelectionPool> pools = link.pools();
1689                while (pools.hasNext()) {
1690                    sb.append("    ").append(pools.next().getName()).append(
1691                            "\n");
1692                }
1693            }
1694 
1695        } finally {
1696            _psuReadLock.unlock();
1697        }
1698        return sb.toString();
1699    }
1700 
1701    // /////////////////////////////////////////////////////////////////////////////
1702    //
1703    // the CLI
1704    //
1705    // ..............................................................
1706    //
1707    // the create's
1708    //
1709    public String hh_psu_create_pgroup = "<pGroup>";
1710 
1711    public String ac_psu_create_pgroup_$_1(Args args) {
1712        String name = args.argv(0);
1713 
1714        _psuWriteLock.lock();
1715        try {
1716            if (_pGroups.get(name) != null)
1717                throw new IllegalArgumentException("Duplicated entry : " + name);
1718 
1719            PGroup group = new PGroup(name);
1720 
1721            _pGroups.put(group.getName(), group);
1722        } finally {
1723            _psuWriteLock.unlock();
1724        }
1725        return "";
1726    }
1727 
1728    public String hh_psu_set_regex = "on | off";
1729 
1730    public String ac_psu_set_regex_$_1(Args args) {
1731        String retVal;
1732        String onOff = args.argv(0);
1733        if (onOff.equals("on")) {
1734            _useRegex = true;
1735            retVal = "regex turned on";
1736        } else if (onOff.equals("off")) {
1737            _useRegex = false;
1738            retVal = "regex turned off";
1739        } else {
1740            throw new IllegalArgumentException(
1741                    "please set regex either on or off");
1742        }
1743        return retVal;
1744    }
1745 
1746    public String hh_psu_create_pool = "<pool> [-noping]";
1747 
1748    public String ac_psu_create_pool_$_1(Args args) {
1749        String name = args.argv(0);
1750 
1751        _psuWriteLock.lock();
1752        try {
1753            if (_pools.get(name) != null)
1754                throw new IllegalArgumentException("Duplicated entry : " + name);
1755 
1756            Pool pool = new Pool(name);
1757            if (args.getOpt("noping") != null)
1758                pool.setPing(false);
1759            if (args.getOpt("disabled") != null)
1760                pool.setEnabled(false);
1761            _pools.put(pool.getName(), pool);
1762        } finally {
1763            _psuWriteLock.unlock();
1764        }
1765        return "";
1766    }
1767 
1768    public String hh_psu_set_pool = "<poolName> enabled|disabled|ping|noping|rdonly|notrdonly";
1769 
1770    public String ac_psu_set_pool_$_2(Args args) throws Exception {
1771        String poolName = args.argv(0);
1772        String mode = args.argv(1);
1773 
1774        _psuWriteLock.lock();
1775        try {
1776            Pool pool = _pools.get(poolName);
1777            if (pool == null)
1778                throw new IllegalArgumentException("Not found : " + poolName);
1779 
1780            if (mode.equals("enabled")) {
1781                pool.setEnabled(true);
1782            } else if (mode.equals("disabled")) {
1783                pool.setEnabled(false);
1784            } else if (mode.equals("ping")) {
1785                pool.setPing(true);
1786            } else if (mode.equals("noping")) {
1787                pool.setPing(false);
1788            } else if (mode.equals("rdonly")) {
1789                pool.setReadOnly(true);
1790            } else if (mode.equals("notrdonly")) {
1791                pool.setReadOnly(false);
1792            } else {
1793                throw new IllegalArgumentException("mode not supported : "
1794                        + mode);
1795            }
1796 
1797        } finally {
1798            _psuWriteLock.unlock();
1799        }
1800        return "";
1801    }
1802 
1803    public String hh_psu_set_enabled = "<poolName>";
1804    public String hh_psu_set_disabled = "<poolName>";
1805 
1806    public String ac_psu_set_enabled_$_1(Args args) {
1807        setEnabled(args.argv(0), true);
1808        return "";
1809    }
1810 
1811    public String ac_psu_set_disabled_$_1(Args args) {
1812        setEnabled(args.argv(0), false);
1813        return "";
1814    }
1815 
1816    public String hh_psu_create_link = "<link> <uGroup> [...]";
1817 
1818    public String ac_psu_create_link_$_2_99(Args args) {
1819 
1820        String name = args.argv(0);
1821 
1822        _psuWriteLock.lock();
1823        try {
1824 
1825            if (_links.get(name) != null)
1826                throw new IllegalArgumentException("Duplicated entry : " + name);
1827 
1828            Link link = new Link(name);
1829            //
1830            // we have to check if all the ugroups really exists.
1831            // only after we know, that all exist we can
1832            // add ourselfs to the uGroupLinkList
1833            //
1834            for (int i = 1; i < args.argc(); i++) {
1835                String uGroupName = args.argv(i);
1836 
1837                UGroup uGroup = _uGroups.get(uGroupName);
1838                if (uGroup == null)
1839                    throw new IllegalArgumentException("uGroup not found : "
1840                            + uGroupName);
1841 
1842                link._uGroupList.put(uGroup.getName(), uGroup);
1843 
1844            }
1845            for (UGroup group : link._uGroupList.values()) {
1846                group._linkList.put(link.getName(), link);
1847            }
1848            _links.put(link.getName(), link);
1849 
1850        } finally {
1851            _psuWriteLock.unlock();
1852        }
1853        return "";
1854    }
1855 
1856    public String hh_psu_create_ugroup = "<uGroup>";
1857 
1858    public String ac_psu_create_ugroup_$_1(Args args) {
1859        String name = args.argv(0);
1860 
1861        _psuWriteLock.lock();
1862        try {
1863            if (_uGroups.get(name) != null)
1864                throw new IllegalArgumentException("Duplicated entry : " + name);
1865 
1866            UGroup group = new UGroup(name);
1867 
1868            _uGroups.put(group.getName(), group);
1869        } finally {
1870            _psuWriteLock.unlock();
1871        }
1872        return "";
1873    }
1874 
1875    public String hh_psu_create_unit = "<unit> -net|-store|-dcache";
1876 
1877    public String ac_psu_create_unit_$_1(Args args) throws UnknownHostException {
1878        String name = args.argv(0);
1879        Unit unit = null;
1880        _psuWriteLock.lock();
1881        try {
1882            if (args.getOpt("net") != null) {
1883                NetUnit net = new NetUnit(name);
1884                _netHandler.add(net);
1885                unit = net;
1886            } else if (args.getOpt("store") != null) {
1887                unit = new Unit(name, STORE);
1888            } else if (args.getOpt("dcache") != null) {
1889                unit = new Unit(name, DCACHE);
1890            } else if (args.getOpt("protocol") != null) {
1891                unit = new ProtocolUnit(name);
1892            }
1893            if (unit == null)
1894                throw new IllegalArgumentException(
1895                        "Unit type missing net/store/dcache/protocol");
1896 
1897            String canonicalName = name; // will use the input name
1898            if (_units.get(canonicalName) != null)
1899                throw new IllegalArgumentException("Duplicated entry : "
1900                        + canonicalName);
1901 
1902            _units.put(canonicalName, unit);
1903        } finally {
1904            _psuWriteLock.unlock();
1905        }
1906        return "";
1907 
1908    }
1909 
1910    public String hh_psu_create_linkGroup = "<group name> [-reset]";
1911 
1912    public String ac_psu_create_linkGroup_$_1(Args args) {
1913 
1914        String newGroupName = args.argv(0);
1915        boolean reset = (args.getOpt("reset") != null);
1916 
1917        _psuWriteLock.lock();
1918        try {
1919 
1920            if (_linkGroups.containsKey(newGroupName) && !reset) {
1921                throw new IllegalArgumentException(
1922                        "LinkGroup already exists : " + newGroupName);
1923            }
1924 
1925            LinkGroup newGroup = new LinkGroup(newGroupName);
1926            _linkGroups.put(newGroupName, newGroup);
1927        } finally {
1928            _psuWriteLock.unlock();
1929        }
1930 
1931        return "";
1932    }
1933 
1934    //
1935    // ..................................................................
1936    //
1937    // the 'psux ... ls'
1938    //
1939    public String hh_psux_ls_pool = "[<pool>]";
1940 
1941    public Object ac_psux_ls_pool_$_0_1(Args args) throws Exception {
1942 
1943        Object xlsResult = null;
1944        _psuReadLock.lock();
1945        try {
1946            if (args.argc() == 0) {
1947                xlsResult = _pools.keySet().toArray();
1948            } else {
1949                String poolName = args.argv(0);
1950                Pool pool = _pools.get(poolName);
1951                if (pool == null)
1952                    throw new IllegalArgumentException("Not found : "
1953                            + poolName);
1954 
1955                Object[] result = new Object[6];
1956                result[0] = poolName;
1957                result[1] = pool._pGroupList.keySet().toArray();
1958                result[2] = pool._linkList.keySet().toArray();
1959                result[3] = Boolean.valueOf(pool._enabled);
1960                result[4] = Long.valueOf(pool.getActive());
1961                result[5] = Boolean.valueOf(pool._rdOnly);
1962                xlsResult = result;
1963            }
1964        } finally {
1965            _psuReadLock.unlock();
1966        }
1967 
1968        return xlsResult;
1969    }
1970 
1971    public String hh_psux_ls_pgroup = "[<pgroup>]";
1972 
1973    public Object ac_psux_ls_pgroup_$_0_1(Args args) throws Exception {
1974 
1975        Object xlsResult = null;
1976        _psuReadLock.lock();
1977        try {
1978 
1979            if (args.argc() == 0) {
1980                xlsResult = _pGroups.keySet().toArray();
1981            } else {
1982 
1983                String groupName = args.argv(0);
1984                PGroup group = _pGroups.get(groupName);
1985                if (group == null)
1986                    throw new IllegalArgumentException("Not found : "
1987                            + groupName);
1988 
1989                Object[] result = new Object[3];
1990                result[0] = groupName;
1991                result[1] = group._poolList.keySet().toArray();
1992                result[2] = group._linkList.keySet().toArray();
1993                xlsResult = result;
1994            }
1995        } finally {
1996            _psuReadLock.unlock();
1997        }
1998 
1999        return xlsResult;
2000    }
2001 
2002    public String hh_psux_ls_unit = "[<unit>]";
2003 
2004    public Object ac_psux_ls_unit_$_0_1(Args args) throws Exception {
2005 
2006        Object xlsResult = null;
2007        _psuReadLock.lock();
2008        try {
2009 
2010            if (args.argc() == 0) {
2011                xlsResult = _units.keySet().toArray();
2012            } else {
2013                String unitName = args.argv(0);
2014                Unit unit = _units.get(unitName);
2015                if (unit == null)
2016                    throw new IllegalArgumentException("Not found : "
2017                            + unitName);
2018 
2019                Object[] result = new Object[3];
2020                result[0] = unitName;
2021                result[1] = unit._type == STORE ? "Store"
2022                        : unit._type == PROTOCOL ? "Protocol"
2023                                : unit._type == DCACHE ? "dCache"
2024                                        : unit._type == NET ? "Net" : "Unknown";
2025                result[2] = unit._uGroupList.keySet().toArray();
2026                xlsResult = result;
2027            }
2028        } finally {
2029            _psuReadLock.unlock();
2030        }
2031 
2032        return xlsResult;
2033    }
2034 
2035    public String hh_psux_ls_ugroup = "[<ugroup>]";
2036 
2037    public Object ac_psux_ls_ugroup_$_0_1(Args args) throws Exception {
2038 
2039        Object xlsResult = null;
2040        _psuReadLock.lock();
2041        try {
2042            if (args.argc() == 0) {
2043                xlsResult = _uGroups.keySet().toArray();
2044            } else {
2045                String groupName = args.argv(0);
2046                UGroup group = _uGroups.get(groupName);
2047                if (group == null)
2048                    throw new IllegalArgumentException("Not found : "
2049                            + groupName);
2050 
2051                Object[] result = new Object[3];
2052                result[0] = groupName;
2053                result[1] = group._unitList.keySet().toArray();
2054                result[2] = group._linkList.keySet().toArray();
2055                xlsResult = result;
2056            }
2057        } finally {
2058            _psuReadLock.unlock();
2059        }
2060 
2061        return xlsResult;
2062    }
2063 
2064    public String hh_psux_ls_link = "[<link>] [-x] [-resolve]";
2065 
2066    public Object ac_psux_ls_link_$_0_1(Args args) throws Exception {
2067 
2068        boolean resolve = args.getOpt("resolve") != null;
2069        Object xlsResult = null;
2070        _psuReadLock.lock();
2071        try {
2072 
2073            if (args.argc() == 0) {
2074                if (args.getOpt("x") == null) {
2075                    xlsResult = _links.keySet().toArray();
2076                } else {
2077                    List array = new ArrayList();
2078                    for (Link link : _links.values()) {
2079                        array.add(fillLinkProperties(link, resolve));
2080                    }
2081                    xlsResult = array;
2082                }
2083            } else {
2084                String linkName = args.argv(0);
2085                Link link = _links.get(linkName);
2086                if (link == null)
2087                    throw new IllegalArgumentException("Not found : "
2088                            + linkName);
2089 
2090                xlsResult = fillLinkProperties(link, resolve);
2091            }
2092        } finally {
2093            _psuReadLock.unlock();
2094        }
2095 
2096        return xlsResult;
2097    }
2098 
2099    private Object[] fillLinkProperties(Link link) {
2100        return fillLinkProperties(link, false);
2101    }
2102 
2103    private Object[] fillLinkProperties(Link link, boolean resolve) {
2104        List<String> pools = new ArrayList<String>();
2105        List<String> groups = new ArrayList<String>();
2106        for (PoolCore core : link._poolList.values()) {
2107            if (core instanceof Pool) {
2108                pools.add(core.getName());
2109            } else {
2110                groups.add(core.getName());
2111                if (!resolve)
2112                    continue;
2113                PGroup pg = (PGroup) core;
2114                if (pg._poolList == null)
2115                    continue;
2116                for (String poolName : pg._poolList.keySet()) {
2117                    pools.add(poolName);
2118                }
2119            }
2120        }
2121 
2122        Object[] result = new Object[resolve ? 13 : 9];
2123        result[0] = link._name;
2124        result[1] = Integer.valueOf(link._readPref);
2125        result[2] = Integer.valueOf(link._cachePref);
2126        result[3] = Integer.valueOf(link._writePref);
2127        result[4] = link._uGroupList.keySet().toArray();
2128        result[5] = pools.toArray();
2129        result[6] = groups.toArray();
2130        result[7] = Integer.valueOf(link._p2pPref);
2131        result[8] = link._tag;
2132 
2133        if ((!resolve) || (link._uGroupList == null))
2134            return result;
2135 
2136        List<String> net = new ArrayList<String>();
2137        List<String> protocol = new ArrayList<String>();
2138        List<String> dcache = new ArrayList<String>();
2139        List<String> store = new ArrayList<String>();
2140 
2141        for (UGroup ug : link._uGroupList.values()) {
2142            if (ug._unitList == null)
2143                continue;
2144            for (Unit unit : ug._unitList.values()) {
2145                switch (unit._type) {
2146                    case NET:
2147                        net.add(unit._name);
2148                        break;
2149                    case PROTOCOL:
2150                        protocol.add(unit._name);
2151                        break;
2152                    case DCACHE:
2153                        dcache.add(unit._name);
2154                        break;
2155                    case STORE:
2156                        store.add(unit._name);
2157                        break;
2158                }
2159            }
2160            result[9] = store.toArray();
2161            result[10] = net.toArray();
2162            result[11] = dcache.toArray();
2163            result[12] = protocol.toArray();
2164        }
2165 
2166        return result;
2167    }
2168 
2169    public String hh_psux_match = "[-linkGroup=<linkGroup>] read|cache|write <storeUnit> <dCacheUnit> <netUnit> <protocolUnit>";
2170 
2171    public Object ac_psux_match_$_5(Args args) throws Exception {
2172 
2173        StorageInfo si = GenericStorageInfo.valueOf(args.argv(1), args.argv(2));
2174 
2175        PoolPreferenceLevel[] list = match(DirectionType.valueOf(args.argv(0).toUpperCase()),
2176                args.argv(3).equals("*") ? null : args.argv(3),
2177                args.argv(4).equals("*") ? null : args.argv(4), si, args.getOpt("linkGroup"));
2178        return list;
2179    }
2180 
2181    // ..................................................................
2182    //
2183    // the 'ls'
2184    //
2185    public String hh_psu_ls_pool = "[-l] [-a] [<pool> [...]]";
2186 
2187    public String ac_psu_ls_pool_$_0_99(Args args) {
2188        StringBuffer sb = new StringBuffer();
2189        boolean more = args.getOpt("a") != null;
2190        boolean detail = (args.getOpt("l") != null) || more;
2191 
2192        _psuReadLock.lock();
2193        try {
2194            Iterator i = null;
2195            if (args.argc() == 0) {
2196                i = _pools.values().iterator();
2197            } else {
2198                ArrayList l = new ArrayList();
2199                for (int n = 0; n < args.argc(); n++) {
2200                    Object o = _pools.get(args.argv(n));
2201                    if (o != null)
2202                        l.add(o);
2203                }
2204                i = l.iterator();
2205            }
2206            while (i.hasNext()) {
2207                Pool pool = (Pool) i.next();
2208                if (!detail) {
2209                    sb.append(pool.getName()).append("\n");
2210                } else {
2211                    sb.append(pool.toString()).append("\n");
2212                    sb.append(" linkList   :\n");
2213                    Iterator i2 = pool._linkList.values().iterator();
2214                    while (i2.hasNext()) {
2215                        sb.append("   ").append(i2.next().toString()).append(
2216                                "\n");
2217                    }
2218                    if (more) {
2219                        sb.append(" pGroupList : \n");
2220                        for (PGroup group : pool._pGroupList.values()) {
2221                            sb.append("   ").append(group.toString()).append(
2222                                    "\n");
2223                        }
2224                    }
2225                }
2226            }
2227        } finally {
2228            _psuReadLock.unlock();
2229        }
2230        return sb.toString();
2231    }
2232 
2233    public String hh_psu_ls_pgroup = "[-l] [-a] [<pgroup> [...]]";
2234 
2235    public String ac_psu_ls_pgroup_$_0_99(Args args) {
2236        StringBuffer sb = new StringBuffer();
2237        boolean more = args.getOpt("a") != null;
2238        boolean detail = (args.getOpt("l") != null) || more;
2239 
2240        _psuReadLock.lock();
2241        try {
2242            Iterator i = null;
2243            if (args.argc() == 0) {
2244                i = _pGroups.values().iterator();
2245            } else {
2246                ArrayList l = new ArrayList();
2247                for (int n = 0; n < args.argc(); n++) {
2248                    Object o = _pGroups.get(args.argv(n));
2249                    if (o != null)
2250                        l.add(o);
2251                }
2252                i = l.iterator();
2253            }
2254            while (i.hasNext()) {
2255                PGroup group = (PGroup) i.next();
2256                sb.append(group.getName()).append("\n");
2257                if (detail) {
2258                    sb.append(" linkList :\n");
2259                    Iterator i2 = group._linkList.values().iterator();
2260                    while (i2.hasNext()) {
2261                        sb.append("   ").append(i2.next().toString()).append(
2262                                "\n");
2263                    }
2264                    sb.append(" poolList :\n");
2265                    for (Pool pool : group._poolList.values()) {
2266                        sb.append("   ").append(pool.toString()).append("\n");
2267                    }
2268                }
2269            }
2270        } finally {
2271            _psuReadLock.unlock();
2272        }
2273        return sb.toString();
2274    }
2275 
2276    public String hh_psu_ls_link = "[-l] [-a] [ <link> [...]]";
2277 
2278    public String ac_psu_ls_link_$_0_99(Args args) {
2279 
2280        StringBuffer sb = new StringBuffer();
2281        boolean more = args.getOpt("a") != null;
2282        boolean detail = (args.getOpt("l") != null) || more;
2283 
2284        _psuReadLock.lock();
2285        try {
2286            Iterator i = null;
2287            if (args.argc() == 0) {
2288                i = _links.values().iterator();
2289            } else {
2290                ArrayList l = new ArrayList();
2291                for (int n = 0; n < args.argc(); n++) {
2292                    Object o = _links.get(args.argv(n));
2293                    if (o != null)
2294                        l.add(o);
2295                }
2296                i = l.iterator();
2297            }
2298            while (i.hasNext()) {
2299                Link link = (Link) i.next();
2300                sb.append(link.getName()).append("\n");
2301                if (detail) {
2302                    sb.append(" readPref  : ").append(link._readPref).append(
2303                            "\n");
2304                    sb.append(" cachePref : ").append(link._cachePref).append(
2305                            "\n");
2306                    sb.append(" writePref : ").append(link._writePref).append(
2307                            "\n");
2308                    sb.append(" p2pPref   : ").append(link._p2pPref).append(
2309                            "\n");
2310                    sb.append(" section   : ").append(
2311                            link._tag == null ? "None" : link._tag)
2312                            .append("\n");
2313                    sb.append(" linkGroup : ").append(
2314                            link.getLinkGroup() == null ? "None" : link
2315                                    .getLinkGroup().getName()).append("\n");
2316                    sb.append(" UGroups :\n");
2317                    for (UGroup group : link._uGroupList.values()) {
2318                        sb.append("   ").append(group.toString()).append("\n");
2319                    }
2320                    if (more) {
2321                        sb.append(" poolList  :\n");
2322                        for (PoolCore core : link._poolList.values()) {
2323                            sb.append("   ").append(core.toString()).append(
2324                                    "\n");
2325                        }
2326                    }
2327                }
2328            }
2329        } finally {
2330            _psuReadLock.unlock();
2331        }
2332        return sb.toString();
2333    }
2334 
2335    public String hh_psu_ls_ugroup = "[-l] [-a] [<uGroup> [...]]";
2336 
2337    public String ac_psu_ls_ugroup_$_0_99(Args args) {
2338 
2339        StringBuffer sb = new StringBuffer();
2340        boolean more = args.getOpt("a") != null;
2341        boolean detail = (args.getOpt("l") != null) || more;
2342 
2343        _psuReadLock.lock();
2344        try {
2345            Iterator i = null;
2346            if (args.argc() == 0) {
2347                i = _uGroups.values().iterator();
2348            } else {
2349                ArrayList l = new ArrayList();
2350                for (int n = 0; n < args.argc(); n++) {
2351                    Object o = _uGroups.get(args.argv(n));
2352                    if (o != null)
2353                        l.add(o);
2354                }
2355                i = l.iterator();
2356            }
2357            while (i.hasNext()) {
2358                UGroup group = (UGroup) i.next();
2359                sb.append(group.getName()).append("\n");
2360                if (detail) {
2361                    sb.append(" unitList :\n");
2362                    for (Unit unit : group._unitList.values()) {
2363                        sb.append("   ").append(unit.toString()).append("\n");
2364                    }
2365                    if (more) {
2366                        sb.append(" linkList :\n");
2367                        for (Link link : group._linkList.values()) {
2368                            sb.append("   ").append(link.toString()).append(
2369                                    "\n");
2370                        }
2371                    }
2372                }
2373            }
2374        } finally {
2375            _psuReadLock.unlock();
2376        }
2377        return sb.toString();
2378    }
2379 
2380    public String hh_psu_ls_netunits = "";
2381 
2382    public String ac_psu_ls_netunits(Args args) {
2383        StringBuffer sb = new StringBuffer();
2384 
2385        _psuReadLock.lock();
2386        try {
2387            for (int i = 0; i < _netHandler._netList.length; i++) {
2388                Map<Long, NetUnit> map = _netHandler._netList[i];
2389                if (map == null)
2390                    continue;
2391                String stringMask = _netHandler.bitsToString(i);
2392                sb.append(stringMask).append("/").append(i).append("\n");
2393                for (NetUnit net : map.values()) {
2394                    sb.append("   ").append(net.getHostAddress().getHostName());
2395                    if (i > 0)
2396                        sb.append("/").append(stringMask);
2397                    sb.append("\n");
2398                }
2399 
2400            }
2401        } finally {
2402            _psuReadLock.unlock();
2403        }
2404        return sb.toString();
2405    }
2406 
2407    public String hh_psu_ls_unit = " [-a] [<unit> [...]]";
2408 
2409    public String ac_psu_ls_unit_$_0_99(Args args) {
2410        StringBuffer sb = new StringBuffer();
2411        boolean more = args.getOpt("a") != null;
2412        boolean detail = (args.getOpt("l") != null) || more;
2413 
2414        _psuReadLock.lock();
2415        try {
2416            Iterator i = null;
2417            if (args.argc() == 0) {
2418                i = _units.values().iterator();
2419            } else {
2420                ArrayList l = new ArrayList();
2421                for (int n = 0; n < args.argc(); n++) {
2422                    Object o = _units.get(args.argv(n));
2423                    if (o != null)
2424                        l.add(o);
2425                }
2426                i = l.iterator();
2427            }
2428            while (i.hasNext()) {
2429                Unit unit = (Unit) i.next();
2430                if (detail) {
2431                    sb.append(unit.toString()).append("\n");
2432                    if (more) {
2433                        sb.append(" uGroupList :\n");
2434                        for (UGroup group : unit._uGroupList.values()) {
2435                            sb.append("   ").append(group.toString()).append(
2436                                    "\n");
2437                        }
2438                    }
2439                } else {
2440                    sb.append(unit.getName()).append("\n");
2441                }
2442            }
2443        } finally {
2444            _psuReadLock.unlock();
2445        }
2446        return sb.toString();
2447    }
2448 
2449    public String hh_psu_ls_linkGroup = "[-l] [<link group1> ... <link groupN>]";
2450 
2451    public String ac_psu_ls_linkGroup_$_0_99(Args args) {
2452 
2453        StringBuilder sb = new StringBuilder();
2454 
2455        boolean isLongOutput = args.getOpt("l") != null;
2456        _psuReadLock.lock();
2457        try {
2458 
2459            if (args.argc() != 0) {
2460                int count = args.argc();
2461                for (int i = 0; i < count; i++) {
2462                    LinkGroup linkGroup = _linkGroups.get(args.argv(i));
2463                    if (linkGroup == null) {
2464                        throw new IllegalArgumentException(
2465                                "LinkGroup not found : " + args.argv(i));
2466                    }
2467 
2468                    if (isLongOutput) {
2469                        sb.append(linkGroup).append("\n");
2470                    } else {
2471                        sb.append(args.argv(i)).append("\n");
2472                    }
2473 
2474                }
2475 
2476            } else {
2477                Set<String> allGroups = _linkGroups.keySet();
2478                for (String groupName : allGroups) {
2479                    LinkGroup linkGroup = _linkGroups.get(groupName);
2480                    if (linkGroup == null) {
2481                        throw new IllegalArgumentException(
2482                                "LinkGroup not found : " + groupName);
2483                    }
2484 
2485                    if (isLongOutput) {
2486                        sb.append(linkGroup).append("\n");
2487                    } else {
2488                        sb.append(groupName).append("\n");
2489                    }
2490                }
2491            }
2492 
2493        } finally {
2494            _psuReadLock.unlock();
2495        }
2496 
2497        return sb.toString();
2498    }
2499 
2500    public String hh_psu_dump_setup = "";
2501    public String ac_psu_dump_setup(Args args)
2502    {
2503        StringWriter s = new StringWriter();
2504        printSetup(new PrintWriter(s));
2505        return s.toString();
2506    }
2507 
2508    //
2509    // .............................................................................
2510    //
2511    // the 'removes'
2512    //
2513    public String hh_psu_remove_unit = "<unit> [-net]";
2514 
2515    public String ac_psu_remove_unit_$_1(Args args) throws UnknownHostException {
2516        String unitName = args.argv(0);
2517 
2518        _psuWriteLock.lock();
2519        try {
2520            if (args.getOpt("net") != null) {
2521                NetUnit netUnit = _netHandler.find(new NetUnit(unitName));
2522                if (netUnit == null)
2523                    throw new IllegalArgumentException(
2524                            "Not found in netList : " + unitName);
2525                unitName = netUnit.getName();
2526            }
2527            Unit unit = _units.get(unitName);
2528            if (unit == null)
2529                throw new IllegalArgumentException("Unit not found : "
2530                        + unitName);
2531 
2532            if (unit instanceof NetUnit)
2533                _netHandler.remove((NetUnit) unit);
2534 
2535            for (UGroup group : unit._uGroupList.values()) {
2536                group._unitList.remove(unit.getCanonicalName());
2537            }
2538 
2539            _units.remove(unitName);
2540        } finally {
2541            _psuWriteLock.unlock();
2542        }
2543        return "";
2544    }
2545 
2546    public String hh_psu_remove_ugroup = "<uGroup>";
2547 
2548    public String ac_psu_remove_ugroup_$_1(Args args) {
2549        String groupName = args.argv(0);
2550 
2551        _psuWriteLock.lock();
2552        try {
2553            UGroup group = _uGroups.get(groupName);
2554            if (group == null)
2555                throw new IllegalArgumentException("UGroup not found : "
2556                        + groupName);
2557 
2558            if (group._unitList.size() > 0)
2559                throw new IllegalArgumentException("UGroup not empty : "
2560                        + groupName);
2561 
2562            if (group._linkList.size() > 0)
2563                throw new IllegalArgumentException(
2564                        "Still link(s) pointing to us : " + groupName);
2565 
2566            _uGroups.remove(groupName);
2567 
2568        } finally {
2569            _psuWriteLock.unlock();
2570        }
2571        return "";
2572    }
2573 
2574    public String hh_psu_remove_pgroup = "<pGroup>";
2575 
2576    public String ac_psu_remove_pgroup_$_1(Args args) {
2577        String name = args.argv(0);
2578 
2579        _psuWriteLock.lock();
2580        try {
2581            PGroup group = _pGroups.get(name);
2582            if (group == null)
2583                throw new IllegalArgumentException("PGroup not found : " + name);
2584 
2585            //
2586            // check if empty
2587            //
2588            if (group._poolList.size() != 0)
2589                throw new IllegalArgumentException("PGroup not empty : " + name);
2590            //
2591            // remove the links
2592            //
2593            PoolCore core = group;
2594            for (Link link : core._linkList.values()) {
2595                link._poolList.remove(core.getName());
2596            }
2597            //
2598            // remove from global
2599            //
2600            _pGroups.remove(name);
2601        } finally {
2602            _psuWriteLock.unlock();
2603        }
2604        return "";
2605    }
2606 
2607    public String hh_psu_remove_pool = "<pool>";
2608 
2609    public String ac_psu_remove_pool_$_1(Args args) {
2610        String name = args.argv(0);
2611 
2612        _psuWriteLock.lock();
2613        try {
2614            Pool pool = _pools.get(name);
2615            if (pool == null)
2616                throw new IllegalArgumentException("Pool not found : " + name);
2617            //
2618            // remove from groups
2619            //
2620            for (PGroup group : pool._pGroupList.values()) {
2621                group._poolList.remove(pool.getName());
2622            }
2623            //
2624            // remove the links
2625            //
2626            PoolCore core = pool;
2627            for (Link link : core._linkList.values()) {
2628                link._poolList.remove(core.getName());
2629            }
2630            //
2631            // remove from global
2632            //
2633            _pools.remove(name);
2634        } finally {
2635            _psuWriteLock.unlock();
2636        }
2637        return "";
2638    }
2639 
2640    public String hh_psu_removefrom_ugroup = "<uGroup> <unit> -net";
2641 
2642    public String ac_psu_removefrom_ugroup_$_2(Args args) throws UnknownHostException {
2643        String groupName = args.argv(0);
2644        String unitName = args.argv(1);
2645 
2646        _psuWriteLock.lock();
2647        try {
2648            UGroup group = _uGroups.get(groupName);
2649            if (group == null)
2650                throw new IllegalArgumentException("UGroup not found : "
2651                        + groupName);
2652 
2653            if (args.getOpt("net") != null) {
2654                NetUnit netUnit = _netHandler.find(new NetUnit(unitName));
2655                if (netUnit == null)
2656                    throw new IllegalArgumentException(
2657                            "Not found in netList : " + unitName);
2658                unitName = netUnit.getName();
2659            }
2660            Unit unit = _units.get(unitName);
2661            if (unit == null)
2662                throw new IllegalArgumentException("Unit not found : "
2663                        + unitName);
2664            String canonicalName = unit.getCanonicalName();
2665            if (group._unitList.get(canonicalName) == null)
2666                throw new IllegalArgumentException(unitName + " not member of "
2667                        + groupName);
2668 
2669            group._unitList.remove(canonicalName);
2670            unit._uGroupList.remove(groupName);
2671        } finally {
2672            _psuWriteLock.unlock();
2673        }
2674        return "";
2675    }
2676 
2677    public String hh_psu_removefrom_pgroup = "<pGroup> <pool>";
2678 
2679    public String ac_psu_removefrom_pgroup_$_2(Args args) {
2680        String groupName = args.argv(0);
2681        String poolName = args.argv(1);
2682 
2683        _psuWriteLock.lock();
2684        try {
2685            Pool pool = _pools.get(poolName);
2686            if (pool == null)
2687                throw new IllegalArgumentException("Pool not found : "
2688                        + poolName);
2689 
2690            PGroup group = _pGroups.get(groupName);
2691            if (group == null)
2692                throw new IllegalArgumentException("PGroup not found : "
2693                        + groupName);
2694 
2695            if (group._poolList.get(poolName) == null)
2696                throw new IllegalArgumentException(poolName + " not member of "
2697                        + groupName);
2698 
2699            group._poolList.remove(poolName);
2700            pool._pGroupList.remove(groupName);
2701        } finally {
2702            _psuWriteLock.unlock();
2703        }
2704        return "";
2705    }
2706 
2707    public String hh_psu_removefrom_linkGroup = "<linkGroup> <link>";
2708 
2709    public String ac_psu_removefrom_linkGroup_$_2(Args args) {
2710 
2711        String linkGroupName = args.argv(0);
2712        String linkName = args.argv(1);
2713 
2714        _psuWriteLock.lock();
2715        try {
2716 
2717            LinkGroup linkGroup = _linkGroups.get(linkGroupName);
2718            if (linkGroup == null) {
2719                throw new IllegalArgumentException("LinkGroup not found : "
2720                        + linkGroupName);
2721            }
2722 
2723            Link link = _links.get(linkName);
2724            if (link == null) {
2725                throw new IllegalArgumentException("Link is not found : "
2726                        + linkName);
2727            }
2728 
2729            if (!linkGroup.remove(link)) {
2730                throw new IllegalArgumentException("Link [" + linkName
2731                        + "] is not part of group : " + linkGroupName);
2732            }
2733            link.setLinkGroup(null);
2734 
2735        } finally {
2736            _psuWriteLock.unlock();
2737        }
2738 
2739        return "";
2740 
2741    }
2742 
2743    public String hh_psu_remove_linkGroup = "<linkGroup>";
2744 
2745    public String ac_psu_remove_linkGroup_$_1(Args args) {
2746 
2747        String linkGroupName = args.argv(0);
2748 
2749        _psuWriteLock.lock();
2750        try {
2751 
2752            LinkGroup linkGroup = _linkGroups.remove(linkGroupName);
2753            if (linkGroup == null) {
2754                throw new IllegalArgumentException("LinkGroup not found : "
2755                        + linkGroupName);
2756            }
2757 
2758            for (SelectionLink link : linkGroup.getAllLinks()) {
2759                if (link instanceof Link) {
2760                    ((Link) link).setLinkGroup(null);
2761                }
2762            }
2763 
2764        } finally {
2765            _psuWriteLock.unlock();
2766        }
2767 
2768        return "";
2769    }
2770 
2771    public String hh_psu_remove_link = "<link>";
2772 
2773    public String ac_psu_remove_link_$_1(Args args) {
2774        String name = args.argv(0);
2775 
2776        _psuWriteLock.lock();
2777        try {
2778            Link link = _links.get(name);
2779            if (link == null)
2780                throw new IllegalArgumentException("Link not found : " + name);
2781            //
2782            // remove from pools
2783            //
2784            for (PoolCore core : link._poolList.values()) {
2785                core._linkList.remove(name);
2786            }
2787            //
2788            // remove from unit group
2789            //
2790            for (UGroup group : link._uGroupList.values()) {
2791                group._linkList.remove(name);
2792            }
2793            //
2794            // remove from link group. A link can be in exactly one linkgroup at
2795            // the same time.
2796            //
2797            for (LinkGroup lGroup : _linkGroups.values()) {
2798                if (lGroup.remove(link)) {
2799                    break;
2800                }
2801            }
2802            //
2803            // remove from global
2804            //
2805            _links.remove(name);
2806        } finally {
2807            _psuWriteLock.unlock();
2808        }
2809        return "";
2810    }
2811 
2812    //
2813    // ........................................................................
2814    //
2815    // relations
2816    //
2817    public String hh_psu_addto_pgroup = "<pGroup> <pool>";
2818 
2819    public String ac_psu_addto_pgroup_$_2(Args args) {
2820        String pGroupName = args.argv(0);
2821        String poolName = args.argv(1);
2822 
2823        // no lock required, while method does it
2824        addtoPoolGroup(pGroupName, poolName);
2825 
2826        return "";
2827    }
2828 
2829    private void addtoPoolGroup(String pGroupName, String poolName) throws IllegalArgumentException {
2830 
2831        _psuWriteLock.lock();
2832        try {
2833            PGroup group = _pGroups.get(pGroupName);
2834            if (group == null)
2835                throw new IllegalArgumentException("Not found : " + pGroupName);
2836            Pool pool = _pools.get(poolName);
2837            if (pool == null)
2838                throw new IllegalArgumentException("Not found : " + poolName);
2839            //
2840            // shall we disallow more than one parent group ?
2841            //
2842            // if( pool._pGroupList.size() > 0 )
2843            // throw new
2844            // IllegalArgumentException( poolName +" already member" ) ;
2845 
2846            pool._pGroupList.put(group.getName(), group);
2847            group._poolList.put(pool.getName(), pool);
2848        } finally {
2849            _psuWriteLock.unlock();
2850        }
2851        return;
2852    }
2853 
2854    public String hh_psu_addto_ugroup = "<uGroup> <unit>";
2855 
2856    public String ac_psu_addto_ugroup_$_2(Args args) throws UnknownHostException {
2857 
2858        String uGroupName = args.argv(0);
2859        String unitName = args.argv(1);
2860 
2861        _psuWriteLock.lock();
2862        try {
2863            if (args.getOpt("net") != null) {
2864                NetUnit netUnit = _netHandler.find(new NetUnit(unitName));
2865                if (netUnit == null)
2866                    throw new IllegalArgumentException(
2867                            "Not found in netList : " + unitName);
2868                unitName = netUnit.getName();
2869            }
2870            UGroup group = _uGroups.get(uGroupName);
2871            if (group == null)
2872                throw new IllegalArgumentException("Not found : " + uGroupName);
2873            Unit unit = _units.get(unitName);
2874            if (unit == null)
2875                throw new IllegalArgumentException("Not found : " + unitName);
2876 
2877            String canonicalName = unit.getCanonicalName();
2878            if (group._unitList.get(canonicalName) != null)
2879                throw new IllegalArgumentException(unitName
2880                        + " already member of " + uGroupName);
2881 
2882            unit._uGroupList.put(group.getName(), group);
2883            group._unitList.put(canonicalName, unit);
2884        } finally {
2885            _psuWriteLock.unlock();
2886        }
2887        return "";
2888    }
2889 
2890    public String hh_psu_addto_linkGroup = "<linkGroup> <link>";
2891 
2892    public String ac_psu_addto_linkGroup_$_2(Args args) {
2893        String linkGroupName = args.argv(0);
2894        String linkName = args.argv(1);
2895 
2896        _psuWriteLock.lock();
2897        try {
2898 
2899            LinkGroup linkGroup = _linkGroups.get(linkGroupName);
2900            if (linkGroup == null) {
2901                throw new IllegalArgumentException("LinkGroup not found : "
2902                        + linkName);
2903            }
2904 
2905            Link link = _links.get(linkName);
2906            if (link == null) {
2907                throw new IllegalArgumentException("Link not found : "
2908                        + linkName);
2909            }
2910 
2911            // search all linkgroups for this link
2912            // a link can be only in one link group at the same time
2913            for (LinkGroup group : _linkGroups.values()) {
2914                if (group.contains(link)) {
2915                    throw new IllegalArgumentException(
2916                            "Link already in linkGroup `" + group.getName()
2917                                    + "`");
2918                }
2919            }
2920 
2921            linkGroup.add(link);
2922            link.setLinkGroup(linkGroup);
2923        } finally {
2924            _psuWriteLock.unlock();
2925        }
2926 
2927        return "";
2928    }
2929 
2930    public String hh_psu_unlink = "<link> <pool>|<pGroup>";
2931 
2932    public String ac_psu_unlink_$_2(Args args) {
2933        String linkName = args.argv(0);
2934        String poolName = args.argv(1);
2935 
2936        _psuWriteLock.lock();
2937        try {
2938            Link link = _links.get(linkName);
2939            if (link == null)
2940                throw new IllegalArgumentException("Not found : " + linkName);
2941 
2942            PoolCore core = _pools.get(poolName);
2943            if (core == null)
2944                core = _pGroups.get(poolName);
2945            if (core == null)
2946                throw new IllegalArgumentException("Not found : " + poolName);
2947 
2948            if (core._linkList.get(linkName) == null)
2949                throw new IllegalArgumentException(poolName + " not member of "
2950                        + linkName);
2951 
2952            core._linkList.remove(linkName);
2953            link._poolList.remove(poolName);
2954        } finally {
2955            _psuWriteLock.unlock();
2956        }
2957 
2958        return "";
2959    }
2960 
2961    public String hh_psu_add_link = "<link> <pool>|<pGroup>";
2962 
2963    public String ac_psu_add_link_$_2(Args args) {
2964        String linkName = args.argv(0);
2965        String poolName = args.argv(1);
2966 
2967        _psuWriteLock.lock();
2968        try {
2969            Link link = _links.get(linkName);
2970            if (link == null)
2971                throw new IllegalArgumentException("Not found : " + linkName);
2972 
2973            PoolCore core = _pools.get(poolName);
2974            if (core == null)
2975                core = _pGroups.get(poolName);
2976            if (core == null)
2977                throw new IllegalArgumentException("Not found : " + poolName);
2978 
2979            core._linkList.put(link.getName(), link);
2980            link._poolList.put(core.getName(), core);
2981        } finally {
2982            _psuWriteLock.unlock();
2983        }
2984 
2985        return "";
2986    }
2987 
2988    public String hh_psu_set_active = "<poolName>|* [-no]";
2989 
2990    public String ac_psu_set_active_$_1(Args args) {
2991        String poolName = args.argv(0);
2992        boolean active = args.getOpt("no") == null;
2993 
2994        _psuWriteLock.lock();
2995 
2996        try {
2997            if (poolName.equals("*")) {
2998                for (Pool pool : _pools.values()) {
2999                    pool.setActive(active);
3000                }
3001            } else {
3002                Pool pool = _pools.get(poolName);
3003                if (pool == null)
3004                    throw new IllegalArgumentException("Pool not found : "
3005                            + poolName);
3006                pool.setActive(active);
3007            }
3008 
3009        } finally {
3010            _psuWriteLock.unlock();
3011        }
3012        return "";
3013    }
3014 
3015    public String hh_psu_set_link = "<link> [-readpref=<readpref>] [-writepref=<writepref>] [-cachepref=<cachepref>] [-p2ppref=<p2ppref>] [-section=<section>|NONE]";
3016 
3017    public String ac_psu_set_link_$_1(Args args) {
3018 
3019        String linkName = args.argv(0);
3020        _psuWriteLock.lock();
3021 
3022        try {
3023            Link link = _links.get(linkName);
3024            if (link == null)
3025                throw new IllegalArgumentException("Not found : " + linkName);
3026 
3027            String tmp = args.getOpt("readpref");
3028            if (tmp != null)
3029                link._readPref = Integer.parseInt(tmp);
3030            tmp = args.getOpt("cachepref");
3031            if (tmp != null)
3032                link._cachePref = Integer.parseInt(tmp);
3033            tmp = args.getOpt("writepref");
3034            if (tmp != null)
3035                link._writePref = Integer.parseInt(tmp);
3036            tmp = args.getOpt("p2ppref");
3037            if (tmp != null)
3038                link._p2pPref = Integer.parseInt(tmp);
3039            tmp = args.getOpt("section");
3040            if (tmp != null)
3041                link._tag = tmp.equals("NONE") ? null : tmp;
3042 
3043        } finally {
3044            _psuWriteLock.unlock();
3045        }
3046        return "";
3047    }
3048 
3049    public String hh_psu_set_linkGroup_attribute = "<linkGroup> [-r] attribute=value";
3050 
3051    public String ac_psu_set_linkGroup_attribute_$_2(Args args) {
3052 
3053        _psuWriteLock.lock();
3054 
3055        try {
3056            String linkGroupName = args.argv(0);
3057 
3058            LinkGroup linkGroup = _linkGroups.get(linkGroupName);
3059            if (linkGroup == null) {
3060                throw new IllegalArgumentException("LinkGroup not found : "
3061                        + linkGroupName);
3062            }
3063 
3064            String[] attrKeyValue = args.argv(1).split("=");
3065 
3066            if (attrKeyValue.length == 1 || attrKeyValue[1] == null
3067                    || attrKeyValue[1].length() == 0) {
3068                return "bad value";
3069            }
3070 
3071            linkGroup.attribute(attrKeyValue[0], attrKeyValue[1], args
3072                    .getOpt("r") != null);
3073 
3074        } finally {
3075            _psuWriteLock.unlock();
3076        }
3077        return "";
3078    }
3079 
3080    public String hh_psu_remove_linkGroup_attribute = "<linkGroup> attribute=value";
3081 
3082    public String ac_psu_remove_linkGroup_attribute_$_2(Args args) {
3083 
3084        _psuWriteLock.lock();
3085 
3086        try {
3087            String linkGroupName = args.argv(0);
3088 
3089            LinkGroup linkGroup = _linkGroups.get(linkGroupName);
3090            if (linkGroup == null) {
3091                throw new IllegalArgumentException("LinkGroup not found : "
3092                        + linkGroupName);
3093            }
3094 
3095            String[] attrKeyValue = args.argv(1).split("=");
3096 
3097            if (attrKeyValue.length == 1 || attrKeyValue[1] == null
3098                    || attrKeyValue[1].length() == 0) {
3099                return "bad value";
3100            }
3101            // remove
3102            linkGroup.removeAttribute(attrKeyValue[0], attrKeyValue[1]);
3103 
3104        } finally {
3105            _psuWriteLock.unlock();
3106        }
3107        return "";
3108    }
3109 
3110    public String hh_psu_set_linkGroup_custodialAllowed = "<linkGroup> <true|false>";
3111 
3112    public String ac_psu_set_linkGroup_custodialAllowed_$_2(Args args) {
3113 
3114        _psuWriteLock.lock();
3115 
3116        try {
3117            String linkGroupName = args.argv(0);
3118 
3119            LinkGroup linkGroup = _linkGroups.get(linkGroupName);
3120            if (linkGroup == null) {
3121                throw new IllegalArgumentException("LinkGroup not found : "
3122                        + linkGroupName);
3123            }
3124 
3125            linkGroup.setCustodialAllowed(Boolean.parseBoolean(args.argv(1)));
3126 
3127        } finally {
3128            _psuWriteLock.unlock();
3129        }
3130        return "";
3131    }
3132 
3133    public String hh_psu_set_linkGroup_outputAllowed = "<linkGroup> <true|false>";
3134 
3135    public String ac_psu_set_linkGroup_outputAllowed_$_2(Args args) {
3136 
3137        _psuWriteLock.lock();
3138 
3139        try {
3140            String linkGroupName = args.argv(0);
3141 
3142            LinkGroup linkGroup = _linkGroups.get(linkGroupName);
3143            if (linkGroup == null) {
3144                throw new IllegalArgumentException("LinkGroup not found : "
3145                        + linkGroupName);
3146            }
3147 
3148            linkGroup.setOutputAllowed(Boolean.parseBoolean(args.argv(1)));
3149 
3150        } finally {
3151            _psuWriteLock.unlock();
3152        }
3153        return "";
3154    }
3155 
3156    public String hh_psu_set_linkGroup_replicaAllowed = "<linkGroup> <true|false>";
3157 
3158    public String ac_psu_set_linkGroup_replicaAllowed_$_2(Args args) {
3159 
3160        _psuWriteLock.lock();
3161 
3162        try {
3163            String linkGroupName = args.argv(0);
3164 
3165            LinkGroup linkGroup = _linkGroups.get(linkGroupName);
3166            if (linkGroup == null) {
3167                throw new IllegalArgumentException("LinkGroup not found : "
3168                        + linkGroupName);
3169            }
3170 
3171            linkGroup.setReplicaAllowed(Boolean.parseBoolean(args.argv(1)));
3172 
3173        } finally {
3174            _psuWriteLock.unlock();
3175        }
3176        return "";
3177    }
3178 
3179    public String hh_psu_set_linkGroup_onlineAllowed = "<linkGroup> <true|false>";
3180 
3181    public String ac_psu_set_linkGroup_onlineAllowed_$_2(Args args) {
3182 
3183        _psuWriteLock.lock();
3184 
3185        try {
3186            String linkGroupName = args.argv(0);
3187 
3188            LinkGroup linkGroup = _linkGroups.get(linkGroupName);
3189            if (linkGroup == null) {
3190                throw new IllegalArgumentException("LinkGroup not found : "
3191                        + linkGroupName);
3192            }
3193 
3194            linkGroup.setOnlineAllowed(Boolean.parseBoolean(args.argv(1)));
3195 
3196        } finally {
3197            _psuWriteLock.unlock();
3198        }
3199        return "";
3200    }
3201 
3202    public String hh_psu_set_linkGroup_nearlineAllowed = "<linkGroup> <true|false>";
3203 
3204    public String ac_psu_set_linkGroup_nearlineAllowed_$_2(Args args) {
3205 
3206        _psuWriteLock.lock();
3207 
3208        try {
3209            String linkGroupName = args.argv(0);
3210 
3211            LinkGroup linkGroup = _linkGroups.get(linkGroupName);
3212            if (linkGroup == null) {
3213                throw new IllegalArgumentException("LinkGroup not found : "
3214                        + linkGroupName);
3215            }
3216 
3217            linkGroup.setNearlineAllowed(Boolean.parseBoolean(args.argv(1)));
3218 
3219        } finally {
3220            _psuWriteLock.unlock();
3221        }
3222        return "";
3223    }
3224 
3225    public String hh_psu_clear_im_really_sure = "# don't use this command";
3226 
3227    public String ac_psu_clear_im_really_sure(Args args) {
3228        // no lock required, while method does it
3229        clear();
3230        return "Voila, now everthing is really gone";
3231    }
3232 
3233    public String[] getLinkGroups() {
3234 
3235        String[] linkGroups;
3236        _psuReadLock.lock();
3237        try {
3238            linkGroups = _linkGroups.keySet().toArray(
3239                    new String[_linkGroups.size()]);
3240        } finally {
3241            _psuReadLock.unlock();
3242        }
3243 
3244        return linkGroups;
3245    }
3246 
3247    public LinkGroup getLinkGroupByName(String linkGroupName) {
3248        LinkGroup linkGroup = null;
3249        _psuReadLock.lock();
3250        try {
3251            linkGroup = _linkGroups.get(linkGroupName);
3252        } finally {
3253            _psuReadLock.unlock();
3254        }
3255        return linkGroup;
3256    }
3257 
3258    public String[] getLinksByGroupName(String linkGroupName) throws NoSuchElementException {
3259 
3260        String[] linkNames = null;
3261 
3262        _psuReadLock.lock();
3263        try {
3264            LinkGroup linkGroup = _linkGroups.get(linkGroupName);
3265            if (linkGroup == null) {
3266                throw new NoSuchElementException("LinkGroup not found : "
3267                        + linkGroupName);
3268            }
3269 
3270            Collection<SelectionLink> links = linkGroup.links();
3271            int count = links.size();
3272            linkNames = new String[count];
3273            int j = 0;
3274            for (SelectionLink link : links) {
3275                linkNames[j++] = link.getName();
3276            }
3277        } finally {
3278            _psuReadLock.unlock();
3279        }
3280 
3281        return linkNames;
3282    }
3283 
3284    /**
3285     * Returns true if and only if the pool can stage the given file. That is
3286     * the only case if the file is located on an HSM connected to the pool.
3287     */
3288    private boolean poolCanStageFile(Pool pool, StorageInfo file) {
3289        boolean rc  = false;
3290        if (file.locations().isEmpty()
3291                && pool.getHsmInstances().contains(file.getHsm())) {
3292            // This is for backwards compatibility until all info
3293            // extractors support URIs.
3294            rc = true;
3295        } else {
3296            for (URI uri : file.locations()) {
3297                if (pool.getHsmInstances().contains(uri.getAuthority())) {
3298                    rc = true;
3299                }
3300            }
3301        }
3302        _log.debug("{}: matching hsm ({}) found?: {}",
3303                new Object[]{
3304                    pool.getName(),
3305                    file.getHsm(),
3306                    rc
3307                });
3308        return rc;
3309    }
3310 
3311    public Collection<SelectionPool> getPoolsByPoolGroup(String poolGroup)
3312        throws NoSuchElementException
3313    {
3314        PGroup group = _pGroups.get(poolGroup);
3315        if (group == null)
3316            throw new NoSuchElementException("No such pool group: " + poolGroup);
3317 
3318        List<SelectionPool> pools = new ArrayList(group._poolList.size());
3319        for (Pool pool: group._poolList.values()) {
3320            pools.add(pool);
3321        }
3322 
3323        return pools;
3324    }
3325}

[all classes][diskCacheV111.poolManager]
EMMA 2.0.5312 (C) Vladimir Roubtsov