VOM reshuffle
[vpp.git] / src / vpp-api / vom / nat_static.hpp
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 #ifndef __VOM_NAT_STATIC_H__
17 #define __VOM_NAT_STATIC_H__
18
19 #include "vom/route.hpp"
20 #include "vom/singular_db.hpp"
21 #include "vom/types.hpp"
22
23 namespace VOM {
24 /**
25  * A entry in the ARP termination table of a Bridge Domain
26  */
27 class nat_static : public object_base
28 {
29 public:
30   /**
31    * The key for a NAT static mapping.
32    *  So far only model the address only case. The address
33    * is the outside.
34    */
35   typedef std::pair<route::table_id_t, boost::asio::ip::address> key_t;
36
37   /**
38    * Construct an NAT Static binding with the outside address in default
39    * table
40    */
41   nat_static(const boost::asio::ip::address& inside,
42              const boost::asio::ip::address_v4& outside);
43
44   /**
45    * Construct an NAT Static binding with the outside address in
46    * route-domain specified
47    */
48   nat_static(const route_domain& rd,
49              const boost::asio::ip::address& inside,
50              const boost::asio::ip::address_v4& outside);
51
52   /**
53    * Copy Construct
54    */
55   nat_static(const nat_static& r);
56
57   /**
58    * Destructor
59    */
60   ~nat_static();
61
62   /**
63    * Return the matching 'singular instance'
64    */
65   std::shared_ptr<nat_static> singular() const;
66
67   /**
68    * Find the instnace of the bridge_domain domain in the OM
69    */
70   static std::shared_ptr<nat_static> find(const nat_static& temp);
71
72   /**
73    * Dump all bridge_domain-doamin into the stream provided
74    */
75   static void dump(std::ostream& os);
76
77   /**
78    * replay the object to create it in hardware
79    */
80   void replay(void);
81
82   /**
83    * Convert to string for debugging
84    */
85   std::string to_string() const;
86
87 private:
88   /**
89    * Class definition for listeners to OM events
90    */
91   class event_handler : public OM::listener, public inspect::command_handler
92   {
93   public:
94     event_handler();
95     virtual ~event_handler() = default;
96
97     /**
98      * Handle a populate event
99      */
100     void handle_populate(const client_db::key_t& key);
101
102     /**
103      * Handle a replay event
104      */
105     void handle_replay();
106
107     /**
108      * Show the object in the Singular DB
109      */
110     void show(std::ostream& os);
111
112     /**
113      * Get the sortable Id of the listener
114      */
115     dependency_t order() const;
116   };
117
118   /**
119    * event_handler to register with OM
120    */
121   static event_handler m_evh;
122
123   /**
124    * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
125    */
126   void update(const nat_static& obj);
127
128   /**
129    * Find or add the instnace of the bridge_domain domain in the OM
130    */
131   static std::shared_ptr<nat_static> find_or_add(const nat_static& temp);
132
133   /*
134    * It's the VPPHW class that updates the objects in HW
135    */
136   friend class OM;
137
138   /**
139    * It's the singular_db class that calls replay()
140    */
141   friend class singular_db<key_t, nat_static>;
142
143   /**
144    * Sweep/reap the object if still stale
145    */
146   void sweep(void);
147
148   /**
149    * HW configuration for the result of creating the bridge_domain
150    */
151   HW::item<bool> m_hw;
152
153   /**
154    * The table-ID the outside address resides in
155    */
156   std::shared_ptr<route_domain> m_rd;
157
158   /**
159    * The 'inside' IP address, could be v4 or v6
160    */
161   const boost::asio::ip::address& m_inside;
162
163   /**
164    * The 'outside' IP address - always v4
165    */
166   const boost::asio::ip::address_v4& m_outside;
167
168   /**
169    * A map of all NAT statics
170    */
171   static singular_db<key_t, nat_static> m_db;
172 };
173
174 std::ostream& operator<<(std::ostream& os, const nat_static::key_t& key);
175 };
176
177 /*
178  * fd.io coding-style-patch-verification: ON
179  *
180  * Local Variables:
181  * eval: (c-set-style "mozilla")
182  * End:
183  */
184
185 #endif