/*** Generated by streamline 0.10.17 (callbacks) - DO NOT EDIT ***/ var __rt=require('streamline/lib/callbacks/runtime').runtime(__filename, false),__func=__rt.__func,__cb=__rt.__cb,__catch=__rt.__catch,__tryCatch=__rt.__tryCatch; var __ = require("underscore");















var util = require("util");
var utils = require("../../../util/utils");
var $ = utils.getLocaleString;
var constants = require("./constants");
var VNetUtil = require("./../../../util/vnet.util");
var Subnet = require("./subnet");

function RouteTable(cli, networkManagementClient) {
  this.networkManagementClient = networkManagementClient;
  this.subnetCrud = new Subnet(cli, networkManagementClient);
  this.vnetUtil = new VNetUtil();
  this.output = cli.output;
  this.interaction = cli.interaction;};


__.extend(RouteTable.prototype, {
  create: function create__1(routeTableName, location, options, _) { var self, progress, __this = this; var __frame = { name: "create__1", line: 33 }; return __func(_, this, arguments, create__1, 3, __frame, function __$create__1() { self = __this;

      options.name = routeTableName;
      options.location = location;

      progress = self.interaction.progress(util.format($("Creating route table \"%s\""), routeTableName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$create__1() {

            return self.networkManagementClient.routes.createRouteTable(options, __cb(_, __frame, 7, 42, function __$create__1() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$create__1() {

              progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$create__1() {

          return self.show(routeTableName, options, __cb(_, __frame, 11, 9, function __$create__1() { _(); }, true)); }); }); }); },


  show: function show__2(routeTableName, options, _) { var self, routeTable, __this = this; var __frame = { name: "show__2", line: 47 }; return __func(_, this, arguments, show__2, 2, __frame, function __$show__2() { self = __this;

      return self.get(routeTableName, true, __cb(_, __frame, 2, 26, function ___(__0, __1) { routeTable = __1;

        self.interaction.formatOutput(routeTable, function(routeTable) {
          if ((routeTable === null)) {
            self.output.warn(util.format($("A route table with name \"%s\" not found"), routeTableName)); }
           else {
            self.output.nameValue($("Name"), routeTable.name);
            self.output.nameValue($("Location"), routeTable.location);
            self.output.nameValue($("Label"), routeTable.label);
            if ((routeTable.routeList.length > 0)) {
              self.output.header($("Route list"));
              self.output.table(routeTable.routeList, function(row, route) {
                row.cell($("Name"), route.name);
                row.cell($("Address prefix"), route.addressPrefix);
                row.cell($("Next hop type"), route.nextHop.type);
                row.cell($("Next hop IP address"), (route.nextHop.ipAddress || "")); }); } ; } ; }); _(); }, true)); }); },






  list: function list__3(options, _) { var self, progress, routeTables, __this = this; var __frame = { name: "list__3", line: 71 }; return __func(_, this, arguments, list__3, 1, __frame, function __$list__3() { self = __this;

      progress = self.interaction.progress(("Looking up route tables")); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$list__3() {



            return self.networkManagementClient.routes.listRouteTables(__cb(_, __frame, 6, 56, function ___(__0, __1) { routeTables = __1; _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$list__3() {

              progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$list__3() {


          self.interaction.formatOutput(routeTables.routeTables, function(data) {
            if ((data.length === 0)) {
              self.output.warn($("No route tables found")); }
             else {
              self.output.table(data, function(row, routeTable) {
                row.cell($("Name"), routeTable.name);
                row.cell($("Location"), routeTable.location); }); } ; }); _(); }); }); }); },





  delete: function delete__4(routeTableName, options, _) { var self, routeTable, progress, __this = this; var __frame = { name: "delete__4", line: 94 }; return __func(_, this, arguments, delete__4, 2, __frame, function __$delete__4() { self = __this;

      return self.get(routeTableName, false, __cb(_, __frame, 2, 26, function ___(__0, __2) { routeTable = __2;
        if (!routeTable) {
          return _(new Error(util.format($("Route table \"%s\" not found"), routeTableName))); } ; return (function __$delete__4(_) {


          var __1 = !options.quiet; if (!__1) { return _(null, __1); } ; return self.interaction.confirm(util.format($("Delete route table \"%s\"? [y/n] "), routeTableName), __cb(_, __frame, 7, 44, function ___(__0, __3) { var __2 = !__3; return _(null, __2); }, true)); })(__cb(_, __frame, -93, 17, function ___(__0, __3) { return (function __$delete__4(__then) { if (__3) { return _(null); } else { __then(); } ; })(function __$delete__4() {



            progress = self.interaction.progress(util.format($("Deleting route table \"%s\""), routeTableName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$delete__4() {

                  return self.networkManagementClient.routes.deleteRouteTable(routeTableName, __cb(_, __frame, 13, 42, function __$delete__4() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$delete__4() {

                    progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$delete__4() { _(); }); }); }); }, true)); }, true)); }); },



  get: function get__5(routeTableName, withDetails, _) { var self, progress, routeTable, __this = this; var __frame = { name: "get__5", line: 113 }; return __func(_, this, arguments, get__5, 2, __frame, function __$get__5() { self = __this;

      progress = self.interaction.progress(util.format($("Getting route table \"%s\""), routeTableName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$get__5() { return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$get__5() { return (function __$get__5(__then) {


                    if (withDetails) {
                      return self.networkManagementClient.routes.getRouteTableWithDetails(routeTableName, "Full", __cb(_, __frame, 6, 57, function ___(__0, __1) { routeTable = __1; __then(); }, true)); } else {

                      return self.networkManagementClient.routes.getRouteTable(routeTableName, __cb(_, __frame, 8, 57, function ___(__0, __2) { routeTable = __2; __then(); }, true)); } ; })(__then); }); })(function ___(e, __result) { __catch(function __$get__5() { if (e) {


                    if ((e.statusCode === 404)) {
                      return _(null, null); } ;

                    return _(e); } else { _(null, __result); } ; }, _); }); })(function ___() { __tryCatch(_, function __$get__5() { _(null, null, true); }); }); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$get__5() {

              progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$get__5() {

          return _(null, routeTable.routeTable); }); }); }); },


  setRoute: function setRoute__6(routeTableName, routeName, addressPrefix, nextHopType, options, _) { var self, parameters, routeTable, route, progress, __this = this; var __frame = { name: "setRoute__6", line: 134 }; return __func(_, this, arguments, setRoute__6, 5, __frame, function __$setRoute__6() { self = __this;

      return self._parseRoute(routeName, addressPrefix, nextHopType, options, __cb(_, __frame, 2, 26, function ___(__0, __1) { parameters = __1;
        return self.get(routeTableName, false, __cb(_, __frame, 3, 26, function ___(__0, __2) { routeTable = __2;
          if (!routeTable) {
            return _(new Error(util.format($("A route table with name \"%s\" not found"), routeTableName))); } ;


          route = utils.findFirstCaseIgnore(routeTable.routeList, { name: routeName }); return (function __$setRoute__6(__then) {

            if (route) {
              self.output.error(util.format($("A route with name \"%s\" already exists in a route table \"%s\""), routeName, routeTableName)); __then(); } else {

              progress = self.interaction.progress(util.format($("Setting route \"%s\" in a route table \"%s\""), routeName, routeTableName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$setRoute__6() {

                    return self.networkManagementClient.routes.setRoute(routeTableName, routeName, parameters, __cb(_, __frame, 15, 44, function __$setRoute__6() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$setRoute__6() {

                      progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, __then); }); } ; })(_); }, true)); }, true)); }); },




  deleteRoute: function deleteRoute__7(routeTableName, routeName, options, _) { var self, routeTable, route, progress, __this = this; var __frame = { name: "deleteRoute__7", line: 156 }; return __func(_, this, arguments, deleteRoute__7, 3, __frame, function __$deleteRoute__7() { self = __this;

      return self.get(routeTableName, true, __cb(_, __frame, 2, 26, function ___(__0, __2) { routeTable = __2;
        if (!routeTable) {
          return _(new Error(util.format($("A route table with name \"%s\" not found"), routeTableName))); } ;


        route = utils.findFirstCaseIgnore(routeTable.routeList, { name: routeName }); return (function __$deleteRoute__7(__then) {
          if (route) { return (function __$deleteRoute__7(_) {
              var __1 = !options.quiet; if (!__1) { return _(null, __1); } ; return self.interaction.confirm(util.format($("Delete a route \"%s\"? [y/n] "), routeName), __cb(_, __frame, 9, 46, function ___(__0, __3) { var __2 = !__3; return _(null, __2); }, true)); })(__cb(_, __frame, -155, 17, function ___(__0, __3) { return (function __$deleteRoute__7(__then) { if (__3) { return _(null); } else { __then(); } ; })(function __$deleteRoute__7() {



                progress = self.interaction.progress(util.format($("Deleting route \"%s\" from a route table \"%s\""), routeName, routeTableName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$deleteRoute__7() {

                      return self.networkManagementClient.routes.deleteRoute(routeTableName, routeName, __cb(_, __frame, 15, 44, function __$deleteRoute__7() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$deleteRoute__7() {

                        progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, __then); }); }); }, true)); } else {


            self.output.error(util.format($("A route with name \"%s\" not found in a route table \"%s\""), routeName, routeTableName)); __then(); } ; })(_); }, true)); }); },



  addRouteTableToSubnet: function addRouteTableToSubnet__8(vnetName, subnetName, routeTableName, options, _) { var self, subnet, routeTable, parameters, progress, __this = this; var __frame = { name: "addRouteTableToSubnet__8", line: 180 }; return __func(_, this, arguments, addRouteTableToSubnet__8, 4, __frame, function __$addRouteTableToSubnet__8() { self = __this;

      return self.subnetCrud.get(vnetName, subnetName, options, __cb(_, __frame, 2, 33, function ___(__0, __1) { subnet = __1;

        if (!subnet) {
          self.output.warn(util.format($("Virtual network subnet with name \"%s\" not found in virtual network \"%s\""), subnetName, vnetName)); return _(null); } ;



        return self.getRouteTableForSubnet(vnetName, subnetName, __cb(_, __frame, 9, 26, function ___(__0, __2) { routeTable = __2;
          if ((routeTable && utils.ignoreCaseEquals(routeTable.routeTableName, routeTableName))) {
            self.output.warn(util.format($("Route table with name \"%s\" is already associated with subnet \"%s\""), routeTableName, subnetName)); return _(null); } ;



          parameters = {
            routeTableName: routeTableName };


          progress = self.interaction.progress(util.format($("Associating route table \"%s\" and subnet \"%s\""), routeTableName, subnetName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$addRouteTableToSubnet__8() {

                return self.networkManagementClient.routes.addRouteTableToSubnet(vnetName, subnetName, parameters, __cb(_, __frame, 21, 42, function __$addRouteTableToSubnet__8() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$addRouteTableToSubnet__8() {

                  progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$addRouteTableToSubnet__8() {

              options.detailed = true;
              return self.showRouteTableForSubnet(vnetName, subnetName, options, __cb(_, __frame, 26, 9, function __$addRouteTableToSubnet__8() { _(); }, true)); }); }); }, true)); }, true)); }); },


  deleteRouteTableFromSubnet: function deleteRouteTableFromSubnet__9(vnetName, subnetName, routeTableName, options, _) { var self, subnet, routeTable, progress, __this = this; var __frame = { name: "deleteRouteTableFromSubnet__9", line: 209 }; return __func(_, this, arguments, deleteRouteTableFromSubnet__9, 4, __frame, function __$deleteRouteTableFromSubnet__9() { self = __this;

      return self.subnetCrud.get(vnetName, subnetName, options, __cb(_, __frame, 2, 33, function ___(__0, __2) { subnet = __2;

        if (!subnet) {
          self.output.warn(util.format($("Virtual network subnet with name \"%s\" not found in virtual network \"%s\""), subnetName, vnetName)); return _(null); } ;



        return self.networkManagementClient.routes.getRouteTableForSubnet(vnetName, subnetName, __cb(_, __frame, 9, 57, function ___(__0, __3) { routeTable = __3;
          if (!utils.ignoreCaseEquals(routeTable.routeTableName, routeTableName)) {
            self.output.warn(util.format($("Route table \"%s\" not found in virtual network \"%s\""), routeTableName, vnetName)); return _(null); } ; return (function __$deleteRouteTableFromSubnet__9(_) {



            var __1 = !options.quiet; if (!__1) { return _(null, __1); } ; return self.interaction.confirm(util.format($("Delete the route table \"%s\" association with subnet \"%s\"? [y/n] "), routeTableName, subnetName), __cb(_, __frame, 15, 44, function ___(__0, __3) { var __2 = !__3; return _(null, __2); }, true)); })(__cb(_, __frame, -208, 17, function ___(__0, __4) { return (function __$deleteRouteTableFromSubnet__9(__then) { if (__4) { return _(null); } else { __then(); } ; })(function __$deleteRouteTableFromSubnet__9() {




              progress = self.interaction.progress(util.format($("Removing route table \"%s\" and subnet \"%s\" association"), routeTableName, subnetName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$deleteRouteTableFromSubnet__9() {

                    return self.networkManagementClient.routes.removeRouteTableFromSubnet(vnetName, subnetName, __cb(_, __frame, 22, 42, function __$deleteRouteTableFromSubnet__9() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$deleteRouteTableFromSubnet__9() {

                      progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$deleteRouteTableFromSubnet__9() { _(); }); }); }); }, true)); }, true)); }, true)); }); },



  showRouteTableForSubnet: function showRouteTableForSubnet__10(vnetName, subnetName, options, _) { var self, defaultDetailLevel, routeTable, gatewayDetails, __this = this; var __frame = { name: "showRouteTableForSubnet__10", line: 237 }; return __func(_, this, arguments, showRouteTableForSubnet__10, 3, __frame, function __$showRouteTableForSubnet__10() { self = __this;

      defaultDetailLevel = 5;
      return self.getRouteTableForSubnet(vnetName, subnetName, __cb(_, __frame, 3, 26, function ___(__0, __1) { routeTable = __1;
        if (!routeTable) {
          self.output.warn(util.format($("No route table was found in virtual network \"%s\" subnet \"%s\""), vnetName, subnetName)); return _(null); } ;



        self.output.nameValue("Route table name", routeTable.routeTableName); return (function __$showRouteTableForSubnet__10(__then) {
          if (options.detailed) {
            return self.networkManagementClient.routes.getRouteTableWithDetails(routeTable.routeTableName, defaultDetailLevel, __cb(_, __frame, 11, 63, function ___(__0, __2) { gatewayDetails = __2;
              gatewayDetails = gatewayDetails.routeTable;
              self.output.nameValue("Location", gatewayDetails.location, 2); __then(); }, true)); } else { __then(); } ; })(_); }, true)); }); },



  getRouteTableForSubnet: function getRouteTableForSubnet__11(vnetName, subnetName, _) { var self, progress, routeTable, __this = this; var __frame = { name: "getRouteTableForSubnet__11", line: 254 }; return __func(_, this, arguments, getRouteTableForSubnet__11, 2, __frame, function __$getRouteTableForSubnet__11() { self = __this;

      progress = self.interaction.progress(util.format($("Looking up network gateway route tables in virtual network \"%s\" subnet \"%s\""), vnetName, subnetName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$getRouteTableForSubnet__11() { return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$getRouteTableForSubnet__11() {


                  return self.networkManagementClient.routes.getRouteTableForSubnet(vnetName, subnetName, __cb(_, __frame, 5, 55, function ___(__0, __1) { routeTable = __1; __then(); }, true)); }); })(function ___(e, __result) { __catch(function __$getRouteTableForSubnet__11() { if (e) {

                    if (((e.code === "ResourceNotFound") || (e.code === "NotFound"))) {
                      return _(null, null); }
                     else {
                      return _(e); } ; __then(); } else { _(null, __result); } ; }, _); }); })(function ___() { __tryCatch(_, function __$getRouteTableForSubnet__11() { _(null, null, true); }); }); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$getRouteTableForSubnet__11() {


              progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$getRouteTableForSubnet__11() {

          return _(null, routeTable); }); }); }); },


  _parseRoute: function _parseRoute__12(routeName, addressPrefix, nextHopType, options, _) { var self, route, ipValidationResult, __this = this; var __frame = { name: "_parseRoute__12", line: 272 }; return __func(_, this, arguments, _parseRoute__12, 4, __frame, function __$_parseRoute__12() { self = __this;


      route = {
        name: routeName,
        addressPrefix: addressPrefix,
        nextHop: { } };


      route.nextHop.type = utils.verifyParamExistsInCollection(constants.route.nextHopType, nextHopType, "--next-hop-type"); return (function __$_parseRoute__12(__then) {


        if (utils.ignoreCaseAndSpaceEquals(route.nextHop.type, constants.route.nextHopType[0])) {
          return self.interaction.promptIfNotGiven($("Next hop ip address: "), options.nextHopIpAddress, __cb(_, __frame, 13, 50, function ___(__0, __1) { options.nextHopIpAddress = __1;
            ipValidationResult = self.vnetUtil.parseIPv4(options.nextHopIpAddress);
            if (ipValidationResult.error) {
              return _(new Error(util.format($("--next-hop-ip-address \"%s\" is not valid IP address"), options.nextHopIpAddress))); } ;

            route.nextHop.ipAddress = options.nextHopIpAddress; __then(); }, true)); } else { __then(); } ; })(function __$_parseRoute__12() {

        return _(null, route); }); }); }});



module.exports = RouteTable;
