GBP: add allowed ethertypes to contracts
[vpp.git] / extras / vom / vom / gbp_vxlan_tunnel.cpp
1 /*
2  * Copyright (c) 2017 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/gbp_vxlan_tunnel.hpp"
17 #include "vom/gbp_vxlan_tunnel_cmds.hpp"
18 #include "vom/interface.hpp"
19 #include "vom/singular_db_funcs.hpp"
20
21 namespace VOM {
22
23 /**
24  * A DB of al the interfaces, key on the name
25  */
26 singular_db<uint32_t, gbp_vxlan_tunnel> gbp_vxlan_tunnel::m_db;
27
28 gbp_vxlan_tunnel::event_handler gbp_vxlan_tunnel::m_evh;
29
30 /**
31  * Construct a new object matching the desried state
32  */
33 gbp_vxlan_tunnel::gbp_vxlan_tunnel(const vxlan_tunnel& vt)
34   : interface(vt)
35   , m_vni(vt.m_vni)
36 {
37 }
38
39 gbp_vxlan_tunnel::gbp_vxlan_tunnel(uint32_t vni)
40   : interface(mk_name(vni),
41               interface::type_t::UNKNOWN,
42               interface::admin_state_t::UP)
43   , m_vni(vt.m_vni)
44 {
45 }
46
47 const gbp_vxlan_tunnel::key_t
48 gbp_vxlan_tunnel::key() const
49 {
50   return (m_vni);
51 }
52
53 bool
54 gbp_vxlan_tunnel::operator==(const gbp_vxlan_tunnel& vt) const
55 {
56   return (m_vni == vt.m_vni);
57 }
58
59 void
60 gbp_vxlan_tunnel::sweep()
61 {
62   if (rc_t::OK == m_id.rc()) {
63     HW::enqueue(new gbp_vxlan_tunnel_cmds::delete_cmd(m_vni));
64   }
65   HW::write();
66 }
67
68 void
69 gbp_vxlan_tunnel::replay()
70 {
71   if (rc_t::OK == m_hdl) {
72     HW::enqueue(new gbp_vxlan_tunnel_cmds::create_cmd(m_vni));
73   }
74 }
75
76 gbp_vxlan_tunnel::~gbp_vxlan_tunnel()
77 {
78   sweep();
79   m_db.release(m_id.data(), this);
80 }
81
82 std::string
83 gbp_vxlan_tunnel::to_string() const
84 {
85   std::ostringstream s;
86   s << "gbp-vxlan:[" << m_vni << "]";
87
88   return (s.str());
89 }
90
91 std::shared_ptr<gbp_vxlan_tunnel>
92 gbp_vxlan_tunnel::find(const key_t& key)
93 {
94   return (m_db.find(key));
95 }
96
97 void
98 gbp_vxlan_tunnel::update(const gbp_vxlan_tunnel& desired)
99 {
100   /*
101    * the desired state is always that the interface should be created
102    */
103   if (rc_t::OK != m_hdl) {
104     HW::enqueue(new gbp_vxlan_tunnel_cmds::create_cmd(m_vni));
105   }
106 }
107
108 std::shared_ptr<gbp_vxlan_tunnel>
109 gbp_vxlan_tunnel::find_or_add(const gbp_vxlan_tunnel& temp)
110 {
111   return (m_db.find_or_add(temp.m_id.data(), temp));
112 }
113
114 std::shared_ptr<gbp_vxlan_tunnel>
115 gbp_vxlan_tunnel::singular() const
116 {
117   return find_or_add(*this);
118 }
119
120 void
121 gbp_vxlan_tunnel::dump(std::ostream& os)
122 {
123   db_dump(m_db, os);
124 }
125
126 void
127 gbp_vxlan_tunnel::event_handler::handle_populate(const client_db::key_t& key)
128 {
129   /*
130    * dump VPP Bridge domains
131    */
132   std::shared_ptr<gbp_vxlan_tunnel_cmds::dump_cmd> cmd =
133     std::make_shared<gbp_vxlan_tunnel_cmds::dump_cmd>();
134
135   HW::enqueue(cmd);
136   HW::write();
137
138   for (auto& record : *cmd) {
139     auto& payload = record.get_payload();
140
141     gbp_vxlan_tunnel vt(payload.tunnel.vni, );
142     OM::commit(key, vt);
143     VOM_LOG(log_level_t::DEBUG) << "dump: " << vt.to_string();
144   }
145   else
146   {
147     gbp_vxlan_tunnel vt(payload.vt.vt_id);
148     OM::commit(key, vt);
149     VOM_LOG(log_level_t::DEBUG) << "dump: " << vt.to_string();
150   }
151 }
152 }
153
154 gbp_vxlan_tunnel::event_handler::event_handler()
155 {
156   OM::register_listener(this);
157   inspect::register_handler({ "gvt", "gbp-vxlan-tunnel" }, "GBP VXLAN Tunnels",
158                             this);
159 }
160
161 void
162 gbp_vxlan_tunnel::event_handler::handle_replay()
163 {
164   m_db.replay();
165 }
166
167 dependency_t
168 gbp_vxlan_tunnel::event_handler::order() const
169 {
170   return (dependency_t::INTERFACE);
171 }
172
173 void
174 gbp_vxlan_tunnel::event_handler::show(std::ostream& os)
175 {
176   db_dump(m_db, os);
177 }
178 }
179
180 /*
181  * fd.io coding-style-patch-verification: ON
182  *
183  * Local Variables:
184  * eval: (c-set-style "mozilla")
185  * End:
186  */