Пример #1
0
 /**
  * @param Node $node
  */
 public function deleteNode(Node $node)
 {
     if (isset($this->nodes[$node->getName()])) {
         unset($this->nodes[$node->getName()]);
     } else {
         foreach ($this->nodes as $n) {
             $n->deleteNode($node);
         }
     }
 }
 /**
  * Recursively inserts a path of nodes
  * 
  * @param Node $curr_node
  * @param string $path a slash delimited path of node names
  * @param array $array optional data array added to last node in the path
  */
 public function insertByPath(&$curr_node, $path = '', $array = null)
 {
     if (is_string($path)) {
         $p = explode('/', $path);
         $n = array_shift($p);
         if ($curr_node instanceof Node) {
             $curr_name = $curr_node->getName();
         }
         if ($curr_name === $n) {
             if (isset($p[0])) {
                 $n = $p[0];
             }
             if ($next_node = $curr_node->getNode($n)) {
                 $next_node = $next_node;
             } else {
                 $next_node = $curr_node;
             }
         } else {
             $new_node = self::build($n);
             $new_node->setData($array);
             $curr_node->insert($new_node);
             $next_node = $new_node;
         }
         if ($n !== '') {
             $remaining_path = implode('/', $p);
             while (count($p) > 0) {
                 return $this->insertByPath($next_node, $remaining_path, $array);
             }
         }
     }
 }
Пример #3
0
 /**
  * Process the result of the given node. Returns false if no other nodes
  * should be run, or a string with the next node name.
  *
  * @param Node $node Node that was run
  *
  * @return string
  */
 protected function processNodeResult(Node $node)
 {
     $result = null;
     $name = $node->getName();
     if (isset($this->nodeResults[$name])) {
         foreach ($this->nodeResults[$name] as $resultInfo) {
             if ($resultInfo->appliesTo($node)) {
                 if ($resultInfo->isActionHangup()) {
                     // hanging up after $name
                     $this->client->hangup();
                 } elseif ($resultInfo->isActionJumpTo()) {
                     $data = $resultInfo->getActionData();
                     if (isset($data['nodeEval'])) {
                         $callback = $data['nodeEval'];
                         $nodeName = $callback($node);
                     } else {
                         $nodeName = $data['nodeName'];
                     }
                     // jumping from $name to $nodeName
                     $result = $nodeName;
                 } elseif ($resultInfo->isActionExecute()) {
                     // executing callback after $name
                     $data = $resultInfo->getActionData();
                     $callback = $data['callback'];
                     $callback($node);
                 }
             }
         }
     }
     return $result;
 }
Пример #4
0
 public function __construct(Node $node, User $user, $action)
 {
     $this->user = $user;
     $this->node = $node;
     $this->name = $node->getName();
     $this->description = $node->getDescription();
     $this->descr = $node->getDescr();
     $this->action = $action;
     $this->action_time = new \DateTime();
 }
Пример #5
0
 private function buildXML(Node $node, $xml)
 {
     $xml .= '<node name="' . htmlentities($node->getName(), ENT_QUOTES, 'UTF-8') . '">';
     if ($node->hasProperties()) {
         $xml = $this->buildXMLProperties($node, $xml);
     }
     if ($node->hasNodes()) {
         $ni = $node->getNodes();
         while ($ni->hasNext()) {
             $temporaryNode = $ni->nextNode();
             if ($temporaryNode->hasNodes()) {
                 $xml = $this->buildXML($temporaryNode, $xml);
             } else {
                 $xml .= '<node name="' . htmlentities($temporaryNode->getName(), ENT_QUOTES, 'UTF-8') . '">';
                 if ($temporaryNode->hasProperties()) {
                     $xml = $this->buildXMLProperties($temporaryNode, $xml);
                 }
                 $xml .= '</node>';
             }
         }
     }
     return $xml .= '</node>';
 }
Пример #6
0
 /**
  * Updates the node pool of Graph
  * 
  * @param  Node   $node Node to be added to the pool
  * @return void
  * @access protected
  */
 public function _updateGraphPool(Node &$node)
 {
     $key = $this->_getKey($node->getName());
     $this->_pool[$key] = $node;
 }
Пример #7
0
 public function nameAccessors()
 {
     $n = new Node('node');
     $n->setName('name');
     $this->assertEquals('name', $n->getName());
 }
Пример #8
0
 /**
  * Return TRUE if the given node is registered for this application
  *
  * @param Node $node
  * @return boolean
  */
 public function hasNode(Node $node)
 {
     return isset($this->nodes[$node->getName()]);
 }
Пример #9
0
 /**
  * Write the given tag to the stream.
  *
  * @param resource $fPtr Stream pointer
  * @param Node     $node Tag to write
  *
  * @return bool
  */
 private function writeTag($fPtr, Node $node)
 {
     return $this->dataHandler->putTAGByte($fPtr, $node->getType()) && $this->dataHandler->putTAGString($fPtr, $node->getName()) && $this->writeType($fPtr, $node->getType(), $node);
 }
Пример #10
0
 /**
  * Recursively deserialize data for the given node.
  *
  * @param   xml.Node node
  * @return  var
  * @throws  lang.IllegalArgumentException if the data cannot be deserialized
  * @throws  lang.ClassNotFoundException in case a XP object's class could not be loaded
  * @throws  xml.XMLFormatException
  */
 protected function _unmarshall(Node $node)
 {
     // value without type is supposed to be string (XML-RPC specs)
     if ('value' == $node->getName() && !isset($node->children[0])) {
         return (string) $node->getContent();
     }
     if (!isset($node->children[0])) {
         throw new XMLFormatException('Tried to access nonexistant node.');
     }
     // Simple form: If no subnode indicating the type exists, the type
     // is string, e.g. <value>Test</value>
     if (!$node->hasChildren()) {
         return (string) $node->getContent();
     }
     // Long form - with subnode, the type is derived from the node's name,
     // e.g. <value><string>Test</string></value>.
     $c = $node->nodeAt(0);
     switch ($c->getName()) {
         case 'struct':
             $ret = array();
             foreach ($c->getChildren() as $child) {
                 $data = array();
                 $data[$child->nodeAt(0)->getName()] = $child->nodeAt(0);
                 $data[$child->nodeAt(1)->getName()] = $child->nodeAt(1);
                 $ret[$data['name']->getContent()] = $this->_unmarshall($data['value']);
                 unset($data);
             }
             if (!isset($ret['__xp_class'])) {
                 return $ret;
             }
             // Check whether this is a XP object. If so, load the class and
             // create an instance without invoking the constructor.
             $fields = XPClass::forName($ret['__xp_class'])->getFields();
             $cname = array_search($ret['__xp_class'], xp::$cn, TRUE);
             $s = '';
             $n = 0;
             foreach ($fields as $field) {
                 if (!isset($ret[$field->getName()])) {
                     continue;
                 }
                 $m = $field->getModifiers();
                 if ($m & MODIFIER_STATIC) {
                     continue;
                 } else {
                     if ($m & MODIFIER_PUBLIC) {
                         $name = $field->getName();
                     } else {
                         if ($m & MODIFIER_PROTECTED) {
                             $name = "*" . $field->getName();
                         } else {
                             if ($m & MODIFIER_PRIVATE) {
                                 $name = "" . array_search($field->getDeclaringClass()->getName(), xp::$cn, TRUE) . "" . $field->getName();
                             }
                         }
                     }
                 }
                 $s .= 's:' . strlen($name) . ':"' . $name . '";' . serialize($ret[$field->getName()]);
                 $n++;
             }
             return unserialize('O:' . strlen($cname) . ':"' . $cname . '":' . $n . ':{' . $s . '}');
         case 'array':
             $ret = array();
             foreach ($c->nodeAt(0)->getChildren() as $child) {
                 $ret[] = $this->_unmarshall($child);
             }
             return $ret;
         case 'int':
         case 'i4':
             return (int) $c->getContent();
         case 'double':
             return (double) $c->getContent();
         case 'boolean':
             return (bool) $c->getContent();
         case 'string':
             return (string) $c->getContent();
         case 'dateTime.iso8601':
             return Date::fromString($c->getContent());
         case 'nil':
             return NULL;
         case 'base64':
             return new Bytes(base64_decode($c->getContent()));
         default:
             throw new IllegalArgumentException('Could not decode node as its type is not supported: ' . $c->getName());
     }
 }
Пример #11
0
 /**
  * Sets a node in the $nodes array; uses the name of the node as index.
  *
  * Nodes can be retrieved by retrieving the property with the same name.
  * Thus 'node1' can be retrieved by invoking: $graph->node1
  *
  * @param \phpDocumentor\GraphViz\Node $node The node to set onto this Graph.
  *
  * @see \phpDocumentor\GraphViz\Node::create()
  *
  * @return \phpDocumentor\GraphViz\Graph
  */
 public function setNode(Node $node)
 {
     $this->nodes[$node->getName()] = $node;
     return $this;
 }
Пример #12
0
 /**
  * @param Node $node
  */
 public function addNode(Node $node)
 {
     $this->nodes[$node->getName()] = $node;
 }
Пример #13
0
 public function testSetNameMethod()
 {
     $this->object->setName('foo');
     $this->assertEquals('foo', $this->object->getName());
 }
Пример #14
0
 /**
  * @param Node $needle
  * @param Node[] $haystack
  * @return bool True if $needle is in $haystack
  */
 private function inArray($needle, array $haystack)
 {
     foreach ($haystack as $straw) {
         if ($straw->getName() === $needle->getName()) {
             return true;
         }
     }
     return false;
 }
Пример #15
0
 /**
  * Returns an array representation of the given Node structure.
  *
  * @param Node $node
  *
  * @return array
  */
 public function toArray(Node $node)
 {
     return array('name' => $node->getName(), 'value' => $node->getValue(), 'attributes' => $node->getAttributes(), 'children' => array_map(function (Node $child) {
         return $this->toArray($child);
     }, $node->getChildren()));
 }
Пример #16
0
 /**
  * Internal recursion for find()
  *
  * @param array $ret
  * @param \USync\AST\NodeInterface[] $node
  * @param int $depth
  *
  * @return \USync\AST\NodeInterface[]
  */
 protected function _find(&$ret, Node $node, $depth = 0)
 {
     $current = $this->segments[$depth];
     if (self::MATCH === $current[0] || self::WILDCARD === $current || $node->getName() === $current) {
         if ($depth === $this->size) {
             $ret[$node->getPath()] = $node;
         } else {
             foreach ($node->getChildren() as $child) {
                 $this->_find($ret, $child, $depth + 1);
             }
         }
     }
 }
 public function remove(Node $node)
 {
     for ($i = 0; $i < $this->numberOfReplicas; ++$i) {
         $nodeKey = $this->hashFunction->hash($node->getName() . $i);
         $this->circle->remove($nodeKey);
     }
 }
Пример #18
0
/**
 * Function to prepare a node before starging it
 *
 * @param   Node    $n                  The Node
 * @param   Int     $id                 Node ID
 * @param   Int     $t                  Tenant ID
 * @param   Array   $nets               Array of networks
 * @return  int                         0 Means ok
 */
function prepareNode($n, $id, $t, $nets)
{
    $user = '******' . $t;
    // Get UID from username
    $cmd = 'id -u ' . $user . ' 2>&1';
    exec($cmd, $o, $rc);
    $uid = $o[0];
    // Creating TAP interfaces
    foreach ($n->getEthernets() as $interface_id => $interface) {
        $tap_name = 'vunl' . $t . '_' . $id . '_' . $interface_id;
        if (isset($nets[$interface->getNetworkId()]) && $nets[$interface->getNetworkId()]->isCloud()) {
            // Network is a Cloud
            $net_name = $nets[$interface->getNetworkId()]->getNType();
        } else {
            $net_name = 'vnet' . $t . '_' . $interface->getNetworkId();
        }
        // Remove interface
        $rc = delTap($tap_name);
        if ($rc !== 0) {
            // Failed to delete TAP interface
            return $rc;
        }
        // Add interface
        $rc = addTap($tap_name, $user);
        if ($rc !== 0) {
            // Failed to add TAP interface
            return $rc;
        }
        if ($interface->getNetworkId() !== 0) {
            // Connect interface to network
            $rc = connectInterface($net_name, $tap_name);
            if ($rc !== 0) {
                // Failed to connect interface to network
                return $rc;
            }
        }
    }
    // Preparing image
    // Dropping privileges
    posix_setsid();
    posix_setgid(32768);
    if ($n->getNType() == 'iol' && !posix_setuid($uid)) {
        error_log('ERROR: ' . $GLOBALS['messages'][80036]);
        return 80036;
    }
    if (!is_file($n->getRunningPath() . '/.prepared') && !is_file($n->getRunningPath() . '/.lock')) {
        // Node is not prepared/locked
        if (!is_dir($n->getRunningPath()) && !mkdir($n->getRunningPath(), 0775, True)) {
            // Cannot create running directory
            error_log('ERROR: ' . $GLOBALS['messages'][80037]);
            return 80037;
        }
        if ($n->getConfig() == 'Saved' && $n->getConfigData() != '') {
            // Node should use saved startup-config
            if (!dumpConfig($n->getConfigData(), $n->getRunningPath() . '/startup-config')) {
                // Cannot dump config to startup-config file
                error_log('WARNING: ' . $GLOBALS['messages'][80067]);
            }
        }
        switch ($n->getNType()) {
            default:
                // Invalid node_type
                error_log('ERROR: ' . $GLOBALS['messages'][80038]);
                return 80038;
            case 'iol':
                // Check license
                if (!is_file('/opt/unetlab/addons/iol/bin/iourc')) {
                    // IOL license not found
                    error_log('ERROR: ' . $GLOBALS['messages'][80039]);
                    return 80039;
                }
                if (!file_exists($n->getRunningPath() . '/iourc') && !symlink('/opt/unetlab/addons/iol/bin/iourc', $n->getRunningPath() . '/iourc')) {
                    // Cannot link IOL license
                    error_log('ERROR: ' . $GLOBALS['messages'][80040]);
                    return 80040;
                }
                break;
            case 'docker':
                if (!is_file('/usr/bin/docker')) {
                    // docker.io is not installed
                    error_log('ERROR: ' . $GLOBALS['messages'][80082]);
                    return 80082;
                }
                $cmd = 'docker inspect --format="{{ .State.Running }}" ' . $n->getUuid();
                exec($cmd, $o, $rc);
                if ($rc != 0) {
                    // Must create docker.io container
                    $cmd = 'docker create -ti --net=none --name=' . $n->getUuid() . ' -h ' . $n->getName() . ' ' . $n->getImage();
                    exec($cmd, $o, $rc);
                    if ($rc != 0) {
                        // Failed to create container
                        error_log('ERROR: ' . $GLOBALS['messages'][80083]);
                        return 80083;
                    }
                }
                break;
            case 'dynamips':
                // Nothing to do
                break;
            case 'qemu':
                $image = '/opt/unetlab/addons/qemu/' . $n->getImage();
                if (!touch($n->getRunningPath() . '/.lock')) {
                    // Cannot lock directory
                    error_log('ERROR: ' . $GLOBALS['messages'][80041]);
                    return 80041;
                }
                // Copy files from template
                foreach (scandir($image) as $filename) {
                    if (preg_match('/^[a-zA-Z0-9]+.qcow2$/', $filename)) {
                        // TODO should check if file exists
                        $cmd = '/opt/qemu/bin/qemu-img create -b "' . $image . '/' . $filename . '" -f qcow2 "' . $n->getRunningPath() . '/' . $filename . '"';
                        exec($cmd, $o, $rc);
                        if ($rc !== 0) {
                            // Cannot make linked clone
                            error_log('ERROR: ' . $GLOBALS['messages'][80045]);
                            error_log(implode("\n", $o));
                            return 80045;
                        }
                    }
                }
                if (!unlink($n->getRunningPath() . '/.lock')) {
                    // Cannot unlock directory
                    error_log('ERROR: ' . $GLOBALS['messages'][80042]);
                    return 80042;
                }
                break;
        }
        // Mark the node as prepared
        if (!touch($n->getRunningPath() . '/.prepared')) {
            // Cannot write on directory
            error_log('ERROR: ' . $GLOBALS['messages'][80044]);
            return 80044;
        }
    }
    return 0;
}
Пример #19
0
 public function addEdge(Node $edge)
 {
     if (!array_key_exists($edge->getName(), $this->edges)) {
         $this->edges[$edge->getName()] = $edge;
     }
 }