blob: 325294acaf4f4fece50f3fa7707cd7295904d733 [file] [log] [blame]
Radek Krejcia53b3fe2015-10-19 17:25:04 +02001/**
2 * \file messages.c
3 * \author Radek Krejci <rkrejci@cesnet.cz>
4 * \brief libnetconf2 - NETCONF messages functions
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of the Company nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 */
22
23#include <stdlib.h>
Radek Krejci695d4fa2015-10-22 13:23:54 +020024#include <string.h>
Radek Krejcia53b3fe2015-10-19 17:25:04 +020025
26#include <libyang/libyang.h>
27
28#include "libnetconf.h"
29#include "messages_p.h"
30
Radek Krejci695d4fa2015-10-22 13:23:54 +020031API struct nc_filter *
32nc_filter_new(NC_FILTER type, char *data, int constdata)
33{
34 struct nc_filter *filter;
35
36 if (!data) {
37 data = "";
38 constdata = 1;
39 }
40
41 filter = malloc(sizeof *filter);
42 if (!filter) {
43 ERRMEM;
44 return NULL;
45 }
46
47 filter->type = type;
48 filter->refs = 1;
49 if (constdata) {
50 filter->data = strdup(data);
51 } else {
52 filter->data = data;
53 }
54
55 return filter;
56}
57
58API void
59nc_filter_free(struct nc_filter *filter)
60{
61 if (!filter) {
62 return;
63 }
64
65 filter->refs--;
66
67 if (!filter->refs) {
68 free(filter->data);
69 free(filter);
70 }
71}
72
73API struct nc_rpc *
Radek Krejci926a5742015-10-31 17:50:49 +010074nc_rpc_get(struct nc_filter *filter)
75{
76 struct nc_rpc_get *rpc;
77
78 rpc = calloc(1, sizeof *rpc);
79 if (!rpc) {
80 ERRMEM;
81 return NULL;
82 }
83
84 rpc->type = NC_RPC_GET;
85 if (filter) {
86 filter->refs++;
87 rpc->filter = filter;
88 }
89
90 return (struct nc_rpc *)rpc;
91}
92
93API struct nc_rpc *
Radek Krejci695d4fa2015-10-22 13:23:54 +020094nc_rpc_getconfig(NC_DATASTORE source, struct nc_filter *filter)
95{
96 struct nc_rpc_getconfig *rpc;
97
98 rpc = calloc(1, sizeof *rpc);
99 if (!rpc) {
100 ERRMEM;
101 return NULL;
102 }
103
104 rpc->type = NC_RPC_GETCONFIG;
105 rpc->source = source;
106 if (filter) {
107 filter->refs++;
108 rpc->filter = filter;
109 }
110
111 return (struct nc_rpc *)rpc;
112}
113
114API struct nc_rpc *
115nc_rpc_lock(NC_DATASTORE target)
116{
117 struct nc_rpc_lock *rpc;
118
119 rpc = malloc(sizeof *rpc);
120 if (!rpc) {
121 ERRMEM;
122 return NULL;
123 }
124
125 rpc->type = NC_RPC_LOCK;
126 rpc->target = target;
127
128 return (struct nc_rpc *)rpc;
129}
130
131API struct nc_rpc *
132nc_rpc_unlock(NC_DATASTORE target)
133{
134 struct nc_rpc_lock *rpc;
135
136 rpc = malloc(sizeof *rpc);
137 if (!rpc) {
138 ERRMEM;
139 return NULL;
140 }
141
142 rpc->type = NC_RPC_UNLOCK;
143 rpc->target = target;
144
145 return (struct nc_rpc *)rpc;
146}
147
Radek Krejcia53b3fe2015-10-19 17:25:04 +0200148API void
149nc_rpc_free(struct nc_rpc *rpc)
150{
Radek Krejci695d4fa2015-10-22 13:23:54 +0200151 struct nc_rpc_server *rpc_server;
152 struct nc_rpc_getconfig *rpc_getconfig;
153
Radek Krejcia53b3fe2015-10-19 17:25:04 +0200154 if (!rpc) {
155 return;
156 }
157
Radek Krejci695d4fa2015-10-22 13:23:54 +0200158 switch(rpc->type) {
159 case NC_RPC_SERVER:
160 rpc_server = (struct nc_rpc_server *)rpc;
161 lyxml_free_elem(rpc_server->ctx, rpc_server->root);
162 lyd_free(rpc_server->tree);
163 break;
164 case NC_RPC_GETCONFIG:
165 rpc_getconfig = (struct nc_rpc_getconfig *)rpc;
166 nc_filter_free(rpc_getconfig->filter);
167 break;
168 case NC_RPC_LOCK:
169 case NC_RPC_UNLOCK:
170 /* nothing special needed */
171 break;
172 }
173
Radek Krejcia53b3fe2015-10-19 17:25:04 +0200174 free(rpc);
175}
176
177API void
178nc_reply_free(struct nc_reply *reply)
179{
Radek Krejci8800a492015-10-31 17:51:27 +0100180 struct lyd_node *node;
181
Radek Krejcia53b3fe2015-10-19 17:25:04 +0200182 if (!reply) {
183 return;
184 }
185
Radek Krejci8800a492015-10-31 17:51:27 +0100186 switch(reply->type) {
187 case NC_REPLY_DATA:
188 for (node = ((struct nc_reply_data *)reply)->data;
189 node;
190 node = ((struct nc_reply_data *)reply)->data) {
191 ((struct nc_reply_data *)reply)->data = node->next;
192 lyd_free(node);
193 }
194 break;
195 case NC_REPLY_OK:
196 case NC_REPLY_ERROR:
197 /* TODO */
198 break;
199 }
Radek Krejcia53b3fe2015-10-19 17:25:04 +0200200 lyxml_free_elem(reply->ctx, reply->root);
Radek Krejcia53b3fe2015-10-19 17:25:04 +0200201 free(reply);
202}
203API void
204nc_notif_free(struct nc_notif *notif)
205{
206 if (!notif) {
207 return;
208 }
209
210 lyxml_free_elem(notif->ctx, notif->root);
211 lyd_free(notif->tree);
212 free(notif);
213}