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