aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-transport-rta/ccnx/api/control/cpi_InterfaceSet.c
blob: 0a9042cf00185624299a7951872dc6f977744ff5 (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
/*
 * 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 <ccnx/api/control/cpi_InterfaceSet.h>

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

#include <LongBow/runtime.h>

struct cpi_interface_set {
    PARCArrayList *listOfInterfaces;
};

static void
_destroyInterface(void **ifaceVoidPtr)
{
    cpiInterface_Destroy((CPIInterface **) ifaceVoidPtr);
}

CPIInterfaceSet *
cpiInterfaceSet_Create(void)
{
    CPIInterfaceSet *set = parcMemory_AllocateAndClear(sizeof(CPIInterfaceSet));
    assertNotNull(set, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(CPIInterfaceSet));
    set->listOfInterfaces = parcArrayList_Create(_destroyInterface);
    return set;
}

void
cpiInterfaceSet_Destroy(CPIInterfaceSet **setPtr)
{
    assertNotNull(setPtr, "Parameter must be non-null double pointer");
    assertNotNull(*setPtr, "Parameter must dereference to non-null pointer");

    CPIInterfaceSet *set = *setPtr;
    parcArrayList_Destroy(&set->listOfInterfaces);
    parcMemory_Deallocate((void **) &set);
    *setPtr = NULL;
}

bool
cpiInterfaceSet_Add(CPIInterfaceSet *set, CPIInterface *iface)
{
    assertNotNull(set, "Parameter set must be non-null");
    assertNotNull(iface, "Parameter iface must be non-null");

    unsigned ifaceIndex = cpiInterface_GetInterfaceIndex(iface);
    size_t length = parcArrayList_Size(set->listOfInterfaces);
    for (size_t i = 0; i < length; i++) {
        CPIInterface *listEntry = (CPIInterface *) parcArrayList_Get(set->listOfInterfaces, i);
        unsigned entryInterfaceIndex = cpiInterface_GetInterfaceIndex(listEntry);
        if (entryInterfaceIndex == ifaceIndex) {
            return false;
        }
    }

    parcArrayList_Add(set->listOfInterfaces, (PARCObject *) iface);
    return true;
}

size_t
cpiInterfaceSet_Length(const CPIInterfaceSet *set)
{
    assertNotNull(set, "Parameter set must be non-null");
    return parcArrayList_Size(set->listOfInterfaces);
}

CPIInterface *
cpiInterfaceSet_GetByOrdinalIndex(CPIInterfaceSet *set, size_t ordinalIndex)
{
    assertNotNull(set, "Parameter set must be non-null");
    return (CPIInterface *) parcArrayList_Get(set->listOfInterfaces, ordinalIndex);
}

CPIInterface *
cpiInterfaceSet_GetByInterfaceIndex(const CPIInterfaceSet *set, unsigned interfaceIndex)
{
    size_t length = parcArrayList_Size(set->listOfInterfaces);
    for (size_t i = 0; i < length; i++) {
        CPIInterface *listEntry = (CPIInterface *) parcArrayList_Get(set->listOfInterfaces, i);
        unsigned entryInterfaceIndex = cpiInterface_GetInterfaceIndex(listEntry);
        if (entryInterfaceIndex == interfaceIndex) {
            return listEntry;
        }
    }
    return NULL;
}

/**
 * Uses the system name (e.g. "en0")
 *
 *   <#Discussion#>
 *
 * @param <#param1#>
 * @return NULL if not found
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
CPIInterface *
cpiInterfaceSet_GetByName(CPIInterfaceSet *set, const char *name)
{
    size_t length = parcArrayList_Size(set->listOfInterfaces);
    for (size_t i = 0; i < length; i++) {
        CPIInterface *listEntry = (CPIInterface *) parcArrayList_Get(set->listOfInterfaces, i);
        if (cpiInterface_NameEquals(listEntry, name)) {
            return listEntry;
        }
    }
    return NULL;
}

bool
cpiInterfaceSet_Equals(const CPIInterfaceSet *a, const CPIInterfaceSet *b)
{
    if (a == NULL && b == NULL) {
        return true;
    }

    if (a == NULL || b == NULL) {
        return false;
    }

    size_t length_a = parcArrayList_Size(a->listOfInterfaces);
    size_t length_b = parcArrayList_Size(b->listOfInterfaces);

    if (length_a == length_b) {
        for (size_t i = 0; i < length_a; i++) {
            CPIInterface *iface_a = (CPIInterface *) parcArrayList_Get(a->listOfInterfaces, i);

            // the set is unique by interface id, so if it exists in set b, it
            // exists there by interface id
            CPIInterface *iface_b = cpiInterfaceSet_GetByInterfaceIndex(b, cpiInterface_GetInterfaceIndex(iface_a));
            if (!cpiInterface_Equals(iface_b, iface_b)) {
                return false;
            }
        }
        return true;
    }
    return false;
}

const char cpi_InterfaceList[] = "Interfaces";

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

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

    CPIInterfaceSet *set = cpiInterfaceSet_Create();

    size_t length = parcJSONArray_GetLength(ifaceSetJson);
    for (size_t i = 0; i < length; i++) {
        value = parcJSONArray_GetValue(ifaceSetJson, i);
        PARCJSON *ifaceJson = parcJSONValue_GetJSON(value);
        CPIInterface *iface = cpiInterface_FromJson(ifaceJson);
        cpiInterfaceSet_Add(set, iface);
    }

    return set;
}

PARCJSON *
cpiInterfaceSet_ToJson(CPIInterfaceSet *set)
{
    assertNotNull(set, "Parameter must be non-null");

    PARCJSONArray *interfaceList = parcJSONArray_Create();

    size_t length = parcArrayList_Size(set->listOfInterfaces);
    for (size_t i = 0; i < length; i++) {
        CPIInterface *iface = (CPIInterface *) parcArrayList_Get(set->listOfInterfaces, i);
        PARCJSON *json = cpiInterface_ToJson(iface);
        PARCJSONValue *value = parcJSONValue_CreateFromJSON(json);
        parcJSON_Release(&json);
        parcJSONArray_AddValue(interfaceList, value);
        parcJSONValue_Release(&value);
    }

    PARCJSON *result = parcJSON_Create();
    parcJSON_AddArray(result, cpi_InterfaceList, interfaceList);
    parcJSONArray_Release(&interfaceList);

    return result;
}