summaryrefslogtreecommitdiffstats
path: root/library/Toplevelview/Tree/TLVTreeNode.php
blob: fd70e494bedf4995111e174eb7b05144c9b7606b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/* Copyright (C) 2017 Icinga Development Team <info@icinga.com> */

namespace Icinga\Module\Toplevelview\Tree;

use Icinga\Application\Benchmark;
use Icinga\Data\Tree\TreeNode;
use Icinga\Exception\ConfigurationError;
use Icinga\Exception\NotImplementedError;
use Icinga\Exception\ProgrammingError;

class TLVTreeNode extends TreeNode
{
    /**
     * @var string
     */
    protected $type = 'node';

    protected $key = null;

    /**
     * @var TLVTree
     */
    protected $root;

    /**
     * @var TLVTreeNode
     */
    protected $parent;

    /**
     * @var string
     */
    protected $fullId;

    /**
     * @var TLVStatus
     */
    protected $status;

    /**
     * @var array
     */
    protected $properties = array();

    protected static $canHaveChildren = true;

    /**
     * The key which represents the display title
     *
     * @var string
     */
    protected static $titleKey = 'name';

    /**
     * Mapping types to its implementation class
     *
     * @var array
     */
    protected static $typeMap = array(
        'host'      => 'Icinga\\Module\\Toplevelview\\Tree\\TLVHostNode',
        'service'   => 'Icinga\\Module\\Toplevelview\\Tree\\TLVServiceNode',
        'hostgroup' => 'Icinga\\Module\\Toplevelview\\Tree\\TLVHostGroupNode',
    );

    /**
     * Mapping keys to a type
     *
     * Warning: order is important when keys overlap!
     *
     * @var array
     */
    protected static $typeKeyMap = array(
        'service'   => array('host', 'service'),
        'host'      => 'host',
        'hostgroup' => 'hostgroup',
    );

    /**
     * @param                  $array
     * @param TLVTreeNode|null $parent
     * @param TLVTree          $root
     *
     * @return static
     *
     * @throws NotImplementedError
     * @throws ProgrammingError
     */
    public static function fromArray($array, TLVTreeNode $parent = null, TLVTree $root = null)
    {
        if ($root === null) {
            Benchmark::measure('Begin loading TLVTree from array');
        }

        // try to detect type
        if (! array_key_exists('type', $array)) {
            foreach (self::$typeKeyMap as $type => $keys) {
                if (! is_array($keys)) {
                    $keys = array($keys);
                }
                $matched = false;
                foreach ($keys as $k) {
                    if (array_key_exists($k, $array)) {
                        $matched = true;
                    } else {
                        continue 2;
                    }
                }
                // if all keys are present
                if ($matched === true) {
                    $array['type'] = $type;
                    break;
                }
            }
        }

        if (array_key_exists('type', $array)) {
            $type = $array['type'];
            if (array_key_exists($type, self::$typeMap)) {
                $node = new self::$typeMap[$type];
                $node->type = $type;
            } else {
                throw new NotImplementedError('Could not find type "%s" for %s', $type, var_export($array, true));
            }
        } elseif ($root === null) {
            $node = new static;
        } else {
            $node = new self;
        }

        if ($root === null) {
            $node->root = true; // is root
            $node->parent = null;
            $root = $parent = $node;
        } elseif ($parent === null) {
            throw new ProgrammingError('You must specify the direct parent!');
        } else {
            $node->root = $root;
            $node->parent = $parent;
        }

        foreach ($array as $key => $value) {
            if ($key !== 'children') {
                $node->properties[$key] = $value;
            } elseif (is_array($value)) { // only array values for children
                foreach ($value as $i => $child) {
                    $childNode = self::fromArray($child, $node, $root);
                    $childNode->id = $i;
                    $node->appendChild($childNode);
                }
            }
        }

        $node->register();

        if ($root === $node) {
            Benchmark::measure('Finished loading TLVTree from array');
        }

        return $node;
    }

    /**
     * Retrieve all objects as breadcrumb
     *
     * @param array $list for recursion
     *
     * @return TLVTreeNode[]
     */
    public function getBreadCrumb(&$list = array())
    {
        array_unshift($list, $this);
        if ($this->parent !== $this->root) {
            $this->parent->getBreadCrumb($list);
        }
        return $list;
    }

    /**
     *
     * @return mixed|string
     */
    public function getFullId()
    {
        if ($this->fullId === null) {
            $id = (string) $this->id;
            if ($this->parent !== $this->root) {
                $this->fullId = $this->parent->getFullId() . '-' . $id;
            } else {
                $this->fullId = $id;
            }
        }
        return $this->fullId;
    }

    /**
     * @return mixed
     */
    public function getType()
    {
        return $this->type;
    }

    public function getProperties()
    {
        return $this->properties;
    }

    public function setProperties($array)
    {
        $this->properties = $array;
        return $this;
    }

    public function get($key)
    {
        if (array_key_exists($key, $this->properties)) {
            return $this->properties[$key];
        } else {
            return null;
        }
    }

    public function set($key, $value)
    {
        $this->properties[$key] = $value;
        return $this;
    }

    public function getTitle()
    {
        if (array_key_exists(static::$titleKey, $this->properties)) {
            return $this->properties[static::$titleKey];
        } else {
            return null;
        }
    }

    public function getKey()
    {
        if ($this->key === null) {
            throw new ProgrammingError('Can not get key for %s', get_class($this));
        }

        if (array_key_exists($this->key, $this->properties)) {
            return $this->properties[$this->key];
        } else {
            throw new ProgrammingError(
                'Can not retrieve key for %s in %s',
                $this->key,
                get_class($this)
            );
        }
    }

    /**
     * @return TLVTree
     */
    public function getRoot()
    {
        return $this->root;
    }

    /**
     * @return TLVTreeNode
     */
    public function getParent()
    {
        return $this->parent;
    }

    /**
     * @return TLVTreeNode[]
     */
    public function getChildren()
    {
        return parent::getChildren();
    }

    /**
     * Append a child node as the last child of this node
     *
     * @param   TreeNode $child The child to append
     *
     * @return $this
     *
     * @throws ConfigurationError When node does not allow children
     */
    public function appendChild(TreeNode $child)
    {
        if (static::$canHaveChildren === true) {
            $this->children[] = $child;
        } else {
            throw new ConfigurationError('Can not add children below type %s', $this->type);
        }
        return $this;
    }

    protected function register()
    {
        if ($this->type !== 'node') {
            $this->root->registerObject($this->type, $this->getKey(), get_class($this));
        }
        return $this;
    }

    /**
     * @return TLVStatus
     * @throws ProgrammingError
     */
    public function getStatus()
    {
        if (static::$canHaveChildren === true) {
            if ($this->status === null) {
                $this->status = new TLVStatus;

                $missed = true;
                foreach ($this->getChildren() as $child) {
                    $this->status->merge($child->getStatus());
                    $missed = false;
                }

                // Note: old TLV does not count an empty branch as missing...
                if ($missed) {
                    $this->status->add('missing', 1);
                }
            }

            return $this->status;
        } else {
            throw new ProgrammingError('getStatus() needs to be implemented for %s', get_class($this));
        }
    }
}