aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-transport-rta/ccnx/api/control/cpi_ManageWldr.c
blob: d0799d6b5c4268eb0af978b02f99d2cab2ce5951 (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
/*
 * 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 <stdlib.h>
#include <sys/time.h>

#include <ccnx/api/control/cpi_ManageWldr.h>
#include <ccnx/api/control/controlPlaneInterface.h>
#include <parc/algol/parc_Memory.h>

#include <LongBow/runtime.h>

#include "cpi_private.h"

static const char *cpiWldrString = "WLDR";
static const char *cpiWldrConn = "CONN";
static const char *cpiWldrOn = "ON";
static const char *cpiWldrOff = "OFF";

struct cpi_manage_wldr {
    char *connectionId;
    bool active;
};

void
cpiManageWldr_Destroy(CPIManageWldr **cpiWldrPtr)
{
    assertNotNull(cpiWldrPtr, "Parameter must be non-null double pointer");
    assertNotNull(*cpiWldrPtr, "Parameter must dereference to non-null pointer");
    CPIManageWldr *cpiWldr = *cpiWldrPtr;

    parcMemory_Deallocate((void **) &cpiWldr->connectionId);
    parcMemory_Deallocate((void **) &cpiWldr);

    *cpiWldrPtr = NULL;
}

CPIManageWldr *
cpiManageWldr_Create(bool active, char *conn)
{
    CPIManageWldr *cpiWldr = parcMemory_AllocateAndClear(sizeof(cpiWldr));
    assertNotNull(cpiWldr, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(cpiWldr));

    cpiWldr->connectionId = parcMemory_StringDuplicate(conn, strlen(conn));
    cpiWldr->active = active;

    return cpiWldr;
}

char *
CPIManageWldr_ToString(CPIManageWldr *cpiWldr)
{
    PARCBufferComposer *composer = parcBufferComposer_Create();

    parcBufferComposer_PutString(composer, cpiManageWldr_GetConnection(cpiWldr));

    parcBufferComposer_PutString(composer, cpiWldrString);

    if (cpiManageWldr_IsActive(cpiWldr)) {
        parcBufferComposer_PutString(composer, cpiWldrOn);
    } else  {
        parcBufferComposer_PutString(composer, cpiWldrOff);
    }

    PARCBuffer *tempBuffer = parcBufferComposer_ProduceBuffer(composer);
    char *result = parcBuffer_ToString(tempBuffer);
    parcBuffer_Release(&tempBuffer);
    parcBufferComposer_Release(&composer);
    return result;
}

CPIManageWldr *
cpiManageWldr_Copy(const CPIManageWldr *original)
{
    assertNotNull(original, "Parameter a must be non-null");
    CPIManageWldr *copy = cpiManageWldr_Create(original->active,
                                               parcMemory_StringDuplicate(original->connectionId, strlen(original->connectionId)));

    return copy;
}

bool
cpiManageWldr_Equals(const CPIManageWldr *a, const CPIManageWldr *b)
{
    assertNotNull(a, "Parameter a must be non-null");
    assertNotNull(b, "Parameter b must be non-null");
    if (a == b) {
        return true;
    }

    if ((a->active == b->active) && (strcmp(a->connectionId, b->connectionId) == 0)) {
        return true;
    }

    return false;
}

bool
cpiManageWldr_IsActive(const CPIManageWldr *cpiWldr)
{
    assertNotNull(cpiWldr, "Parameter must be non-null");
    return cpiWldr->active;
}

const char *
cpiManageWldr_GetConnection(const CPIManageWldr *cpiWldr)
{
    assertNotNull(cpiWldr, "Parameter must be non-null");
    return cpiWldr->connectionId;
}


PARCJSON *
cpiManageWldr_ToJson(const CPIManageWldr *cpiWldr)
{
    PARCJSON *wldrJson = parcJSON_Create();

    parcJSON_AddString(wldrJson, cpiWldrConn, cpiWldr->connectionId);

    if (cpiWldr->active) {
        parcJSON_AddString(wldrJson, cpiWldrString, cpiWldrOn);
    } else {
        parcJSON_AddString(wldrJson, cpiWldrString, cpiWldrOff);
    }

    return wldrJson;
}
CPIManageWldr *
cpiManageWldr_FromJson(PARCJSON *json)
{
    assertNotNull(json, "Parameter json must be non-null");
    PARCJSON *cpiWldrJson = json;

    PARCJSONValue *value = parcJSON_GetValueByName(cpiWldrJson, cpiWldrConn);
    assertNotNull(value, "Couldn't locate tag %s in: %s", cpiWldrConn, parcJSON_ToString(json));

    PARCBuffer *wBuf = parcJSONValue_GetString(value);
    char *conn = parcBuffer_Overlay(wBuf, 0);

    value = parcJSON_GetValueByName(cpiWldrJson, cpiWldrString);
    assertNotNull(value, "Couldn't locate tag %s in: %s", cpiWldrString, parcJSON_ToString(json));

    wBuf = parcJSONValue_GetString(value);
    char *active = parcBuffer_Overlay(wBuf, 0);

    CPIManageWldr *cpiWldr;
    if (strcmp(active, cpiWldrOn) == 0) {
        cpiWldr = cpiManageWldr_Create(true, conn);
    } else  {
        cpiWldr = cpiManageWldr_Create(false, conn);
    }

    return cpiWldr;
}