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 <algorithm> |
| 17 | |
Neale Ranns | f756401 | 2018-03-20 16:30:51 -0700 | [diff] [blame^] | 18 | #include "vom/logger.hpp" |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 19 | #include "vom/om.hpp" |
| 20 | |
| 21 | namespace VOM { |
| 22 | client_db* OM::m_db; |
| 23 | |
| 24 | std::unique_ptr<OM::listener_list> OM::m_listeners; |
| 25 | |
| 26 | /** |
| 27 | * Initialse the connection to VPP |
| 28 | */ |
| 29 | void |
| 30 | OM::init() |
| 31 | { |
| 32 | m_db = new client_db(); |
| 33 | } |
| 34 | |
| 35 | void |
| 36 | OM::mark(const client_db::key_t& key) |
| 37 | { |
| 38 | /* |
| 39 | * Find if the object already stored on behalf of this key. |
| 40 | * and mark them stale |
| 41 | */ |
| 42 | object_ref_list& objs = m_db->find(key); |
| 43 | |
| 44 | auto mark_obj = [](const object_ref& oref) { oref.mark(); }; |
| 45 | |
| 46 | std::for_each(objs.begin(), objs.end(), mark_obj); |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | OM::sweep(const client_db::key_t& key) |
| 51 | { |
| 52 | /* |
Neale Ranns | fd92060 | 2017-11-23 12:15:00 -0800 | [diff] [blame] | 53 | * Find if the object already stored on behalf of this key. |
| 54 | * and mark them stale |
| 55 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 56 | object_ref_list& objs = m_db->find(key); |
| 57 | |
| 58 | for (auto it = objs.begin(); it != objs.end();) { |
| 59 | if (it->stale()) { |
| 60 | it = objs.erase(it); |
| 61 | } else { |
| 62 | ++it; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | HW::write(); |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | OM::remove(const client_db::key_t& key) |
| 71 | { |
| 72 | /* |
Neale Ranns | fd92060 | 2017-11-23 12:15:00 -0800 | [diff] [blame] | 73 | * Simply reset the list for this key. This will desctruct the |
| 74 | * object list and shared_ptrs therein. When the last shared_ptr |
| 75 | * goes the objects desctructor is called and the object is |
| 76 | * removed from OM |
| 77 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 78 | m_db->flush(key); |
| 79 | |
| 80 | HW::write(); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | OM::replay() |
| 85 | { |
Neale Ranns | f756401 | 2018-03-20 16:30:51 -0700 | [diff] [blame^] | 86 | VOM_LOG(log_level_t::INFO) << "replay"; |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 87 | /* |
Neale Ranns | fd92060 | 2017-11-23 12:15:00 -0800 | [diff] [blame] | 88 | * the listeners are sorted in dependency order |
| 89 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 90 | for (listener* l : *m_listeners) { |
| 91 | l->handle_replay(); |
Mohsin Kazmi | b5eb3b1 | 2018-02-26 18:36:17 +0100 | [diff] [blame] | 92 | HW::write(); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 93 | } |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void |
| 97 | OM::dump(const client_db::key_t& key, std::ostream& os) |
| 98 | { |
| 99 | m_db->dump(key, os); |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | OM::dump(std::ostream& os) |
| 104 | { |
| 105 | m_db->dump(os); |
| 106 | } |
| 107 | |
| 108 | void |
| 109 | OM::populate(const client_db::key_t& key) |
| 110 | { |
Neale Ranns | f756401 | 2018-03-20 16:30:51 -0700 | [diff] [blame^] | 111 | VOM_LOG(log_level_t::INFO) << "populate"; |
| 112 | |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 113 | /* |
Neale Ranns | fd92060 | 2017-11-23 12:15:00 -0800 | [diff] [blame] | 114 | * the listeners are sorted in dependency order |
| 115 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 116 | for (listener* l : *m_listeners) { |
| 117 | l->handle_populate(key); |
| 118 | } |
| 119 | |
| 120 | /* |
Neale Ranns | fd92060 | 2017-11-23 12:15:00 -0800 | [diff] [blame] | 121 | * once we have it all, mark it stale. |
| 122 | */ |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 123 | mark(key); |
| 124 | } |
| 125 | |
| 126 | bool |
| 127 | OM::register_listener(OM::listener* listener) |
| 128 | { |
| 129 | if (!m_listeners) { |
| 130 | m_listeners.reset(new listener_list); |
| 131 | } |
| 132 | |
| 133 | m_listeners->insert(listener); |
| 134 | |
| 135 | return (true); |
| 136 | } |
| 137 | |
| 138 | OM::mark_n_sweep::mark_n_sweep(const client_db::key_t& key) |
| 139 | : m_key(key) |
| 140 | { |
| 141 | OM::mark(m_key); |
| 142 | } |
| 143 | |
| 144 | OM::mark_n_sweep::~mark_n_sweep() |
| 145 | { |
| 146 | OM::sweep(m_key); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * fd.io coding-style-patch-verification: ON |
| 152 | * |
| 153 | * Local Variables: |
| 154 | * eval: (c-set-style "mozilla") |
| 155 | * End: |
| 156 | */ |