icingaweb2-module-businessp.../library/Businessprocess/ImportedNode.php

239 lines
5.2 KiB
PHP
Raw Normal View History

<?php
namespace Icinga\Module\Businessprocess;
use Icinga\Application\Config;
use Icinga\Module\Businessprocess\Html\Link;
use Icinga\Module\Businessprocess\State\MonitoringState;
use Icinga\Module\Businessprocess\Storage\LegacyStorage;
2017-01-03 05:17:17 -05:00
use Icinga\Module\Businessprocess\Web\Url;
use Exception;
class ImportedNode extends Node
{
/** @var string */
protected $configName;
2017-01-03 05:17:17 -05:00
/** @var string */
protected $nodeName;
/** @var BpNode */
private $node;
protected $className = 'subtree';
2017-01-11 08:04:45 -05:00
/** @var BpConfig */
private $config;
2017-01-03 05:17:17 -05:00
/**
* @inheritdoc
*/
2017-01-11 08:04:45 -05:00
public function __construct(BpConfig $bp, $object)
{
2017-01-03 05:17:17 -05:00
$this->bp = $bp;
$this->configName = $object->configName;
2017-01-03 05:17:17 -05:00
$this->name = '@' . $object->configName;
if (property_exists($object, 'node')) {
$this->nodeName = $object->node;
$this->name .= ':' . $object->node;
} else {
$this->useAllRootNodes();
}
if (isset($object->state)) {
$this->setState($object->state);
}
}
2017-01-03 05:17:17 -05:00
public function hasNode()
{
return $this->nodeName !== null;
}
/**
* @return string
*/
public function getConfigName()
{
return $this->configName;
}
/**
* @inheritdoc
*/
public function getState()
{
if ($this->state === null) {
2017-01-03 05:17:17 -05:00
try {
MonitoringState::apply($this->importedConfig());
2017-01-03 05:17:17 -05:00
} catch (Exception $e) {
}
$this->state = $this->importedNode()->getState();
$this->setMissing(false);
}
return $this->state;
}
/**
* @inheritdoc
*/
public function getAlias()
{
return $this->importedNode()->getAlias();
}
2016-12-22 08:18:13 -05:00
public function getUrl()
{
$params = array(
'config' => $this->getConfigName(),
2016-12-22 08:19:00 -05:00
'node' => $this->importedNode()->getName()
2016-12-22 08:18:13 -05:00
);
return Url::fromPath('businessprocess/process/show', $params);
}
/**
* @inheritdoc
*/
public function isMissing()
{
$this->getState();
// Probably doesn't work, as we create a fake node with worse state
return $this->missing;
}
/**
* @inheritdoc
*/
public function isInDowntime()
{
if ($this->downtime === null) {
2017-01-03 05:17:17 -05:00
$this->getState();
$this->downtime = $this->importedNode()->isInDowntime();
}
2017-01-03 05:17:17 -05:00
return $this->downtime;
}
/**
* @inheritdoc
*/
public function isAcknowledged()
{
if ($this->ack === null) {
2017-01-03 05:17:17 -05:00
$this->getState();
$this->downtime = $this->importedNode()->isAcknowledged();
}
2017-01-03 05:17:17 -05:00
return $this->ack;
}
/**
* @return BpNode
*/
protected function importedNode()
{
if ($this->node === null) {
$this->node = $this->loadImportedNode();
}
return $this->node;
}
/**
* @return BpNode
*/
protected function loadImportedNode()
{
try {
2017-01-03 05:17:17 -05:00
$import = $this->importedConfig();
return $import->getNode($this->nodeName);
} catch (Exception $e) {
return $this->createFailedNode($e);
}
}
protected function useAllRootNodes()
{
try {
$bp = $this->importedConfig();
$this->node = new BpNode($bp, (object) array(
'name' => $this->getName(),
'operator' => '&',
'child_names' => $bp->listRootNodes(),
));
} catch (Exception $e) {
$this->createFailedNode($e);
}
}
/**
* @return BpConfig
*/
2017-01-03 05:17:17 -05:00
protected function importedConfig()
{
if ($this->config === null) {
$import = $this->storage()->loadProcess($this->configName);
if ($this->bp->usesSoftStates()) {
$import->useSoftStates();
} else {
$import->useHardStates();
}
2015-03-16 04:08:00 -04:00
2017-01-03 05:17:17 -05:00
$this->config = $import;
}
2017-01-03 05:17:17 -05:00
return $this->config;
}
2015-03-16 04:08:00 -04:00
/**
* @return LegacyStorage
*/
protected function storage()
{
return new LegacyStorage(
Config::module('businessprocess')->getSection('global')
);
2015-03-16 04:08:00 -04:00
}
/**
* @param Exception $e
*
* @return BpNode
*/
protected function createFailedNode(Exception $e)
2015-03-16 04:08:00 -04:00
{
2017-01-03 05:17:17 -05:00
$this->bp->addError($e->getMessage());
$node = new BpNode($this->importedConfig(), (object) array(
'name' => $this->getName(),
'operator' => '&',
'child_names' => array()
2015-03-16 04:08:00 -04:00
));
$node->setState(2);
$node->setMissing(false)
->setDowntime(false)
->setAck(false)
->setAlias($e->getMessage());
return $node;
}
/**
* @inheritdoc
*/
public function getLink()
{
return Link::create(
$this->getAlias(),
'businessprocess/process/show',
array(
2017-02-17 12:40:54 -05:00
'config' => $this->configName,
'node' => $this->nodeName
)
);
}
}