1: <?php
2:
3: namespace Saklient\Cloud\Resources;
4:
5: require_once __DIR__ . "/../../../Saklient/Errors/HttpException.php";
6: use \Saklient\Errors\HttpException;
7: require_once __DIR__ . "/../../../Saklient/Errors/SaklientException.php";
8: use \Saklient\Errors\SaklientException;
9: require_once __DIR__ . "/../../../Saklient/Cloud/Client.php";
10: use \Saklient\Cloud\Client;
11: require_once __DIR__ . "/../../../Saklient/Cloud/Resources/Resource.php";
12: use \Saklient\Cloud\Resources\Resource;
13: require_once __DIR__ . "/../../../Saklient/Cloud/Resources/Icon.php";
14: use \Saklient\Cloud\Resources\Icon;
15: require_once __DIR__ . "/../../../Saklient/Cloud/Resources/Swytch.php";
16: use \Saklient\Cloud\Resources\Swytch;
17: require_once __DIR__ . "/../../../Saklient/Cloud/Resources/Ipv4Net.php";
18: use \Saklient\Cloud\Resources\Ipv4Net;
19: require_once __DIR__ . "/../../../Saklient/Cloud/Resources/Ipv6Net.php";
20: use \Saklient\Cloud\Resources\Ipv6Net;
21: require_once __DIR__ . "/../../../Saklient/Cloud/Resources/RouterActivity.php";
22: use \Saklient\Cloud\Resources\RouterActivity;
23: require_once __DIR__ . "/../../../Saklient/Cloud/Models/Model_Swytch.php";
24: use \Saklient\Cloud\Models\Model_Swytch;
25: require_once __DIR__ . "/../../../Saklient/Util.php";
26: use \Saklient\Util;
27:
28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38:
39: class Router extends Resource {
40:
41: 42: 43: 44: 45: 46: 47:
48: protected $m_id;
49:
50: 51: 52: 53: 54: 55: 56:
57: protected $m_name;
58:
59: 60: 61: 62: 63: 64: 65:
66: protected $m_description;
67:
68: 69: 70: 71: 72: 73: 74:
75: protected $m_networkMaskLen;
76:
77: 78: 79: 80: 81: 82: 83:
84: protected $m_bandWidthMbps;
85:
86: 87: 88: 89: 90: 91: 92:
93: protected $m_swytchId;
94:
95: 96: 97: 98: 99: 100:
101: protected function _apiPath()
102: {
103: return "/internet";
104: }
105:
106: 107: 108: 109: 110: 111:
112: protected function _rootKey()
113: {
114: return "Internet";
115: }
116:
117: 118: 119: 120: 121: 122:
123: protected function _rootKeyM()
124: {
125: return "Internet";
126: }
127:
128: 129: 130: 131: 132: 133:
134: public function _className()
135: {
136: return "Router";
137: }
138:
139: 140: 141: 142: 143: 144:
145: public function _id()
146: {
147: return $this->get_id();
148: }
149:
150: 151: 152: 153: 154: 155:
156: public function save()
157: {
158: return $this->_save();
159: }
160:
161: 162: 163: 164: 165: 166:
167: public function reload()
168: {
169: return $this->_reload();
170: }
171:
172: 173: 174: 175: 176: 177:
178: protected $_activity;
179:
180: 181: 182: 183: 184:
185: public function get_activity()
186: {
187: return $this->_activity;
188: }
189:
190:
191:
192: 193: 194: 195: 196: 197: 198:
199: public function __construct(\Saklient\Cloud\Client $client, $obj, $wrapped=false)
200: {
201: parent::__construct($client);
202: Util::validateArgCount(func_num_args(), 2);
203: Util::validateType($client, "\\Saklient\\Cloud\\Client");
204: Util::validateType($wrapped, "boolean");
205: $this->_activity = new RouterActivity($client);
206: $this->apiDeserialize($obj, $wrapped);
207: }
208:
209: 210: 211: 212: 213: 214: 215: 216:
217: protected function _onAfterApiDeserialize($r, $root)
218: {
219: Util::validateArgCount(func_num_args(), 2);
220: if ($r != null) {
221: $this->_activity->setSourceId($this->_id());
222: }
223: }
224:
225: 226: 227: 228: 229: 230: 231: 232: 233:
234: public function afterCreate($timeoutSec, $callback)
235: {
236: Util::validateArgCount(func_num_args(), 2);
237: Util::validateType($timeoutSec, "int");
238: Util::validateType($callback, "callback");
239: $ret = $this->sleepWhileCreating($timeoutSec);
240: $callback($this, $ret);
241: }
242:
243: 244: 245: 246: 247: 248: 249:
250: public function sleepWhileCreating($timeoutSec=120)
251: {
252: Util::validateType($timeoutSec, "int");
253: $step = 3;
254: $isOk = false;
255: while (0 < $timeoutSec) {
256: try {
257: if ($this->exists()) {
258: $this->reload();
259: $isOk = true;
260: }
261: }
262: catch (HttpException $ex) {
263: }
264: $timeoutSec -= $step;
265: if ($isOk) {
266: $timeoutSec = 0;
267: }
268: if (0 < $timeoutSec) {
269: Util::sleep($step);
270: }
271: }
272: return $isOk;
273: }
274:
275: 276: 277: 278: 279: 280:
281: public function getSwytch()
282: {
283: $model = Util::createClassInstance("saklient.cloud.models.Model_Swytch", new \ArrayObject([$this->_client]));
284: $id = $this->get_swytchId();
285: return $model->getById($id);
286: }
287:
288: 289: 290: 291: 292: 293:
294: public function addIpv6Net()
295: {
296: $result = $this->_client->request("POST", $this->_apiPath() . "/" . Util::urlEncode($this->_id()) . "/ipv6net");
297: $this->reload();
298: return new Ipv6Net($this->_client, $result->{"IPv6Net"});
299: }
300:
301: 302: 303: 304: 305: 306: 307:
308: public function removeIpv6Net(\Saklient\Cloud\Resources\Ipv6Net $ipv6Net)
309: {
310: Util::validateArgCount(func_num_args(), 1);
311: Util::validateType($ipv6Net, "\\Saklient\\Cloud\\Resources\\Ipv6Net");
312: $this->_client->request("DELETE", $this->_apiPath() . "/" . Util::urlEncode($this->_id()) . "/ipv6net/" . $ipv6Net->_id());
313: $this->reload();
314: return $this;
315: }
316:
317: 318: 319: 320: 321: 322: 323: 324:
325: public function addStaticRoute($maskLen, $nextHop)
326: {
327: Util::validateArgCount(func_num_args(), 2);
328: Util::validateType($maskLen, "int");
329: Util::validateType($nextHop, "string");
330: $q = (object)[];
331: Util::setByPath($q, "NetworkMaskLen", $maskLen);
332: Util::setByPath($q, "NextHop", $nextHop);
333: $result = $this->_client->request("POST", $this->_apiPath() . "/" . Util::urlEncode($this->_id()) . "/subnet", $q);
334: $this->reload();
335: return new Ipv4Net($this->_client, $result->{"Subnet"});
336: }
337:
338: 339: 340: 341: 342: 343: 344:
345: public function removeStaticRoute(\Saklient\Cloud\Resources\Ipv4Net $ipv4Net)
346: {
347: Util::validateArgCount(func_num_args(), 1);
348: Util::validateType($ipv4Net, "\\Saklient\\Cloud\\Resources\\Ipv4Net");
349: $this->_client->request("DELETE", $this->_apiPath() . "/" . Util::urlEncode($this->_id()) . "/subnet/" . $ipv4Net->_id());
350: $this->reload();
351: return $this;
352: }
353:
354: 355: 356: 357: 358: 359: 360: 361: 362:
363: public function changePlan($bandWidthMbps)
364: {
365: Util::validateArgCount(func_num_args(), 1);
366: Util::validateType($bandWidthMbps, "int");
367: $path = $this->_apiPath() . "/" . Util::urlEncode($this->_id()) . "/bandwidth";
368: $q = (object)[];
369: Util::setByPath($q, "Internet.BandWidthMbps", $bandWidthMbps);
370: $result = $this->_client->request("PUT", $path, $q);
371: $this->apiDeserialize($result, true);
372: return $this;
373: }
374:
375: 376: 377: 378: 379:
380: private $n_id = false;
381:
382: 383: 384: 385: 386: 387: 388:
389: private function get_id()
390: {
391: return $this->m_id;
392: }
393:
394:
395:
396: 397: 398: 399: 400:
401: private $n_name = false;
402:
403: 404: 405: 406: 407: 408: 409:
410: private function get_name()
411: {
412: return $this->m_name;
413: }
414:
415: 416: 417: 418: 419: 420: 421: 422:
423: private function set_name($v)
424: {
425: Util::validateArgCount(func_num_args(), 1);
426: Util::validateType($v, "string");
427: $this->m_name = $v;
428: $this->n_name = true;
429: return $this->m_name;
430: }
431:
432:
433:
434: 435: 436: 437: 438:
439: private $n_description = false;
440:
441: 442: 443: 444: 445: 446: 447:
448: private function get_description()
449: {
450: return $this->m_description;
451: }
452:
453: 454: 455: 456: 457: 458: 459: 460:
461: private function set_description($v)
462: {
463: Util::validateArgCount(func_num_args(), 1);
464: Util::validateType($v, "string");
465: $this->m_description = $v;
466: $this->n_description = true;
467: return $this->m_description;
468: }
469:
470:
471:
472: 473: 474: 475: 476:
477: private $n_networkMaskLen = false;
478:
479: 480: 481: 482: 483: 484: 485:
486: private function get_networkMaskLen()
487: {
488: return $this->m_networkMaskLen;
489: }
490:
491: 492: 493: 494: 495: 496: 497: 498:
499: private function set_networkMaskLen($v)
500: {
501: Util::validateArgCount(func_num_args(), 1);
502: Util::validateType($v, "int");
503: if (!$this->isNew) {
504: throw new SaklientException("immutable_field", "Immutable fields cannot be modified after the resource creation: " . "Saklient\\Cloud\\Resources\\Router#networkMaskLen");
505: }
506: $this->m_networkMaskLen = $v;
507: $this->n_networkMaskLen = true;
508: return $this->m_networkMaskLen;
509: }
510:
511:
512:
513: 514: 515: 516: 517:
518: private $n_bandWidthMbps = false;
519:
520: 521: 522: 523: 524: 525: 526:
527: private function get_bandWidthMbps()
528: {
529: return $this->m_bandWidthMbps;
530: }
531:
532: 533: 534: 535: 536: 537: 538: 539:
540: private function set_bandWidthMbps($v)
541: {
542: Util::validateArgCount(func_num_args(), 1);
543: Util::validateType($v, "int");
544: if (!$this->isNew) {
545: throw new SaklientException("immutable_field", "Immutable fields cannot be modified after the resource creation: " . "Saklient\\Cloud\\Resources\\Router#bandWidthMbps");
546: }
547: $this->m_bandWidthMbps = $v;
548: $this->n_bandWidthMbps = true;
549: return $this->m_bandWidthMbps;
550: }
551:
552:
553:
554: 555: 556: 557: 558:
559: private $n_swytchId = false;
560:
561: 562: 563: 564: 565: 566: 567:
568: private function get_swytchId()
569: {
570: return $this->m_swytchId;
571: }
572:
573:
574:
575: 576: 577: 578: 579: 580: 581:
582: protected function apiDeserializeImpl($r)
583: {
584: Util::validateArgCount(func_num_args(), 1);
585: $this->isNew = $r == null;
586: if ($this->isNew) {
587: $r = (object)[];
588: }
589: $this->isIncomplete = false;
590: if (Util::existsPath($r, "ID")) {
591: $this->m_id = Util::getByPath($r, "ID") == null ? null : "" . Util::getByPath($r, "ID");
592: }
593: else {
594: $this->m_id = null;
595: $this->isIncomplete = true;
596: }
597: $this->n_id = false;
598: if (Util::existsPath($r, "Name")) {
599: $this->m_name = Util::getByPath($r, "Name") == null ? null : "" . Util::getByPath($r, "Name");
600: }
601: else {
602: $this->m_name = null;
603: $this->isIncomplete = true;
604: }
605: $this->n_name = false;
606: if (Util::existsPath($r, "Description")) {
607: $this->m_description = Util::getByPath($r, "Description") == null ? null : "" . Util::getByPath($r, "Description");
608: }
609: else {
610: $this->m_description = null;
611: $this->isIncomplete = true;
612: }
613: $this->n_description = false;
614: if (Util::existsPath($r, "NetworkMaskLen")) {
615: $this->m_networkMaskLen = Util::getByPath($r, "NetworkMaskLen") == null ? null : intval("" . Util::getByPath($r, "NetworkMaskLen"));
616: }
617: else {
618: $this->m_networkMaskLen = null;
619: $this->isIncomplete = true;
620: }
621: $this->n_networkMaskLen = false;
622: if (Util::existsPath($r, "BandWidthMbps")) {
623: $this->m_bandWidthMbps = Util::getByPath($r, "BandWidthMbps") == null ? null : intval("" . Util::getByPath($r, "BandWidthMbps"));
624: }
625: else {
626: $this->m_bandWidthMbps = null;
627: $this->isIncomplete = true;
628: }
629: $this->n_bandWidthMbps = false;
630: if (Util::existsPath($r, "Switch.ID")) {
631: $this->m_swytchId = Util::getByPath($r, "Switch.ID") == null ? null : "" . Util::getByPath($r, "Switch.ID");
632: }
633: else {
634: $this->m_swytchId = null;
635: $this->isIncomplete = true;
636: }
637: $this->n_swytchId = false;
638: }
639:
640: 641: 642: 643: 644: 645:
646: protected function apiSerializeImpl($withClean=false)
647: {
648: Util::validateType($withClean, "boolean");
649: $missing = new \ArrayObject([]);
650: $ret = (object)[];
651: if ($withClean || $this->n_id) {
652: Util::setByPath($ret, "ID", $this->m_id);
653: }
654: if ($withClean || $this->n_name) {
655: Util::setByPath($ret, "Name", $this->m_name);
656: }
657: else {
658: if ($this->isNew) {
659: $missing->append("name");
660: }
661: }
662: if ($withClean || $this->n_description) {
663: Util::setByPath($ret, "Description", $this->m_description);
664: }
665: if ($withClean || $this->n_networkMaskLen) {
666: Util::setByPath($ret, "NetworkMaskLen", $this->m_networkMaskLen);
667: }
668: else {
669: if ($this->isNew) {
670: $missing->append("networkMaskLen");
671: }
672: }
673: if ($withClean || $this->n_bandWidthMbps) {
674: Util::setByPath($ret, "BandWidthMbps", $this->m_bandWidthMbps);
675: }
676: else {
677: if ($this->isNew) {
678: $missing->append("bandWidthMbps");
679: }
680: }
681: if ($withClean || $this->n_swytchId) {
682: Util::setByPath($ret, "Switch.ID", $this->m_swytchId);
683: }
684: if ($missing->count() > 0) {
685: throw new SaklientException("required_field", "Required fields must be set before the Router creation: " . implode(", ", (array)($missing)));
686: }
687: return $ret;
688: }
689:
690: 691: 692:
693: public function __get($key) {
694: switch ($key) {
695: case "activity": return $this->get_activity();
696: case "id": return $this->get_id();
697: case "name": return $this->get_name();
698: case "description": return $this->get_description();
699: case "networkMaskLen": return $this->get_networkMaskLen();
700: case "bandWidthMbps": return $this->get_bandWidthMbps();
701: case "swytchId": return $this->get_swytchId();
702: default: return parent::__get($key);
703: }
704: }
705:
706: 707: 708:
709: public function __set($key, $v) {
710: switch ($key) {
711: case "name": return $this->set_name($v);
712: case "description": return $this->set_description($v);
713: case "networkMaskLen": return $this->set_networkMaskLen($v);
714: case "bandWidthMbps": return $this->set_bandWidthMbps($v);
715: default: return parent::__set($key, $v);
716: }
717: }
718:
719: }
720:
721: