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