summaryrefslogtreecommitdiffstats
path: root/pceplib/pcep_utils_counters.c
blob: d8078f683f29d4777b6f84add8ca44e78b0a9be6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
 * This file is part of the PCEPlib, a PCEP protocol library.
 *
 * Copyright (C) 2020 Volta Networks https://voltanet.io/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * Author : Brady Johnson <brady@voltanet.io>
 *
 */


/*
 * Implementation of PCEP Counters.
 */

#include <zebra.h>

#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "pcep_utils_counters.h"
#include "pcep_utils_logging.h"
#include "pcep_utils_memory.h"

struct counters_group *create_counters_group(const char *group_name,
					     uint16_t max_subgroups)
{
	if (group_name == NULL) {
		pcep_log(
			LOG_INFO,
			"%s: Cannot create counters group: group_name is NULL.",
			__func__);
		return NULL;
	}

	if (max_subgroups > MAX_COUNTER_GROUPS) {
		pcep_log(
			LOG_INFO,
			"%s: Cannot create counters group: max_subgroups [%d] is larger than max the [%d].",
			__func__, max_subgroups, MAX_COUNTER_GROUPS);
		return NULL;
	}

	struct counters_group *group =
		pceplib_malloc(PCEPLIB_INFRA, sizeof(struct counters_group));
	memset(group, 0, sizeof(struct counters_group));
	group->subgroups =
		pceplib_malloc(PCEPLIB_INFRA, sizeof(struct counters_subgroup *)
						      * (max_subgroups + 1));
	memset(group->subgroups, 0,
	       sizeof(struct counters_subgroup *) * (max_subgroups + 1));

	strlcpy(group->counters_group_name, group_name,
		sizeof(group->counters_group_name));
	group->max_subgroups = max_subgroups;
	group->start_time = time(NULL);

	return group;
}

struct counters_subgroup *create_counters_subgroup(const char *subgroup_name,
						   uint16_t subgroup_id,
						   uint16_t max_counters)
{
	if (subgroup_name == NULL) {
		pcep_log(
			LOG_INFO,
			"%s: Cannot create counters subgroup: subgroup_name is NULL.",
			__func__);
		return NULL;
	}

	if (max_counters > MAX_COUNTERS) {
		pcep_log(
			LOG_INFO,
			"%s: Cannot create counters subgroup: max_counters [%d] is larger than the max [%d].",
			__func__, max_counters, MAX_COUNTERS);
		return NULL;
	}

	if (subgroup_id > MAX_COUNTER_GROUPS) {
		pcep_log(
			LOG_INFO,
			"%s: Cannot create counters subgroup: subgroup_id [%d] is larger than max the [%d].",
			__func__, subgroup_id, MAX_COUNTER_GROUPS);
		return NULL;
	}

	struct counters_subgroup *subgroup =
		pceplib_malloc(PCEPLIB_INFRA, sizeof(struct counters_subgroup));
	memset(subgroup, 0, sizeof(struct counters_subgroup));
	subgroup->counters = pceplib_malloc(
		PCEPLIB_INFRA, sizeof(struct counter *) * (max_counters + 1));
	memset(subgroup->counters, 0,
	       sizeof(struct counter *) * (max_counters + 1));

	strlcpy(subgroup->counters_subgroup_name, subgroup_name,
		sizeof(subgroup->counters_subgroup_name));
	subgroup->subgroup_id = subgroup_id;
	subgroup->max_counters = max_counters;

	return subgroup;
}

struct counters_subgroup *
clone_counters_subgroup(struct counters_subgroup *subgroup,
			const char *subgroup_name, uint16_t subgroup_id)
{
	if (subgroup == NULL) {
		pcep_log(
			LOG_INFO,
			"%s: Cannot clone counters subgroup: input counters_subgroup is NULL.",
			__func__);
		return NULL;
	}

	if (subgroup_name == NULL) {
		pcep_log(
			LOG_INFO,
			"%s: Cannot clone counters subgroup: subgroup_name is NULL.",
			__func__);
		return NULL;
	}

	if (subgroup_id > MAX_COUNTER_GROUPS) {
		pcep_log(
			LOG_INFO,
			"%s: Cannot clone counters subgroup: subgroup_id [%d] is larger than max the [%d].",
			__func__, subgroup_id, MAX_COUNTER_GROUPS);
		return NULL;
	}

	struct counters_subgroup *cloned_subgroup = create_counters_subgroup(
		subgroup_name, subgroup_id, subgroup->max_counters);
	int i = 0;
	for (; i <= subgroup->max_counters; i++) {
		struct counter *counter = subgroup->counters[i];
		if (counter != NULL) {
			create_subgroup_counter(cloned_subgroup,
						counter->counter_id,
						counter->counter_name);
		}
	}

	return cloned_subgroup;
}

bool add_counters_subgroup(struct counters_group *group,
			   struct counters_subgroup *subgroup)
{
	if (group == NULL) {
		pcep_log(
			LOG_INFO,
			"%s: Cannot add counters subgroup: counters_group is NULL.",
			__func__);
		return false;
	}

	if (subgroup == NULL) {
		pcep_log(
			LOG_INFO,
			"%s: Cannot add counters subgroup: counters_subgroup is NULL.",
			__func__);
		return false;
	}

	if (subgroup->subgroup_id >= group->max_subgroups) {
		pcep_log(
			LOG_INFO,
			"%s: Cannot add counters subgroup: counters_subgroup id [%d] is larger than the group max_subgroups [%d].",
			__func__, subgroup->subgroup_id, group->max_subgroups);
		return false;
	}

	group->num_subgroups++;
	group->subgroups[subgroup->subgroup_id] = subgroup;

	return true;
}

bool create_subgroup_counter(struct counters_subgroup *subgroup,
			     uint32_t counter_id, const char *counter_name)
{
	if (subgroup == NULL) {
		pcep_log(
			LOG_INFO,
			"%s: Cannot create subgroup counter: counters_subgroup is NULL.",
			__func__);
		return false;
	}

	if (counter_id >= subgroup->max_counters) {
		pcep_log(
			LOG_INFO,
			"%s: Cannot create subgroup counter: counter_id [%d] is larger than the subgroup max_counters [%d].",
			__func__, counter_id, subgroup->max_counters);
		return false;
	}

	if (counter_name == NULL) {
		pcep_log(
			LOG_INFO,
			"%s: Cannot create subgroup counter: counter_name is NULL.",
			__func__);
		return NULL;
	}

	struct counter *counter =
		pceplib_malloc(PCEPLIB_INFRA, sizeof(struct counter));
	memset(counter, 0, sizeof(struct counter));
	counter->counter_id = counter_id;
	strlcpy(counter->counter_name, counter_name,
		sizeof(counter->counter_name));

	subgroup->num_counters++;
	subgroup->counters[counter->counter_id] = counter;

	return true;
}

bool delete_counters_group(struct counters_group *group)
{
	if (group == NULL) {
		pcep_log(
			LOG_INFO,
			"%s: Cannot delete group counters: counters_group is NULL.",
			__func__);
		return false;
	}

	int i = 0;
	for (; i <= group->max_subgroups; i++) {
		struct counters_subgroup *subgroup = group->subgroups[i];
		if (subgroup != NULL) {
			delete_counters_subgroup(subgroup);
		}
	}

	pceplib_free(PCEPLIB_INFRA, group->subgroups);
	pceplib_free(PCEPLIB_INFRA, group);

	return true;
}

bool delete_counters_subgroup(struct counters_subgroup *subgroup)
{
	if (subgroup == NULL || subgroup->counters == NULL) {
		pcep_log(
			LOG_INFO,
			"%s: Cannot delete subgroup counters: counters_subgroup is NULL.",
			__func__);
		return false;
	}

	int i = 0;
	for (; i <= subgroup->max_counters; i++) {
		struct counter *counter = subgroup->counters[i];
		if (counter != NULL) {
			pceplib_free(PCEPLIB_INFRA, counter);
		}
	}

	pceplib_free(PCEPLIB_INFRA, subgroup->counters);
	pceplib_free(PCEPLIB_INFRA, subgroup);

	return true;
}

bool reset_group_counters(struct counters_group *group)
{
	if (group == NULL) {
		pcep_log(
			LOG_INFO,
			"%s: Cannot reset group counters: counters_group is NULL.",
			__func__);
		return false;
	}

	int i = 0;
	for (; i <= group->max_subgroups; i++) {
		struct counters_subgroup *subgroup = group->subgroups[i];
		if (subgroup != NULL) {
			reset_subgroup_counters(subgroup);
		}
	}

	group->start_time = time(NULL);

	return true;
}

bool reset_subgroup_counters(struct counters_subgroup *subgroup)
{
	if (subgroup == NULL) {
		pcep_log(
			LOG_INFO,
			"%s: Cannot reset subgroup counters: counters_subgroup is NULL.",
			__func__);
		return false;
	}

	int i = 0;
	for (; i <= subgroup->max_counters; i++) {
		struct counter *counter = subgroup->counters[i];
		if (counter != NULL) {
			counter->counter_value = 0;
		}
	}

	return true;
}

bool increment_counter(struct counters_group *group, uint16_t subgroup_id,
		       uint16_t counter_id)
{
	if (group == NULL) {
		pcep_log(
			LOG_INFO,
			"%s: Cannot increment counter: counters_group is NULL.",
			__func__);
		return false;
	}

	if (subgroup_id >= group->max_subgroups) {
		pcep_log(
			LOG_DEBUG,
			"%s: Cannot increment counter: subgroup_id [%d] is larger than the group max_subgroups [%d].",
			__func__, subgroup_id, group->max_subgroups);
		return false;
	}

	struct counters_subgroup *subgroup = group->subgroups[subgroup_id];
	if (subgroup == NULL) {
		pcep_log(
			LOG_INFO,
			"%s: Cannot increment counter: counters_subgroup in counters_group is NULL.",
			__func__);
		return false;
	}

	return increment_subgroup_counter(subgroup, counter_id);
}

bool increment_subgroup_counter(struct counters_subgroup *subgroup,
				uint16_t counter_id)
{
	if (subgroup == NULL) {
		pcep_log(
			LOG_INFO,
			"%s: Cannot increment counter: counters_subgroup is NULL.",
			__func__);
		return false;
	}

	if (counter_id >= subgroup->max_counters) {
		pcep_log(
			LOG_DEBUG,
			"%s: Cannot increment counter: counter_id [%d] is larger than the subgroup max_counters [%d].",
			__func__, counter_id, subgroup->max_counters);
		return false;
	}

	if (subgroup->counters[counter_id] == NULL) {
		pcep_log(
			LOG_INFO,
			"%s: Cannot increment counter: No counter exists for counter_id [%d].",
			__func__, counter_id);
		return false;
	}

	subgroup->counters[counter_id]->counter_value++;

	return true;
}

bool dump_counters_group_to_log(struct counters_group *group)
{
	if (group == NULL) {
		pcep_log(
			LOG_INFO,
			"%s: Cannot dump group counters to log: counters_group is NULL.",
			__func__);
		return false;
	}

	time_t now = time(NULL);
	pcep_log(
		LOG_INFO,
		"%s: PCEP Counters group:\n  %s \n  Sub-Groups [%d] \n  Active for [%d seconds]",
		__func__, group->counters_group_name, group->num_subgroups,
		(now - group->start_time));

	int i = 0;
	for (; i <= group->max_subgroups; i++) {
		struct counters_subgroup *subgroup = group->subgroups[i];
		if (subgroup != NULL) {
			dump_counters_subgroup_to_log(subgroup);
		}
	}

	return true;
}

bool dump_counters_subgroup_to_log(struct counters_subgroup *subgroup)
{
	if (subgroup == NULL) {
		pcep_log(
			LOG_INFO,
			"%s: Cannot dump subgroup counters to log: counters_subgroup is NULL.",
			__func__);
		return false;
	}

	pcep_log(LOG_INFO,
		 "%s: \tPCEP Counters sub-group [%s] with [%d] counters",
		 __func__, subgroup->counters_subgroup_name,
		 subgroup->num_counters);

	int i = 0;
	for (; i <= subgroup->max_counters; i++) {
		struct counter *counter = subgroup->counters[i];
		if (counter != NULL) {
			pcep_log(LOG_INFO, "%s: \t\t%s %d", __func__,
				 counter->counter_name, counter->counter_value);
		}
	}

	return true;
}

struct counters_subgroup *find_subgroup(const struct counters_group *group,
					uint16_t subgroup_id)
{
	int i = 0;
	for (; i <= group->max_subgroups; i++) {
		struct counters_subgroup *subgroup = group->subgroups[i];
		if (subgroup != NULL) {
			if (subgroup->subgroup_id == subgroup_id) {
				return subgroup;
			}
		}
	}

	return NULL;
}

uint32_t subgroup_counters_total(struct counters_subgroup *subgroup)
{
	if (subgroup == NULL) {
		return 0;
	}
	uint32_t counter_total = 0;
	int i = 0;
	for (; i <= subgroup->max_counters; i++) {
		struct counter *counter = subgroup->counters[i];
		if (counter != NULL) {
			counter_total += counter->counter_value;
		}
	}

	return counter_total;
}