GBP: add allowed ethertypes to contracts
[vpp.git] / extras / vom / vom / l2_xconnect.cpp
1 /*
2  * Copyright (c) 2018 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/l2_xconnect.hpp"
17 #include "vom/l2_xconnect_cmds.hpp"
18 #include "vom/singular_db_funcs.hpp"
19
20 namespace VOM {
21 /**
22  * A DB of all the L2 x-connect Configs
23  */
24 singular_db<l2_xconnect::key_t, l2_xconnect> l2_xconnect::m_db;
25
26 l2_xconnect::event_handler l2_xconnect::m_evh;
27
28 /**
29  * Construct a new object matching the desried state
30  */
31 l2_xconnect::l2_xconnect(const interface& east_itf, const interface& west_itf)
32   : m_east_itf(east_itf.singular())
33   , m_west_itf(west_itf.singular())
34   , m_xconnect_east(0)
35   , m_xconnect_west(0)
36 {
37 }
38
39 l2_xconnect::l2_xconnect(const l2_xconnect& o)
40   : m_east_itf(o.m_east_itf)
41   , m_west_itf(o.m_west_itf)
42   , m_xconnect_east(o.m_xconnect_east)
43   , m_xconnect_west(o.m_xconnect_west)
44 {
45 }
46
47 const l2_xconnect::key_t
48 l2_xconnect::key() const
49 {
50   if (m_east_itf->name() < m_west_itf->name())
51     return (std::make_pair(m_east_itf->key(), m_west_itf->key()));
52   return (std::make_pair(m_west_itf->key(), m_east_itf->key()));
53 }
54
55 bool
56 l2_xconnect::operator==(const l2_xconnect& l) const
57 {
58   return ((*m_east_itf == *l.m_east_itf) && (*m_west_itf == *l.m_west_itf));
59 }
60
61 std::shared_ptr<l2_xconnect>
62 l2_xconnect::find(const key_t& key)
63 {
64   return (m_db.find(key));
65 }
66
67 void
68 l2_xconnect::sweep()
69 {
70   if (m_xconnect_east && m_xconnect_west &&
71       handle_t::INVALID != m_east_itf->handle() &&
72       handle_t::INVALID != m_west_itf->handle()) {
73     HW::enqueue(new l2_xconnect_cmds::unbind_cmd(
74       m_xconnect_east, m_east_itf->handle(), m_west_itf->handle()));
75     HW::enqueue(new l2_xconnect_cmds::unbind_cmd(
76       m_xconnect_west, m_west_itf->handle(), m_east_itf->handle()));
77   }
78
79   HW::write();
80 }
81
82 void
83 l2_xconnect::replay()
84 {
85   if (m_xconnect_east && m_xconnect_west &&
86       handle_t::INVALID != m_east_itf->handle() &&
87       handle_t::INVALID != m_west_itf->handle()) {
88     HW::enqueue(new l2_xconnect_cmds::bind_cmd(
89       m_xconnect_east, m_east_itf->handle(), m_west_itf->handle()));
90     HW::enqueue(new l2_xconnect_cmds::bind_cmd(
91       m_xconnect_west, m_west_itf->handle(), m_east_itf->handle()));
92   }
93 }
94
95 l2_xconnect::~l2_xconnect()
96 {
97   sweep();
98
99   // not in the DB anymore.
100   m_db.release(key(), this);
101 }
102
103 std::string
104 l2_xconnect::to_string() const
105 {
106   std::ostringstream s;
107   s << "L2-xconnect:[" << m_east_itf->to_string() << " "
108     << m_west_itf->to_string() << " " << m_xconnect_east.to_string() << " "
109     << m_xconnect_west.to_string() << "]";
110
111   return (s.str());
112 }
113
114 void
115 l2_xconnect::update(const l2_xconnect& desired)
116 {
117   /*
118    * the desired state is always that the interface should be created
119    */
120   if (rc_t::OK != m_xconnect_east.rc() && rc_t::OK != m_xconnect_west.rc()) {
121     HW::enqueue(new l2_xconnect_cmds::bind_cmd(
122       m_xconnect_east, m_east_itf->handle(), m_west_itf->handle()));
123     HW::enqueue(new l2_xconnect_cmds::bind_cmd(
124       m_xconnect_west, m_west_itf->handle(), m_east_itf->handle()));
125   }
126 }
127
128 std::shared_ptr<l2_xconnect>
129 l2_xconnect::find_or_add(const l2_xconnect& temp)
130 {
131   return (m_db.find_or_add(temp.key(), temp));
132 }
133
134 std::shared_ptr<l2_xconnect>
135 l2_xconnect::singular() const
136 {
137   return find_or_add(*this);
138 }
139
140 void
141 l2_xconnect::dump(std::ostream& os)
142 {
143   db_dump(m_db, os);
144 }
145
146 l2_xconnect::event_handler::event_handler()
147 {
148   OM::register_listener(this);
149   inspect::register_handler({ "l2-xconnect" }, "L2 xconnects", this);
150 }
151
152 void
153 l2_xconnect::event_handler::handle_replay()
154 {
155   m_db.replay();
156 }
157
158 void
159 l2_xconnect::event_handler::handle_populate(const client_db::key_t& key)
160 {
161   /**
162    * This needs to be done here
163    */
164   std::shared_ptr<l2_xconnect_cmds::dump_cmd> cmd =
165     std::make_shared<l2_xconnect_cmds::dump_cmd>();
166
167   HW::enqueue(cmd);
168   HW::write();
169
170   for (auto& x_record : *cmd) {
171     auto& payload = x_record.get_payload();
172
173     VOM_LOG(log_level_t::DEBUG) << "l2-xconnect dump: "
174                                 << " east-itf: " << payload.rx_sw_if_index
175                                 << " west-itf: " << payload.tx_sw_if_index;
176
177     std::shared_ptr<interface> east_itf =
178       interface::find(payload.rx_sw_if_index);
179     std::shared_ptr<interface> west_itf =
180       interface::find(payload.tx_sw_if_index);
181
182     if (east_itf && west_itf) {
183       if (east_itf->name() > west_itf->name())
184         continue;
185       l2_xconnect l2_xc(*east_itf, *west_itf);
186       OM::commit(key, l2_xc);
187     }
188   }
189 }
190
191 dependency_t
192 l2_xconnect::event_handler::order() const
193 {
194   return (dependency_t::BINDING);
195 }
196
197 void
198 l2_xconnect::event_handler::show(std::ostream& os)
199 {
200   db_dump(m_db, os);
201 }
202 }
203
204 /*
205  * fd.io coding-style-patch-verification: ON
206  *
207  * Local Variables:
208  * eval: (c-set-style "mozilla")
209  * End:
210  */