aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-transport-rta/ccnx/api/control/cpi_Forwarding.c
blob: 3cd836d22869dcb30e910b6fa7fa3e3fafff65cf (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
/*
 * 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/controlPlaneInterface.h>
#include <ccnx/api/control/cpi_Forwarding.h>
#include <LongBow/runtime.h>

#include "cpi_private.h"

static const char *cpiRegister = "REGISTER";
static const char *cpiUnregister = "UNREGISTER";
static const char *cpiRouteList = "ROUTE_LIST";
static const char *cpiSetStrategy = "SET_STRATEGY";

PARCJSON *
cpiForwarding_CreateSetStrategyRequest(CPIForwardingStrategy *fwdStrategy)
{
    PARCJSON *json = cpiForwardingStrategy_ToJson(fwdStrategy);
    PARCJSON *result = cpi_CreateRequest(cpiSetStrategy, json);
    parcJSON_Release(&json);

    return result;
}

PARCJSON *
cpiForwarding_CreateAddRouteRequest(const CPIRouteEntry *route)
{
    PARCJSON *routeAsJSON = cpiRouteEntry_ToJson(route);
    PARCJSON *result = cpi_CreateRequest(cpiRegister, routeAsJSON);
    parcJSON_Release(&routeAsJSON);

    return result;
}

PARCJSON *
cpiForwarding_CreateRemoveRouteRequest(const CPIRouteEntry *route)
{
    PARCJSON *routeAsJSON = cpiRouteEntry_ToJson(route);
    PARCJSON *result = cpi_CreateRequest(cpiUnregister, routeAsJSON);
    parcJSON_Release(&routeAsJSON);

    return result;
}

PARCJSON *
cpiForwarding_AddRouteToSelf(const CCNxName *prefix)
{
    CPIRouteEntry *route = cpiRouteEntry_CreateRouteToSelf(prefix);
    PARCJSON *result = cpiForwarding_AddRoute(route);
    cpiRouteEntry_Destroy(&route);
    return result;
}

PARCJSON *
cpiForwarding_RemoveRouteToSelf(const CCNxName *prefix)
{
    CPIRouteEntry *route = cpiRouteEntry_CreateRouteToSelf(prefix);
    PARCJSON *result = cpiForwarding_RemoveRoute(route);
    cpiRouteEntry_Destroy(&route);
    return result;
}

PARCJSON *
cpiForwarding_AddRoute(const CPIRouteEntry *route)
{
    PARCJSON *operation = cpiRouteEntry_ToJson(route);
    PARCJSON *result = cpi_CreateRequest(cpiRegister, operation);
    parcJSON_Release(&operation);

    return result;
}

PARCJSON *
cpiForwarding_RemoveRoute(const CPIRouteEntry *route)
{
    PARCJSON *operation = cpiRouteEntry_ToJson(route);
    PARCJSON *result = cpi_CreateRequest(cpiUnregister, operation);
    parcJSON_Release(&operation);

    return result;
}

CPIRouteEntry *
cpiForwarding_RouteFromControlMessage(CCNxControl *control)
{
    assertNotNull(control, "Parameter control must be non-null");
    PARCJSON *json = ccnxControl_GetJson(control);

    PARCJSONPair *routeOpPair = cpi_ParseRequest(json);
    PARCJSON *routeJson = parcJSONValue_GetJSON(parcJSONPair_GetValue(routeOpPair));

    CPIRouteEntry *route = cpiRouteEntry_FromJson(routeJson);

    return route;
}

CPIForwardingStrategy *
cpiForwarding_ForwardingStrategyFromControlMessage(CCNxControl *control)
{
    assertNotNull(control, "Parameter control must be non-null");
    PARCJSON *json = ccnxControl_GetJson(control);

    PARCJSONPair *fwdStrOpPair = cpi_ParseRequest(json);
    PARCJSON *fwdStrategyJson = parcJSONValue_GetJSON(parcJSONPair_GetValue(fwdStrOpPair));
    CPIForwardingStrategy *fwdStrategy = cpiForwardingStrategy_FromJson(fwdStrategyJson);

    return fwdStrategy;
}

const char *
cpiForwarding_AddRouteJsonTag()
{
    return cpiRegister;
}

const char *
cpiForwarding_RemoveRouteJsonTag()
{
    return cpiUnregister;
}

const char *
cpiForwarding_RouteListJsonTag()
{
    return cpiRouteList;
}

const char *
cpiForwarding_SetStrategyJsonTag()
{
    return cpiSetStrategy;
}


PARCJSON *
cpiForwarding_CreateRouteListRequest()
{
    PARCJSON *json = parcJSON_Create();
    PARCJSON *result = cpi_CreateRequest(cpiRouteList, json);
    parcJSON_Release(&json);

    return result;
}

CPIRouteEntryList *
cpiForwarding_RouteListFromControlMessage(CCNxControl *control)
{
    PARCJSON *json = ccnxControl_GetJson(control);
    PARCJSONValue *value = parcJSON_GetValueByName(json, cpiRequest_GetJsonTag());
    if (value == NULL) {
        value = parcJSON_GetValueByName(json, cpiResponse_GetJsonTag());
    }
    PARCJSON *innerJson = parcJSONValue_GetJSON(value);

    value = parcJSON_GetValueByName(innerJson, cpiRouteList);
    PARCJSON *operation = parcJSONValue_GetJSON(value);
    return cpiRouteEntryList_FromJson(operation);
}