vom: Add support for redirect contracts in gbp
[vpp.git] / extras / vom / vom / gbp_contract.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_GBP_CONTRACT_H__
17 #define __VOM_GBP_CONTRACT_H__
18
19 #include "vom/acl_list.hpp"
20 #include "vom/gbp_endpoint.hpp"
21 #include "vom/gbp_rule.hpp"
22 #include "vom/interface.hpp"
23 #include "vom/singular_db.hpp"
24 #include "vom/types.hpp"
25
26 namespace VOM {
27
28 /**
29  * A entry in the ARP termination table of a Bridge Domain
30  */
31 class gbp_contract : public object_base
32 {
33 public:
34   /**
35    * set of gbp rules
36    */
37   typedef std::set<gbp_rule> gbp_rules_t;
38
39   /**
40    * The key for a contract is the pari of EPG-IDs
41    */
42   typedef std::pair<epg_id_t, epg_id_t> key_t;
43
44   /**
45    * Construct a GBP contract
46    */
47   gbp_contract(epg_id_t src_epg_id,
48                epg_id_t dst_epg_id,
49                const ACL::l3_list& acl);
50
51   /**
52    * Copy Construct
53    */
54   gbp_contract(const gbp_contract& r);
55
56   /**
57    * Destructor
58    */
59   ~gbp_contract();
60
61   /**
62    * Return the object's key
63    */
64   const key_t key() const;
65
66   /**
67    * comparison operator
68    */
69   bool operator==(const gbp_contract& bdae) const;
70
71   /**
72    * Return the matching 'singular instance'
73    */
74   std::shared_ptr<gbp_contract> singular() const;
75
76   /**
77    * Find the instnace of the bridge_domain domain in the OM
78    */
79   static std::shared_ptr<gbp_contract> find(const key_t& k);
80
81   /**
82    * Dump all bridge_domain-doamin into the stream provided
83    */
84   static void dump(std::ostream& os);
85
86   /**
87    * replay the object to create it in hardware
88    */
89   void replay(void);
90
91   /**
92    * Convert to string for debugging
93    */
94   std::string to_string() const;
95
96   /**
97    * Set gbp_rules in case of Redirect Contract
98    */
99   void set_gbp_rules(const gbp_rules_t& gbp_rules);
100
101 private:
102   /**
103    * Class definition for listeners to OM events
104    */
105   class event_handler : public OM::listener, public inspect::command_handler
106   {
107   public:
108     event_handler();
109     virtual ~event_handler() = default;
110
111     /**
112      * Handle a populate event
113      */
114     void handle_populate(const client_db::key_t& key);
115
116     /**
117      * Handle a replay event
118      */
119     void handle_replay();
120
121     /**
122      * Show the object in the Singular DB
123      */
124     void show(std::ostream& os);
125
126     /**
127      * Get the sortable Id of the listener
128      */
129     dependency_t order() const;
130   };
131
132   /**
133    * event_handler to register with OM
134    */
135   static event_handler m_evh;
136
137   /**
138    * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
139    */
140   void update(const gbp_contract& obj);
141
142   /**
143    * Find or add the instance of the contract domain in the OM
144    */
145   static std::shared_ptr<gbp_contract> find_or_add(const gbp_contract& temp);
146
147   /*
148    * It's the VPPHW class that updates the objects in HW
149    */
150   friend class OM;
151
152   /**
153    * It's the singular_db class that calls replay()
154    */
155   friend class singular_db<key_t, gbp_contract>;
156
157   /**
158    * Sweep/reap the object if still stale
159    */
160   void sweep(void);
161
162   /**
163    * HW configuration for the result of creating the endpoint
164    */
165   HW::item<bool> m_hw;
166
167   /**
168    * The source EPG ID
169    */
170   epg_id_t m_src_epg_id;
171
172   /**
173    * The destination EPG ID
174    */
175   epg_id_t m_dst_epg_id;
176
177   /**
178    * The ACL applied to traffic between the gourps
179    */
180   std::shared_ptr<ACL::l3_list> m_acl;
181
182   /**
183    * The gbp rules applied to traffic between the gourps
184    */
185   gbp_rules_t m_gbp_rules;
186
187   /**
188    * A map of all bridge_domains
189    */
190   static singular_db<key_t, gbp_contract> m_db;
191 };
192
193 std::ostream& operator<<(std::ostream& os, const gbp_contract::key_t& key);
194 }; // namespace
195
196 /*
197  * fd.io coding-style-patch-verification: ON
198  *
199  * Local Variables:
200  * eval: (c-set-style "mozilla")
201  * End:
202  */
203
204 #endif