isisd: API: basic area config

Move out basic area configuration (metric type, overload and attachment
bits, dynamic hostname extension enable) into isis_vty.c.

[v2: moved stuff back here that accidentally was in the previous patch]

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
diff --git a/isisd/isis_vty.c b/isisd/isis_vty.c
index 4b45df1..06d59a8 100644
--- a/isisd/isis_vty.c
+++ b/isisd/isis_vty.c
@@ -641,6 +641,172 @@
        "Specify metric for level-2 routing\n")
 /* end of metrics */
 
+static int
+validate_metric_style_narrow (struct vty *vty, struct isis_area *area)
+{
+  struct isis_circuit *circuit;
+  struct listnode *node;
+
+  if (! vty)
+    return CMD_ERR_AMBIGUOUS;
+
+  if (! area)
+    {
+      vty_out (vty, "ISIS area is invalid%s", VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
+    }
+
+  for (ALL_LIST_ELEMENTS_RO (area->circuit_list, node, circuit))
+    {
+      if ((area->is_type & IS_LEVEL_1) &&
+          (circuit->is_type & IS_LEVEL_1) &&
+          (circuit->te_metric[0] > MAX_NARROW_LINK_METRIC))
+        {
+          vty_out (vty, "ISIS circuit %s metric is invalid%s",
+                   circuit->interface->name, VTY_NEWLINE);
+          return CMD_ERR_AMBIGUOUS;
+        }
+      if ((area->is_type & IS_LEVEL_2) &&
+          (circuit->is_type & IS_LEVEL_2) &&
+          (circuit->te_metric[1] > MAX_NARROW_LINK_METRIC))
+        {
+          vty_out (vty, "ISIS circuit %s metric is invalid%s",
+                   circuit->interface->name, VTY_NEWLINE);
+          return CMD_ERR_AMBIGUOUS;
+        }
+    }
+
+  return CMD_SUCCESS;
+}
+
+DEFUN (metric_style,
+       metric_style_cmd,
+       "metric-style (narrow|transition|wide)",
+       "Use old-style (ISO 10589) or new-style packet formats\n"
+       "Use old style of TLVs with narrow metric\n"
+       "Send and accept both styles of TLVs during transition\n"
+       "Use new style of TLVs to carry wider metric\n")
+{
+  struct isis_area *area = vty->index;
+  int ret;
+
+  assert(area);
+
+  if (strncmp (argv[0], "w", 1) == 0)
+    {
+      isis_area_metricstyle_set(area, false, true);
+      return CMD_SUCCESS;
+    }
+
+  ret = validate_metric_style_narrow (vty, area);
+  if (ret != CMD_SUCCESS)
+    return ret;
+
+  if (strncmp (argv[0], "t", 1) == 0)
+    isis_area_metricstyle_set(area, true, true);
+  else if (strncmp (argv[0], "n", 1) == 0)
+    isis_area_metricstyle_set(area, true, false);
+      return CMD_SUCCESS;
+
+  return CMD_SUCCESS;
+}
+
+DEFUN (no_metric_style,
+       no_metric_style_cmd,
+       "no metric-style",
+       NO_STR
+       "Use old-style (ISO 10589) or new-style packet formats\n")
+{
+  struct isis_area *area = vty->index;
+  int ret;
+
+  assert (area);
+  ret = validate_metric_style_narrow (vty, area);
+  if (ret != CMD_SUCCESS)
+    return ret;
+
+  isis_area_metricstyle_set(area, true, false);
+  return CMD_SUCCESS;
+}
+
+DEFUN (set_overload_bit,
+       set_overload_bit_cmd,
+       "set-overload-bit",
+       "Set overload bit to avoid any transit traffic\n"
+       "Set overload bit\n")
+{
+  struct isis_area *area = vty->index;
+  assert (area);
+
+  isis_area_overload_bit_set(area, true);
+  return CMD_SUCCESS;
+}
+
+DEFUN (no_set_overload_bit,
+       no_set_overload_bit_cmd,
+       "no set-overload-bit",
+       "Reset overload bit to accept transit traffic\n"
+       "Reset overload bit\n")
+{
+  struct isis_area *area = vty->index;
+  assert (area);
+
+  isis_area_overload_bit_set(area, false);
+  return CMD_SUCCESS;
+}
+
+DEFUN (set_attached_bit,
+       set_attached_bit_cmd,
+       "set-attached-bit",
+       "Set attached bit to identify as L1/L2 router for inter-area traffic\n"
+       "Set attached bit\n")
+{
+  struct isis_area *area = vty->index;
+  assert (area);
+
+  isis_area_attached_bit_set(area, true);
+  return CMD_SUCCESS;
+}
+
+DEFUN (no_set_attached_bit,
+       no_set_attached_bit_cmd,
+       "no set-attached-bit",
+       "Reset attached bit\n")
+{
+  struct isis_area *area = vty->index;
+  assert (area);
+
+  isis_area_attached_bit_set(area, false);
+  return CMD_SUCCESS;
+}
+
+DEFUN (dynamic_hostname,
+       dynamic_hostname_cmd,
+       "hostname dynamic",
+       "Dynamic hostname for IS-IS\n"
+       "Dynamic hostname\n")
+{
+  struct isis_area *area = vty->index;
+  assert(area);
+
+  isis_area_dynhostname_set(area, true);
+  return CMD_SUCCESS;
+}
+
+DEFUN (no_dynamic_hostname,
+       no_dynamic_hostname_cmd,
+       "no hostname dynamic",
+       NO_STR
+       "Dynamic hostname for IS-IS\n"
+       "Dynamic hostname\n")
+{
+  struct isis_area *area = vty->index;
+  assert(area);
+
+  isis_area_dynhostname_set(area, false);
+  return CMD_SUCCESS;
+}
+
 void
 isis_vty_init (void)
 {
@@ -675,4 +841,16 @@
   install_element (INTERFACE_NODE, &isis_metric_l2_cmd);
   install_element (INTERFACE_NODE, &no_isis_metric_l2_cmd);
   install_element (INTERFACE_NODE, &no_isis_metric_l2_arg_cmd);
+
+  install_element (ISIS_NODE, &metric_style_cmd);
+  install_element (ISIS_NODE, &no_metric_style_cmd);
+
+  install_element (ISIS_NODE, &set_overload_bit_cmd);
+  install_element (ISIS_NODE, &no_set_overload_bit_cmd);
+
+  install_element (ISIS_NODE, &set_attached_bit_cmd);
+  install_element (ISIS_NODE, &no_set_attached_bit_cmd);
+
+  install_element (ISIS_NODE, &dynamic_hostname_cmd);
+  install_element (ISIS_NODE, &no_dynamic_hostname_cmd);
 }
diff --git a/isisd/isisd.c b/isisd/isisd.c
index d3bddc0..11f7d23 100644
--- a/isisd/isisd.c
+++ b/isisd/isisd.c
@@ -2177,213 +2177,47 @@
        "Set interval for level 2 only\n"
        "Minimum interval in seconds\n")
 
-static int
-validate_metric_style_narrow (struct vty *vty, struct isis_area *area)
+void isis_area_metricstyle_set(struct isis_area *area, bool old_metric,
+			       bool new_metric)
 {
-  struct isis_circuit *circuit;
-  struct listnode *node;
-  
-  if (! vty)
-    return CMD_ERR_AMBIGUOUS;
-
-  if (! area)
+  if (area->oldmetric != old_metric
+      || area->newmetric != new_metric)
     {
-      vty_out (vty, "ISIS area is invalid%s", VTY_NEWLINE);
-      return CMD_ERR_AMBIGUOUS;
+      area->oldmetric = old_metric;
+      area->newmetric = new_metric;
+      lsp_regenerate_schedule(area, IS_LEVEL_1 | IS_LEVEL_2, 1);
     }
+}
 
-  for (ALL_LIST_ELEMENTS_RO (area->circuit_list, node, circuit))
+void isis_area_overload_bit_set(struct isis_area *area, bool overload_bit)
+{
+  char new_overload_bit = overload_bit ? LSPBIT_OL : 0;
+
+  if (new_overload_bit != area->overload_bit)
     {
-      if ((area->is_type & IS_LEVEL_1) &&
-          (circuit->is_type & IS_LEVEL_1) &&
-          (circuit->te_metric[0] > MAX_NARROW_LINK_METRIC))
-        {
-          vty_out (vty, "ISIS circuit %s metric is invalid%s",
-                   circuit->interface->name, VTY_NEWLINE);
-          return CMD_ERR_AMBIGUOUS;
-        }
-      if ((area->is_type & IS_LEVEL_2) &&
-          (circuit->is_type & IS_LEVEL_2) &&
-          (circuit->te_metric[1] > MAX_NARROW_LINK_METRIC))
-        {
-          vty_out (vty, "ISIS circuit %s metric is invalid%s",
-                   circuit->interface->name, VTY_NEWLINE);
-          return CMD_ERR_AMBIGUOUS;
-        }
+      area->overload_bit = new_overload_bit;
+      lsp_regenerate_schedule(area, IS_LEVEL_1 | IS_LEVEL_2, 1);
     }
-
-  return CMD_SUCCESS;
 }
 
-DEFUN (metric_style,
-       metric_style_cmd,
-       "metric-style (narrow|transition|wide)",
-       "Use old-style (ISO 10589) or new-style packet formats\n"
-       "Use old style of TLVs with narrow metric\n"
-       "Send and accept both styles of TLVs during transition\n"
-       "Use new style of TLVs to carry wider metric\n")
+void isis_area_attached_bit_set(struct isis_area *area, bool attached_bit)
 {
-  struct isis_area *area;
-  int ret;
+  char new_attached_bit = attached_bit ? LSPBIT_ATT : 0;
 
-  area = vty->index;
-  assert (area);
-
-  if (strncmp (argv[0], "w", 1) == 0)
+  if (new_attached_bit != area->attached_bit)
     {
-      area->newmetric = 1;
-      area->oldmetric = 0;
+      area->attached_bit = new_attached_bit;
+      lsp_regenerate_schedule(area, IS_LEVEL_1 | IS_LEVEL_2, 1);
     }
-  else
+}
+
+void isis_area_dynhostname_set(struct isis_area *area, bool dynhostname)
+{
+  if (area->dynhostname != dynhostname)
     {
-      ret = validate_metric_style_narrow (vty, area);
-      if (ret != CMD_SUCCESS)
-        return ret;
-
-      if (strncmp (argv[0], "t", 1) == 0)
-	{
-	  area->newmetric = 1;
-	  area->oldmetric = 1;
-	}
-      else if (strncmp (argv[0], "n", 1) == 0)
-	{
-	  area->newmetric = 0;
-	  area->oldmetric = 1;
-	}
+      area->dynhostname = dynhostname;
+      lsp_regenerate_schedule(area, IS_LEVEL_1 | IS_LEVEL_2, 0);
     }
-
-  return CMD_SUCCESS;
-}
-
-DEFUN (no_metric_style,
-       no_metric_style_cmd,
-       "no metric-style",
-       NO_STR
-       "Use old-style (ISO 10589) or new-style packet formats\n")
-{
-  struct isis_area *area;
-  int ret;
-
-  area = vty->index;
-  assert (area);
-
-  ret = validate_metric_style_narrow (vty, area);
-  if (ret != CMD_SUCCESS)
-    return ret;
-
-  /* Default is narrow metric. */
-  area->newmetric = 0;
-  area->oldmetric = 1;
-
-  return CMD_SUCCESS;
-}
-
-DEFUN (set_overload_bit,
-       set_overload_bit_cmd,
-       "set-overload-bit",
-       "Set overload bit to avoid any transit traffic\n"
-       "Set overload bit\n")
-{
-  struct isis_area *area;
-
-  area = vty->index;
-  assert (area);
-
-  area->overload_bit = LSPBIT_OL;
-  lsp_regenerate_schedule (area, IS_LEVEL_1 | IS_LEVEL_2, 1);
-
-  return CMD_SUCCESS;
-}
-
-DEFUN (no_set_overload_bit,
-       no_set_overload_bit_cmd,
-       "no set-overload-bit",
-       "Reset overload bit to accept transit traffic\n"
-       "Reset overload bit\n")
-{
-  struct isis_area *area;
-
-  area = vty->index;
-  assert (area);
-
-  area->overload_bit = 0;
-  lsp_regenerate_schedule (area, IS_LEVEL_1 | IS_LEVEL_2, 1);
-
-  return CMD_SUCCESS;
-}
-
-DEFUN (set_attached_bit,
-       set_attached_bit_cmd,
-       "set-attached-bit",
-       "Set attached bit to identify as L1/L2 router for inter-area traffic\n"
-       "Set attached bit\n")
-{
-  struct isis_area *area;
-
-  area = vty->index;
-  assert (area);
-
-  area->attached_bit = LSPBIT_ATT;
-  lsp_regenerate_schedule (area, IS_LEVEL_1 | IS_LEVEL_2, 1);
-
-  return CMD_SUCCESS;
-}
-
-DEFUN (no_set_attached_bit,
-       no_set_attached_bit_cmd,
-       "no set-attached-bit",
-       "Reset attached bit\n")
-{
-  struct isis_area *area;
-
-  area = vty->index;
-  assert (area);
-
-  area->attached_bit = 0;
-  lsp_regenerate_schedule (area, IS_LEVEL_1 | IS_LEVEL_2, 1);
-
-  return CMD_SUCCESS;
-}
-
-DEFUN (dynamic_hostname,
-       dynamic_hostname_cmd,
-       "hostname dynamic",
-       "Dynamic hostname for IS-IS\n"
-       "Dynamic hostname\n")
-{
-  struct isis_area *area;
-
-  area = vty->index;
-  assert (area);
-
-  if (!area->dynhostname)
-   {
-     area->dynhostname = 1;
-     lsp_regenerate_schedule (area, IS_LEVEL_1 | IS_LEVEL_2, 0);
-   }
-
-  return CMD_SUCCESS;
-}
-
-DEFUN (no_dynamic_hostname,
-       no_dynamic_hostname_cmd,
-       "no hostname dynamic",
-       NO_STR
-       "Dynamic hostname for IS-IS\n"
-       "Dynamic hostname\n")
-{
-  struct isis_area *area;
-
-  area = vty->index;
-  assert (area);
-
-  if (area->dynhostname)
-    {
-      area->dynhostname = 0;
-      lsp_regenerate_schedule (area, IS_LEVEL_1 | IS_LEVEL_2, 0);
-    }
-
-  return CMD_SUCCESS;
 }
 
 DEFUN (spf_interval,
@@ -3420,18 +3254,6 @@
   install_element (ISIS_NODE, &no_lsp_refresh_interval_l2_cmd);
   install_element (ISIS_NODE, &no_lsp_refresh_interval_l2_arg_cmd);
 
-  install_element (ISIS_NODE, &set_overload_bit_cmd);
-  install_element (ISIS_NODE, &no_set_overload_bit_cmd);
-
-  install_element (ISIS_NODE, &set_attached_bit_cmd);
-  install_element (ISIS_NODE, &no_set_attached_bit_cmd);
-
-  install_element (ISIS_NODE, &dynamic_hostname_cmd);
-  install_element (ISIS_NODE, &no_dynamic_hostname_cmd);
-
-  install_element (ISIS_NODE, &metric_style_cmd);
-  install_element (ISIS_NODE, &no_metric_style_cmd);
-
   install_element (ISIS_NODE, &log_adj_changes_cmd);
   install_element (ISIS_NODE, &no_log_adj_changes_cmd);
 
diff --git a/isisd/isisd.h b/isisd/isisd.h
index 2803a64..691d134 100644
--- a/isisd/isisd.h
+++ b/isisd/isisd.h
@@ -141,6 +141,12 @@
 int isis_area_get (struct vty *vty, const char *area_tag);
 void print_debug(struct vty *, int, int);
 
+void isis_area_overload_bit_set(struct isis_area *area, bool overload_bit);
+void isis_area_attached_bit_set(struct isis_area *area, bool attached_bit);
+void isis_area_dynhostname_set(struct isis_area *area, bool dynhostname);
+void isis_area_metricstyle_set(struct isis_area *area, bool old_metric,
+			       bool new_metric);
+
 void isis_vty_init (void);
 
 /* Master of threads. */