File: ifmgr_cmd_queue.hh

package info (click to toggle)
xorp 1.8.5-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,560 kB
  • ctags: 54,995
  • sloc: cpp: 397,204; sh: 17,490; ansic: 17,029; python: 7,643; lex: 1,632; yacc: 1,474; awk: 956; makefile: 251; perl: 217; sed: 33
file content (408 lines) | stat: -rw-r--r-- 10,125 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
// vim:set sts=4 ts=8:

// Copyright (c) 2001-2011 XORP, Inc and Others
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License, Version
// 2.1, June 1999 as published by the Free Software Foundation.
// Redistribution and/or modification of this program under the terms of
// any other version of the GNU Lesser General Public License is not
// permitted.
// 
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
// see the GNU Lesser General Public License, Version 2.1, a copy of
// which can be found in the XORP LICENSE.lgpl file.
// 
// XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
// http://xorp.net

#ifndef __IFMGR_CMD_QUEUE_HH__
#define __IFMGR_CMD_QUEUE_HH__


#include "libxorp/ref_ptr.hh"

class IfMgrCommandBase;

class IfMgrIfTree;
class IfMgrIfAtom;
class IfMgrVifAtom;
class IfMgrIPv4Atom;
#ifdef HAVE_IPV6
class IfMgrIPv6Atom;
#endif

/**
 * @short Base class for command sinks.
 */
class IfMgrCommandSinkBase {
public:
    typedef ref_ptr<IfMgrCommandBase> Cmd;
public:
    virtual void push(const Cmd& cmd) = 0;
    virtual ~IfMgrCommandSinkBase();
};

/**
 * @short 2-way IfMgr Command Tee.
 *
 * Instances push commands pushed into them into two object derived
 * from @ref IfMgrCommandSinkBase.
 */
class IfMgrCommandTee : public IfMgrCommandSinkBase {
public:
    typedef IfMgrCommandSinkBase::Cmd Cmd;

public:
    IfMgrCommandTee(IfMgrCommandSinkBase& o1, IfMgrCommandSinkBase& o2);
    void push(const Cmd& cmd);

protected:
    IfMgrCommandSinkBase& _o1;
    IfMgrCommandSinkBase& _o2;
};


/**
 * @short N-way IfMgr Command Tee.
 *
 * Instances push commands pushed into them into multiple objects derived
 * from @ref IfMgrCommandSinkBase.
 */
template <typename SinkType = IfMgrCommandSinkBase>
class IfMgrNWayCommandTee : public IfMgrCommandSinkBase {
public:
    typedef IfMgrCommandSinkBase::Cmd Cmd;
    typedef list<SinkType*> SinkList;

public:
    void push(const Cmd& cmd);

    /**
     * Add an additional output for pushed commands.
     * @param sink receiver for commands pushed into instance.
     * @return true if sink is successfully added, false otherwise.
     */
    bool add_sink(SinkType* sink);

    /**
     * Remove an sink for pushed commands.
     * @param sink receiver for commands pushed into instance.
     * @return true if sink is successfully remove, false otherwise.
     */
    bool remove_sink(SinkType* sink);

protected:
    SinkList _sinks;
};

template <typename SinkType>
void
IfMgrNWayCommandTee<SinkType>::push(const Cmd& cmd)
{
    typename SinkList::iterator i;
    for (i = _sinks.begin(); i != _sinks.end(); ++i) {
	(*i)->push(cmd);
    }
}

template <typename SinkType>
bool
IfMgrNWayCommandTee<SinkType>::add_sink(SinkType* o)
{
    if (find(_sinks.begin(), _sinks.end(), o) != _sinks.end())
	return false;
    _sinks.push_back(o);
    return true;
}

template <typename SinkType>
bool
IfMgrNWayCommandTee<SinkType>::remove_sink(SinkType* o)
{
    typename SinkList::iterator i = find(_sinks.begin(), _sinks.end(), o);
    if (i == _sinks.end())
	return false;
    _sinks.erase(i);
    return true;
}


/**
 * @short Class to dispatch Interface Manager Commands.
 *
 * This class buffers exactly one Interface Manager Command (@ref
 * IfMgrCommandBase) and applies it to an Interface Manager
 * Configuration Tree (@ref IfMgrIfTree) when it's execute() method is
 * called.
 */
class IfMgrCommandDispatcher : public IfMgrCommandSinkBase {
public:
    typedef IfMgrCommandSinkBase::Cmd Cmd;

public:
    /**
     * Constructor
     * @param tree configuration tree to apply commands to.
     */
    IfMgrCommandDispatcher(IfMgrIfTree& tree);

    /**
     * Push a command into local storage ready for execution.
     */
    void push(const Cmd& cmd);

    /**
     * Execute command.
     *
     * @return command return status (true indicates success, false failure).
     */
    virtual bool execute();

protected:
    Cmd		 _cmd;
    IfMgrIfTree& _iftree;
};

/**
 * @short Base class for Command Queue classes.
 */
class IfMgrCommandQueueBase : public IfMgrCommandSinkBase {
public:
    typedef IfMgrCommandSinkBase::Cmd Cmd;
public:
    /**
     * Add an item to the queue.
     */
    virtual void push(const Cmd& cmd) = 0;

    /**
     * @return true if queue has no items, false otherwise.
     */
    virtual bool empty() const = 0;

    /**
     * Accessor for front item from queue.
     *
     * @return reference to front item if queue is not empty, junk otherwise.
     */
    virtual Cmd& front() = 0;

    /**
     * Accessor for front item from queue.
     *
     * @return reference to front item if queue is not empty, junk otherwise.
     */
    virtual const Cmd& front() const = 0;

    /**
     * Pop the front item from queue.
     */
    virtual void pop_front() = 0;
};

/**
 * @short FIFO Queue for command objects.
 */
class IfMgrCommandFifoQueue : public IfMgrCommandQueueBase {
public:
    typedef IfMgrCommandQueueBase::Cmd Cmd;

public:
    void 	push(const Cmd& cmd);
    bool	empty() const;
    Cmd&	front();
    const Cmd&	front() const;
    void	pop_front();

protected:
    list<Cmd> _fifo;
};

/**
 * @short Interface Command Clustering Queue.
 *
 * This Queue attempts to cluster commands based on their interface
 * name.  Only command objects derived from IfMgrIfCommandBase may be
 * placed in the queue, command objects not falling in this category will
 * give rise to assertion failures.
 */
class IfMgrCommandIfClusteringQueue : public IfMgrCommandQueueBase {
public:
    typedef IfMgrCommandQueueBase::Cmd Cmd;
    typedef list<Cmd> CmdList;

public:
    IfMgrCommandIfClusteringQueue();

    void 	push(const Cmd& cmd);
    bool	empty() const;
    Cmd&	front();
    const Cmd&	front() const;
    void	pop_front();

protected:
    void	change_active_interface();

protected:
    string		_current_ifname;	// ifname of commands current
    						// commands queue
    						// last command output
    CmdList		_future_cmds;		// commands with ifname not
						// matching _current_ifname
    CmdList		_current_cmds;
};


/**
 * @short Class to convert an IfMgrIfTree object into a sequence of commands.
 */
class IfMgrIfTreeToCommands {
public:
    /**
     * Constructor
     */
    IfMgrIfTreeToCommands(const IfMgrIfTree& tree)
	: _tree(tree)
    {}

    /**
     * Convert the entire contents of IfMgrIfTree object to a sequence of
     * configuration commands.
     *
     * @param sink output target for commands that would generate tree.
     */
    void convert(IfMgrCommandSinkBase& sink) const;

protected:
    const IfMgrIfTree& _tree;
};

/**
 * @short Class to convert an IfMgrIfAtom object into a sequence of commands.
 */
class IfMgrIfAtomToCommands {
public:
    /**
     * Constructor
     */
    IfMgrIfAtomToCommands(const IfMgrIfAtom& interface)
	: _i(interface)
    {}

    /**
     * Convert the entire contents of IfMgrIfTree object to a sequence of
     * configuration commands.
     *
     * @param sink output target for commands that would generate
     * interface state.
     */
    void convert(IfMgrCommandSinkBase& sink) const;

protected:
    const IfMgrIfAtom& _i;
};

/**
 * @short Class to convert an IfMgrVifAtom object into a sequence of commands.
 */
class IfMgrVifAtomToCommands {
public:
    /**
     * Constructor
     *
     * @param ifn the name of the interface the vif belongs to.
     * @param vif the vif to be converted into a sequence of commands.
     */
    IfMgrVifAtomToCommands(const string& ifn, const IfMgrVifAtom& vif)
	: _ifn(ifn), _v(vif)
    {}

    /**
     * Convert the entire contents of IfMgrIfTree object to a sequence of
     * configuration commands.
     *
     * @param sink output target for commands that would generate
     * virtual interface state.
     */
    void convert(IfMgrCommandSinkBase& sink) const;

protected:
    const string&	_ifn;		// Interface name
    const IfMgrVifAtom&	_v;
};

/**
 * @short Class to convert an IfMgrIPv4Atom object into a sequence of commands.
 */
class IfMgrIPv4AtomToCommands {
public:
    /**
     * Constructor
     *
     * @param ifn the name of the interface the vif owning the address
     *            belongs to.
     * @param vifn the name of the vif owning the address.
     * @param a address atom to be converted into a sequence of commands.
     */
    IfMgrIPv4AtomToCommands(const string& ifn,
			    const string& vifn,
			    const IfMgrIPv4Atom& a)
	: _ifn(ifn), _vifn(vifn), _a(a)
    {}

    /**
     * Convert the entire contents of IfMgrIfTree object to a sequence of
     * configuration commands.
     *
     * @param sink output target for commands that would generate
     * interface state.
     */
    void convert(IfMgrCommandSinkBase& sink) const;

protected:
    const string& _ifn;
    const string& _vifn;
    const IfMgrIPv4Atom& _a;
};

#ifdef HAVE_IPV6
/**
 * @short Class to convert an IfMgrIPv6Atom object into a sequence of commands.
 */
class IfMgrIPv6AtomToCommands {
public:
    /**
     * Constructor
     *
     * @param ifn the name of the interface the vif owning the address
     *            belongs to.
     * @param vifn the name of the vif owning the address.
     * @param a address atom to be converted into a sequence of commands.
     */
    IfMgrIPv6AtomToCommands(const string& ifn,
			    const string& vifn,
			    const IfMgrIPv6Atom& a)
	: _ifn(ifn), _vifn(vifn), _a(a)
    {}

    /**
     * Convert the entire contents of IfMgrIfTree object to a sequence of
     * configuration commands.
     *
     * @param sink output target for commands that would generate
     * interface state.
     */
    void convert(IfMgrCommandSinkBase& sink) const;

protected:
    const string&	 _ifn;
    const string&	 _vifn;
    const IfMgrIPv6Atom& _a;
};
#endif //ipv6

#endif // __IFMGR_CMD_QUEUE_HH__