aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-transport-rta/ccnx/api/control/cpi_RouteEntryList.c
blob: c13b3f0da33bddfaf7883fcc6f18624692a1ca2c (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
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <config.h>
#include <stdio.h>

#include <parc/algol/parc_Memory.h>
#include <parc/algol/parc_ArrayList.h>

#include <ccnx/api/control/cpi_RouteEntryList.h>
#include <LongBow/runtime.h>

struct cpi_route_entry_list {
    PARCArrayList *listOfRouteEntries;
};

/**
 * PARCArrayList entry destroyer
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
static void
_cpiRouteEntryList_ArrayDestroyer(void **voidPtr)
{
    CPIRouteEntry **entryPtr = (CPIRouteEntry **) voidPtr;
    cpiRouteEntry_Destroy(entryPtr);
}

CPIRouteEntryList *
cpiRouteEntryList_Create()
{
    CPIRouteEntryList *list = parcMemory_AllocateAndClear(sizeof(CPIRouteEntryList));
    assertNotNull(list, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(CPIRouteEntryList));
    list->listOfRouteEntries = parcArrayList_Create(_cpiRouteEntryList_ArrayDestroyer);
    return list;
}

void
cpiRouteEntryList_Destroy(CPIRouteEntryList **listPtr)
{
    assertNotNull(listPtr, "Parameter must be non-null double pointer");
    assertNotNull(*listPtr, "Parameter must dereference to non-null pointer");
    CPIRouteEntryList *list = *listPtr;
    parcArrayList_Destroy(&list->listOfRouteEntries);
    parcMemory_Deallocate((void **) &list);
    *listPtr = NULL;
}

void
cpiRouteEntryList_Append(CPIRouteEntryList *list, CPIRouteEntry *entry)
{
    assertNotNull(list, "Parameter list must be non-null");
    assertNotNull(entry, "Parameter entry must be non-null");

    parcArrayList_Add(list->listOfRouteEntries, (PARCObject *) entry);
}

size_t
cpiRouteEntryList_Length(const CPIRouteEntryList *list)
{
    assertNotNull(list, "Parameter list must be non-null");
    return parcArrayList_Size(list->listOfRouteEntries);
}

CPIRouteEntry *
cpiRouteEntryList_Get(CPIRouteEntryList *list, size_t index)
{
    assertNotNull(list, "Parameter list must be non-null");
    CPIRouteEntry *original = (CPIRouteEntry *) parcArrayList_Get(list->listOfRouteEntries, index);
    return cpiRouteEntry_Copy(original);
}


bool
cpiRouteEntryList_Equals(const CPIRouteEntryList *a, const CPIRouteEntryList *b)
{
    if (a == NULL && b == NULL) {
        return true;
    }
    if (a == NULL || b == NULL) {
        return false;
    }

    if (parcArrayList_Size(a->listOfRouteEntries) == parcArrayList_Size(b->listOfRouteEntries)) {
        size_t length = parcArrayList_Size(a->listOfRouteEntries);
        for (size_t i = 0; i < length; i++) {
            CPIRouteEntry *route_a = (CPIRouteEntry *) parcArrayList_Get(a->listOfRouteEntries, i);
            CPIRouteEntry *route_b = (CPIRouteEntry *) parcArrayList_Get(b->listOfRouteEntries, i);
            if (!cpiRouteEntry_Equals(route_a, route_b)) {
                return false;
            }
        }
        return true;
    }
    return false;
}


const char cpi_RouteEntryList[] = "Routes";

PARCJSON *
cpiRouteEntryList_ToJson(const CPIRouteEntryList *list)
{
    assertNotNull(list, "Parameter must be non-null");

    PARCJSONArray *routeList = parcJSONArray_Create();

    size_t length = parcArrayList_Size(list->listOfRouteEntries);
    for (size_t i = 0; i < length; i++) {
        CPIRouteEntry *route = (CPIRouteEntry *) parcArrayList_Get(list->listOfRouteEntries, i);
        PARCJSON *json = cpiRouteEntry_ToJson(route);
        PARCJSONValue *value = parcJSONValue_CreateFromJSON(json);
        parcJSON_Release(&json);
        parcJSONArray_AddValue(routeList, value);
        parcJSONValue_Release(&value);
    }

    PARCJSON *result = parcJSON_Create();
    parcJSON_AddArray(result, cpi_RouteEntryList, routeList);
    parcJSONArray_Release(&routeList);

    return result;
}

CPIRouteEntryList *
cpiRouteEntryList_FromJson(PARCJSON *json)
{
    assertNotNull(json, "Parameter must be non-null");

    PARCJSONValue *value = parcJSON_GetValueByName(json, cpi_RouteEntryList);
    assertNotNull(value,
                  "JSON key not found %s: %s",
                  cpi_RouteEntryList,
                  parcJSON_ToString(json));
    PARCJSONArray *routeList = parcJSONValue_GetArray(value);

    CPIRouteEntryList *list = cpiRouteEntryList_Create();

    size_t length = parcJSONArray_GetLength(routeList);
    for (size_t i = 0; i < length; i++) {
        PARCJSONValue *value = parcJSONArray_GetValue(routeList, i);
        PARCJSON *routeJson = parcJSONValue_GetJSON(value);
        CPIRouteEntry *route = cpiRouteEntry_FromJson(routeJson);
        cpiRouteEntryList_Append(list, route);
    }

    return list;
}