summaryrefslogtreecommitdiffstats
path: root/libccnx-transport-rta/ccnx/api/control/cpi_InterfaceEthernet.c
blob: e6fb9621749b4b8212f32cab2387c06c5db912c1 (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
/*
 * 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_InterfaceEthernet.h>
#include <ccnx/api/control/cpi_InterfaceGeneric.h>
#include <LongBow/runtime.h>
#include <parc/algol/parc_Memory.h>
#include <string.h>

const static char cpiIFIDX[] = "IFIDX";
const static char cpiADDRS[] = "ADDRS";
const static char cpiSTATE[] = "STATE";

static const char *cpiAddEtherConnection = "AddConnEther";

struct cpi_interface_ethernet {
    CPIInterfaceGeneric *generic;
};

CPIInterfaceEthernet *
cpiInterfaceEthernet_Create(unsigned ifidx, CPIAddressList *addresses)
{
    assertNotNull(addresses, "Parameter addresses must be non-null");

    CPIInterfaceEthernet *ethernet = parcMemory_AllocateAndClear(sizeof(CPIInterfaceEthernet));
    assertNotNull(ethernet, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(CPIInterfaceEthernet));
    ethernet->generic = cpiInterfaceGeneric_Create(ifidx, addresses);
    return ethernet;
}

CPIInterfaceEthernet *
cpiInterfaceEthernet_Copy(const CPIInterfaceEthernet *original)
{
    assertNotNull(original, "Parameter original must be non-null");

    CPIInterfaceEthernet *ethernet = parcMemory_AllocateAndClear(sizeof(CPIInterfaceEthernet));
    assertNotNull(ethernet, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(CPIInterfaceEthernet));
    ethernet->generic = cpiInterfaceGeneric_Copy(original->generic);
    return ethernet;
}

void
cpiInterfaceEthernet_Destroy(CPIInterfaceEthernet **ethernetPtr)
{
    assertNotNull(ethernetPtr, "Parameter must be non-null double pointer");
    assertNotNull(*ethernetPtr, "Parameter must dereference to non-null pointer");

    CPIInterfaceEthernet *ethernet = *ethernetPtr;
    cpiInterfaceGeneric_Destroy(&ethernet->generic);
    parcMemory_Deallocate((void **) &ethernet);
    *ethernetPtr = NULL;
}

void
cpiInterfaceEthernet_SetState(CPIInterfaceEthernet *ethernet, CPIInterfaceStateType state)
{
    assertNotNull(ethernet, "Parameter must be non-null pointer");
    cpiInterfaceGeneric_SetState(ethernet->generic, state);
}

unsigned
cpiInterfaceEthernet_GetIndex(const CPIInterfaceEthernet *ethernet)
{
    assertNotNull(ethernet, "Parameter must be non-null pointer");
    return cpiInterfaceGeneric_GetIndex(ethernet->generic);
}

const CPIAddressList *
cpiInterfaceEthernet_GetAddresses(const CPIInterfaceEthernet *ethernet)
{
    assertNotNull(ethernet, "Parameter must be non-null pointer");
    return cpiInterfaceGeneric_GetAddresses(ethernet->generic);
}

CPIInterfaceStateType
cpiInterfaceEthernet_GetState(const CPIInterfaceEthernet *ethernet)
{
    assertNotNull(ethernet, "Parameter must be non-null pointer");
    return cpiInterfaceGeneric_GetState(ethernet->generic);
}

PARCJSON *
cpiInterfaceEthernet_ToJson(const CPIInterfaceEthernet *ethernet)
{
    assertNotNull(ethernet, "Parameter must be non-null");

    PARCJSON *innerJson = parcJSON_Create();

    parcJSON_AddInteger(innerJson, cpiIFIDX, cpiInterfaceEthernet_GetIndex(ethernet));

    if (cpiInterfaceEthernet_GetState(ethernet) != CPI_IFACE_UNKNOWN) {
        parcJSON_AddString(innerJson,
                           cpiSTATE,
                           cpiInterfaceStateType_ToString(cpiInterfaceEthernet_GetState(ethernet)));
    }

    PARCJSONArray *addrsArray = cpiAddressList_ToJson(cpiInterfaceEthernet_GetAddresses(ethernet));
    parcJSON_AddArray(innerJson, cpiADDRS, addrsArray);
    parcJSONArray_Release(&addrsArray);

    PARCJSON *result = parcJSON_Create();
    parcJSON_AddObject(result, cpiInterfaceType_ToString(CPI_IFACE_ETHERNET), innerJson);
    parcJSON_Release(&innerJson);

    return result;
}

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

    PARCJSONValue *value = parcJSON_GetValueByName(json, cpiInterfaceType_ToString(CPI_IFACE_ETHERNET));
    assertNotNull(value,
                  "JSON key not found %s: %s",
                  cpiInterfaceType_ToString(CPI_IFACE_ETHERNET),
                  parcJSON_ToString(json));
    PARCJSON *etherJson = parcJSONValue_GetJSON(value);

    value = parcJSON_GetValueByName(etherJson, cpiIFIDX);
    assertNotNull(value,
                  "JSON key not found %s: %s",
                  cpiIFIDX,
                  parcJSON_ToString(json));
    assertTrue(parcJSONValue_IsNumber(value),
               "%s is not a number: %s",
               cpiIFIDX,
               parcJSON_ToString(json));
    unsigned ifidx = (unsigned) parcJSONValue_GetInteger(value);

    value = parcJSON_GetValueByName(etherJson, cpiADDRS);
    assertNotNull(value,
                  "JSON key not found %s: %s",
                  cpiADDRS,
                  parcJSON_ToString(json));
    assertTrue(parcJSONValue_IsArray(value),
               "%s is not a number: %s",
               cpiADDRS,
               parcJSON_ToString(json));
    PARCJSONArray *addrsJson = parcJSONValue_GetArray(value);

    CPIAddressList *addrs = cpiAddressList_CreateFromJson(addrsJson);
    CPIInterfaceEthernet *ethernet = cpiInterfaceEthernet_Create(ifidx, addrs);

    value = parcJSON_GetValueByName(etherJson, cpiSTATE);
    if (value != NULL) {
        PARCBuffer *sBuf = parcJSONValue_GetString(value);
        char *state = parcBuffer_Overlay(sBuf, 0);
        cpiInterfaceEthernet_SetState(ethernet, cpiInterfaceStateType_FromString(state));
    }

    return ethernet;
}

bool
cpiInterfaceEthernet_Equals(const CPIInterfaceEthernet *a, const CPIInterfaceEthernet *b)
{
    assertNotNull(a, "Parameter a must be non-null");
    assertNotNull(b, "Parameter b must be non-null");

    return cpiInterfaceGeneric_Equals(a->generic, b->generic);
}

const char *
cpiLinks_AddEtherConnectionJasonTag()
{
    return cpiAddEtherConnection;
}