/*** 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,__tryCatch=__rt.__tryCatch; var __ = require("underscore");















var util = require("util");
var utils = require("../../../util/utils");
var $ = utils.getLocaleString;
var VNetUtil = require("./../../../util/vnet.util");
var NetworkConfig = require("./networkConfig");

function LocalNetwork(cli, networkManagementClient) {
  this.networkManagementClient = networkManagementClient;
  this.networkConfig = new NetworkConfig(cli, networkManagementClient);
  this.vnetUtil = new VNetUtil();
  this.output = cli.output;
  this.interaction = cli.interaction;};


__.extend(LocalNetwork.prototype, {
  create: function create__1(localNetworkName, addressPrefixes, options, _) { var self, networkConfig, lNetList, lNet, localNetwork, ipValidated, progress, __this = this; var __frame = { name: "create__1", line: 32 }; return __func(_, this, arguments, create__1, 3, __frame, function __$create__1() { self = __this;

      return self.networkConfig.get(__cb(_, __frame, 2, 43, function ___(__0, __1) { networkConfig = __1;
        lNetList = networkConfig.VirtualNetworkConfiguration.LocalNetworkSites;
        if (!lNetList) {
          networkConfig.VirtualNetworkConfiguration.LocalNetworkSites = []; } ;


        lNet = utils.findFirstCaseIgnore(lNetList, { Name: localNetworkName });
        if (lNet) {
          return _(new Error(util.format($("A local network with name \"%s\" already exists"), localNetworkName))); } ;


        localNetwork = { Name: localNetworkName, AddressSpace: [] };
        addressPrefixes.split(",").forEach(function(addressPrefix) {
          try {
            self._validateAddressPrefix(addressPrefix);
            localNetwork.AddressSpace.push(addressPrefix);
          } catch (ex) {
            self.output.warn(util.format($("Address prefix \"%s\" would not be added: %s"), addressPrefix, ex)); }; });



        if ((localNetwork.AddressSpace.length === 0)) {
          return _(new Error("All provided address prefixes are invalid.")); } ;


        if (options.vpnGatewayAddress) {
          ipValidated = self.vnetUtil.parseIPv4(options.vpnGatewayAddress, "--vpn-gateway-address");
          if (ipValidated.error) {
            return _(new Error(ipValidated.error)); } ;

          localNetwork.VPNGatewayAddress = options.vpnGatewayAddress; } ;


        networkConfig.VirtualNetworkConfiguration.LocalNetworkSites.push(localNetwork);
        progress = self.interaction.progress(util.format($("Creating local network \"%s\""), localNetworkName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$create__1() {

              return self.networkConfig.set(networkConfig, __cb(_, __frame, 38, 25, 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(localNetworkName, options, __cb(_, __frame, 42, 9, function __$create__1() { _(); }, true)); }); }); }, true)); }); },


  set: function set__2(localNetworkName, options, _) { var self, networkConfig, lNetList, lNet, ipValidated, progress, __this = this; var __frame = { name: "set__2", line: 77 }; return __func(_, this, arguments, set__2, 2, __frame, function __$set__2() { self = __this;

      return self.networkConfig.get(__cb(_, __frame, 2, 43, function ___(__0, __1) { networkConfig = __1;
        lNetList = networkConfig.VirtualNetworkConfiguration.LocalNetworkSites;
        if (!lNetList) {
          return _(new Error($("No local network found"), localNetworkName)); } ;


        lNet = utils.findFirstCaseIgnore(lNetList, { Name: localNetworkName });
        if (!lNet) {
          return _(new Error(util.format($("A local network with name \"%s\" not found"), localNetworkName))); } ;


        if (options.addressPrefixes) {
          lNet.AddressSpace = [];
          options.addressPrefixes.split(",").forEach(function(addressPrefix) {
            try {
              self._validateAddressPrefix(addressPrefix);
              lNet.AddressSpace.push(addressPrefix);
            } catch (ex) {
              self.output.warn(util.format($("Address prefix \"%s\" would not be added: %s"), addressPrefix, ex)); }; });


          if ((lNet.AddressSpace.length === 0)) {
            return _(new Error("All provided address prefixes are invalid.")); } ; } ;



        if (options.vpnGatewayAddress) {
          ipValidated = self.vnetUtil.parseIPv4(options.vpnGatewayAddress, "--vpn-gateway-address");
          if (ipValidated.error) {
            return _(new Error(ipValidated.error)); } ;

          lNet.VPNGatewayAddress = options.vpnGatewayAddress; } ;


        progress = self.interaction.progress(util.format($("Updating local network \"%s\""), localNetworkName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$set__2() {

              return self.networkConfig.set(networkConfig, __cb(_, __frame, 38, 25, function __$set__2() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$set__2() {

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

            return self.show(localNetworkName, options, __cb(_, __frame, 42, 9, function __$set__2() { _(); }, true)); }); }); }, true)); }); },


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

      return self.networkConfig.get(__cb(_, __frame, 2, 43, function ___(__0, __1) { networkConfig = __1;
        networkConfig = networkConfig.VirtualNetworkConfiguration;

        if (!networkConfig.LocalNetworkSites) { networkConfig.LocalNetworkSites = []; } ;
        self.interaction.formatOutput(networkConfig.LocalNetworkSites, function(data) {
          if ((data.length === 0)) {
            self.output.warn($("No local network sites found")); }
           else {
            self.output.table(data, function(row, item) {
              row.cell($("Name"), item.Name);
              row.cell($("Gateway address"), item.VPNGatewayAddress);
              var address = item.AddressSpace[0];
              if ((item.AddressSpace.length > 1)) { address += ", ..."; };
              row.cell($("Address space"), address); }); } ; }); _(); }, true)); }); },





  show: function show__4(localNetworkName, options, _) { var self, networkConfig, localNetwork, __this = this; var __frame = { name: "show__4", line: 143 }; return __func(_, this, arguments, show__4, 2, __frame, function __$show__4() { self = __this;

      return self.networkConfig.get(__cb(_, __frame, 2, 43, function ___(__0, __1) { networkConfig = __1;
        networkConfig = networkConfig.VirtualNetworkConfiguration;

        if (!networkConfig.LocalNetworkSites) { networkConfig.LocalNetworkSites = []; } ;
        localNetwork = utils.findFirstCaseIgnore(networkConfig.LocalNetworkSites, { Name: localNetworkName });
        if (localNetwork) {
          self.interaction.formatOutput(localNetwork, function(network) {
            self.output.nameValue($("Name"), network.Name);
            self.output.nameValue($("Gateway address"), network.VPNGatewayAddress);
            self.output.header("Address space");
            network.AddressSpace.forEach(function(address) {
              self.output.listItem(address, 2); }); }); }


         else {
          if (self.output.format().json) {
            self.output.json({ }); }
           else {
            self.output.warn(util.format($("A local network with name \"%s\" not found"), localNetworkName)); } ; } ; _(); }, true)); }); },




  delete: function delete__5(localNetworkName, options, _) { var self, networkConfig, localNetworkIndex, progress, __this = this; var __frame = { name: "delete__5", line: 168 }; return __func(_, this, arguments, delete__5, 2, __frame, function __$delete__5() { self = __this;

      return self.networkConfig.get(__cb(_, __frame, 2, 43, function ___(__0, __2) { networkConfig = __2;
        if (!networkConfig.VirtualNetworkConfiguration.LocalNetworkSites) {
          return _(new Error($("Network configuration does not have any local network sites"))); } ;


        networkConfig.VirtualNetworkConfiguration.VirtualNetworkSites.forEach(function(net) {
          if (net.Gateway) {
            var associationFound = utils.findFirstCaseIgnore(net.Gateway.ConnectionsToLocalNetwork, { Name: localNetworkName });
            if (associationFound) {
              throw new Error(util.format($("Cannot delete local network \"%s\" because it is associated with the virtual network \"%s\""), localNetworkName, net.Name)); } ; } ; });




        localNetworkIndex = utils.indexOfCaseIgnore(networkConfig.VirtualNetworkConfiguration.LocalNetworkSites, { Name: localNetworkName });
        if ((localNetworkIndex === -1)) {
          return _(new Error(util.format($("Local network \"%s\" does not exist"), localNetworkName))); } ;


        networkConfig.VirtualNetworkConfiguration.LocalNetworkSites.splice(localNetworkIndex, 1); return (function __$delete__5(_) {

          var __1 = !options.quiet; if (!__1) { return _(null, __1); } ; return self.interaction.confirm(util.format($("Delete local network site \"%s\"? [y/n] "), localNetworkName), __cb(_, __frame, 23, 44, function ___(__0, __3) { var __2 = !__3; return _(null, __2); }, true)); })(__cb(_, __frame, -167, 17, function ___(__0, __3) { return (function __$delete__5(__then) { if (__3) { return _(null); } else { __then(); } ; })(function __$delete__5() {



            progress = self.interaction.progress(util.format($("Deleting local network site \"%s\""), localNetworkName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$delete__5() {

                  return self.networkConfig.set(networkConfig, __cb(_, __frame, 29, 25, function __$delete__5() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$delete__5() {

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



  addAssociation: function addAssociation__6(virtualNetworkName, localNetworkName, options, _) { var self, networkConfig, localNetworkIndex, virtualNetworkIndex, progress, __this = this; var __frame = { name: "addAssociation__6", line: 203 }; return __func(_, this, arguments, addAssociation__6, 3, __frame, function __$addAssociation__6() { self = __this;

      return self.networkConfig.get(__cb(_, __frame, 2, 43, function ___(__0, __1) { networkConfig = __1;

        if (!networkConfig.VirtualNetworkConfiguration.LocalNetworkSites) {
          return _(new Error($("Network configuration does not have any local network sites"))); } ;


        if (!networkConfig.VirtualNetworkConfiguration.VirtualNetworkSites) {
          return _(new Error($("Network configuration does not have any local network sites"))); } ;


        localNetworkIndex = utils.indexOfCaseIgnore(networkConfig.VirtualNetworkConfiguration.LocalNetworkSites, { Name: localNetworkName });
        if ((localNetworkIndex === -1)) {
          return _(new Error(util.format($("Local network \"%s\" does not exist"), localNetworkName))); } ;


        virtualNetworkIndex = utils.indexOfCaseIgnore(networkConfig.VirtualNetworkConfiguration.VirtualNetworkSites, { Name: virtualNetworkName });
        if ((virtualNetworkIndex === -1)) {
          return _(new Error(util.format($("Virtual network \"%s\" does not exist"), virtualNetworkName))); } ;


        if (!networkConfig.VirtualNetworkConfiguration.VirtualNetworkSites[virtualNetworkIndex].Gateway) {
          networkConfig.VirtualNetworkConfiguration.VirtualNetworkSites[virtualNetworkIndex].Gateway = { }; } ;


        if (!networkConfig.VirtualNetworkConfiguration.VirtualNetworkSites[virtualNetworkIndex].Gateway.ConnectionsToLocalNetwork) {
          networkConfig.VirtualNetworkConfiguration.VirtualNetworkSites[virtualNetworkIndex].Gateway.ConnectionsToLocalNetwork = []; } ;


        if (utils.findFirstCaseIgnore(networkConfig.VirtualNetworkConfiguration.VirtualNetworkSites[virtualNetworkIndex].Gateway.ConnectionsToLocalNetwork, { Name: localNetworkName })) {
          return _(new Error(util.format($("Local network \"%s\" already associated with virtual network \"%s\""), localNetworkName, virtualNetworkName))); } ;


        networkConfig.VirtualNetworkConfiguration.VirtualNetworkSites[virtualNetworkIndex].Gateway.ConnectionsToLocalNetwork.push({
          Name: localNetworkName,
          Connection: { Type: "IPsec" } });


        self.output.warn($("A subnet with name \"GatewaySubnet\" must exist in the virtual network in-order to associate a local network with the virtual network"));

        progress = self.interaction.progress(util.format($("Associating local network \"%s\" with a virtual network \"%s\""), localNetworkName, virtualNetworkName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$addAssociation__6() {

              return self.networkConfig.set(networkConfig, __cb(_, __frame, 43, 25, function __$addAssociation__6() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$addAssociation__6() {

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



  removeAssociation: function removeAssociation__7(virtualNetworkName, localNetworkName, options, _) { var self, networkConfig, localNetworkIndex, virtualNetworkIndex, connectionIndex, progress, __this = this; var __frame = { name: "removeAssociation__7", line: 252 }; return __func(_, this, arguments, removeAssociation__7, 3, __frame, function __$removeAssociation__7() { self = __this;

      return self.networkConfig.get(__cb(_, __frame, 2, 43, function ___(__0, __1) { networkConfig = __1;

        if (!networkConfig.VirtualNetworkConfiguration.LocalNetworkSites) {
          return _(new Error($("Network configuration does not have any local network sites"))); } ;


        if (!networkConfig.VirtualNetworkConfiguration.VirtualNetworkSites) {
          return _(new Error($("Network configuration does not have any virtual network sites"))); } ;


        localNetworkIndex = utils.indexOfCaseIgnore(networkConfig.VirtualNetworkConfiguration.LocalNetworkSites, { Name: localNetworkName });
        if ((localNetworkIndex === -1)) {
          return _(new Error(util.format($("Local network \"%s\" does not exist"), localNetworkName))); } ;


        virtualNetworkIndex = utils.indexOfCaseIgnore(networkConfig.VirtualNetworkConfiguration.VirtualNetworkSites, { Name: virtualNetworkName });
        if ((virtualNetworkIndex === -1)) {
          return _(new Error(util.format($("Virtual network \"%s\" does not exist"), virtualNetworkName))); } ;


        if (!networkConfig.VirtualNetworkConfiguration.VirtualNetworkSites[virtualNetworkIndex].Gateway) {
          return _(new Error(util.format($("Virtual network \"%s\" does not have any local network sites associated"), virtualNetworkName))); } ;


        if (!networkConfig.VirtualNetworkConfiguration.VirtualNetworkSites[virtualNetworkIndex].Gateway.ConnectionsToLocalNetwork) {
          return _(new Error(util.format($("Virtual network \"%s\" does not have any local network connections"), virtualNetworkName))); } ;


        connectionIndex = utils.indexOfCaseIgnore(networkConfig.VirtualNetworkConfiguration.VirtualNetworkSites[virtualNetworkIndex].Gateway.ConnectionsToLocalNetwork, { Name: localNetworkName });
        if ((connectionIndex === -1)) {
          return _(new Error(util.format($("Virtual network \"%s\" does not have connection to local network"), virtualNetworkName))); } ;


        networkConfig.VirtualNetworkConfiguration.VirtualNetworkSites[virtualNetworkIndex].Gateway.ConnectionsToLocalNetwork.splice(connectionIndex, 1);
        progress = self.interaction.progress(util.format($("Removing association between a local network \"%s\" and a virtual network \"%s\""), localNetworkName, virtualNetworkName)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$removeAssociation__7() {

              return self.networkConfig.set(networkConfig, __cb(_, __frame, 38, 25, function __$removeAssociation__7() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$removeAssociation__7() {

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



  _validateAddressPrefix: function(addressPrefix) {
    var self = this;

    if ((addressPrefix.split("/").length !== 2)) {
      throw new Error(util.format($("The address space \"%s\" is invalid."), addressPrefix)); } ;


    var ipValidated = self.vnetUtil.parseIPv4(addressPrefix.split("/")[0], "--address-prefix");
    if (ipValidated.error) {
      throw new Error(ipValidated.error); } ;


    var cidrValidated = self.vnetUtil.parseIPv4Cidr(addressPrefix);
    if (cidrValidated.error) {
      throw new Error(cidrValidated.error); } ; }});




module.exports = LocalNetwork;
