1: <?php
2:
3: namespace Saklient\Cloud\Resources;
4:
5: require_once __DIR__ . "/../../../Saklient/Errors/SaklientException.php";
6: use \Saklient\Errors\SaklientException;
7: require_once __DIR__ . "/../../../Saklient/Cloud/Client.php";
8: use \Saklient\Cloud\Client;
9: require_once __DIR__ . "/../../../Saklient/Cloud/Resources/Resource.php";
10: use \Saklient\Cloud\Resources\Resource;
11: require_once __DIR__ . "/../../../Saklient/Cloud/Resources/Icon.php";
12: use \Saklient\Cloud\Resources\Icon;
13: require_once __DIR__ . "/../../../Saklient/Cloud/Resources/Router.php";
14: use \Saklient\Cloud\Resources\Router;
15: require_once __DIR__ . "/../../../Saklient/Cloud/Resources/Ipv4Net.php";
16: use \Saklient\Cloud\Resources\Ipv4Net;
17: require_once __DIR__ . "/../../../Saklient/Cloud/Resources/Ipv6Net.php";
18: use \Saklient\Cloud\Resources\Ipv6Net;
19: require_once __DIR__ . "/../../../Saklient/Cloud/Resources/Bridge.php";
20: use \Saklient\Cloud\Resources\Bridge;
21: require_once __DIR__ . "/../../../Saklient/Util.php";
22: use \Saklient\Util;
23:
24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38:
39: class Swytch 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_tags;
76:
77: 78: 79: 80: 81: 82: 83:
84: protected $m_icon;
85:
86: 87: 88: 89: 90: 91: 92:
93: protected $m_userDefaultRoute;
94:
95: 96: 97: 98: 99: 100: 101:
102: protected $m_userMaskLen;
103:
104: 105: 106: 107: 108: 109: 110:
111: protected $m_router;
112:
113: 114: 115: 116: 117: 118: 119:
120: protected $m_bridge;
121:
122: 123: 124: 125: 126: 127: 128:
129: protected $m_ipv4Nets;
130:
131: 132: 133: 134: 135: 136: 137:
138: protected $m_ipv6Nets;
139:
140: 141: 142: 143: 144: 145:
146: protected function _apiPath()
147: {
148: return "/switch";
149: }
150:
151: 152: 153: 154: 155: 156:
157: protected function _rootKey()
158: {
159: return "Switch";
160: }
161:
162: 163: 164: 165: 166: 167:
168: protected function _rootKeyM()
169: {
170: return "Switches";
171: }
172:
173: 174: 175: 176: 177: 178:
179: public function _className()
180: {
181: return "Swytch";
182: }
183:
184: 185: 186: 187: 188: 189:
190: public function _id()
191: {
192: return $this->get_id();
193: }
194:
195: 196: 197: 198: 199: 200:
201: public function save()
202: {
203: return $this->_save();
204: }
205:
206: 207: 208: 209: 210: 211:
212: public function reload()
213: {
214: return $this->_reload();
215: }
216:
217: 218: 219: 220: 221: 222: 223:
224: public function __construct(\Saklient\Cloud\Client $client, $obj, $wrapped=false)
225: {
226: parent::__construct($client);
227: Util::validateArgCount(func_num_args(), 2);
228: Util::validateType($client, "\\Saklient\\Cloud\\Client");
229: Util::validateType($wrapped, "boolean");
230: $this->apiDeserialize($obj, $wrapped);
231: }
232:
233: 234: 235: 236: 237: 238:
239: public function addIpv6Net()
240: {
241: $ret = $this->get_router()->addIpv6Net();
242: $this->reload();
243: return $ret;
244: }
245:
246: 247: 248: 249: 250: 251:
252: public function removeIpv6Net()
253: {
254: $nets = $this->get_ipv6Nets();
255: $this->get_router()->removeIpv6Net($nets[0]);
256: $this->reload();
257: return $this;
258: }
259:
260: 261: 262: 263: 264: 265: 266: 267:
268: public function addStaticRoute($maskLen, $nextHop)
269: {
270: Util::validateArgCount(func_num_args(), 2);
271: Util::validateType($maskLen, "int");
272: Util::validateType($nextHop, "string");
273: $ret = $this->get_router()->addStaticRoute($maskLen, $nextHop);
274: $this->reload();
275: return $ret;
276: }
277:
278: 279: 280: 281: 282: 283: 284:
285: public function removeStaticRoute(\Saklient\Cloud\Resources\Ipv4Net $ipv4Net)
286: {
287: Util::validateArgCount(func_num_args(), 1);
288: Util::validateType($ipv4Net, "\\Saklient\\Cloud\\Resources\\Ipv4Net");
289: $this->get_router()->removeStaticRoute($ipv4Net);
290: $this->reload();
291: return $this;
292: }
293:
294: 295: 296: 297: 298: 299: 300:
301: public function changePlan($bandWidthMbps)
302: {
303: Util::validateArgCount(func_num_args(), 1);
304: Util::validateType($bandWidthMbps, "int");
305: $this->get_router()->changePlan($bandWidthMbps);
306: $this->reload();
307: return $this;
308: }
309:
310: 311: 312: 313: 314: 315: 316: 317:
318: public function connectToBridge(\Saklient\Cloud\Resources\Bridge $bridge)
319: {
320: Util::validateArgCount(func_num_args(), 1);
321: Util::validateType($bridge, "\\Saklient\\Cloud\\Resources\\Bridge");
322: $result = $this->_client->request("PUT", $this->_apiPath() . "/" . $this->_id() . "/to/bridge/" . $bridge->_id());
323: $this->reload();
324: return $this;
325: }
326:
327: 328: 329: 330: 331: 332:
333: public function disconnectFromBridge()
334: {
335: $result = $this->_client->request("DELETE", $this->_apiPath() . "/" . $this->_id() . "/to/bridge");
336: $this->reload();
337: return $this;
338: }
339:
340: 341: 342: 343: 344: 345:
346: protected function _usedIpv4AddressHash()
347: {
348: $filter = (object)[];
349: $filter->{"Switch" . ".ID"} = $this->_id();
350: $query = (object)[];
351: Util::setByPath($query, "Count", 0);
352: Util::setByPath($query, "Filter", $filter);
353: Util::setByPath($query, "Include", new \ArrayObject(["IPAddress", "UserIPAddress"]));
354: $result = $this->_client->request("GET", "/interface", $query);
355: if ($result == null) {
356: return null;
357: }
358: $result = $result->{"Interfaces"};
359: if ($result == null) {
360: return null;
361: }
362: $ifaces = $result;
363: if ($ifaces == null) {
364: return null;
365: }
366: $found = (object)[];
367: foreach ($ifaces as $iface) {
368: $ip = $iface->{"IPAddress"};
369: $userIp = $iface->{"UserIPAddress"};
370: if ($ip == null) {
371: $ip = $userIp;
372: }
373: if ($ip != null) {
374: $found->{$ip} = true;
375: }
376: }
377: return $found;
378: }
379:
380: 381: 382: 383: 384: 385:
386: public function collectUsedIpv4Addresses()
387: {
388: $found = $this->_usedIpv4AddressHash();
389: return Util::sortArray(new \ArrayObject(array_keys((array)$found)));
390: }
391:
392: 393: 394: 395: 396: 397:
398: public function collectUnusedIpv4Addresses()
399: {
400: $nets = $this->get_ipv4Nets();
401: if ($nets->count() < 1) {
402: return null;
403: }
404: $used = $this->_usedIpv4AddressHash();
405: $ret = new \ArrayObject([]);
406: foreach ($nets[0]->get_range()->get_asArray() as $ip) {
407: if (!array_key_exists($ip, (array)($used))) {
408: $ret->append($ip);
409: }
410: }
411: return Util::sortArray($ret);
412: }
413:
414: 415: 416: 417: 418:
419: private $n_id = false;
420:
421: 422: 423: 424: 425: 426: 427:
428: private function get_id()
429: {
430: return $this->m_id;
431: }
432:
433:
434:
435: 436: 437: 438: 439:
440: private $n_name = false;
441:
442: 443: 444: 445: 446: 447: 448:
449: private function get_name()
450: {
451: return $this->m_name;
452: }
453:
454: 455: 456: 457: 458: 459: 460: 461:
462: private function set_name($v)
463: {
464: Util::validateArgCount(func_num_args(), 1);
465: Util::validateType($v, "string");
466: $this->m_name = $v;
467: $this->n_name = true;
468: return $this->m_name;
469: }
470:
471:
472:
473: 474: 475: 476: 477:
478: private $n_description = false;
479:
480: 481: 482: 483: 484: 485: 486:
487: private function get_description()
488: {
489: return $this->m_description;
490: }
491:
492: 493: 494: 495: 496: 497: 498: 499:
500: private function set_description($v)
501: {
502: Util::validateArgCount(func_num_args(), 1);
503: Util::validateType($v, "string");
504: $this->m_description = $v;
505: $this->n_description = true;
506: return $this->m_description;
507: }
508:
509:
510:
511: 512: 513: 514: 515:
516: private $n_tags = false;
517:
518: 519: 520: 521: 522: 523: 524:
525: private function get_tags()
526: {
527: $this->n_tags = true;
528: return $this->m_tags;
529: }
530:
531: 532: 533: 534: 535: 536: 537: 538:
539: private function set_tags($v)
540: {
541: Util::validateArgCount(func_num_args(), 1);
542: Util::validateType($v, "\\ArrayObject");
543: if (is_array($v)) $v = Client::array2ArrayObject($v);
544: $this->m_tags = $v;
545: $this->n_tags = true;
546: return $this->m_tags;
547: }
548:
549:
550:
551: 552: 553: 554: 555:
556: private $n_icon = false;
557:
558: 559: 560: 561: 562: 563: 564:
565: private function get_icon()
566: {
567: return $this->m_icon;
568: }
569:
570: 571: 572: 573: 574: 575: 576: 577:
578: private function set_icon(\Saklient\Cloud\Resources\Icon $v=null)
579: {
580: Util::validateArgCount(func_num_args(), 1);
581: Util::validateType($v, "\\Saklient\\Cloud\\Resources\\Icon");
582: $this->m_icon = $v;
583: $this->n_icon = true;
584: return $this->m_icon;
585: }
586:
587:
588:
589: 590: 591: 592: 593:
594: private $n_userDefaultRoute = false;
595:
596: 597: 598: 599: 600: 601: 602:
603: private function get_userDefaultRoute()
604: {
605: return $this->m_userDefaultRoute;
606: }
607:
608:
609:
610: 611: 612: 613: 614:
615: private $n_userMaskLen = false;
616:
617: 618: 619: 620: 621: 622: 623:
624: private function get_userMaskLen()
625: {
626: return $this->m_userMaskLen;
627: }
628:
629:
630:
631: 632: 633: 634: 635:
636: private $n_router = false;
637:
638: 639: 640: 641: 642: 643: 644:
645: private function get_router()
646: {
647: return $this->m_router;
648: }
649:
650:
651:
652: 653: 654: 655: 656:
657: private $n_bridge = false;
658:
659: 660: 661: 662: 663: 664: 665:
666: private function get_bridge()
667: {
668: return $this->m_bridge;
669: }
670:
671:
672:
673: 674: 675: 676: 677:
678: private $n_ipv4Nets = false;
679:
680: 681: 682: 683: 684: 685: 686:
687: private function get_ipv4Nets()
688: {
689: return $this->m_ipv4Nets;
690: }
691:
692:
693:
694: 695: 696: 697: 698:
699: private $n_ipv6Nets = false;
700:
701: 702: 703: 704: 705: 706: 707:
708: private function get_ipv6Nets()
709: {
710: return $this->m_ipv6Nets;
711: }
712:
713:
714:
715: 716: 717: 718: 719: 720: 721:
722: protected function apiDeserializeImpl($r)
723: {
724: Util::validateArgCount(func_num_args(), 1);
725: $this->isNew = $r == null;
726: if ($this->isNew) {
727: $r = (object)[];
728: }
729: $this->isIncomplete = false;
730: if (Util::existsPath($r, "ID")) {
731: $this->m_id = Util::getByPath($r, "ID") == null ? null : "" . Util::getByPath($r, "ID");
732: }
733: else {
734: $this->m_id = null;
735: $this->isIncomplete = true;
736: }
737: $this->n_id = false;
738: if (Util::existsPath($r, "Name")) {
739: $this->m_name = Util::getByPath($r, "Name") == null ? null : "" . Util::getByPath($r, "Name");
740: }
741: else {
742: $this->m_name = null;
743: $this->isIncomplete = true;
744: }
745: $this->n_name = false;
746: if (Util::existsPath($r, "Description")) {
747: $this->m_description = Util::getByPath($r, "Description") == null ? null : "" . Util::getByPath($r, "Description");
748: }
749: else {
750: $this->m_description = null;
751: $this->isIncomplete = true;
752: }
753: $this->n_description = false;
754: if (Util::existsPath($r, "Tags")) {
755: if (Util::getByPath($r, "Tags") == null) {
756: $this->m_tags = new \ArrayObject([]);
757: }
758: else {
759: $this->m_tags = new \ArrayObject([]);
760: foreach (Util::getByPath($r, "Tags") as $t) {
761: $v1 = null;
762: $v1 = $t == null ? null : "" . $t;
763: $this->m_tags->append($v1);
764: }
765: }
766: }
767: else {
768: $this->m_tags = null;
769: $this->isIncomplete = true;
770: }
771: $this->n_tags = false;
772: if (Util::existsPath($r, "Icon")) {
773: $this->m_icon = Util::getByPath($r, "Icon") == null ? null : new Icon($this->_client, Util::getByPath($r, "Icon"));
774: }
775: else {
776: $this->m_icon = null;
777: $this->isIncomplete = true;
778: }
779: $this->n_icon = false;
780: if (Util::existsPath($r, "UserSubnet.DefaultRoute")) {
781: $this->m_userDefaultRoute = Util::getByPath($r, "UserSubnet.DefaultRoute") == null ? null : "" . Util::getByPath($r, "UserSubnet.DefaultRoute");
782: }
783: else {
784: $this->m_userDefaultRoute = null;
785: $this->isIncomplete = true;
786: }
787: $this->n_userDefaultRoute = false;
788: if (Util::existsPath($r, "UserSubnet.NetworkMaskLen")) {
789: $this->m_userMaskLen = Util::getByPath($r, "UserSubnet.NetworkMaskLen") == null ? null : intval("" . Util::getByPath($r, "UserSubnet.NetworkMaskLen"));
790: }
791: else {
792: $this->m_userMaskLen = null;
793: $this->isIncomplete = true;
794: }
795: $this->n_userMaskLen = false;
796: if (Util::existsPath($r, "Internet")) {
797: $this->m_router = Util::getByPath($r, "Internet") == null ? null : new Router($this->_client, Util::getByPath($r, "Internet"));
798: }
799: else {
800: $this->m_router = null;
801: $this->isIncomplete = true;
802: }
803: $this->n_router = false;
804: if (Util::existsPath($r, "Bridge")) {
805: $this->m_bridge = Util::getByPath($r, "Bridge") == null ? null : new Bridge($this->_client, Util::getByPath($r, "Bridge"));
806: }
807: else {
808: $this->m_bridge = null;
809: $this->isIncomplete = true;
810: }
811: $this->n_bridge = false;
812: if (Util::existsPath($r, "Subnets")) {
813: if (Util::getByPath($r, "Subnets") == null) {
814: $this->m_ipv4Nets = new \ArrayObject([]);
815: }
816: else {
817: $this->m_ipv4Nets = new \ArrayObject([]);
818: foreach (Util::getByPath($r, "Subnets") as $t) {
819: $v2 = null;
820: $v2 = $t == null ? null : new Ipv4Net($this->_client, $t);
821: $this->m_ipv4Nets->append($v2);
822: }
823: }
824: }
825: else {
826: $this->m_ipv4Nets = null;
827: $this->isIncomplete = true;
828: }
829: $this->n_ipv4Nets = false;
830: if (Util::existsPath($r, "IPv6Nets")) {
831: if (Util::getByPath($r, "IPv6Nets") == null) {
832: $this->m_ipv6Nets = new \ArrayObject([]);
833: }
834: else {
835: $this->m_ipv6Nets = new \ArrayObject([]);
836: foreach (Util::getByPath($r, "IPv6Nets") as $t) {
837: $v3 = null;
838: $v3 = $t == null ? null : new Ipv6Net($this->_client, $t);
839: $this->m_ipv6Nets->append($v3);
840: }
841: }
842: }
843: else {
844: $this->m_ipv6Nets = null;
845: $this->isIncomplete = true;
846: }
847: $this->n_ipv6Nets = false;
848: }
849:
850: 851: 852: 853: 854: 855:
856: protected function apiSerializeImpl($withClean=false)
857: {
858: Util::validateType($withClean, "boolean");
859: $missing = new \ArrayObject([]);
860: $ret = (object)[];
861: if ($withClean || $this->n_id) {
862: Util::setByPath($ret, "ID", $this->m_id);
863: }
864: if ($withClean || $this->n_name) {
865: Util::setByPath($ret, "Name", $this->m_name);
866: }
867: else {
868: if ($this->isNew) {
869: $missing->append("name");
870: }
871: }
872: if ($withClean || $this->n_description) {
873: Util::setByPath($ret, "Description", $this->m_description);
874: }
875: if ($withClean || $this->n_tags) {
876: Util::setByPath($ret, "Tags", new \ArrayObject([]));
877: foreach ($this->m_tags as $r1) {
878: $v = null;
879: $v = $r1;
880: $ret->{"Tags"}->append($v);
881: }
882: }
883: if ($withClean || $this->n_icon) {
884: Util::setByPath($ret, "Icon", $withClean ? ($this->m_icon == null ? null : $this->m_icon->apiSerialize($withClean)) : ($this->m_icon == null ? (object)['ID' => "0"] : $this->m_icon->apiSerializeID()));
885: }
886: if ($withClean || $this->n_userDefaultRoute) {
887: Util::setByPath($ret, "UserSubnet.DefaultRoute", $this->m_userDefaultRoute);
888: }
889: if ($withClean || $this->n_userMaskLen) {
890: Util::setByPath($ret, "UserSubnet.NetworkMaskLen", $this->m_userMaskLen);
891: }
892: if ($withClean || $this->n_router) {
893: Util::setByPath($ret, "Internet", $withClean ? ($this->m_router == null ? null : $this->m_router->apiSerialize($withClean)) : ($this->m_router == null ? (object)['ID' => "0"] : $this->m_router->apiSerializeID()));
894: }
895: if ($withClean || $this->n_bridge) {
896: Util::setByPath($ret, "Bridge", $withClean ? ($this->m_bridge == null ? null : $this->m_bridge->apiSerialize($withClean)) : ($this->m_bridge == null ? (object)['ID' => "0"] : $this->m_bridge->apiSerializeID()));
897: }
898: if ($withClean || $this->n_ipv4Nets) {
899: Util::setByPath($ret, "Subnets", new \ArrayObject([]));
900: foreach ($this->m_ipv4Nets as $r2) {
901: $v = null;
902: $v = $withClean ? ($r2 == null ? null : $r2->apiSerialize($withClean)) : ($r2 == null ? (object)['ID' => "0"] : $r2->apiSerializeID());
903: $ret->{"Subnets"}->append($v);
904: }
905: }
906: if ($withClean || $this->n_ipv6Nets) {
907: Util::setByPath($ret, "IPv6Nets", new \ArrayObject([]));
908: foreach ($this->m_ipv6Nets as $r3) {
909: $v = null;
910: $v = $withClean ? ($r3 == null ? null : $r3->apiSerialize($withClean)) : ($r3 == null ? (object)['ID' => "0"] : $r3->apiSerializeID());
911: $ret->{"IPv6Nets"}->append($v);
912: }
913: }
914: if ($missing->count() > 0) {
915: throw new SaklientException("required_field", "Required fields must be set before the Swytch creation: " . implode(", ", (array)($missing)));
916: }
917: return $ret;
918: }
919:
920: 921: 922:
923: public function __get($key) {
924: switch ($key) {
925: case "id": return $this->get_id();
926: case "name": return $this->get_name();
927: case "description": return $this->get_description();
928: case "tags": return $this->get_tags();
929: case "icon": return $this->get_icon();
930: case "userDefaultRoute": return $this->get_userDefaultRoute();
931: case "userMaskLen": return $this->get_userMaskLen();
932: case "router": return $this->get_router();
933: case "bridge": return $this->get_bridge();
934: case "ipv4Nets": return $this->get_ipv4Nets();
935: case "ipv6Nets": return $this->get_ipv6Nets();
936: default: return parent::__get($key);
937: }
938: }
939:
940: 941: 942:
943: public function __set($key, $v) {
944: switch ($key) {
945: case "name": return $this->set_name($v);
946: case "description": return $this->set_description($v);
947: case "tags": return $this->set_tags($v);
948: case "icon": return $this->set_icon($v);
949: default: return parent::__set($key, $v);
950: }
951: }
952:
953: }
954:
955: