1 /*
2  * Graph data structure.
3  *
4  * --
5  * Copyright (C) 2016 Cumulus Networks, Inc.
6  *
7  * This file is part of GNU Zebra.
8  *
9  * GNU Zebra is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the
11  * Free Software Foundation; either version 2, or (at your option) any
12  * later version.
13  *
14  * GNU Zebra is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; see the file COPYING; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #ifndef _ZEBRA_COMMAND_GRAPH_H
25 #define _ZEBRA_COMMAND_GRAPH_H
26 
27 #include <stdbool.h>
28 #include "vector.h"
29 #include "buffer.h"
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 struct graph {
36 	vector nodes;
37 };
38 
39 struct graph_node {
40 	vector from; // nodes which have edges to this node
41 	vector to;   // nodes which this node has edges to
42 
43 	void *data;		 // node data
44 	void (*del)(void *data); // deletion callback
45 };
46 
47 struct graph *graph_new(void);
48 
49 /**
50  * Creates a new node.
51  *
52  * @struct graph the graph this node exists in
53  * @param[in] data this node's data
54  * @param[in] del data deletion callback
55  * @return the new node
56  */
57 struct graph_node *graph_new_node(struct graph *graph, void *data,
58 				  void (*del)(void *));
59 
60 /**
61  * Deletes a node.
62  *
63  * Before deletion, this function removes all edges to and from this node from
64  * any neighbor nodes.
65  *
66  * If *data and *del are non-null, the following call is made:
67  *   (*node->del) (node->data);
68  *
69  * @param[in] graph the graph this node belongs to
70  * @param[out] node pointer to node to delete
71  */
72 void graph_delete_node(struct graph *graph, struct graph_node *node);
73 
74 /**
75  * Makes a directed edge between two nodes.
76  *
77  * @param[in] from
78  * @param[in] to
79  * @return to
80  */
81 struct graph_node *graph_add_edge(struct graph_node *from,
82 				  struct graph_node *to);
83 
84 /**
85  * Removes a directed edge between two nodes.
86  *
87  * @param[in] from
88  * @param[in] to
89  */
90 void graph_remove_edge(struct graph_node *from, struct graph_node *to);
91 
92 /**
93  * Deletes a graph.
94  * Calls graph_delete_node on each node before freeing the graph struct itself.
95  *
96  * @param graph the graph to delete
97  */
98 void graph_delete_graph(struct graph *graph);
99 
100 /*
101  * Finds a node in the graph.
102  *
103  * @param[in] graph the graph to search in
104  * @param[in] data the data to key off
105  * @return the first graph node whose data pointer matches `data`
106  */
107 struct graph_node *graph_find_node(struct graph *graph, void *data);
108 
109 
110 /*
111  * Determines whether two nodes have a directed edge between them.
112  *
113  * @param from
114  * @param to
115  * @return whether there is a directed edge from `from` to `to`.
116  */
117 bool graph_has_edge(struct graph_node *from, struct graph_node *to);
118 
119 /*
120  * Depth-first search.
121  *
122  * Performs a depth-first traversal of the given graph, visiting each node
123  * exactly once and calling the user-provided callback for each visit.
124  *
125  * @param graph the graph to operate on
126  * @param start the node to take as the root
127  * @param dfs_cb callback called for each node visited in the traversal
128  * @param arg argument to provide to dfs_cb
129  */
130 void graph_dfs(struct graph *graph, struct graph_node *start,
131 	       void (*dfs_cb)(struct graph_node *, void *), void *arg);
132 
133 #ifndef BUILDING_CLIPPY
134 /*
135  * Clippy relies on a small subset of sources in lib/, but it cannot link
136  * libfrr since clippy itself is required to build libfrr. Instead it directly
137  * includes the sources it needs. One of these is the command graph
138  * implementation, which wraps this graph implementation. Since we need to use
139  * the buffer.[ch] sources here, which indirectly rely on most of libfrr, we
140  * have to ignore them when compiling clippy to avoid build dependency issues.
141  *
142  * TODO: Fix clippy build.
143  */
144 
145 /*
146  * Default node printer for use with graph_dump_dot.
147  *
148  * @param gn the node to print
149  * @param buf the buffer to print into
150  */
151 void graph_dump_dot_default_print_cb(struct graph_node *gn, struct buffer *buf);
152 
153 /*
154  * Prints a graph in the DOT language.
155  *
156  * The generated output is produced from a depth-first traversal of the graph.
157  *
158  * @param graph the graph to print
159  * @param start the node to take as the root
160  * @param pcb callback called for each node in the traversal that should
161  *        print the node in the DOT language. Passing NULL for this argument
162  *        will use the default printer. See graph_dump_dot_default_print_cb for
163  *        an example.
164  * @return representation of graph in DOT language, allocated with MTYPE_TMP.
165  *         Caller is responsible for freeing this string.
166  */
167 char *graph_dump_dot(struct graph *graph, struct graph_node *start,
168 		     void (*pcb)(struct graph_node *, struct buffer *buf));
169 
170 #endif /* BUILDING_CLIPPY */
171 
172 #ifdef __cplusplus
173 }
174 #endif
175 
176 #endif /* _ZEBRA_COMMAND_GRAPH_H */
177