Add Utility methods to get a list of controller-ip-addresses and OF Port
[netvirt.git] / ovsdb / src / main / java / org / opendaylight / ovsdb / plugin / ConfigurationService.java
1 package org.opendaylight.ovsdb.plugin;
2
3 import java.net.InetAddress;
4 import java.net.NetworkInterface;
5 import java.net.SocketException;
6 import java.util.*;
7
8 import org.eclipse.osgi.framework.console.CommandInterpreter;
9 import org.eclipse.osgi.framework.console.CommandProvider;
10 import org.opendaylight.ovsdb.lib.database.OVSBridge;
11 import org.opendaylight.ovsdb.lib.database.OVSInstance;
12 import org.opendaylight.ovsdb.lib.database.OvsdbType;
13 import org.opendaylight.ovsdb.lib.message.TransactBuilder;
14 import org.opendaylight.ovsdb.lib.message.operations.InsertOperation;
15 import org.opendaylight.ovsdb.lib.message.operations.MutateOperation;
16 import org.opendaylight.ovsdb.lib.message.operations.Operation;
17 import org.opendaylight.ovsdb.lib.message.operations.OperationResult;
18 import org.opendaylight.ovsdb.lib.notation.Condition;
19 import org.opendaylight.ovsdb.lib.notation.Function;
20 import org.opendaylight.ovsdb.lib.notation.Mutation;
21 import org.opendaylight.ovsdb.lib.notation.Mutator;
22 import org.opendaylight.ovsdb.lib.notation.OvsDBSet;
23 import org.opendaylight.ovsdb.lib.notation.UUID;
24 import org.opendaylight.ovsdb.lib.table.Bridge;
25 import org.opendaylight.ovsdb.lib.table.Interface;
26 import org.opendaylight.ovsdb.lib.table.Open_vSwitch;
27 import org.opendaylight.ovsdb.lib.table.Port;
28 import org.opendaylight.ovsdb.lib.table.internal.Table;
29 import org.opendaylight.controller.clustering.services.IClusterGlobalServices;
30 import org.opendaylight.controller.sal.connection.ConnectionConstants;
31 import org.opendaylight.controller.sal.core.Node;
32 import org.opendaylight.controller.sal.core.NodeConnector;
33 import org.opendaylight.controller.sal.networkconfig.bridgedomain.ConfigConstants;
34 import org.opendaylight.controller.sal.networkconfig.bridgedomain.IPluginInBridgeDomainConfigService;
35 import org.opendaylight.controller.sal.utils.NetUtils;
36 import org.opendaylight.controller.sal.utils.Status;
37 import org.opendaylight.controller.sal.utils.StatusCode;
38 import org.osgi.framework.BundleContext;
39 import org.osgi.framework.FrameworkUtil;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 import com.google.common.util.concurrent.ListenableFuture;
44
45 public class ConfigurationService implements IPluginInBridgeDomainConfigService, CommandProvider
46 {
47     private static final Logger logger = LoggerFactory
48             .getLogger(ConfigurationService.class);
49
50     IConnectionServiceInternal connectionService;
51     InventoryServiceInternal inventoryServiceInternal;
52     private IClusterGlobalServices clusterServices;
53     boolean forceConnect = false;
54
55     void init() {
56     }
57
58     /**
59      * Function called by the dependency manager when at least one dependency
60      * become unsatisfied or when the component is shutting down because for
61      * example bundle is being stopped.
62      *
63      */
64     void destroy() {
65     }
66
67     /**
68      * Function called by dependency manager after "init ()" is called and after
69      * the services provided by the class are registered in the service registry
70      *
71      */
72     void start() {
73         registerWithOSGIConsole();
74     }
75
76     private void registerWithOSGIConsole() {
77         BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass())
78                 .getBundleContext();
79         bundleContext.registerService(CommandProvider.class.getName(), this,
80                 null);
81     }
82
83     /**
84      * Function called by the dependency manager before the services exported by
85      * the component are unregistered, this will be followed by a "destroy ()"
86      * calls
87      *
88      */
89     void stop() {
90     }
91
92     public void setConnectionServiceInternal(IConnectionServiceInternal connectionService) {
93         this.connectionService = connectionService;
94     }
95
96     public void unsetConnectionServiceInternal(IConnectionServiceInternal connectionService) {
97         if (this.connectionService == connectionService) {
98             this.connectionService = null;
99         }
100     }
101
102     public void setInventoryServiceInternal(InventoryServiceInternal inventoryServiceInternal) {
103         this.inventoryServiceInternal = inventoryServiceInternal;
104     }
105
106     public void unsetInventoryServiceInternal(InventoryServiceInternal inventoryServiceInternal) {
107         if (this.inventoryServiceInternal == inventoryServiceInternal) {
108             this.inventoryServiceInternal = null;
109         }
110     }
111
112     public void setClusterServices(IClusterGlobalServices i) {
113         this.clusterServices = i;
114     }
115
116     public void unsetClusterServices(IClusterGlobalServices i) {
117         if (this.clusterServices == i) {
118             this.clusterServices = null;
119         }
120     }
121
122     private Connection getConnection (Node node) {
123         Connection connection = connectionService.getConnection(node);
124         if (connection == null || !connection.getChannel().isActive()) {
125             return null;
126         }
127
128         return connection;
129     }
130
131     /**
132      * Add a new bridge
133      * @param node Node serving this configuration service
134      * @param bridgeConnectorIdentifier String representation of a Bridge Connector
135      * @return Bridge Connector configurations
136      */
137     @Override
138     public Status createBridgeDomain(Node node, String bridgeIdentifier,
139             Map<ConfigConstants, Object> configs) throws Throwable {
140         try{
141             if (connectionService == null) {
142                 logger.error("Couldn't refer to the ConnectionService");
143                 return new Status(StatusCode.NOSERVICE);
144             }
145
146             Connection connection = this.getConnection(node);
147             if (connection == null) {
148                 return new Status(StatusCode.NOSERVICE, "Connection to ovsdb-server not available");
149             }
150
151             Map<String, Table<?>> ovsTable = inventoryServiceInternal.getTableCache(node, Open_vSwitch.NAME.getName());
152             String newBridge = "new_bridge";
153             String newInterface = "new_interface";
154             String newPort = "new_port";
155             String newSwitch = "new_switch";
156
157             Operation addSwitchRequest = null;
158
159             if(ovsTable != null){
160                 String ovsTableUUID = (String) ovsTable.keySet().toArray()[0];
161                 UUID bridgeUuidPair = new UUID(newBridge);
162                 Mutation bm = new Mutation("bridges", Mutator.INSERT, bridgeUuidPair);
163                 List<Mutation> mutations = new ArrayList<Mutation>();
164                 mutations.add(bm);
165
166                 UUID uuid = new UUID(ovsTableUUID);
167                 Condition condition = new Condition("_uuid", Function.EQUALS, uuid);
168                 List<Condition> where = new ArrayList<Condition>();
169                 where.add(condition);
170                 addSwitchRequest = new MutateOperation(Open_vSwitch.NAME.getName(), where, mutations);
171             }
172             else{
173                 Open_vSwitch ovsTableRow = new Open_vSwitch();
174                 OvsDBSet<UUID> bridges = new OvsDBSet<UUID>();
175                 UUID bridgeUuidPair = new UUID(newBridge);
176                 bridges.add(bridgeUuidPair);
177                 ovsTableRow.setBridges(bridges);
178                 addSwitchRequest = new InsertOperation(Open_vSwitch.NAME.getName(), newSwitch, ovsTableRow);
179             }
180
181             Bridge bridgeRow = new Bridge();
182             bridgeRow.setName(bridgeIdentifier);
183             OvsDBSet<UUID> ports = new OvsDBSet<UUID>();
184             UUID port = new UUID(newPort);
185             ports.add(port);
186             bridgeRow.setPorts(ports);
187             InsertOperation addBridgeRequest = new InsertOperation(Bridge.NAME.getName(), newBridge, bridgeRow);
188
189             Port portRow = new Port();
190             portRow.setName(bridgeIdentifier);
191             OvsDBSet<UUID> interfaces = new OvsDBSet<UUID>();
192             UUID interfaceid = new UUID(newInterface);
193             interfaces.add(interfaceid);
194             portRow.setInterfaces(interfaces);
195             InsertOperation addPortRequest = new InsertOperation(Port.NAME.getName(), newPort, portRow);
196
197             Interface interfaceRow = new Interface();
198             interfaceRow.setName(bridgeIdentifier);
199             interfaceRow.setType("internal");
200             InsertOperation addIntfRequest = new InsertOperation(Interface.NAME.getName(), newInterface, interfaceRow);
201
202             TransactBuilder transaction = new TransactBuilder();
203             transaction.addOperations(new ArrayList<Operation>(
204                                       Arrays.asList(addSwitchRequest, addIntfRequest, addPortRequest, addBridgeRequest)));
205
206             ListenableFuture<List<OperationResult>> transResponse = connection.getRpc().transact(transaction);
207             List<OperationResult> tr = transResponse.get();
208             List<Operation> requests = transaction.getRequests();
209             Status status = new Status(StatusCode.SUCCESS);
210             for (int i = 0; i < tr.size() ; i++) {
211                 if (i < requests.size()) requests.get(i).setResult(tr.get(i));
212                 if (tr.get(i).getError() != null && tr.get(i).getError().trim().length() > 0) {
213                     OperationResult result = tr.get(i);
214                     status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
215                 }
216             }
217
218             if (tr.size() > requests.size()) {
219                 OperationResult result = tr.get(tr.size()-1);
220                 logger.error("Error creating Bridge : {}\n Error : {}\n Details : {}", bridgeIdentifier,
221                                                                                        result.getError(),
222                                                                                        result.getDetails());
223                 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
224             }
225             return status;
226         } catch(Exception e){
227             e.printStackTrace();
228         }
229         return new Status(StatusCode.INTERNALERROR);
230     }
231
232     /**
233      * Create a Port Attached to a Bridge
234      * Ex. ovs-vsctl add-port br0 vif0
235      * @param node Node serving this configuration service
236      * @param bridgeDomainIdentifier String representation of a Bridge Domain
237      * @param portIdentifier String representation of a user defined Port Name
238      */
239     @Override
240     public Status addPort(Node node, String bridgeIdentifier, String portIdentifier, Map<ConfigConstants, Object> configs) {
241         try{
242             if (connectionService == null) {
243                 logger.error("Couldn't refer to the ConnectionService");
244                 return new Status(StatusCode.NOSERVICE);
245             }
246             Connection connection = this.getConnection(node);
247             if (connection == null) {
248                 return new Status(StatusCode.NOSERVICE, "Connection to ovsdb-server not available");
249             }
250
251             if (connection != null) {
252                 String newInterface = "new_interface";
253                 String newPort = "new_port";
254
255                 Map<String, OVSBridge> existingBridges = OVSBridge.monitorBridge(connection);
256
257                 OVSBridge bridge = existingBridges.get(bridgeIdentifier);
258
259                 List<String> portUuidPair = new ArrayList<String>();
260                 portUuidPair.add("named-uuid");
261                 portUuidPair.add(newPort);
262
263                 List<Object> mutation = new ArrayList<Object>();
264                 mutation.add("ports");
265                 mutation.add("insert");
266                 mutation.add(portUuidPair);
267                 List<Object> mutations = new ArrayList<Object>();
268                 mutations.add(mutation);
269
270                 List<String> bridgeUuidPair = new ArrayList<String>();
271                 bridgeUuidPair.add("uuid");
272                 bridgeUuidPair.add(bridge.getUuid());
273
274                 List<Object> whereInner = new ArrayList<Object>();
275                 whereInner.add("_uuid");
276                 whereInner.add("==");
277                 whereInner.add(bridgeUuidPair);
278
279                 List<Object> where = new ArrayList<Object>();
280                 where.add(whereInner);
281
282                 MutateRequest mutateBridgeRequest = new MutateRequest("Bridge", where, mutations);
283
284                 Map<String, Object> portRow = new HashMap<String, Object>();
285                 portRow.put("name", portIdentifier);
286                 String portType = null;
287                 if (configs != null) {
288                     portType = (String)configs.get(ConfigConstants.TYPE);
289                     if (portType != null && portType.equalsIgnoreCase(OvsdbType.PortType.VLAN.name())) {
290                         try {
291                         portRow.put("tag", Integer.parseInt((String)configs.get(ConfigConstants.VLAN)));
292                         } catch (Exception e) {
293                         }
294                     }
295                 }
296                 ArrayList<String> interfaces = new ArrayList<String>();
297                 interfaces.add("named-uuid");
298                 interfaces.add(newInterface);
299                 portRow.put("interfaces", interfaces);
300                 InsertRequest addPortRequest = new InsertRequest("insert", "Port", newPort, portRow);
301
302                 Map<String, Object> interfaceRow = new HashMap<String, Object>();
303                 interfaceRow.put("name", portIdentifier);
304                 //Tunnel specific
305
306                 if (portType != null && portType.equalsIgnoreCase(OvsdbType.PortType.TUNNEL.name())) {
307                     interfaceRow.put("type", configs.get(ConfigConstants.TUNNEL_TYPE));
308                     ArrayList<Object> intopt = new ArrayList<Object>();
309                     interfaceRow.put("options", intopt);
310                     ArrayList<Object> intoptmap = new ArrayList<Object>();
311                     ArrayList<String> intoptep = new ArrayList<String>();
312                     intopt.add("map");
313                     intopt.add(intoptmap);
314                     intoptmap.add(intoptep);
315                     intoptep.add("remote_ip");
316                     intoptep.add((String)configs.get(ConfigConstants.DEST_IP));
317                 }
318                 InsertRequest addIntfRequest = new InsertRequest("insert", "Interface", newInterface, interfaceRow);
319
320                 Object[] params = {"Open_vSwitch", mutateBridgeRequest, addIntfRequest, addPortRequest};
321                 OvsdbMessage msg = new OvsdbMessage("transact", params);
322
323                 //connection.sendMessage(msg);
324             }
325         }catch(Exception e){
326             e.printStackTrace();
327         }
328         return new Status(StatusCode.SUCCESS);
329     }
330     /**
331      * Implements the OVS Connection for Managers
332      *
333      * @param node Node serving this configuration service
334      * @param String with IP and connection types
335      */
336     @SuppressWarnings("unchecked")
337     public boolean setManager(Node node, String managerip) throws Throwable{
338         try{
339             if (connectionService == null) {
340                 logger.error("Couldn't refer to the ConnectionService");
341                 return false;
342             }
343             Connection connection = this.getConnection(node);
344             if (connection == null) {
345                 return false;
346             }
347
348             if (connection != null) {
349                 String newmanager = "new_manager";
350
351                 OVSInstance instance = OVSInstance.monitorOVS(connection);
352
353                 Map ovsoutter = new LinkedHashMap();
354                 Map ovsinner = new LinkedHashMap();
355                 ArrayList ovsalist1 = new ArrayList();
356                 ArrayList ovsalist2 = new ArrayList();
357                 ArrayList ovsalist3 = new ArrayList();
358                 ArrayList ovsalist4 = new ArrayList();
359
360                 //OVS Table Update
361                 ovsoutter.put("where", ovsalist1);
362                 ovsalist1.add(ovsalist2);
363                 ovsalist2.add("_uuid");
364                 ovsalist2.add("==");
365                 ovsalist2.add(ovsalist3);
366                 ovsalist3.add("uuid");
367                 ovsalist3.add(instance.getUuid());
368                 ovsoutter.put("op", "update");
369                 ovsoutter.put("table", "Open_vSwitch");
370                 ovsoutter.put("row", ovsinner);
371                 ovsinner.put("manager_options", ovsalist4);
372                 ovsalist4.add("named-uuid");
373                 ovsalist4.add(newmanager);
374
375                 Map mgroutside = new LinkedHashMap();
376                 Map mgrinside = new LinkedHashMap();
377
378                 //Manager Table Insert
379                 mgroutside.put("uuid-name", newmanager);
380                 mgroutside.put("op", "insert");
381                 mgroutside.put("table","Manager");
382                 mgroutside.put("row", mgrinside);
383                 mgrinside.put("target", managerip);
384
385                 Object[] params = {"Open_vSwitch", ovsoutter, mgroutside};
386                 OvsdbMessage msg = new OvsdbMessage("transact", params);
387
388                 //connection.sendMessage(msg);
389
390             }
391         }catch(Exception e){
392             e.printStackTrace();
393         }
394         return true;
395     }
396
397     @Override
398     public Status addBridgeDomainConfig(Node node, String bridgeIdentfier,
399             Map<ConfigConstants, Object> configs) {
400         String mgmt = (String)configs.get(ConfigConstants.MGMT);
401         if (mgmt != null) {
402             try {
403                 if (setManager(node, mgmt)) return new Status(StatusCode.SUCCESS);
404             } catch (Throwable e) {
405                 // TODO Auto-generated catch block
406                 e.printStackTrace();
407                 return new Status(StatusCode.INTERNALERROR);
408             }
409         }
410         return new Status(StatusCode.BADREQUEST);
411     }
412
413     @Override
414     public Status addPortConfig(Node node, String bridgeIdentifier, String portIdentifier,
415             Map<ConfigConstants, Object> configs) {
416         // TODO Auto-generated method stub
417         return null;
418     }
419
420     @Override
421     public Status deletePort(Node node, String bridgeIdentifier, String portIdentifier) {
422         // TODO Auto-generated method stub
423         return null;
424     }
425
426     @Override
427     public Node getBridgeDomainNode(Node node, String bridgeIdentifier) {
428         // TODO Auto-generated method stub
429         return null;
430     }
431
432     @Override
433     public Map<ConfigConstants, Object> getPortConfigs(Node node, String bridgeIdentifier,
434             String portIdentifier) {
435         // TODO Auto-generated method stub
436         return null;
437     }
438
439     @Override
440     public Status removeBridgeDomainConfig(Node node, String bridgeIdentifier,
441             Map<ConfigConstants, Object> configs) {
442         // TODO Auto-generated method stub
443         return null;
444     }
445
446     @Override
447     public Status removePortConfig(Node node, String bridgeIdentifier, String portIdentifier,
448             Map<ConfigConstants, Object> configs) {
449         // TODO Auto-generated method stub
450         return null;
451     }
452
453     @Override
454     public Status deleteBridgeDomain(Node node, String bridgeIdentifier) {
455         // TODO Auto-generated method stub
456         return null;
457     }
458
459     @Override
460     public Map<ConfigConstants, Object> getBridgeDomainConfigs(Node node, String bridgeIdentifier) {
461         // TODO Auto-generated method stub
462         return null;
463     }
464
465     @Override
466     public List<String> getBridgeDomains(Node node) {
467
468         Connection connection = connectionService.getConnection(node);
469         Map<String, OVSBridge> existingBridges = OVSBridge.monitorBridge(connection);
470         List<String> bridgeDomains = new ArrayList<String>(existingBridges.keySet());
471         return bridgeDomains;
472     }
473
474     @Override
475     public NodeConnector getNodeConnector(Node arg0, String arg1, String arg2) {
476         return null;
477     }
478
479     private short getControllerOFPort() {
480         Short defaultOpenFlowPort = 6633;
481         Short openFlowPort = defaultOpenFlowPort;
482         String portString = System.getProperty("of.listenPort");
483         if (portString != null) {
484             try {
485                 openFlowPort = Short.decode(portString).shortValue();
486             } catch (NumberFormatException e) {
487                 logger.warn("Invalid port:{}, use default({})", portString,
488                         openFlowPort);
489             }
490         }
491         return openFlowPort;
492     }
493
494     private List<InetAddress> getControllerIPAddresses() {
495         List<InetAddress> controllers = null;
496         if (clusterServices != null) {
497             controllers = clusterServices.getClusteredControllers();
498             if (controllers != null && controllers.size() > 0) {
499                 if (controllers.size() == 1) {
500                     InetAddress controller = controllers.get(0);
501                     if (!controller.equals(InetAddress.getLoopbackAddress())) {
502                         return controllers;
503                     }
504                 } else {
505                     return controllers;
506                 }
507             }
508         }
509
510         controllers = new ArrayList<InetAddress>();
511         InetAddress controllerIP;
512         Enumeration<NetworkInterface> nets;
513         try {
514             nets = NetworkInterface.getNetworkInterfaces();
515             for (NetworkInterface netint : Collections.list(nets)) {
516                 Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
517                 for (InetAddress inetAddress : Collections.list(inetAddresses)) {
518                     if (!inetAddress.isLoopbackAddress() &&
519                             NetUtils.isIPv4AddressValid(inetAddress.getHostAddress())) {
520                         controllers.add(inetAddress);
521                     }
522                 }
523             }
524         } catch (SocketException e) {
525             controllers.add(InetAddress.getLoopbackAddress());
526         }
527         return controllers;
528     }
529
530     public void _ovsconnect (CommandInterpreter ci) {
531         String bridgeName = ci.nextArgument();
532         if (bridgeName == null) {
533             ci.println("Please enter Bridge Name");
534             return;
535         }
536
537         String ovsdbserver = ci.nextArgument();
538         if (ovsdbserver == null) {
539             ci.println("Please enter valid IP-Address");
540             return;
541         }
542         try {
543             InetAddress.getByName(ovsdbserver);
544         }  catch (Exception e) {
545             e.printStackTrace();
546             ci.println("Please enter valid IP-Address");
547             return;
548         }
549         String port = ci.nextArgument();
550         if (port == null) {
551             port = "6634";
552         }
553
554         ci.println("connecting to ovsdb server : "+ovsdbserver+":"+port+" ... ");
555         Map<ConnectionConstants, String> params = new HashMap<ConnectionConstants, String>();
556         params.put(ConnectionConstants.ADDRESS, ovsdbserver);
557         params.put(ConnectionConstants.PORT, port);
558         Node node = connectionService.connect(bridgeName, params);
559         if (node != null) ci.println("Node Name: "+node.toString());
560         else ci.println("Could not connect to Node");
561     }
562
563     public void _addBridge (CommandInterpreter ci) {
564         String nodeName = ci.nextArgument();
565         if (nodeName == null) {
566             ci.println("Please enter Node Name");
567             return;
568         }
569
570         String bridgeName = ci.nextArgument();
571         if (bridgeName == null) {
572             ci.println("Please enter Bridge Name");
573             return;
574         }
575         Status status;
576         try {
577             status = this.createBridgeDomain(Node.fromString(nodeName), bridgeName, null);
578             ci.println("Bridge creation status : "+status.toString());
579         } catch (Throwable e) {
580             // TODO Auto-generated catch block
581             e.printStackTrace();
582             ci.println("Failed to create Bridge "+bridgeName);
583         }
584     }
585
586     public void _addPort (CommandInterpreter ci) {
587         String nodeName = ci.nextArgument();
588         if (nodeName == null) {
589             ci.println("Please enter Node Name");
590             return;
591         }
592
593         String bridgeName = ci.nextArgument();
594         if (bridgeName == null) {
595             ci.println("Please enter Bridge Name");
596             return;
597         }
598
599         String portName = ci.nextArgument();
600         if (portName == null) {
601             ci.println("Please enter Port Name");
602             return;
603         }
604
605         Status status;
606         try {
607             status = this.addPort(Node.fromString(nodeName), bridgeName, portName, null);
608             ci.println("Port creation status : "+status.toString());
609         } catch (Throwable e) {
610             // TODO Auto-generated catch block
611             e.printStackTrace();
612             ci.println("Failed to create Port "+portName+" in Bridge "+bridgeName);
613         }
614     }
615
616     public void _addPortVlan (CommandInterpreter ci) {
617         String nodeName = ci.nextArgument();
618         if (nodeName == null) {
619             ci.println("Please enter Node Name");
620             return;
621         }
622
623         String bridgeName = ci.nextArgument();
624         if (bridgeName == null) {
625             ci.println("Please enter Bridge Name");
626             return;
627         }
628
629         String portName = ci.nextArgument();
630         if (portName == null) {
631             ci.println("Please enter Port Name");
632             return;
633         }
634
635         String vlan = ci.nextArgument();
636         if (vlan == null) {
637             ci.println("Please enter Valid Vlan");
638             return;
639         } else {
640             try {
641             Integer.parseInt(vlan);
642             } catch (Exception e) {
643                 ci.println("Please enter Valid Vlan");
644                 return;
645             }
646         }
647
648         Map<ConfigConstants, Object> configs = new HashMap<ConfigConstants, Object>();
649         configs.put(ConfigConstants.TYPE, "VLAN");
650         configs.put(ConfigConstants.VLAN, vlan);
651
652         Status status;
653         try {
654             status = this.addPort(Node.fromString(nodeName), bridgeName, portName, configs);
655             ci.println("Port creation status : "+status.toString());
656         } catch (Throwable e) {
657             // TODO Auto-generated catch block
658             e.printStackTrace();
659             ci.println("Failed to create Port "+portName+" in Bridge "+bridgeName);
660         }
661     }
662
663     public void _addTunnel (CommandInterpreter ci) {
664         String nodeName = ci.nextArgument();
665         if (nodeName == null) {
666             ci.println("Please enter Node Name");
667             return;
668         }
669
670         String bridgeName = ci.nextArgument();
671         if (bridgeName == null) {
672             ci.println("Please enter Bridge Name");
673             return;
674         }
675
676         String portName = ci.nextArgument();
677         if (portName == null) {
678             ci.println("Please enter Port Name");
679             return;
680         }
681
682         String tunnelType = ci.nextArgument();
683         if (tunnelType == null) {
684             ci.println("Please enter Tunnel Type");
685             return;
686         }
687
688         String remoteIp = ci.nextArgument();
689         if (remoteIp == null) {
690             ci.println("Please enter valid Remote IP Address");
691             return;
692         }
693
694         try {
695             InetAddress.getByName(remoteIp);
696         }  catch (Exception e) {
697             e.printStackTrace();
698             ci.println("Please enter valid Remote IP Address");
699             return;
700         }
701
702         Map<ConfigConstants, Object> configs = new HashMap<ConfigConstants, Object>();
703         configs.put(ConfigConstants.TYPE, "TUNNEL");
704         configs.put(ConfigConstants.TUNNEL_TYPE, tunnelType);
705         configs.put(ConfigConstants.DEST_IP, remoteIp);
706
707         Status status;
708         try {
709             status = this.addPort(Node.fromString(nodeName), bridgeName, portName, configs);
710             ci.println("Port creation status : "+status.toString());
711         } catch (Throwable e) {
712             // TODO Auto-generated catch block
713             e.printStackTrace();
714             ci.println("Failed to create Port "+portName+" in Bridge "+bridgeName);
715         }
716     }
717
718     public void _printCache (CommandInterpreter ci) {
719         String nodeName = ci.nextArgument();
720         if (nodeName == null) {
721             ci.println("Please enter Node Name");
722             return;
723         }
724         inventoryServiceInternal.printCache(Node.fromString(nodeName));
725     }
726
727     public void _forceConnect (CommandInterpreter ci) {
728         String force = ci.nextArgument();
729         if (force.equalsIgnoreCase("YES")) forceConnect = true;
730         else if (force.equalsIgnoreCase("NO")) forceConnect = false;
731         else ci.println("Please enter YES or NO.");
732         ci.println("Current ForceConnect State : "+forceConnect);
733         return;
734     }
735
736     @Override
737     public String getHelp() {
738         StringBuffer help = new StringBuffer();
739         help.append("---OVSDB CLI---\n");
740         help.append("\t ovsconnect <ConnectionName> <ip-address>                        - Connect to OVSDB\n");
741         help.append("\t addBridge <Node> <BridgeName>                                   - Add Bridge\n");
742         help.append("\t addPort <Node> <BridgeName> <PortName>                          - Add Port\n");
743         help.append("\t addPortVlan <Node> <BridgeName> <PortName> <vlan>               - Add Port, Vlan\n");
744         help.append("\t addTunnel <Node> <Bridge> <Port> <tunnel-type> <remote-ip>      - Add Tunnel\n");
745         help.append("\t printCache <Node>                                               - Prints Table Cache");
746         return help.toString();
747     }
748 }