GBP V2
[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& 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& 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    * Comparison operator - for UT
64    */
65   bool operator==(const nat_static& n) const;
66
67   /**
68    * Return the object's key
69    */
70   const key_t key() const;
71
72   /**
73    * Return the matching 'singular instance'
74    */
75   std::shared_ptr<nat_static> singular() const;
76
77   /**
78    * Find the instnace of the bridge_domain domain in the OM
79    */
80   static std::shared_ptr<nat_static> find(const key_t& key);
81
82   /**
83    * Dump all bridge_domain-doamin into the stream provided
84    */
85   static void dump(std::ostream& os);
86
87   /**
88    * replay the object to create it in hardware
89    */
90   void replay(void);
91
92   /**
93    * Convert to string for debugging
94    */
95   std::string to_string() const;
96
97 private:
98   /**
99    * Class definition for listeners to OM events
100    */
101   class event_handler : public OM::listener, public inspect::command_handler
102   {
103   public:
104     event_handler();
105     virtual ~event_handler() = default;
106
107     /**
108      * Handle a populate event
109      */
110     void handle_populate(const client_db::key_t& key);
111
112     /**
113      * Handle a replay event
114      */
115     void handle_replay();
116
117     /**
118      * Show the object in the Singular DB
119      */
120     void show(std::ostream& os);
121
122     /**
123      * Get the sortable Id of the listener
124      */
125     dependency_t order() const;
126   };
127
128   /**
129    * event_handler to register with OM
130    */
131   static event_handler m_evh;
132
133   /**
134    * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
135    */
136   void update(const nat_static& obj);
137
138   /**
139    * Find or add the instnace of the bridge_domain domain in the OM
140    */
141   static std::shared_ptr<nat_static> find_or_add(const nat_static& temp);
142
143   /*
144    * It's the VPPHW class that updates the objects in HW
145    */
146   friend class OM;
147
148   /**
149    * It's the singular_db class that calls replay()
150    */
151   friend class singular_db<key_t, nat_static>;
152
153   /**
154    * Sweep/reap the object if still stale
155    */
156   void sweep(void);
157
158   /**
159    * HW configuration for the result of creating the bridge_domain
160    */
161   HW::item<bool> m_hw;
162
163   /**
164    * The table-ID the outside address resides in
165    */
166   std::shared_ptr<route_domain> m_rd;
167
168   /**
169    * The 'inside' IP address, could be v4 or v6
170    */
171   const boost::asio::ip::address m_inside;
172
173   /**
174    * The 'outside' IP address
175    */
176   const boost::asio::ip::address m_outside;
177
178   /**
179    * A map of all NAT statics
180    */
181   static singular_db<key_t, nat_static> m_db;
182 };
183
184 std::ostream& operator<<(std::ostream& os, const nat_static::key_t& key);
185 };
186
187 /*
188  * fd.io coding-style-patch-verification: ON
189  *
190  * Local Variables:
191  * eval: (c-set-style "mozilla")
192  * End:
193  */
194
195 #endif