| 1 |
<?php |
| 2 |
/* Copyright (C) 2017 Icinga Development Team <[email protected]> */ |
| 3 |
|
| 4 |
namespace Icinga\Module\Toplevelview\Tree; |
| 5 |
|
| 6 |
use Icinga\Module\Toplevelview\Util\Str; |
| 7 |
|
| 8 |
use Icinga\Application\Benchmark; |
| 9 |
use Icinga\Data\Tree\TreeNode; |
| 10 |
use Icinga\Exception\ConfigurationError; |
| 11 |
use Icinga\Exception\NotImplementedError; |
| 12 |
use Icinga\Exception\ProgrammingError; |
| 13 |
|
| 14 |
/** |
| 15 |
* TLVTreeNode represents a node in the TLV tree |
| 16 |
*/ |
| 17 |
class TLVTreeNode extends TreeNode |
| 18 |
{ |
| 19 |
/** |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
protected $type = 'node'; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
protected $key = null; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var TLVTree |
| 31 |
*/ |
| 32 |
protected $root; |
| 33 |
|
| 34 |
/** |
| 35 |
* @var TLVTreeNode |
| 36 |
*/ |
| 37 |
protected $parent; |
| 38 |
|
| 39 |
/** |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
protected $fullId; |
| 43 |
|
| 44 |
/** |
| 45 |
* @var TLVStatus |
| 46 |
*/ |
| 47 |
protected $status; |
| 48 |
|
| 49 |
/** |
| 50 |
* @var array |
| 51 |
*/ |
| 52 |
protected $properties = array(); |
| 53 |
|
| 54 |
/** |
| 55 |
* Determine if this node is a leaf |
| 56 |
* |
| 57 |
* @var bool |
| 58 |
*/ |
| 59 |
protected static $canHaveChildren = true; |
| 60 |
|
| 61 |
/** |
| 62 |
* The key which represents the display title |
| 63 |
* |
| 64 |
* @var string |
| 65 |
*/ |
| 66 |
protected static $titleKey = 'name'; |
| 67 |
|
| 68 |
/** |
| 69 |
* Mapping types to its implementation class |
| 70 |
* |
| 71 |
* @var array |
| 72 |
*/ |
| 73 |
protected static $typeMap = [ |
| 74 |
'host' => 'Icinga\\Module\\Toplevelview\\Tree\\TLVHostNode', |
| 75 |
'service' => 'Icinga\\Module\\Toplevelview\\Tree\\TLVServiceNode', |
| 76 |
'hostgroup' => 'Icinga\\Module\\Toplevelview\\Tree\\TLVHostGroupNode', |
| 77 |
'servicegroup' => 'Icinga\\Module\\Toplevelview\\Tree\\TLVServiceGroupNode', |
| 78 |
]; |
| 79 |
|
| 80 |
/** |
| 81 |
* Mapping keys to a type |
| 82 |
* |
| 83 |
* Warning: order is important when keys overlap! |
| 84 |
* |
| 85 |
* @var array |
| 86 |
*/ |
| 87 |
protected static $typeKeyMap = [ |
| 88 |
'service' => ['host', 'service'], |
| 89 |
'host' => 'host', |
| 90 |
'hostgroup' => 'hostgroup', |
| 91 |
'servicegroup' => 'servicegroup', |
| 92 |
]; |
| 93 |
|
| 94 |
/** |
| 95 |
* @param $array |
| 96 |
* @param TLVTreeNode|null $parent |
| 97 |
* @param TLVTree|null $root |
| 98 |
* |
| 99 |
* @return static |
| 100 |
* |
| 101 |
* @throws NotImplementedError |
| 102 |
* @throws ProgrammingError |
| 103 |
*/ |
| 104 |
public static function fromArray($array, ?TLVTreeNode $parent = null, ?TLVTree $root = null) |
| 105 |
{ |
| 106 |
if ($root === null) { |
| 107 |
Benchmark::measure('Begin loading TLVTree from array'); |
| 108 |
} |
| 109 |
|
| 110 |
// Check if gots an array |
| 111 |
if (! is_array($array)) { |
| 112 |
throw new NotImplementedError("YAML not valid. %s needs to be an array", var_export($array, true)); |
| 113 |
} |
| 114 |
|
| 115 |
// try to detect type |
| 116 |
if (! array_key_exists('type', $array)) { |
| 117 |
foreach (self::$typeKeyMap as $type => $keys) { |
| 118 |
if (! is_array($keys)) { |
| 119 |
$keys = [$keys]; |
| 120 |
} |
| 121 |
$matched = false; |
| 122 |
foreach ($keys as $k) { |
| 123 |
if (array_key_exists($k, $array)) { |
| 124 |
$matched = true; |
| 125 |
} else { |
| 126 |
continue 2; |
| 127 |
} |
| 128 |
} |
| 129 |
// if all keys are present |
| 130 |
if ($matched === true) { |
| 131 |
$array['type'] = $type; |
| 132 |
break; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
if (array_key_exists('type', $array)) { |
| 138 |
$type = $array['type']; |
| 139 |
if (array_key_exists($type, self::$typeMap)) { |
| 140 |
$node = new self::$typeMap[$type]; |
| 141 |
$node->type = $type; |
| 142 |
} else { |
| 143 |
throw new NotImplementedError('Could not find type "%s" for %s', $type, var_export($array, true)); |
| 144 |
} |
| 145 |
} elseif ($root === null) { |
| 146 |
$node = new static; |
| 147 |
} else { |
| 148 |
$node = new self; |
| 149 |
} |
| 150 |
|
| 151 |
if ($root === null) { |
| 152 |
$node->root = true; // is root |
| 153 |
$node->parent = null; |
| 154 |
$root = $parent = $node; |
| 155 |
} elseif ($parent === null) { |
| 156 |
throw new ProgrammingError('You must specify the direct parent!'); |
| 157 |
} else { |
| 158 |
$node->root = $root; |
| 159 |
$node->parent = $parent; |
| 160 |
} |
| 161 |
|
| 162 |
foreach ($array as $key => $value) { |
| 163 |
if ($key !== 'children') { |
| 164 |
$node->properties[$key] = $value; |
| 165 |
} elseif (is_array($value)) { // only array values for children |
| 166 |
foreach ($value as $i => $child) { |
| 167 |
$childNode = self::fromArray($child, $node, $root); |
| 168 |
$childNode->id = $i; |
| 169 |
$node->appendChild($childNode); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
$node->register(); |
| 175 |
|
| 176 |
if ($root === $node) { |
| 177 |
Benchmark::measure('Finished loading TLVTree from array'); |
| 178 |
} |
| 179 |
|
| 180 |
return $node; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Retrieve all objects as breadcrumb |
| 185 |
* |
| 186 |
* @param array $list for recursion |
| 187 |
* |
| 188 |
* @return TLVTreeNode[] |
| 189 |
*/ |
| 190 |
public function getBreadCrumb(&$list = array()) |
| 191 |
{ |
| 192 |
array_unshift($list, $this); |
| 193 |
if ($this->parent !== $this->root) { |
| 194 |
$this->parent->getBreadCrumb($list); |
| 195 |
} |
| 196 |
return $list; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* |
| 201 |
* @return mixed|string |
| 202 |
*/ |
| 203 |
public function getFullId() |
| 204 |
{ |
| 205 |
if ($this->fullId === null) { |
| 206 |
$id = (string) $this->id; |
| 207 |
if ($this->parent !== $this->root) { |
| 208 |
$this->fullId = $this->parent->getFullId() . '-' . $id; |
| 209 |
} else { |
| 210 |
$this->fullId = $id; |
| 211 |
} |
| 212 |
} |
| 213 |
return $this->fullId; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* @return mixed |
| 218 |
*/ |
| 219 |
public function getType() |
| 220 |
{ |
| 221 |
return $this->type; |
| 222 |
} |
| 223 |
|
| 224 |
public function getProperties() |
| 225 |
{ |
| 226 |
return $this->properties; |
| 227 |
} |
| 228 |
|
| 229 |
public function setProperties($array) |
| 230 |
{ |
| 231 |
$this->properties = $array; |
| 232 |
return $this; |
| 233 |
} |
| 234 |
|
| 235 |
public function get($key) |
| 236 |
{ |
| 237 |
if (array_key_exists($key, $this->properties)) { |
| 238 |
return $this->properties[$key]; |
| 239 |
} else { |
| 240 |
return null; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
public function set($key, $value) |
| 245 |
{ |
| 246 |
$this->properties[$key] = $value; |
| 247 |
return $this; |
| 248 |
} |
| 249 |
|
| 250 |
public function getTitle() |
| 251 |
{ |
| 252 |
if (array_key_exists(static::$titleKey, $this->properties)) { |
| 253 |
return Str::limit($this->properties[static::$titleKey], 45); |
| 254 |
} else { |
| 255 |
return null; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* getKey returns the object's key |
| 261 |
* |
| 262 |
* @throws ProgrammingError if the key does not exist |
| 263 |
*/ |
| 264 |
public function getKey() |
| 265 |
{ |
| 266 |
if ($this->key === null) { |
| 267 |
throw new ProgrammingError('Can not get key for %s', get_class($this)); |
| 268 |
} |
| 269 |
|
| 270 |
if (array_key_exists($this->key, $this->properties)) { |
| 271 |
return $this->properties[$this->key]; |
| 272 |
} else { |
| 273 |
throw new ProgrammingError( |
| 274 |
'Can not retrieve key for %s in %s', |
| 275 |
$this->key, |
| 276 |
get_class($this) |
| 277 |
); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* @return TLVTree |
| 283 |
*/ |
| 284 |
public function getRoot() |
| 285 |
{ |
| 286 |
return $this->root; |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* @return TLVTreeNode |
| 291 |
*/ |
| 292 |
public function getParent() |
| 293 |
{ |
| 294 |
return $this->parent; |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* @return TLVTreeNode[] |
| 299 |
*/ |
| 300 |
public function getChildren() |
| 301 |
{ |
| 302 |
return parent::getChildren(); |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Append a child node as the last child of this node |
| 307 |
* |
| 308 |
* @param TreeNode $child The child to append |
| 309 |
* |
| 310 |
* @return $this |
| 311 |
* |
| 312 |
* @throws ConfigurationError When node does not allow children |
| 313 |
*/ |
| 314 |
public function appendChild(TreeNode $child) |
| 315 |
{ |
| 316 |
if (static::$canHaveChildren === true) { |
| 317 |
$this->children[] = $child; |
| 318 |
} else { |
| 319 |
throw new ConfigurationError('Can not add children below type %s', $this->type); |
| 320 |
} |
| 321 |
return $this; |
| 322 |
} |
| 323 |
|
| 324 |
protected function register() |
| 325 |
{ |
| 326 |
if ($this->type !== 'node') { |
| 327 |
$this->root->registerObject($this->type, $this->getKey(), get_class($this)); |
| 328 |
} |
| 329 |
return $this; |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* getStatus returns the object's status |
| 334 |
* |
| 335 |
* @return TLVStatus |
| 336 |
* @throws ProgrammingError |
| 337 |
*/ |
| 338 |
public function getStatus() |
| 339 |
{ |
| 340 |
if (static::$canHaveChildren === true) { |
| 341 |
if ($this->status === null) { |
| 342 |
$this->status = new TLVStatus; |
| 343 |
|
| 344 |
$missed = true; |
| 345 |
foreach ($this->getChildren() as $child) { |
| 346 |
$this->status->merge($child->getStatus()); |
| 347 |
$missed = false; |
| 348 |
} |
| 349 |
|
| 350 |
// Note: old TLV does not count an empty branch as missing... |
| 351 |
if ($missed) { |
| 352 |
$this->status->add('missing', 1); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
return $this->status; |
| 357 |
} else { |
| 358 |
throw new ProgrammingError('getStatus() needs to be implemented for %s', get_class($this)); |
| 359 |
} |
| 360 |
} |
| 361 |
} |