Added VLAN tagging support to port creations
[ovsdb.git] / ovsdb / src / main / java / org / opendaylight / ovsdb / internal / ConfigurationService.java
1 package org.opendaylight.ovsdb.internal;
2
3 import java.net.InetAddress;
4 import java.util.*;
5
6 import org.opendaylight.ovsdb.database.OVSBridge;
7 import org.opendaylight.ovsdb.database.OVSInstance;
8 import org.opendaylight.ovsdb.sal.configuration.IPluginInNetworkConfigurationService;
9 import org.opendaylight.controller.sal.core.Node;
10 import org.opendaylight.controller.sal.core.NodeConnector;
11 import org.opendaylight.ovsdb.database.Uuid;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 public class ConfigurationService implements IPluginInNetworkConfigurationService
16 {
17     private static final Logger logger = LoggerFactory
18             .getLogger(ConfigurationService.class);
19
20     IConnectionServiceInternal connectionService;
21
22     void init() {
23     }
24
25     /**
26      * Function called by the dependency manager when at least one dependency
27      * become unsatisfied or when the component is shutting down because for
28      * example bundle is being stopped.
29      *
30      */
31     void destroy() {
32     }
33
34     /**
35      * Function called by dependency manager after "init ()" is called and after
36      * the services provided by the class are registered in the service registry
37      *
38      */
39     void start() {
40     }
41
42     /**
43      * Function called by the dependency manager before the services exported by
44      * the component are unregistered, this will be followed by a "destroy ()"
45      * calls
46      *
47      */
48     void stop() {
49     }
50
51     public void setConnectionServiceInternal(IConnectionServiceInternal connectionService) {
52         this.connectionService = connectionService;
53     }
54
55     public void unsetConnectionServiceInternal(IConnectionServiceInternal connectionService) {
56         if (this.connectionService == connectionService) {
57             this.connectionService = null;
58         }
59     }
60
61     /**
62      * Add a new bridge
63      * @param node Node serving this configuration service
64      * @param bridgeConnectorIdentifier String representation of a Bridge Connector
65      * @return Bridge Connector configurations
66      */
67     @Override
68     @SuppressWarnings("unchecked")
69     public boolean createBridgeDomain(Node node, String bridgeIdentifier) throws Throwable{
70         try{
71             if (connectionService == null) {
72                 logger.error("Couldn't refer to the ConnectionService");
73                 return false;
74             }
75
76             Connection connection = connectionService.getConnection(node);
77             String identifier = "TEST";
78
79             if (connection != null) {
80                 String newBridge = "new_bridge";
81                 String newInterface = "new_interface";
82                 String newPort = "new_port";
83                 String newSwitch = "new_switch";
84
85                 Object addSwitchRequest;
86
87                 OVSInstance instance = new OVSInstance();
88                 instance.monitorOVS(connection);
89
90                 if(instance.getUuid() != null){
91                     List<String> bridgeUuidPair = new ArrayList<String>();
92                     bridgeUuidPair.add("named-uuid");
93                     bridgeUuidPair.add(newBridge);
94
95                     List<Object> mutation = new ArrayList<Object>();
96                     mutation.add("bridges");
97                     mutation.add("insert");
98                     mutation.add(bridgeUuidPair);
99
100                     List<Object> mutations = new ArrayList<Object>();
101                     mutations.add(mutation);
102
103                     List<String> ovsUuidPair = new ArrayList<String>();
104                     ovsUuidPair.add("uuid");
105                     ovsUuidPair.add(instance.getUuid());
106
107                     List<Object> whereInner = new ArrayList<Object>();
108                     whereInner.add("_uuid");
109                     whereInner.add("==");
110                     whereInner.add(ovsUuidPair);
111
112                     List<Object> where = new ArrayList<Object>();
113                     where.add(whereInner);
114
115                     addSwitchRequest = new MutateRequest("Open_vSwitch", where, mutations);
116                 }
117                 else{
118                     Map<String, Object> vswitchRow = new HashMap<String, Object>();
119                     ArrayList<String> bridges = new ArrayList<String>();
120                     bridges.add("named-uuid");
121                     bridges.add(newBridge);
122                     vswitchRow.put("bridges", bridges);
123                     addSwitchRequest = new InsertRequest("insert", "Open_vSwitch", newSwitch, vswitchRow);
124                 }
125
126                 Map<String, Object> bridgeRow = new HashMap<String, Object>();
127                 bridgeRow.put("name", bridgeIdentifier);
128                 ArrayList<String> ports = new ArrayList<String>();
129                 ports.add("named-uuid");
130                 ports.add(newPort);
131                 bridgeRow.put("ports", ports);
132                 InsertRequest addBridgeRequest = new InsertRequest("insert", "Bridge", newBridge, bridgeRow);
133
134                 Map<String, Object> portRow = new HashMap<String, Object>();
135                 portRow.put("name", bridgeIdentifier);
136                 ArrayList<String> interfaces = new ArrayList<String>();
137                 interfaces.add("named-uuid");
138                 interfaces.add(newInterface);
139                 portRow.put("interfaces", interfaces);
140                 InsertRequest addPortRequest = new InsertRequest("insert", "Port", newPort, portRow);
141
142                 Map<String, Object> interfaceRow = new HashMap<String, Object>();
143                 interfaceRow.put("name", bridgeIdentifier);
144                 interfaceRow.put("type", "internal");
145                 InsertRequest addIntfRequest = new InsertRequest("insert", "Interface", newInterface, interfaceRow);
146
147                 Object[] params = {"Open_vSwitch", addSwitchRequest, addIntfRequest, addPortRequest, addBridgeRequest};
148                 OvsdbMessage msg = new OvsdbMessage("transact", params);
149
150                 connection.sendMessage(msg);
151                 connection.readResponse(Uuid[].class);
152             }
153         }catch(Exception e){
154             e.printStackTrace();
155         }
156         return true;
157     }
158
159     @Override
160     public boolean deleteBridgeDomain(Node node, String bridgeIdentifier) {
161         // TODO Auto-generated method stub
162         return false;
163     }
164
165     @Override
166     public List<String> getBridgeDomains(Node node) {
167         // TODO Auto-generated method stub
168         return null;
169     }
170
171     @Override
172     public boolean addBridgeDomainConfig(Node node, String bridgeIdentifier, Map<String, String> config) {
173         // TODO Auto-generated method stub
174         return false;
175     }
176
177     @Override
178     public boolean removeBridgeDomainConfig(Node node, String bridgeIdentifier, Map<String, String> config) {
179         // TODO Auto-generated method stub
180         return false;
181     }
182
183     @Override
184     public Map<String, String> getBridgeDomainConfigs(Node node, String bridgeIdentifier) {
185         // TODO Auto-generated method stub
186         return null;
187     }
188
189     @Override
190     public boolean createBridgeConnector(Node node, String bridgeConnectorIdentifier) {
191         // TODO Auto-generated method stub
192         return false;
193     }
194
195     @Override
196     public boolean deleteBridgeConnector(Node node, String bridgeConnectorIdentifier) {
197         // TODO Auto-generated method stub
198         return false;
199     }
200
201     @Override
202     public boolean associateBridgeConnector(Node node, String bridgeIdentifier, String bridgeConnectorIdentifier) {
203         // TODO Auto-generated method stub
204         return false;
205     }
206
207     @Override
208     public boolean disassociateBridgeConnector(Node node, String bridgeIdentifier, String bridgeConnectorIdentifier) {
209         // TODO Auto-generated method stub
210         return false;
211     }
212
213     @Override
214     public boolean addBridgeConnectorConfig(Node node, String bridgeConnectorIdentifier, Map<String, String> config) {
215         // TODO Auto-generated method stub
216         return false;
217     }
218
219     @Override
220     public boolean removeBridgeConnectorConfig(Node node, String bridgeConnectorIdentifier, Map<String, String> config) {
221         // TODO Auto-generated method stub
222         return false;
223     }
224
225     @Override
226     public Map<String, String> getBridgeConnectorConfigs(Node node, String bridgeConnectorIdentifier) {
227         // TODO Auto-generated method stub
228         return null;
229     }
230
231     /**
232      * Create a Port Attached to a Bridge
233      * Ex. ovs-vsctl add-port br0 vif0
234      * @param node Node serving this configuration service
235      * @param bridgeDomainIdentifier String representation of a Bridge Domain
236      * @param portIdentifier String representation of a user defined Port Name
237      */
238     @Override
239     @SuppressWarnings("unchecked")
240     public boolean addPort(Node node, String bridgeIdentifier, String portIdentifier) throws Throwable{
241         try{
242             if (connectionService == null) {
243                 logger.error("Couldn't refer to the ConnectionService");
244                 return false;
245             }
246             Connection connection = connectionService.getConnection(node);
247
248             if (connection != null) {
249                 String newBridge = "new_bridge";
250                 String newInterface = "new_interface";
251                 String newPort = "new_port";
252                 String newSwitch = "new_switch";
253
254                 Map<String, OVSBridge> existingBridges = OVSBridge.monitorBridge(connection);
255
256                 OVSBridge bridge = existingBridges.get(bridgeIdentifier);
257
258                 List<String> portUuidPair = new ArrayList<String>();
259                 portUuidPair.add("named-uuid");
260                 portUuidPair.add(newPort);
261
262                 List<Object> mutation = new ArrayList<Object>();
263                 mutation.add("ports");
264                 mutation.add("insert");
265                 mutation.add(portUuidPair);
266                 List<Object> mutations = new ArrayList<Object>();
267                 mutations.add(mutation);
268
269                 List<String> bridgeUuidPair = new ArrayList<String>();
270                 bridgeUuidPair.add("uuid");
271                 bridgeUuidPair.add(bridge.getUuid());
272
273                 List<Object> whereInner = new ArrayList<Object>();
274                 whereInner.add("_uuid");
275                 whereInner.add("==");
276                 whereInner.add(bridgeUuidPair);
277
278                 List<Object> where = new ArrayList<Object>();
279                 where.add(whereInner);
280
281                 MutateRequest mutateBridgeRequest = new MutateRequest("Bridge", where, mutations);
282
283                 Map<String, Object> portRow = new HashMap<String, Object>();
284                 portRow.put("name", portIdentifier);
285                 ArrayList<String> interfaces = new ArrayList<String>();
286                 interfaces.add("named-uuid");
287                 interfaces.add(newInterface);
288                 portRow.put("interfaces", interfaces);
289                 InsertRequest addPortRequest = new InsertRequest("insert", "Port", newPort, portRow);
290
291                 Map<String, Object> interfaceRow = new HashMap<String, Object>();
292                 interfaceRow.put("name", portIdentifier);
293                 InsertRequest addIntfRequest = new InsertRequest("insert", "Interface", newInterface, interfaceRow);
294
295                 Object[] params = {"Open_vSwitch", mutateBridgeRequest, addIntfRequest, addPortRequest};
296                 OvsdbMessage msg = new OvsdbMessage("transact", params);
297
298                 connection.sendMessage(msg);
299                 connection.readResponse(Uuid[].class);
300             }
301         }catch(Exception e){
302             e.printStackTrace();
303         }
304         return true;
305     }
306
307     /**
308      * Create a Port with a VLAN Tag and it to a Bridge
309      * Ex. ovs-vsctl add-port br0 vxlan1 -- set interface vxlan1 type=vxlan options:remote_ip=192.168.1.11
310      * @param node Node serving this configuration service
311      * @param portIdentifier String representation of a user defined Port Name
312      * @param vlanid integer representing the VLAN Tag
313      */
314     @Override
315     @SuppressWarnings("unchecked")
316     public boolean addPortVlan(Node node, String bridgeIdentifier, String portIdentifier, int vlanid) throws Throwable{
317         try{
318             if (connectionService == null) {
319                 logger.error("Couldn't refer to the ConnectionService");
320                 return false;
321             }
322             Connection connection = connectionService.getConnection(node);
323
324             if (connection != null) {
325                 String newBridge = "new_bridge";
326                 String newInterface = "new_interface";
327                 String newPort = "new_port";
328                 String newSwitch = "new_switch";
329
330                 Map<String, OVSBridge> existingBridges = OVSBridge.monitorBridge(connection);
331
332                 OVSBridge bridge = existingBridges.get(bridgeIdentifier);
333
334                 List<String> portUuidPair = new ArrayList<String>();
335                 portUuidPair.add("named-uuid");
336                 portUuidPair.add(newPort);
337
338                 List<Object> mutation = new ArrayList<Object>();
339                 mutation.add("ports");
340                 mutation.add("insert");
341                 mutation.add(portUuidPair);
342                 List<Object> mutations = new ArrayList<Object>();
343                 mutations.add(mutation);
344
345                 List<String> bridgeUuidPair = new ArrayList<String>();
346                 bridgeUuidPair.add("uuid");
347                 bridgeUuidPair.add(bridge.getUuid());
348
349                 List<Object> whereInner = new ArrayList<Object>();
350                 whereInner.add("_uuid");
351                 whereInner.add("==");
352                 whereInner.add(bridgeUuidPair);
353
354                 List<Object> where = new ArrayList<Object>();
355                 where.add(whereInner);
356
357                 MutateRequest mutateBridgeRequest = new MutateRequest("Bridge", where, mutations);
358
359                 Map<String, Object> portRow = new HashMap<String, Object>();
360                 portRow.put("name", portIdentifier);
361                 portRow.put("tag", vlanid);
362                 ArrayList<String> interfaces = new ArrayList<String>();
363                 interfaces.add("named-uuid");
364                 interfaces.add(newInterface);
365                 portRow.put("interfaces", interfaces);
366                 InsertRequest addPortRequest = new InsertRequest("insert", "Port", newPort, portRow);
367
368                 Map<String, Object> interfaceRow = new HashMap<String, Object>();
369                 interfaceRow.put("name", portIdentifier);
370                 InsertRequest addIntfRequest = new InsertRequest("insert", "Interface", newInterface, interfaceRow);
371
372                 Object[] params = {"Open_vSwitch", mutateBridgeRequest, addIntfRequest, addPortRequest};
373                 OvsdbMessage msg = new OvsdbMessage("transact", params);
374
375                 connection.sendMessage(msg);
376                 connection.readResponse(Uuid[].class);
377             }
378         }catch(Exception e){
379             e.printStackTrace();
380         }
381         return true;
382     }
383
384     /**
385      * Create an Encapsulated Tunnel Interface and destination Tunnel Endpoint
386      * Ex. ovs-vsctl add-port br0 vxlan1 -- set interface vxlan1 type=vxlan options:remote_ip=192.168.1.11
387      * @param node Node serving this configuration service
388      * @param bridgeDomainIdentifier String representation of a Bridge Domain
389      * @param portIdentifier String representation of a user defined Port Name
390      * @param tunnelendpoint IP address of the destination Tunnel Endpoint
391      * @param tunencap is the tunnel encapsulation options being CAPWAP, GRE or VXLAN
392      * The Bridge must already be defined before calling addTunnel.
393      */
394     @Override
395     @SuppressWarnings("unchecked")
396     public boolean addTunnel(Node node, String bridgeIdentifier,
397         String portidentifier, String tunnelendpoint, String tunencap)
398                 throws Throwable{
399         try{
400             if (connectionService == null) {
401                 logger.error("Couldn't refer to the ConnectionService");
402                 return false;
403             }
404             Connection connection = connectionService.getConnection(node);
405
406             if (connection != null) {
407                 String newBridge = "new_bridge";
408                 String newInterface = "new_interface";
409                 String newPort = "new_port";
410                 String newSwitch = "new_switch";
411
412                 Map<String, OVSBridge> existingBridges = OVSBridge.monitorBridge(connection);
413
414                 OVSBridge bridge = existingBridges.get(bridgeIdentifier);
415
416                 List<String> portUuidPair = new ArrayList<String>();
417                 portUuidPair.add("named-uuid");
418                 portUuidPair.add(newPort);
419
420                 List<Object> mutation = new ArrayList<Object>();
421                 mutation.add("ports");
422                 mutation.add("insert");
423                 mutation.add(portUuidPair);
424                 List<Object> mutations = new ArrayList<Object>();
425                 mutations.add(mutation);
426
427                 List<String> bridgeUuidPair = new ArrayList<String>();
428                 bridgeUuidPair.add("uuid");
429                 bridgeUuidPair.add(bridge.getUuid());
430
431                 List<Object> whereInner = new ArrayList<Object>();
432                 whereInner.add("_uuid");
433                 whereInner.add("==");
434                 whereInner.add(bridgeUuidPair);
435
436                 List<Object> where = new ArrayList<Object>();
437                 where.add(whereInner);
438
439                 MutateRequest mutateBridgeRequest = new MutateRequest("Bridge", where, mutations);
440
441                 Map<String, Object> portRow = new HashMap<String, Object>();
442                 portRow.put("name", portidentifier);
443                 ArrayList<String> interfaces = new ArrayList<String>();
444                 interfaces.add("named-uuid");
445                 interfaces.add(newInterface);
446                 portRow.put("interfaces", interfaces);
447                 InsertRequest addPortRequest = new InsertRequest("insert", "Port", newPort, portRow);
448
449                 Map<String, Object> interfaceRow = new HashMap<String, Object>();
450                 interfaceRow.put("name", portidentifier);
451                 interfaceRow.put("type", tunencap);
452                 ArrayList<Object> intopt = new ArrayList<Object>();
453                 interfaceRow.put("options", intopt);
454                 ArrayList<Object> intoptmap = new ArrayList<Object>();
455                 ArrayList<String> intoptep = new ArrayList<String>();
456                 intopt.add("map");
457                 intopt.add(intoptmap);
458                 intoptmap.add(intoptep);
459                 intoptep.add("remote_ip");
460                 intoptep.add(tunnelendpoint);
461
462                 InsertRequest addIntfRequest = new InsertRequest("insert", "Interface",
463                         newInterface, interfaceRow);
464
465                 Object[] params = {"Open_vSwitch", mutateBridgeRequest, addIntfRequest, addPortRequest};
466                 OvsdbMessage msg = new OvsdbMessage("transact", params);
467
468                 connection.sendMessage(msg);
469                 connection.readResponse(Uuid[].class);
470             }
471         }catch(Exception e){
472             e.printStackTrace();
473         }
474         return true;
475     }
476
477     @Override
478     public Object genericConfigurationEvent(Node node, Map<String, String> config) {
479         // TODO Auto-generated method stub
480         return null;
481     }
482
483   }