1 /*
2  * Copyright (C) 2018        Vmware
3  *                           Vishal Dhingra
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the Free
7  * Software Foundation; either version 2 of the License, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; see the file COPYING; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #include "northbound.h"
21 #include "libfrr.h"
22 #include "vrf.h"
23 #include "lib_errors.h"
24 #include "routing_nb.h"
25 
26 
27 DEFINE_HOOK(routing_conf_event, (struct nb_cb_create_args *args), (args))
28 
29 /*
30  * XPath: /frr-routing:routing/control-plane-protocols/control-plane-protocol
31  */
32 
routing_control_plane_protocols_control_plane_protocol_create(struct nb_cb_create_args * args)33 int routing_control_plane_protocols_control_plane_protocol_create(
34 	struct nb_cb_create_args *args)
35 {
36 	struct vrf *vrf;
37 	const char *vrfname;
38 
39 	switch (args->event) {
40 	case NB_EV_VALIDATE:
41 		if (hook_call(routing_conf_event, args))
42 			return NB_ERR_VALIDATION;
43 		break;
44 	case NB_EV_PREPARE:
45 	case NB_EV_ABORT:
46 		break;
47 	case NB_EV_APPLY:
48 		/*
49 		 * If the daemon relies on the VRF pointer stored in this
50 		 * dnode, then it should register the dependency between this
51 		 * module and the VRF module using
52 		 * routing_control_plane_protocols_register_vrf_dependency.
53 		 * If such dependency is not registered, then nothing is
54 		 * stored in the dnode. If the dependency is registered,
55 		 * find the vrf and store the pointer.
56 		 */
57 		if (nb_node_has_dependency(args->dnode->schema->priv)) {
58 			vrfname = yang_dnode_get_string(args->dnode, "./vrf");
59 			vrf = vrf_lookup_by_name(vrfname);
60 			assert(vrf);
61 			nb_running_set_entry(args->dnode, vrf);
62 		}
63 		break;
64 	};
65 
66 	return NB_OK;
67 }
68 
routing_control_plane_protocols_control_plane_protocol_destroy(struct nb_cb_destroy_args * args)69 int routing_control_plane_protocols_control_plane_protocol_destroy(
70 	struct nb_cb_destroy_args *args)
71 {
72 	if (args->event != NB_EV_APPLY)
73 		return NB_OK;
74 
75 	/*
76 	 * If dependency on VRF module is registered, then VRF
77 	 * pointer was stored and must be cleared.
78 	 */
79 	if (nb_node_has_dependency(args->dnode->schema->priv))
80 		nb_running_unset_entry(args->dnode);
81 
82 	return NB_OK;
83 }
84 
vrf_to_control_plane_protocol(const struct lyd_node * dnode,char * xpath)85 static void vrf_to_control_plane_protocol(const struct lyd_node *dnode,
86 					  char *xpath)
87 {
88 	const char *vrf;
89 
90 	vrf = yang_dnode_get_string(dnode, "./name");
91 
92 	snprintf(xpath, XPATH_MAXLEN, FRR_ROUTING_KEY_XPATH_VRF, vrf);
93 }
94 
control_plane_protocol_to_vrf(const struct lyd_node * dnode,char * xpath)95 static void control_plane_protocol_to_vrf(const struct lyd_node *dnode,
96 					  char *xpath)
97 {
98 	const char *vrf;
99 
100 	vrf = yang_dnode_get_string(dnode, "./vrf");
101 
102 	snprintf(xpath, XPATH_MAXLEN, FRR_VRF_KEY_XPATH, vrf);
103 }
104 
routing_control_plane_protocols_register_vrf_dependency(void)105 void routing_control_plane_protocols_register_vrf_dependency(void)
106 {
107 	struct nb_dependency_callbacks cbs;
108 
109 	cbs.get_dependant_xpath = vrf_to_control_plane_protocol;
110 	cbs.get_dependency_xpath = control_plane_protocol_to_vrf;
111 
112 	nb_node_set_dependency_cbs(FRR_VRF_XPATH, FRR_ROUTING_XPATH, &cbs);
113 }
114