From 8a67c5f69a57524b142c99d27520848ea5103017 Mon Sep 17 00:00:00 2001
From: b29332 <bao.zhen@h3c.com>
Date: Fri, 18 Oct 2024 11:17:08 +0800
Subject: [PATCH] isisd: Command 'show isis interface IFNAME json' displays the
 circuit ID of all interfaces

1.before the commit
if the interface exists:
sonic# show isis interface lo json
{
 "areas":[
   {
     "area":"10",
     "circuits":[
       {
         "circuit":13
       },
       {
         "circuit":14
       },
       {
         "circuit":0,
         "interface":{
           "name":"lo",
           "state":"Up",
           "is-passive":"passive",
           "circuit-id":"0x0",
           "type":"loopback",
           "level":"L1",
           "levels":[
             {
               "level":"L1",
               "metric":10
             }
           ],
           "ip-prefix":{
             "ip":"7.7.7.7/32"
           }
         }
       }
     ]
   }
 ]
}

if the interface doesn't exist:
sonic# show isis interface abc json
{
 "areas":[
   {
     "area":"10",
     "circuits":[
       {
         "circuit":13
       },
       {
         "circuit":14
       },
       {
         "circuit":0
       }
     ]
   }
 ]
}

2.after the commit
if the interface exists:
sonic# show isis interface lo json
{
 "areas":[
   {
     "area":"10",
     "circuits":[
       {
         "circuit":0,
         "interface":{
           "name":"lo",
           "state":"Up",
           "is-passive":"passive",
           "circuit-id":"0x0",
           "type":"loopback",
           "level":"L1",
           "levels":[
             {
               "level":"L1",
               "metric":10
             }
           ],
           "ip-prefix":{
             "ip":"7.7.7.7/32"
           }
         }
       }
     ]
   }
 ]
}

if the interface doesn't exist:
sonic# show isis interface abc json
{
 "areas":[
   {
     "area":"10",
     "circuits":[
     ]
   }
 ]
}

Signed-off-by: baozhen-H3C <bao.zhen@h3c.com>
---
 isisd/isis_circuit.c |  2 ++
 isisd/isisd.c        | 37 ++++++++++++-------------------------
 2 files changed, 14 insertions(+), 25 deletions(-)

diff --git a/isisd/isis_circuit.c b/isisd/isis_circuit.c
index fa1ce3007f8a..db73f21f84b9 100644
--- a/isisd/isis_circuit.c
+++ b/isisd/isis_circuit.c
@@ -955,6 +955,8 @@ void isis_circuit_print_json(struct isis_circuit *circuit,
 	char buf_prx[INET6_BUFSIZ];
 	char buf[255];
 
+	json_object_int_add(json, "circuit", circuit->circuit_id);
+
 	snprintfrr(buf, sizeof(buf), "0x%x", circuit->circuit_id);
 	if (detail == ISIS_UI_LEVEL_BRIEF) {
 		iface_json = json_object_new_object();
diff --git a/isisd/isisd.c b/isisd/isisd.c
index 2863fd913f8f..93e778a84a8e 100644
--- a/isisd/isisd.c
+++ b/isisd/isisd.c
@@ -1043,20 +1043,14 @@ int show_isis_interface_common_json(struct json_object *json,
 				for (ALL_LIST_ELEMENTS_RO(area->circuit_list,
 							  cnode, circuit)) {
 					circuit_json = json_object_new_object();
-					json_object_int_add(
-						circuit_json, "circuit",
-						circuit->circuit_id);
-					if (!ifname)
+					if (!ifname || strmatch(circuit->interface->name, ifname)) {
 						isis_circuit_print_json(circuit,
 									circuit_json,
 									detail);
-					else if (strcmp(circuit->interface->name,
-							ifname) == 0)
-						isis_circuit_print_json(circuit,
-									circuit_json,
-									detail);
-					json_object_array_add(circuits_json,
-							      circuit_json);
+						json_object_array_add(circuits_json, circuit_json);
+						if (ifname)
+							break;
+					}
 				}
 				json_object_array_add(areas_json, area_json);
 			}
@@ -1076,22 +1070,16 @@ int show_isis_interface_common_json(struct json_object *json,
 			circuits_json = json_object_new_array();
 			json_object_object_add(area_json, "circuits",
 					       circuits_json);
-			for (ALL_LIST_ELEMENTS_RO(area->circuit_list, cnode,
-						  circuit)) {
+			for (ALL_LIST_ELEMENTS_RO(area->circuit_list, cnode, circuit)) {
 				circuit_json = json_object_new_object();
-				json_object_int_add(circuit_json, "circuit",
-						    circuit->circuit_id);
-				if (!ifname)
+				if (!ifname || strmatch(circuit->interface->name, ifname)) {
 					isis_circuit_print_json(circuit,
 								circuit_json,
 								detail);
-				else if (strcmp(circuit->interface->name,
-						ifname) == 0)
-					isis_circuit_print_json(circuit,
-								circuit_json,
-								detail);
-				json_object_array_add(circuits_json,
-						      circuit_json);
+					json_object_array_add(circuits_json, circuit_json);
+					if (ifname)
+						break;
+				}
 			}
 			json_object_array_add(areas_json, area_json);
 		}
@@ -1297,8 +1285,7 @@ static void isis_neighbor_common_json(struct json_object *json, const char *id,
 				for (i = 0; i < 2; i++) {
 					adjdb = circuit->u.bc.adjdb[i];
 					if (adjdb && adjdb->count) {
-						for (ALL_LIST_ELEMENTS_RO(
-							     adjdb, node, adj))
+						for (ALL_LIST_ELEMENTS_RO(adjdb, node, adj))
 							if (!id ||
 							    !memcmp(adj->sysid,
 								    sysid,