-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetconfOperation.cc
250 lines (225 loc) · 5.76 KB
/
NetconfOperation.cc
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <sstream>
#include <iostream>
#include "DomUtils.hh"
#include "YdbStr.hh"
#include "Ydb.hh"
#include "NetconfException.hh"
#include "NetconfOperation.hh"
#include "NetconfResponse.hh"
#include "NetconfIdentities.hh"
#include "NetconfResponseFactory.hh"
using namespace xercesc;
// NetconfOperation
NetconfOperation::NetconfOperation(DOMNode* rpc) :
_rpc(rpc)
{
}
NetconfOperation*
NetconfOperation::create(DOMNode* rpc)
{
try {
if (!rpc->hasChildNodes()) {
std::stringstream ss;
ss << "<rpc>: does not have child nodes";
throw OperationFailed(ss, OperationFailed::Protocol);
}
for (DOMNode* n = rpc->getFirstChild();
n != 0;
n = n->getNextSibling()) {
if (n->getNodeType() != DOMNode::ELEMENT_NODE) {
continue;
}
char* name = XMLString::transcode(n->getLocalName());
if (strcmp(name, GET) == 0) {
return new Get(n);
} else if (strcmp(name, GET_CONFIG) == 0) {
return new GetConfig(n);
} else if (strcmp(name, EDIT_CONFIG) == 0) {
return new EditConfig(n);
} else if (strcmp(name, DELETE_CONFIG) == 0) {
return new DeleteConfig(n);
} else if (strcmp(name, COPY_CONFIG) == 0) {
return new CopyConfig(n);
} else if (strcmp(name, LOCK) == 0) {
return new Lock(n);
} else if (strcmp(name, UNLOCK) == 0) {
return new Unlock(n);
} else if (strcmp(name, CLOSE_SESSION) == 0) {
return new CloseSession(n);
} else if (strcmp(name, KILL_SESSION) == 0) {
return new KillSession(n);
}
std::stringstream ss;
ss << "Could not find operation: " << name << std::endl;
XMLString::release(&name);
throw OperationFailed(ss, OperationFailed::Protocol);
}
} catch (const DOMException& toCatch) {
char* message = XMLString::transcode(toCatch.msg);
std::stringstream ss;
ss << "Failed to get operation: " << message << std::endl;
XMLString::release(&message);
throw OperationFailed(ss, OperationFailed::Protocol);
}
}
// Get
Get::Get(DOMNode* rpc) :
NetconfOperation(rpc)
{
}
DOMNode*
Get::apply(Ydb& ydb)
{
std::string xpath; // @todo implement xpath filter
return ydb.get(xpath);
}
// GetConfig
GetConfig::GetConfig(DOMNode* rpc) :
NetconfOperation(rpc)
{
}
DOMNode*
GetConfig::apply(Ydb& ydb)
{
std::stringstream ss;
ss << "get-config not supported";
throw OperationNotSupported(ss, OperationNotSupported::Protocol);
}
// EditConfig
EditConfig::EditConfig(DOMNode* rpc) :
NetconfOperation(rpc)
{
}
DOMNode*
EditConfig::apply(Ydb& ydb)
{
DOMNode* configNode = nullptr;
DOMNode* errorOptionNode = nullptr;
YdbStr defaultOperation(NetconfIdentities::MERGE);
for (DOMNode* n = _rpc->getFirstChild();
n != 0;
n = n->getNextSibling()) {
if (n->getNodeType() != DOMNode::ELEMENT_NODE) {
continue;
}
YdbStr name(n->getLocalName());
if (name == NetconfIdentities::CONFIG) {
configNode = n;
break;
} else if (name == NetconfIdentities::ERROR_OPTION) {
if (errorOptionNode != nullptr) {
std::stringstream ss;
ss << __PRETTY_FUNCTION__ << " mulltiple error-options ";
throw BadElement(ss, BadElement::Protocol);
}
errorOptionNode = n;
} else if (name == NetconfIdentities::DEFAULT_OPERATION) {
defaultOperation = DomUtils::getText(n);
}
}
if (configNode != nullptr) {
Ydb::ErrorOption errorOption = getErrorOption(errorOptionNode);
Ydb::EditOperation operation;
if (defaultOperation == NetconfIdentities::MERGE) {
operation = Ydb::Merge;
} else if (defaultOperation == NetconfIdentities::REPLACE) {
operation = Ydb::Replace;
} else if (defaultOperation == NetconfIdentities::NONE) {
operation = Ydb::None;
} else {
// throw
}
ydb.edit(configNode, operation, errorOption);
} else {
std::cerr << "edit-config: did not contain config node"
<< std::endl;
}
return nullptr;
}
Ydb::ErrorOption
EditConfig::getErrorOption(xercesc::DOMNode* errorOptionNode)
{
Ydb::ErrorOption result = Ydb::StopOnError;
if (errorOptionNode == nullptr) {
return result;
}
YdbStr option = DomUtils::getText(errorOptionNode);
if (option == NetconfIdentities::CONTINUE_ON_ERROR) {
result = Ydb::ContinueOnError;
} else if (option == NetconfIdentities::ROLLBACK_ON_ERROR) {
result = Ydb::RollbackOnError;
}
return result;
}
// CopyConfig
CopyConfig::CopyConfig(DOMNode* rpc) :
NetconfOperation(rpc)
{
}
DOMNode*
CopyConfig::apply(Ydb& ydb)
{
std::stringstream ss;
ss << "copy-config not supported";
throw OperationNotSupported(ss, OperationNotSupported::Application);
}
// DeleteConfig
DeleteConfig::DeleteConfig(DOMNode* rpc) :
NetconfOperation(rpc)
{
}
DOMNode*
DeleteConfig::apply(Ydb& ydb)
{
std::stringstream ss;
ss << "delete-config not supported";
throw OperationNotSupported(ss, OperationNotSupported::Application);
}
// Lock
Lock::Lock(DOMNode* rpc) :
NetconfOperation(rpc)
{
}
DOMNode*
Lock::apply(Ydb& ydb)
{
std::stringstream ss;
ss << "lock not supported";
throw OperationNotSupported(ss, OperationNotSupported::Application);
}
// unlock
Unlock::Unlock(DOMNode* rpc) :
NetconfOperation(rpc)
{
}
DOMNode*
Unlock::apply(Ydb& ydb)
{
std::stringstream ss;
ss << "unllock not supported";
throw OperationNotSupported(ss, OperationNotSupported::Application);
}
// CloseSession
CloseSession::CloseSession(DOMNode* rpc) :
NetconfOperation(rpc)
{
}
DOMNode*
CloseSession::apply(Ydb& ydb)
{
std::stringstream ss;
ss << "close-session not supported";
throw OperationNotSupported(ss, OperationNotSupported::Application);
}
// KillSession
KillSession::KillSession(DOMNode* rpc) :
NetconfOperation(rpc)
{
}
DOMNode*
KillSession::apply(Ydb& ydb)
{
std::stringstream ss;
ss << "kill-session not supported";
throw OperationNotSupported(ss, OperationNotSupported::Application);
}