vxlan: vxlan/vxlan.api API cleanup
[vpp.git] / extras / vom / vom / qos_map.cpp
1 /*
2  * Copyright (c) 2019 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include "vom/qos_map.hpp"
17 #include "vom/api_types.hpp"
18 #include "vom/qos_map_cmds.hpp"
19 #include "vom/qos_types_api.hpp"
20 #include "vom/singular_db_funcs.hpp"
21
22 namespace VOM {
23 namespace QoS {
24
25 singular_db<map::key_t, map> map::m_db;
26
27 map::event_handler map::m_evh;
28
29 map::map(uint32_t id, const outputs_t& o)
30   : m_config(false)
31   , m_id(id)
32   , m_outputs(o)
33 {
34 }
35
36 map::map(const map& r)
37   : m_config(r.m_config)
38   , m_id(r.m_id)
39   , m_outputs(r.m_outputs)
40 {
41 }
42
43 map::~map()
44 {
45   sweep();
46   m_db.release(key(), this);
47 }
48
49 const map::key_t
50 map::key() const
51 {
52   return m_id;
53 }
54
55 const uint32_t
56 map::id() const
57 {
58   return m_id;
59 }
60
61 bool
62 map::operator==(const map& m) const
63 {
64   return (key() == m.key() && m_outputs == m.m_outputs);
65 }
66
67 void
68 map::sweep()
69 {
70   if (m_config) {
71     HW::enqueue(new map_cmds::delete_cmd(m_config, m_id));
72   }
73   HW::write();
74 }
75
76 void
77 map::replay()
78 {
79   if (m_config) {
80     HW::enqueue(new map_cmds::create_cmd(m_config, m_id, m_outputs));
81   }
82 }
83
84 std::string
85 map::to_string() const
86 {
87   std::ostringstream s;
88   s << "qos-map:" << (int)m_id;
89
90   return (s.str());
91 }
92
93 void
94 map::update(const map& m)
95 {
96   m_outputs = m.m_outputs;
97
98   if (rc_t::OK != m_config.rc()) {
99     HW::enqueue(new map_cmds::create_cmd(m_config, m_id, m_outputs));
100   }
101 }
102
103 std::shared_ptr<map>
104 map::find_or_add(const map& temp)
105 {
106   return (m_db.find_or_add(temp.key(), temp));
107 }
108
109 std::shared_ptr<map>
110 map::find(const key_t& k)
111 {
112   return (m_db.find(k));
113 }
114
115 std::shared_ptr<map>
116 map::singular() const
117 {
118   return find_or_add(*this);
119 }
120
121 void
122 map::dump(std::ostream& os)
123 {
124   db_dump(m_db, os);
125 }
126
127 map::event_handler::event_handler()
128 {
129   OM::register_listener(this);
130   inspect::register_handler({ "qos-map" }, "QoS Map", this);
131 }
132
133 void
134 map::event_handler::handle_replay()
135 {
136   m_db.replay();
137 }
138
139 static const map::outputs_t
140 from_api(vapi_type_qos_egress_map_row rows[4])
141 {
142   map::outputs_t o;
143
144   for (uint32_t ii = 0; ii < 4; ii++) {
145     std::copy(std::begin(rows[ii].outputs), std::end(rows[ii].outputs),
146               o[ii].begin());
147   }
148
149   return o;
150 }
151
152 void
153 map::event_handler::handle_populate(const client_db::key_t& key)
154 {
155   std::shared_ptr<map_cmds::dump_cmd> cmd =
156     std::make_shared<map_cmds::dump_cmd>();
157
158   HW::enqueue(cmd);
159   HW::write();
160
161   for (auto& rr : *cmd) {
162     auto& payload = rr.get_payload();
163
164     map qr(payload.map.id, from_api(payload.map.rows));
165     OM::commit(key, qr);
166   }
167 }
168
169 dependency_t
170 map::event_handler::order() const
171 {
172   return (dependency_t::TABLE);
173 }
174
175 void
176 map::event_handler::show(std::ostream& os)
177 {
178   db_dump(m_db, os);
179 }
180
181 }; // namespace QoS
182 }; // namespace VOM
183
184 /*
185  * fd.io coding-style-patch-verification: ON
186  *
187  * Local Variables:
188  * eval: (c-set-style "mozilla")
189  * End:
190  */