Line data Source code
1 : /*
2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3 : * Copyright Contributors to the OpenSDN project
4 : */
5 :
6 : #include <cmn/agent.h>
7 : #include <oper_db.h>
8 : #include <vnc_cfg_types.h>
9 : #include <agent_types.h>
10 : #include <oper_db.h>
11 : #include <oper/config_manager.h>
12 : #include <oper/agent_sandesh.h>
13 : #include <qos_queue.h>
14 : #include <forwarding_class.h>
15 : #include <oper/ifmap_dependency_manager.h>
16 : #include <cfg/cfg_init.h>
17 :
18 0 : ForwardingClass::ForwardingClass(const boost::uuids::uuid &uuid):
19 0 : uuid_(uuid) {
20 0 : }
21 :
22 0 : ForwardingClass::~ForwardingClass() {
23 0 : }
24 :
25 0 : DBEntryBase::KeyPtr ForwardingClass::GetDBRequestKey() const {
26 0 : ForwardingClassKey *key = new ForwardingClassKey(uuid_);
27 0 : return DBEntryBase::KeyPtr(key);
28 : }
29 :
30 0 : std::string ForwardingClass::ToString() const {
31 0 : std::ostringstream buffer;
32 0 : buffer << uuid_;
33 0 : buffer << "id : " << id_;
34 0 : return buffer.str();
35 0 : }
36 :
37 0 : bool ForwardingClass::DBEntrySandesh(Sandesh *sresp, std::string &name) const {
38 0 : ForwardingClassSandeshResp *resp = static_cast<ForwardingClassSandeshResp *>(sresp);
39 0 : ForwardingClassSandeshData data;
40 0 : data.set_uuid(UuidToString(uuid_));
41 0 : data.set_id(id_);
42 0 : data.set_dscp(dscp_);
43 0 : data.set_vlan_priority(vlan_priority_);
44 0 : data.set_mpls_exp(mpls_exp_);
45 0 : if (qos_queue_ref_.get()) {
46 0 : data.set_qos_queue(qos_queue_ref_->id());
47 : }
48 0 : data.set_name(name_);
49 :
50 : std::vector<ForwardingClassSandeshData> &list =
51 0 : const_cast<std::vector<ForwardingClassSandeshData>&>(resp->get_fc_list());
52 0 : list.push_back(data);
53 0 : return true;
54 0 : }
55 :
56 0 : bool ForwardingClass::IsLess(const DBEntry &rhs) const {
57 0 : const ForwardingClass &fc = static_cast<const ForwardingClass &>(rhs);
58 0 : return (uuid_ < fc.uuid_);
59 : }
60 :
61 0 : bool ForwardingClass::Change(const DBRequest *req) {
62 0 : bool ret = false;
63 : const ForwardingClassData *data =
64 0 : static_cast<const ForwardingClassData *>(req->data.get());
65 0 : const Agent *agent = data->agent();
66 :
67 0 : if (id_ != data->id_) {
68 0 : id_ = data->id_;
69 0 : ret = true;
70 : }
71 :
72 0 : if (dscp_ != data->dscp_) {
73 0 : dscp_ = data->dscp_;
74 0 : ret = true;
75 : }
76 :
77 0 : if (vlan_priority_ != data->vlan_priority_) {
78 0 : vlan_priority_ = data->vlan_priority_;
79 0 : ret = true;
80 : }
81 :
82 0 : if (mpls_exp_ != data->mpls_exp_) {
83 0 : mpls_exp_ = data->mpls_exp_;
84 0 : ret = true;
85 : }
86 :
87 0 : if (name_ != data->name_) {
88 0 : name_ = data->name_;
89 0 : ret = true;
90 : }
91 :
92 0 : const QosQueueKey key(data->qos_queue_uuid_);
93 0 : const QosQueue *queue = static_cast<const QosQueue *>(
94 0 : agent->qos_queue_table()->FindActiveEntry(&key));
95 0 : if (queue != qos_queue_ref_.get()) {
96 0 : qos_queue_ref_ = queue;
97 0 : ret = true;
98 : }
99 :
100 0 : if (qos_queue_ref_.get()) {
101 0 : if (nic_queue_id_ != qos_queue_ref_->nic_queue_id()) {
102 0 : nic_queue_id_ = qos_queue_ref_->nic_queue_id();
103 0 : ret = true;
104 : }
105 : }
106 0 : return ret;
107 0 : }
108 :
109 0 : void ForwardingClass::Delete(const DBRequest *req) {
110 0 : }
111 :
112 0 : void ForwardingClass::SetKey(const DBRequestKey *key) {
113 0 : const ForwardingClassKey *fc_key =
114 : static_cast<const ForwardingClassKey *>(key);
115 0 : uuid_ = fc_key->uuid_;
116 0 : }
117 :
118 2 : ForwardingClassTable::ForwardingClassTable(Agent *agent,
119 2 : DB *db, const std::string &name):
120 2 : AgentOperDBTable(db, name) {
121 2 : set_agent(agent);
122 2 : }
123 :
124 4 : ForwardingClassTable::~ForwardingClassTable() {
125 4 : }
126 :
127 : DBTableBase*
128 2 : ForwardingClassTable::CreateTable(Agent *agent, DB *db, const std::string &name) {
129 2 : ForwardingClassTable *fc_table = new ForwardingClassTable(agent, db, name);
130 2 : (static_cast<DBTable *>(fc_table))->Init();
131 2 : return fc_table;
132 : }
133 :
134 : std::unique_ptr<DBEntry>
135 0 : ForwardingClassTable::AllocEntry(const DBRequestKey *k) const {
136 0 : const ForwardingClassKey *key =
137 : static_cast<const ForwardingClassKey *>(k);
138 0 : ForwardingClass *fc = new ForwardingClass(key->uuid_);
139 0 : return std::unique_ptr<DBEntry>(static_cast<DBEntry *>(fc));
140 : }
141 :
142 0 : DBEntry* ForwardingClassTable::OperDBAdd(const DBRequest *req) {
143 : const ForwardingClassKey *key =
144 0 : static_cast<const ForwardingClassKey *>(req->key.get());
145 0 : ForwardingClass *fc = new ForwardingClass(key->uuid_);
146 0 : fc->Change(req);
147 0 : return static_cast<DBEntry *>(fc);
148 : }
149 :
150 0 : bool ForwardingClassTable::OperDBOnChange(DBEntry *entry, const DBRequest *req) {
151 0 : ForwardingClass *fc = static_cast<ForwardingClass *>(entry);
152 0 : return fc->Change(req);
153 : }
154 :
155 0 : bool ForwardingClassTable::OperDBResync(DBEntry *entry, const DBRequest *req) {
156 0 : return OperDBOnChange(entry, req);
157 : }
158 :
159 0 : bool ForwardingClassTable::OperDBDelete(DBEntry *entry, const DBRequest *req) {
160 0 : return true;
161 : }
162 :
163 0 : bool ForwardingClassTable::IFNodeToReq(IFMapNode *node, DBRequest &req,
164 : const boost::uuids::uuid &u) {
165 0 : assert(!u.is_nil());
166 0 : if ((req.oper == DBRequest::DB_ENTRY_DELETE) || node->IsDeleted()) {
167 0 : req.key.reset(new ForwardingClassKey(u));
168 0 : req.oper = DBRequest::DB_ENTRY_DELETE;
169 0 : Enqueue(&req);
170 0 : return false;
171 : }
172 :
173 0 : agent()->config_manager()->AddForwardingClassNode(node);
174 0 : return false;
175 : }
176 :
177 0 : bool ForwardingClassTable::IFNodeToUuid(IFMapNode *node, boost::uuids::uuid &u) {
178 : autogen::ForwardingClass *cfg =
179 0 : static_cast <autogen::ForwardingClass *> (node->GetObject());
180 0 : assert(cfg);
181 0 : autogen::IdPermsType id_perms = cfg->id_perms();
182 0 : CfgUuidSet(id_perms.uuid.uuid_mslong, id_perms.uuid.uuid_lslong, u);
183 0 : return true;
184 0 : }
185 :
186 0 : bool ForwardingClassTable::ProcessConfig(IFMapNode *node, DBRequest &req,
187 : const boost::uuids::uuid &u) {
188 0 : if (node->IsDeleted()) {
189 0 : return false;
190 : }
191 :
192 0 : req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
193 0 : req.key.reset(new ForwardingClassKey(u));
194 0 : req.data.reset(BuildData(node));
195 0 : Enqueue(&req);
196 0 : return false;
197 : }
198 :
199 : ForwardingClassData*
200 0 : ForwardingClassTable::BuildData(IFMapNode *node) {
201 : autogen::ForwardingClass *data =
202 0 : static_cast<autogen::ForwardingClass *>(node->GetObject());
203 0 : IFMapAgentTable *table = static_cast<IFMapAgentTable *>(node->table());
204 :
205 0 : boost::uuids::uuid qos_queue_uuid = boost::uuids::nil_uuid();
206 0 : for (DBGraphVertex::adjacency_iterator iter = node->begin(table->GetGraph());
207 0 : iter != node->end(table->GetGraph()); ++iter) {
208 :
209 0 : IFMapNode *adj_node = static_cast<IFMapNode *>(iter.operator->());
210 0 : if (agent()->config_manager()->SkipNode(adj_node)) {
211 0 : continue;
212 : }
213 :
214 0 : if (adj_node->table() == agent()->cfg()->cfg_qos_queue_table()) {
215 : autogen::QosQueue *qos_queue =
216 0 : static_cast<autogen::QosQueue *>(adj_node->GetObject());
217 0 : autogen::IdPermsType id_perms = qos_queue->id_perms();
218 0 : CfgUuidSet(id_perms.uuid.uuid_mslong,
219 : id_perms.uuid.uuid_lslong, qos_queue_uuid);
220 0 : }
221 : }
222 :
223 : ForwardingClassData *fc_data =
224 0 : new ForwardingClassData(agent(), node, data->dscp(), data->vlan_priority(),
225 0 : data->mpls_exp(), data->id(), qos_queue_uuid,
226 0 : node->name());
227 0 : return fc_data;
228 : }
229 :
230 : AgentSandeshPtr
231 0 : ForwardingClassTable::GetAgentSandesh(const AgentSandeshArguments *args,
232 : const std::string &context) {
233 : return AgentSandeshPtr(new ForwardingClassSandesh(context,
234 0 : args->GetString("uuid"), args->GetString("id"),
235 0 : args->GetString("name")));
236 : }
237 :
238 0 : void ForwardingClassSandeshReq::HandleRequest() const {
239 0 : AgentSandeshPtr sand(new ForwardingClassSandesh(context(), get_uuid(),
240 0 : get_name(), get_id()));
241 0 : sand->DoSandesh(sand);
242 0 : }
243 :
244 0 : void AddForwardingClass::HandleRequest() const {
245 0 : QosResponse *resp = new QosResponse();
246 0 : resp->set_context(context());
247 :
248 0 : if (get_id() > 255 || get_dscp_value() > 63 || get_mpls_exp() > 7 ||
249 0 : get_vlan_priority() > 7) {
250 0 : resp->set_resp("Error");
251 0 : resp->Response();
252 0 : return;
253 : }
254 :
255 : char str[50];
256 0 : sprintf(str, "00000000-0000-0000-0000-00%010x", get_uuid());
257 0 : boost::uuids::uuid u1 = StringToUuid(std::string(str));
258 :
259 : char str1[50];
260 0 : sprintf(str1, "00000000-0000-0000-0000-00%010x", get_qos_queue_uuid());
261 0 : boost::uuids::uuid u2 = StringToUuid(std::string(str1));
262 :
263 0 : DBTable *table = Agent::GetInstance()->forwarding_class_table();
264 0 : DBRequest req;
265 0 : req.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
266 0 : req.key.reset(new ForwardingClassKey(u1));
267 0 : req.data.reset(new ForwardingClassData(Agent::GetInstance(), NULL,
268 0 : get_dscp_value(), get_vlan_priority(),
269 0 : get_mpls_exp(), get_id(),
270 0 : u2, get_name()));
271 0 : table->Enqueue(&req);
272 0 : resp->set_resp("Success");
273 0 : resp->Response();
274 0 : }
275 :
276 0 : void DeleteForwardingClass::HandleRequest() const {
277 0 : QosResponse *resp = new QosResponse();
278 0 : resp->set_context(context());
279 :
280 : char str[50];
281 0 : sprintf(str, "00000000-0000-0000-0000-00%010x", get_uuid());
282 0 : boost::uuids::uuid u1 = StringToUuid(std::string(str));
283 :
284 0 : DBTable *table = Agent::GetInstance()->forwarding_class_table();
285 0 : DBRequest req;
286 0 : req.oper = DBRequest::DB_ENTRY_DELETE;
287 0 : req.key.reset(new ForwardingClassKey(u1));
288 0 : req.data.reset(NULL);
289 0 : table->Enqueue(&req);
290 0 : resp->set_resp("Success");
291 0 : resp->Response();
292 0 : }
|