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