Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 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/l3_binding.hpp" |
| 17 | #include "vom/cmd.hpp" |
| 18 | |
| 19 | namespace VOM { |
| 20 | singular_db<l3_binding::key_type_t, l3_binding> l3_binding::m_db; |
| 21 | |
| 22 | l3_binding::event_handler l3_binding::m_evh; |
| 23 | |
| 24 | /** |
| 25 | * Construct a new object matching the desried state |
| 26 | */ |
| 27 | l3_binding::l3_binding(const interface& itf, const route::prefix_t& pfx) |
| 28 | : m_itf(itf.singular()) |
| 29 | , m_pfx(pfx) |
| 30 | , m_binding(true) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | l3_binding::l3_binding(const l3_binding& o) |
| 35 | : m_itf(o.m_itf) |
| 36 | , m_pfx(o.m_pfx) |
| 37 | , m_binding(true) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | l3_binding::~l3_binding() |
| 42 | { |
| 43 | sweep(); |
| 44 | |
| 45 | // not in the DB anymore. |
| 46 | m_db.release(make_pair(m_itf->key(), m_pfx), this); |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | l3_binding::sweep() |
| 51 | { |
| 52 | if (m_binding) { |
| 53 | HW::enqueue(new unbind_cmd(m_binding, m_itf->handle(), m_pfx)); |
| 54 | } |
| 55 | HW::write(); |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | l3_binding::replay() |
| 60 | { |
| 61 | if (m_binding) { |
| 62 | HW::enqueue(new bind_cmd(m_binding, m_itf->handle(), m_pfx)); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | const route::prefix_t& |
| 67 | l3_binding::prefix() const |
| 68 | { |
| 69 | return (m_pfx); |
| 70 | } |
| 71 | |
| 72 | std::string |
| 73 | l3_binding::to_string() const |
| 74 | { |
| 75 | std::ostringstream s; |
| 76 | s << "L3-config:[" << m_itf->to_string() << " prefix:" << m_pfx.to_string() |
| 77 | << " " << m_binding.to_string() << "]"; |
| 78 | |
| 79 | return (s.str()); |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | l3_binding::update(const l3_binding& desired) |
| 84 | { |
| 85 | /* |
| 86 | * the desired state is always that the interface should be created |
| 87 | */ |
| 88 | if (!m_binding) { |
| 89 | HW::enqueue(new bind_cmd(m_binding, m_itf->handle(), m_pfx)); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | std::shared_ptr<l3_binding> |
| 94 | l3_binding::find_or_add(const l3_binding& temp) |
| 95 | { |
| 96 | return (m_db.find_or_add(make_pair(temp.m_itf->key(), temp.m_pfx), temp)); |
| 97 | } |
| 98 | |
| 99 | std::shared_ptr<l3_binding> |
| 100 | l3_binding::singular() const |
| 101 | { |
| 102 | return find_or_add(*this); |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | l3_binding::dump(std::ostream& os) |
| 107 | { |
| 108 | m_db.dump(os); |
| 109 | } |
| 110 | |
| 111 | std::ostream& |
| 112 | operator<<(std::ostream& os, const l3_binding::key_type_t& key) |
| 113 | { |
| 114 | os << "[" << key.first << ", " << key.second << "]"; |
| 115 | |
| 116 | return (os); |
| 117 | } |
| 118 | |
| 119 | std::deque<std::shared_ptr<l3_binding>> |
| 120 | l3_binding::find(const interface& i) |
| 121 | { |
| 122 | /* |
| 123 | * Loop throught the entire map looking for matching interface. |
| 124 | * not the most efficient algorithm, but it will do for now. The |
| 125 | * number of L3 configs is low and this is only called during bootup |
| 126 | */ |
| 127 | std::deque<std::shared_ptr<l3_binding>> l3s; |
| 128 | |
| 129 | auto it = m_db.cbegin(); |
| 130 | |
| 131 | while (it != m_db.cend()) { |
| 132 | /* |
| 133 | * The key in the DB is a pair of the interface's name and prefix. |
| 134 | * If the keys match, save the L3-config |
| 135 | */ |
| 136 | auto key = it->first; |
| 137 | |
| 138 | if (i.key() == key.first) { |
| 139 | l3s.push_back(it->second.lock()); |
| 140 | } |
| 141 | |
| 142 | ++it; |
| 143 | } |
| 144 | |
| 145 | return (l3s); |
| 146 | } |
| 147 | |
| 148 | l3_binding::event_handler::event_handler() |
| 149 | { |
| 150 | OM::register_listener(this); |
| 151 | inspect::register_handler({ "l3" }, "L3 bindings", this); |
| 152 | } |
| 153 | |
| 154 | void |
| 155 | l3_binding::event_handler::handle_replay() |
| 156 | { |
| 157 | m_db.replay(); |
| 158 | } |
| 159 | |
| 160 | void |
| 161 | l3_binding::event_handler::handle_populate(const client_db::key_t& key) |
| 162 | { |
| 163 | /** |
| 164 | * This is done while populating the interfaces |
| 165 | */ |
| 166 | } |
| 167 | |
| 168 | dependency_t |
| 169 | l3_binding::event_handler::order() const |
| 170 | { |
| 171 | return (dependency_t::BINDING); |
| 172 | } |
| 173 | |
| 174 | void |
| 175 | l3_binding::event_handler::show(std::ostream& os) |
| 176 | { |
| 177 | m_db.dump(os); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * fd.io coding-style-patch-verification: ON |
| 183 | * |
| 184 | * Local Variables: |
| 185 | * eval: (c-set-style "mozilla") |
| 186 | * End: |
| 187 | */ |