Move VOM to extras/vom
[vpp.git] / extras / vom / vom / gbp_contract.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_contract.hpp"
17 #include "vom/gbp_contract_cmds.hpp"
18 #include "vom/singular_db_funcs.hpp"
19
20 namespace VOM {
21
22 singular_db<gbp_contract::key_t, gbp_contract> gbp_contract::m_db;
23
24 gbp_contract::event_handler gbp_contract::m_evh;
25
26 gbp_contract::gbp_contract(epg_id_t src_epg_id,
27                            epg_id_t dst_epg_id,
28                            const ACL::l3_list& acl)
29   : m_hw(false)
30   , m_src_epg_id(src_epg_id)
31   , m_dst_epg_id(dst_epg_id)
32   , m_acl(acl.singular())
33 {
34 }
35
36 gbp_contract::gbp_contract(const gbp_contract& gbpc)
37   : m_hw(gbpc.m_hw)
38   , m_src_epg_id(gbpc.m_src_epg_id)
39   , m_dst_epg_id(gbpc.m_dst_epg_id)
40   , m_acl(gbpc.m_acl)
41 {
42 }
43
44 gbp_contract::~gbp_contract()
45 {
46   sweep();
47
48   // not in the DB anymore.
49   m_db.release(key(), this);
50 }
51
52 const gbp_contract::key_t
53 gbp_contract::key() const
54 {
55   return (std::make_pair(m_src_epg_id, m_dst_epg_id));
56 }
57
58 bool
59 gbp_contract::operator==(const gbp_contract& gbpc) const
60 {
61   return ((key() == gbpc.key()) && (m_acl->handle() == gbpc.m_acl->handle()));
62 }
63
64 void
65 gbp_contract::sweep()
66 {
67   if (m_hw) {
68     HW::enqueue(
69       new gbp_contract_cmds::delete_cmd(m_hw, m_src_epg_id, m_dst_epg_id));
70   }
71   HW::write();
72 }
73
74 void
75 gbp_contract::replay()
76 {
77   if (m_hw) {
78     HW::enqueue(new gbp_contract_cmds::create_cmd(
79       m_hw, m_src_epg_id, m_dst_epg_id, m_acl->handle()));
80   }
81 }
82
83 std::string
84 gbp_contract::to_string() const
85 {
86   std::ostringstream s;
87   s << "gbp-contract:[{" << m_src_epg_id << ", " << m_dst_epg_id << "}, "
88     << m_acl->to_string() << "]";
89
90   return (s.str());
91 }
92
93 void
94 gbp_contract::update(const gbp_contract& r)
95 {
96   /*
97  * create the table if it is not yet created
98  */
99   if (rc_t::OK != m_hw.rc()) {
100     HW::enqueue(new gbp_contract_cmds::create_cmd(
101       m_hw, m_src_epg_id, m_dst_epg_id, m_acl->handle()));
102   }
103 }
104
105 std::shared_ptr<gbp_contract>
106 gbp_contract::find_or_add(const gbp_contract& temp)
107 {
108   return (m_db.find_or_add(temp.key(), temp));
109 }
110
111 std::shared_ptr<gbp_contract>
112 gbp_contract::find(const key_t& k)
113 {
114   return (m_db.find(k));
115 }
116
117 std::shared_ptr<gbp_contract>
118 gbp_contract::singular() const
119 {
120   return find_or_add(*this);
121 }
122
123 void
124 gbp_contract::dump(std::ostream& os)
125 {
126   db_dump(m_db, os);
127 }
128
129 gbp_contract::event_handler::event_handler()
130 {
131   OM::register_listener(this);
132   inspect::register_handler({ "gbp-contract" }, "GBP Contract", this);
133 }
134
135 void
136 gbp_contract::event_handler::handle_replay()
137 {
138   m_db.replay();
139 }
140
141 void
142 gbp_contract::event_handler::handle_populate(const client_db::key_t& key)
143 {
144   std::shared_ptr<gbp_contract_cmds::dump_cmd> cmd =
145     std::make_shared<gbp_contract_cmds::dump_cmd>();
146
147   HW::enqueue(cmd);
148   HW::write();
149
150   for (auto& record : *cmd) {
151     auto& payload = record.get_payload();
152
153     std::shared_ptr<ACL::l3_list> acl =
154       ACL::l3_list::find(payload.contract.acl_index);
155
156     if (acl) {
157       gbp_contract gbpc(payload.contract.src_epg, payload.contract.dst_epg,
158                         *acl);
159       OM::commit(key, gbpc);
160
161       VOM_LOG(log_level_t::DEBUG) << "read: " << gbpc.to_string();
162     }
163   }
164 }
165
166 dependency_t
167 gbp_contract::event_handler::order() const
168 {
169   return (dependency_t::ENTRY);
170 }
171
172 void
173 gbp_contract::event_handler::show(std::ostream& os)
174 {
175   db_dump(m_db, os);
176 }
177
178 std::ostream&
179 operator<<(std::ostream& os, const gbp_contract::key_t& key)
180 {
181   os << "{ " << key.first << "," << key.second << "}";
182
183   return (os);
184 }
185
186 } // namespace VOM
187
188 /*
189  * fd.io coding-style-patch-verification: ON
190  *
191  * Local Variables:
192  * eval: (c-set-style "mozilla")
193  * End:
194  */