bcdc6cbed10699876934df57f4e40c8646e3f969
[vpp.git] / extras / vom / vom / igmp_binding.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/igmp_binding.hpp"
17 #include "vom/igmp_binding_cmds.hpp"
18 #include "vom/singular_db_funcs.hpp"
19
20 namespace VOM {
21
22 /**
23  * A DB of all igmp bindings configs
24  */
25 singular_db<interface::key_t, igmp_binding> igmp_binding::m_db;
26
27 igmp_binding::event_handler igmp_binding::m_evh;
28
29 igmp_binding::igmp_binding(const interface& itf)
30   : m_itf(itf.singular())
31   , m_binding(true)
32 {
33 }
34
35 igmp_binding::igmp_binding(const igmp_binding& o)
36   : m_itf(o.m_itf)
37   , m_binding(o.m_binding)
38 {
39 }
40
41 igmp_binding::~igmp_binding()
42 {
43   sweep();
44   m_db.release(m_itf->key(), this);
45 }
46
47 bool
48 igmp_binding::operator==(const igmp_binding& l) const
49 {
50   return (*m_itf == *l.m_itf);
51 }
52
53 void
54 igmp_binding::sweep()
55 {
56   if (m_binding) {
57     HW::enqueue(new igmp_binding_cmds::unbind_cmd(m_binding, m_itf->handle()));
58   }
59   HW::write();
60 }
61
62 void
63 igmp_binding::dump(std::ostream& os)
64 {
65   db_dump(m_db, os);
66 }
67
68 void
69 igmp_binding::replay()
70 {
71   if (m_binding) {
72     HW::enqueue(new igmp_binding_cmds::bind_cmd(m_binding, m_itf->handle()));
73   }
74 }
75
76 std::string
77 igmp_binding::to_string() const
78 {
79   std::ostringstream s;
80   s << "igmp-binding: [" << m_itf->to_string() << " mode:host]";
81
82   return (s.str());
83 }
84
85 void
86 igmp_binding::update(const igmp_binding& desired)
87 {
88   /*
89    * the desired state is always that the interface should be created
90    */
91   if (!m_binding) {
92     HW::enqueue(new igmp_binding_cmds::bind_cmd(m_binding, m_itf->handle()));
93   }
94 }
95
96 std::shared_ptr<igmp_binding>
97 igmp_binding::find_or_add(const igmp_binding& temp)
98 {
99   return (m_db.find_or_add(temp.m_itf->key(), temp));
100 }
101
102 std::shared_ptr<igmp_binding>
103 igmp_binding::singular() const
104 {
105   return find_or_add(*this);
106 }
107
108 std::shared_ptr<interface>
109 igmp_binding::itf() const
110 {
111   return m_itf;
112 }
113
114 igmp_binding::event_handler::event_handler()
115 {
116   OM::register_listener(this);
117   inspect::register_handler({ "igmp-binding" }, "IGMP bindings", this);
118 }
119
120 void
121 igmp_binding::event_handler::handle_replay()
122 {
123   m_db.replay();
124 }
125
126 void
127 igmp_binding::event_handler::handle_populate(const client_db::key_t& key)
128 {
129   /* done with igmp_dump in igmp_listen */
130 }
131
132 dependency_t
133 igmp_binding::event_handler::order() const
134 {
135   return (dependency_t::BINDING);
136 }
137
138 void
139 igmp_binding::event_handler::show(std::ostream& os)
140 {
141   db_dump(m_db, os);
142 }
143 }
144 /*
145  * fd.io coding-style-patch-verification: ON
146  *
147  * Local Variables:
148  * eval: (c-set-style "mozilla")
149  * End:
150  */