1 package org.opendaylight.ovsdb.plugin;
3 import java.math.BigInteger;
4 import java.net.InetAddress;
5 import java.net.NetworkInterface;
6 import java.net.SocketException;
9 import org.eclipse.osgi.framework.console.CommandInterpreter;
10 import org.eclipse.osgi.framework.console.CommandProvider;
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.OvsDBMap;
23 import org.opendaylight.ovsdb.lib.notation.OvsDBSet;
24 import org.opendaylight.ovsdb.lib.notation.UUID;
25 import org.opendaylight.ovsdb.lib.table.Bridge;
26 import org.opendaylight.ovsdb.lib.table.Controller;
27 import org.opendaylight.ovsdb.lib.table.Interface;
28 import org.opendaylight.ovsdb.lib.table.Open_vSwitch;
29 import org.opendaylight.ovsdb.lib.table.Port;
30 import org.opendaylight.ovsdb.lib.table.internal.Table;
31 import org.opendaylight.controller.clustering.services.IClusterGlobalServices;
32 import org.opendaylight.controller.sal.connection.ConnectionConstants;
33 import org.opendaylight.controller.sal.core.Node;
34 import org.opendaylight.controller.sal.core.NodeConnector;
35 import org.opendaylight.controller.sal.networkconfig.bridgedomain.ConfigConstants;
36 import org.opendaylight.controller.sal.networkconfig.bridgedomain.IPluginInBridgeDomainConfigService;
37 import org.opendaylight.controller.sal.utils.NetUtils;
38 import org.opendaylight.controller.sal.utils.Status;
39 import org.opendaylight.controller.sal.utils.StatusCode;
40 import org.osgi.framework.BundleContext;
41 import org.osgi.framework.FrameworkUtil;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import com.google.common.util.concurrent.ListenableFuture;
47 public class ConfigurationService implements IPluginInBridgeDomainConfigService, CommandProvider
49 private static final Logger logger = LoggerFactory
50 .getLogger(ConfigurationService.class);
52 IConnectionServiceInternal connectionService;
53 InventoryServiceInternal inventoryServiceInternal;
54 private IClusterGlobalServices clusterServices;
55 boolean forceConnect = false;
61 * Function called by the dependency manager when at least one dependency
62 * become unsatisfied or when the component is shutting down because for
63 * example bundle is being stopped.
70 * Function called by dependency manager after "init ()" is called and after
71 * the services provided by the class are registered in the service registry
75 registerWithOSGIConsole();
78 private void registerWithOSGIConsole() {
79 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass())
81 bundleContext.registerService(CommandProvider.class.getName(), this,
86 * Function called by the dependency manager before the services exported by
87 * the component are unregistered, this will be followed by a "destroy ()"
94 public void setConnectionServiceInternal(IConnectionServiceInternal connectionService) {
95 this.connectionService = connectionService;
98 public void unsetConnectionServiceInternal(IConnectionServiceInternal connectionService) {
99 if (this.connectionService == connectionService) {
100 this.connectionService = null;
104 public void setInventoryServiceInternal(InventoryServiceInternal inventoryServiceInternal) {
105 this.inventoryServiceInternal = inventoryServiceInternal;
108 public void unsetInventoryServiceInternal(InventoryServiceInternal inventoryServiceInternal) {
109 if (this.inventoryServiceInternal == inventoryServiceInternal) {
110 this.inventoryServiceInternal = null;
114 public void setClusterServices(IClusterGlobalServices i) {
115 this.clusterServices = i;
118 public void unsetClusterServices(IClusterGlobalServices i) {
119 if (this.clusterServices == i) {
120 this.clusterServices = null;
124 private Connection getConnection (Node node) {
125 Connection connection = connectionService.getConnection(node);
126 if (connection == null || !connection.getChannel().isActive()) {
135 * @param node Node serving this configuration service
136 * @param bridgeConnectorIdentifier String representation of a Bridge Connector
137 * @return Bridge Connector configurations
140 public Status createBridgeDomain(Node node, String bridgeIdentifier,
141 Map<ConfigConstants, Object> configs) throws Throwable {
143 if (connectionService == null) {
144 logger.error("Couldn't refer to the ConnectionService");
145 return new Status(StatusCode.NOSERVICE);
148 Connection connection = this.getConnection(node);
149 if (connection == null) {
150 return new Status(StatusCode.NOSERVICE, "Connection to ovsdb-server not available");
153 Map<String, Table<?>> ovsTable = inventoryServiceInternal.getTableCache(node, Open_vSwitch.NAME.getName());
154 String newBridge = "new_bridge";
155 String newInterface = "new_interface";
156 String newPort = "new_port";
157 String newSwitch = "new_switch";
159 Operation addSwitchRequest = null;
161 if(ovsTable != null){
162 String ovsTableUUID = (String) ovsTable.keySet().toArray()[0];
163 UUID bridgeUuidPair = new UUID(newBridge);
164 Mutation bm = new Mutation("bridges", Mutator.INSERT, bridgeUuidPair);
165 List<Mutation> mutations = new ArrayList<Mutation>();
168 UUID uuid = new UUID(ovsTableUUID);
169 Condition condition = new Condition("_uuid", Function.EQUALS, uuid);
170 List<Condition> where = new ArrayList<Condition>();
171 where.add(condition);
172 addSwitchRequest = new MutateOperation(Open_vSwitch.NAME.getName(), where, mutations);
175 Open_vSwitch ovsTableRow = new Open_vSwitch();
176 OvsDBSet<UUID> bridges = new OvsDBSet<UUID>();
177 UUID bridgeUuidPair = new UUID(newBridge);
178 bridges.add(bridgeUuidPair);
179 ovsTableRow.setBridges(bridges);
180 addSwitchRequest = new InsertOperation(Open_vSwitch.NAME.getName(), newSwitch, ovsTableRow);
183 Bridge bridgeRow = new Bridge();
184 bridgeRow.setName(bridgeIdentifier);
185 OvsDBSet<UUID> ports = new OvsDBSet<UUID>();
186 UUID port = new UUID(newPort);
188 bridgeRow.setPorts(ports);
189 InsertOperation addBridgeRequest = new InsertOperation(Bridge.NAME.getName(), newBridge, bridgeRow);
191 Port portRow = new Port();
192 portRow.setName(bridgeIdentifier);
193 OvsDBSet<UUID> interfaces = new OvsDBSet<UUID>();
194 UUID interfaceid = new UUID(newInterface);
195 interfaces.add(interfaceid);
196 portRow.setInterfaces(interfaces);
197 InsertOperation addPortRequest = new InsertOperation(Port.NAME.getName(), newPort, portRow);
199 Interface interfaceRow = new Interface();
200 interfaceRow.setName(bridgeIdentifier);
201 interfaceRow.setType("internal");
202 InsertOperation addIntfRequest = new InsertOperation(Interface.NAME.getName(), newInterface, interfaceRow);
204 /* Update config version */
205 String ovsTableUUID = (String) ovsTable.keySet().toArray()[0];
206 Mutation bm = new Mutation("next_cfg", Mutator.SUM, 1);
207 List<Mutation> mutations = new ArrayList<Mutation>();
210 UUID uuid = new UUID(ovsTableUUID);
211 Condition condition = new Condition("_uuid", Function.EQUALS, uuid);
212 List<Condition> where = new ArrayList<Condition>();
213 where.add(condition);
214 MutateOperation updateCfgVerRequest = new MutateOperation(Open_vSwitch.NAME.getName(), where, mutations);
216 TransactBuilder transaction = new TransactBuilder();
217 transaction.addOperations(new ArrayList<Operation>(
218 Arrays.asList(addSwitchRequest, addIntfRequest, addPortRequest, addBridgeRequest, updateCfgVerRequest)));
220 ListenableFuture<List<OperationResult>> transResponse = connection.getRpc().transact(transaction);
221 List<OperationResult> tr = transResponse.get();
222 List<Operation> requests = transaction.getRequests();
223 Status status = new Status(StatusCode.SUCCESS);
224 for (int i = 0; i < tr.size() ; i++) {
225 if (i < requests.size()) requests.get(i).setResult(tr.get(i));
226 if (tr.get(i) != null && tr.get(i).getError() != null && tr.get(i).getError().trim().length() > 0) {
227 OperationResult result = tr.get(i);
228 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
232 if (tr.size() > requests.size()) {
233 OperationResult result = tr.get(tr.size()-1);
234 logger.error("Error creating Bridge : {}\n Error : {}\n Details : {}", bridgeIdentifier,
236 result.getDetails());
237 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
239 if (status.isSuccess()) {
240 setBridgeOFController(node, bridgeIdentifier);
243 } catch(Exception e){
246 return new Status(StatusCode.INTERNALERROR);
250 * Create a Port Attached to a Bridge
251 * Ex. ovs-vsctl add-port br0 vif0
252 * @param node Node serving this configuration service
253 * @param bridgeDomainIdentifier String representation of a Bridge Domain
254 * @param portIdentifier String representation of a user defined Port Name
257 public Status addPort(Node node, String bridgeIdentifier, String portIdentifier,
258 Map<ConfigConstants, Object> configs) {
260 if (connectionService == null) {
261 logger.error("Couldn't refer to the ConnectionService");
262 return new Status(StatusCode.NOSERVICE);
264 Connection connection = this.getConnection(node);
265 if (connection == null) {
266 return new Status(StatusCode.NOSERVICE, "Connection to ovsdb-server not available");
268 if (connection != null) {
269 Map<String, Table<?>> brTable = inventoryServiceInternal.getTableCache(node, Bridge.NAME.getName());
270 String newBridge = "new_bridge";
271 String newInterface = "new_interface";
272 String newPort = "new_port";
275 Operation addBrMutRequest = null;
276 String brUuid = null;
277 for (String uuid : brTable.keySet()) {
278 Bridge bridge = (Bridge) brTable.get(uuid);
279 if (bridge.getName().contains(bridgeIdentifier)) {
284 UUID brUuidPair = new UUID(newPort);
285 Mutation bm = new Mutation("ports", Mutator.INSERT, brUuidPair);
286 List<Mutation> mutations = new ArrayList<Mutation>();
289 UUID uuid = new UUID(brUuid);
290 Condition condition = new Condition("_uuid", Function.EQUALS, uuid);
291 List<Condition> where = new ArrayList<Condition>();
292 where.add(condition);
293 addBrMutRequest = new MutateOperation(Bridge.NAME.getName(), where, mutations);
295 OvsDBMap<String, String> options = null;
297 OvsDBSet<BigInteger> tags = null;
298 if (configs != null) {
299 type = (String) configs.get(ConfigConstants.TYPE);
300 Map<String, String> customConfigs = (Map<String, String>) configs.get(ConfigConstants.CUSTOM);
301 if (customConfigs != null) {
302 options = new OvsDBMap<String, String>();
303 for (String customConfig : customConfigs.keySet()) {
304 options.put(customConfig, customConfigs.get(customConfig));
309 Interface interfaceRow = new Interface();
310 interfaceRow.setName(portIdentifier);
313 if (type.equalsIgnoreCase(OvsdbType.PortType.TUNNEL.name())) {
314 interfaceRow.setType((String)configs.get(ConfigConstants.TUNNEL_TYPE));
315 if (options == null) options = new OvsDBMap<String, String>();
316 options.put("remote_ip", (String)configs.get(ConfigConstants.DEST_IP));
317 } else if (type.equalsIgnoreCase(OvsdbType.PortType.VLAN.name())) {
318 tags = new OvsDBSet<BigInteger>();
319 tags.add(BigInteger.valueOf(Integer.parseInt((String)configs.get(ConfigConstants.VLAN))));
320 } else if (type.equalsIgnoreCase(OvsdbType.PortType.PATCH.name())) {
321 interfaceRow.setType(type.toLowerCase());
324 if (options != null) {
325 interfaceRow.setOptions(options);
328 InsertOperation addIntfRequest = new InsertOperation(Interface.NAME.getName(),
329 newInterface, interfaceRow);
331 Port portRow = new Port();
332 portRow.setName(portIdentifier);
333 if (tags != null) portRow.setTag(tags);
334 OvsDBSet<UUID> interfaces = new OvsDBSet<UUID>();
335 UUID interfaceid = new UUID(newInterface);
336 interfaces.add(interfaceid);
337 portRow.setInterfaces(interfaces);
338 InsertOperation addPortRequest = new InsertOperation(Port.NAME.getName(), newPort, portRow);
340 TransactBuilder transaction = new TransactBuilder();
341 transaction.addOperations(new ArrayList<Operation>
342 (Arrays.asList(addBrMutRequest, addPortRequest, addIntfRequest)));
344 ListenableFuture<List<OperationResult>> transResponse = connection.getRpc().transact(transaction);
345 List<OperationResult> tr = transResponse.get();
346 List<Operation> requests = transaction.getRequests();
347 Status status = new Status(StatusCode.SUCCESS);
348 for (int i = 0; i < tr.size() ; i++) {
349 if (i < requests.size()) requests.get(i).setResult(tr.get(i));
350 if (tr.get(i).getError() != null && tr.get(i).getError().trim().length() > 0) {
351 OperationResult result = tr.get(i);
352 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
356 if (tr.size() > requests.size()) {
357 OperationResult result = tr.get(tr.size()-1);
358 logger.error("Error creating Bridge : {}\n Error : {}\n Details : {}", bridgeIdentifier,
360 result.getDetails());
361 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
365 return new Status(StatusCode.INTERNALERROR);
367 } catch(Exception e){
370 return new Status(StatusCode.INTERNALERROR);
374 * Implements the OVS Connection for Managers
376 * @param node Node serving this configuration service
377 * @param String with IP and connection types
379 @SuppressWarnings("unchecked")
380 public boolean setManager(Node node, String managerip) throws Throwable{
382 if (connectionService == null) {
383 logger.error("Couldn't refer to the ConnectionService");
386 Connection connection = this.getConnection(node);
387 if (connection == null) {
391 if (connection != null) {
392 String newmanager = "new_manager";
394 OVSInstance instance = OVSInstance.monitorOVS(connection);
396 Map ovsoutter = new LinkedHashMap();
397 Map ovsinner = new LinkedHashMap();
398 ArrayList ovsalist1 = new ArrayList();
399 ArrayList ovsalist2 = new ArrayList();
400 ArrayList ovsalist3 = new ArrayList();
401 ArrayList ovsalist4 = new ArrayList();
404 ovsoutter.put("where", ovsalist1);
405 ovsalist1.add(ovsalist2);
406 ovsalist2.add("_uuid");
408 ovsalist2.add(ovsalist3);
409 ovsalist3.add("uuid");
410 ovsalist3.add(instance.getUuid());
411 ovsoutter.put("op", "update");
412 ovsoutter.put("table", "Open_vSwitch");
413 ovsoutter.put("row", ovsinner);
414 ovsinner.put("manager_options", ovsalist4);
415 ovsalist4.add("named-uuid");
416 ovsalist4.add(newmanager);
418 Map mgroutside = new LinkedHashMap();
419 Map mgrinside = new LinkedHashMap();
421 //Manager Table Insert
422 mgroutside.put("uuid-name", newmanager);
423 mgroutside.put("op", "insert");
424 mgroutside.put("table","Manager");
425 mgroutside.put("row", mgrinside);
426 mgrinside.put("target", managerip);
428 Object[] params = {"Open_vSwitch", ovsoutter, mgroutside};
429 OvsdbMessage msg = new OvsdbMessage("transact", params);
431 //connection.sendMessage(msg);
441 public Status addBridgeDomainConfig(Node node, String bridgeIdentfier,
442 Map<ConfigConstants, Object> configs) {
443 String mgmt = (String)configs.get(ConfigConstants.MGMT);
446 if (setManager(node, mgmt)) return new Status(StatusCode.SUCCESS);
447 } catch (Throwable e) {
448 // TODO Auto-generated catch block
450 return new Status(StatusCode.INTERNALERROR);
453 return new Status(StatusCode.BADREQUEST);
457 public Status addPortConfig(Node node, String bridgeIdentifier, String portIdentifier,
458 Map<ConfigConstants, Object> configs) {
459 // TODO Auto-generated method stub
464 public Status deletePort(Node node, String bridgeIdentifier, String portIdentifier) {
467 if (connectionService == null) {
468 logger.error("Couldn't refer to the ConnectionService");
469 return new Status(StatusCode.NOSERVICE);
472 Connection connection = this.getConnection(node);
473 if (connection == null) {
474 return new Status(StatusCode.NOSERVICE, "Connection to ovsdb-server not available");
477 Map<String, Table<?>> brTable = inventoryServiceInternal.getTableCache(node, Bridge.NAME.getName());
478 Map<String, Table<?>> portTable = inventoryServiceInternal.getTableCache(node, Port.NAME.getName());
479 Operation delPortRequest = null;
480 String brUuid = null;
481 String portUuid = null;
483 for (String uuid : brTable.keySet()) {
484 Bridge bridge = (Bridge) brTable.get(uuid);
485 if (bridge.getName().contains(bridgeIdentifier)) {
490 if(portTable != null){
491 for (String uuid : portTable.keySet()) {
492 Port port = (Port) portTable.get(uuid);
493 if (port.getName().contains(portIdentifier)) {
499 UUID portUuidPair = new UUID(portUuid);
500 Mutation bm = new Mutation("ports", Mutator.DELETE, portUuidPair);
501 List<Mutation> mutations = new ArrayList<Mutation>();
504 UUID uuid = new UUID(brUuid);
505 Condition condition = new Condition("_uuid", Function.EQUALS, uuid);
506 List<Condition> where = new ArrayList<Condition>();
507 where.add(condition);
508 delPortRequest = new MutateOperation(Bridge.NAME.getName(), where, mutations);
510 TransactBuilder transaction = new TransactBuilder();
511 transaction.addOperations(new ArrayList<Operation>(Arrays.asList(delPortRequest)));
513 ListenableFuture<List<OperationResult>> transResponse = connection.getRpc().transact(transaction);
514 List<OperationResult> tr = transResponse.get();
515 List<Operation> requests = transaction.getRequests();
516 Status status = new Status(StatusCode.SUCCESS);
517 for (int i = 0; i < tr.size() ; i++) {
518 if (i < requests.size()) requests.get(i).setResult(tr.get(i));
519 if (tr.get(i).getError() != null && tr.get(i).getError().trim().length() > 0) {
520 OperationResult result = tr.get(i);
521 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
525 if (tr.size() > requests.size()) {
526 OperationResult result = tr.get(tr.size()-1);
527 logger.error("Error creating Bridge : {}\n Error : {}\n Details : {}", bridgeIdentifier,
529 result.getDetails());
530 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
533 } catch(Exception e){
536 return new Status(StatusCode.INTERNALERROR);
540 public Node getBridgeDomainNode(Node node, String bridgeIdentifier) {
541 // TODO Auto-generated method stub
546 public Map<ConfigConstants, Object> getPortConfigs(Node node, String bridgeIdentifier,
547 String portIdentifier) {
548 // TODO Auto-generated method stub
553 public Status removeBridgeDomainConfig(Node node, String bridgeIdentifier,
554 Map<ConfigConstants, Object> configs) {
555 // TODO Auto-generated method stub
560 public Status removePortConfig(Node node, String bridgeIdentifier, String portIdentifier,
561 Map<ConfigConstants, Object> configs) {
562 // TODO Auto-generated method stub
567 public Status deleteBridgeDomain(Node node, String bridgeIdentifier) {
570 if (connectionService == null) {
571 logger.error("Couldn't refer to the ConnectionService");
572 return new Status(StatusCode.NOSERVICE);
574 Connection connection = this.getConnection(node);
575 if (connection == null) {
576 return new Status(StatusCode.NOSERVICE, "Connection to ovsdb-server not available");
578 Map<String, Table<?>> ovsTable = inventoryServiceInternal.getTableCache(node, Open_vSwitch.NAME.getName());
579 Map<String, Table<?>> brTable = inventoryServiceInternal.getTableCache(node, Bridge.NAME.getName());
580 Operation delBrRequest = null;
581 String ovsUuid = null;
582 String brUuid = null;
584 if (brTable != null) {
585 for (String uuid : brTable.keySet()) {
586 Bridge bridge = (Bridge) brTable.get(uuid);
587 if (bridge.getName().contains(bridgeIdentifier)) {
592 if (ovsTable != null) {
593 ovsUuid = (String) ovsTable.keySet().toArray()[0];
595 UUID bridgeUuidPair = new UUID(brUuid);
596 Mutation bm = new Mutation("bridges", Mutator.DELETE, bridgeUuidPair);
597 List<Mutation> mutations = new ArrayList<Mutation>();
600 UUID uuid = new UUID(ovsUuid);
601 Condition condition = new Condition("_uuid", Function.EQUALS, uuid);
602 List<Condition> where = new ArrayList<Condition>();
603 where.add(condition);
604 delBrRequest = new MutateOperation(Open_vSwitch.NAME.getName(), where, mutations);
606 TransactBuilder transaction = new TransactBuilder();
607 transaction.addOperations(new ArrayList<Operation>(Arrays.asList(delBrRequest)));
609 ListenableFuture<List<OperationResult>> transResponse = connection.getRpc().transact(transaction);
610 List<OperationResult> tr = transResponse.get();
611 List<Operation> requests = transaction.getRequests();
612 Status status = new Status(StatusCode.SUCCESS);
613 for (int i = 0; i < tr.size(); i++) {
614 if (i < requests.size()) requests.get(i).setResult(tr.get(i));
615 if (tr.get(i) != null && tr.get(i).getError() != null && tr.get(i).getError().trim().length() > 0) {
616 OperationResult result = tr.get(i);
617 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
621 if (tr.size() > requests.size()) {
622 OperationResult result = tr.get(tr.size() - 1);
623 logger.error("Error creating Bridge : {}\n Error : {}\n Details : {}",
624 bridgeIdentifier, result.getError(), result.getDetails());
625 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
628 } catch (Exception e) {
631 return new Status(StatusCode.INTERNALERROR);
635 public Map<ConfigConstants, Object> getBridgeDomainConfigs(Node node, String bridgeIdentifier) {
636 // TODO Auto-generated method stub
641 public List<String> getBridgeDomains(Node node) {
642 List<String> brlist = new ArrayList<String>();
643 Map<String, Table<?>> brTableCache = inventoryServiceInternal.getTableCache(node, Bridge.NAME.getName());
644 if(brTableCache != null){
645 for (String uuid : brTableCache.keySet()) {
646 Bridge bridge = (Bridge) brTableCache.get(uuid);
647 brlist.add(bridge.getName());
654 public NodeConnector getNodeConnector(Node arg0, String arg1, String arg2) {
658 private short getControllerOFPort() {
659 Short defaultOpenFlowPort = 6633;
660 Short openFlowPort = defaultOpenFlowPort;
661 String portString = System.getProperty("of.listenPort");
662 if (portString != null) {
664 openFlowPort = Short.decode(portString).shortValue();
665 } catch (NumberFormatException e) {
666 logger.warn("Invalid port:{}, use default({})", portString,
673 private List<InetAddress> getControllerIPAddresses() {
674 List<InetAddress> controllers = null;
675 if (clusterServices != null) {
676 controllers = clusterServices.getClusteredControllers();
677 if (controllers != null && controllers.size() > 0) {
678 if (controllers.size() == 1) {
679 InetAddress controller = controllers.get(0);
680 if (!controller.equals(InetAddress.getLoopbackAddress())) {
689 controllers = new ArrayList<InetAddress>();
690 InetAddress controllerIP;
691 Enumeration<NetworkInterface> nets;
693 nets = NetworkInterface.getNetworkInterfaces();
694 for (NetworkInterface netint : Collections.list(nets)) {
695 Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
696 for (InetAddress inetAddress : Collections.list(inetAddresses)) {
697 if (!inetAddress.isLoopbackAddress() &&
698 NetUtils.isIPv4AddressValid(inetAddress.getHostAddress())) {
699 controllers.add(inetAddress);
703 } catch (SocketException e) {
704 controllers.add(InetAddress.getLoopbackAddress());
709 public Boolean setBridgeOFController(Node node, String bridgeIdentifier) {
711 if (connectionService == null) {
712 logger.error("Couldn't refer to the ConnectionService");
715 Connection connection = this.getConnection(node);
716 if (connection == null) {
720 if (connection != null) {
721 List<InetAddress> ofControllerAddrs = getControllerIPAddresses();
722 short ofControllerPort = getControllerOFPort();
723 OvsDBSet<UUID> controllerUUIDs = new OvsDBSet<UUID>();
724 List<Operation> controllerInsertOperations = new ArrayList<Operation>();
725 Map<String, Table<?>> controllerCache = inventoryServiceInternal.getTableCache(node, Controller.NAME.getName());
728 for (InetAddress ofControllerAddress : ofControllerAddrs) {
729 String cntrlUuid = null;
730 String newController = "tcp:"+ofControllerAddress.getHostAddress()+":"+ofControllerPort;
731 if (controllerCache != null) {
732 for (String uuid : controllerCache.keySet()) {
733 Controller controller = (Controller)controllerCache.get(uuid);
734 if (controller.getTarget().equals(newController)) {
736 controllerUUIDs.add(new UUID(uuid));
741 if (cntrlUuid == null) {
743 String uuid_name = "new_controller_"+count;
744 controllerUUIDs.add(new UUID(uuid_name));
745 Controller controllerRow = new Controller();
746 controllerRow.setTarget(newController);
747 InsertOperation addCtlRequest = new InsertOperation(Controller.NAME.getName(), uuid_name, controllerRow);
748 controllerInsertOperations.add(addCtlRequest);
751 String brCntrlUuid = null;
752 Map<String, Table<?>> brTableCache = inventoryServiceInternal.getTableCache(node, Bridge.NAME.getName());
753 for (String uuid : brTableCache.keySet()) {
754 Bridge bridge = (Bridge)brTableCache.get(uuid);
755 if (bridge.getName().contains(bridgeIdentifier)) {
759 Operation addControlRequest = null;
760 Mutation bm = new Mutation("controller", Mutator.INSERT, controllerUUIDs);
761 List<Mutation> mutations = new ArrayList<Mutation>();
764 UUID uuid = new UUID(brCntrlUuid);
765 Condition condition = new Condition("_uuid", Function.EQUALS, uuid);
766 List<Condition> where = new ArrayList<Condition>();
767 where.add(condition);
768 addControlRequest = new MutateOperation(Bridge.NAME.getName(), where, mutations);
770 TransactBuilder transaction = new TransactBuilder();
771 transaction.addOperations(controllerInsertOperations);
772 transaction.addOperation(addControlRequest);
774 ListenableFuture<List<OperationResult>> transResponse = connection.getRpc().transact(transaction);
775 List<OperationResult> tr = transResponse.get();
776 List<Operation> requests = transaction.getRequests();
777 Status status = new Status(StatusCode.SUCCESS);
778 for (int i = 0; i < tr.size() ; i++) {
779 if (i < requests.size()) requests.get(i).setResult(tr.get(i));
780 if (tr.get(i) != null && tr.get(i).getError() != null && tr.get(i).getError().trim().length() > 0) {
781 OperationResult result = tr.get(i);
782 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
786 if (tr.size() > requests.size()) {
787 OperationResult result = tr.get(tr.size()-1);
788 logger.error("Error creating Bridge : {}\n Error : {}\n Details : {}", bridgeIdentifier,
790 result.getDetails());
791 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
801 public void _ovsconnect (CommandInterpreter ci) {
802 String bridgeName = ci.nextArgument();
803 if (bridgeName == null) {
804 ci.println("Please enter Bridge Name");
808 String ovsdbserver = ci.nextArgument();
809 if (ovsdbserver == null) {
810 ci.println("Please enter valid IP-Address");
814 InetAddress.getByName(ovsdbserver);
815 } catch (Exception e) {
817 ci.println("Please enter valid IP-Address");
820 String port = ci.nextArgument();
825 ci.println("connecting to ovsdb server : "+ovsdbserver+":"+port+" ... ");
826 Map<ConnectionConstants, String> params = new HashMap<ConnectionConstants, String>();
827 params.put(ConnectionConstants.ADDRESS, ovsdbserver);
828 params.put(ConnectionConstants.PORT, port);
829 Node node = connectionService.connect(bridgeName, params);
830 if (node != null) ci.println("Node Name: "+node.toString());
831 else ci.println("Could not connect to Node");
834 public void _addBridge (CommandInterpreter ci) {
835 String nodeName = ci.nextArgument();
836 if (nodeName == null) {
837 ci.println("Please enter Node Name");
840 String bridgeName = ci.nextArgument();
841 if (bridgeName == null) {
842 ci.println("Please enter Bridge Name");
848 Node node = Node.fromString(nodeName);
850 ci.println("Invalid Node");
853 status = this.createBridgeDomain(node, bridgeName, null);
854 ci.println("Bridge creation status : "+status.toString());
855 } catch (Throwable e) {
856 // TODO Auto-generated catch block
858 ci.println("Failed to create Bridge "+bridgeName);
862 public void _getBridgeDomains (CommandInterpreter ci) {
863 String nodeName = ci.nextArgument();
864 if (nodeName == null) {
865 ci.println("Please enter Node Name");
870 List<String> brlist = new ArrayList<String>();
872 Node node = Node.fromString(nodeName);
873 brlist = this.getBridgeDomains(node);
875 ci.println("Invalid Node");
878 ci.println("Existing Bridges: "+brlist.toString());
879 } catch (Throwable e) {
881 ci.println("Failed to list Bridges");
885 public void _deleteBridgeDomain (CommandInterpreter ci) {
886 String nodeName = ci.nextArgument();
887 if (nodeName == null) {
888 ci.println("Please enter Node Name");
891 String bridgeName = ci.nextArgument();
892 if (bridgeName == null) {
893 ci.println("Please enter Bridge Name");
898 Node node = Node.fromString(nodeName);
900 ci.println("Invalid Node");
903 status = this.deleteBridgeDomain(node, bridgeName);
904 ci.println("Bridge deletion status : "+status.toString());
905 } catch (Throwable e) {
907 ci.println("Failed to delete Bridge "+bridgeName);
911 public void _addPort (CommandInterpreter ci) {
912 String nodeName = ci.nextArgument();
913 if (nodeName == null) {
914 ci.println("Please enter Node Name");
918 String bridgeName = ci.nextArgument();
919 if (bridgeName == null) {
920 ci.println("Please enter Bridge Name");
924 String portName = ci.nextArgument();
925 if (portName == null) {
926 ci.println("Please enter Port Name");
930 String type = ci.nextArgument();
932 Map<String, String> configs = new HashMap<String, String>();
934 String configKey = ci.nextArgument();
935 if (configKey == null) break;
936 String configValue = ci.nextArgument();
937 if (configValue == null) break;
938 configs.put(configKey, configValue);
941 Map<ConfigConstants, Object> customConfigs = null;
943 customConfigs = new HashMap<ConfigConstants, Object>();
944 customConfigs.put(ConfigConstants.TYPE, type);
947 if (configs.size() > 0) {
948 if (customConfigs == null) customConfigs = new HashMap<ConfigConstants, Object>();
949 customConfigs.put(ConfigConstants.CUSTOM, configs);
950 ci.println(customConfigs.toString());
954 Node node = Node.fromString(nodeName);
956 ci.println("Invalid Node");
959 status = this.addPort(node, bridgeName, portName, customConfigs);
960 ci.println("Port creation status : "+status.toString());
961 } catch (Throwable e) {
962 // TODO Auto-generated catch block
964 ci.println("Failed to create Port "+portName+" in Bridge "+bridgeName);
968 public void _deletePort (CommandInterpreter ci) {
969 String nodeName = ci.nextArgument();
970 if (nodeName == null) {
971 ci.println("Please enter Node Name");
975 String bridgeName = ci.nextArgument();
976 if (bridgeName == null) {
977 ci.println("Please enter Bridge Name");
981 String portName = ci.nextArgument();
982 if (portName == null) {
983 ci.println("Please enter Port Name");
989 Node node = Node.fromString(nodeName);
991 ci.println("Invalid Node");
994 status = this.deletePort(node, bridgeName, portName);
995 ci.println("Port deletion status : "+status.toString());
996 } catch (Throwable e) {
997 // TODO Auto-generated catch block
999 ci.println("Failed to delete Port "+portName+" in Bridge "+bridgeName);
1003 public void _addPortVlan (CommandInterpreter ci) {
1004 String nodeName = ci.nextArgument();
1005 if (nodeName == null) {
1006 ci.println("Please enter Node Name");
1010 String bridgeName = ci.nextArgument();
1011 if (bridgeName == null) {
1012 ci.println("Please enter Bridge Name");
1016 String portName = ci.nextArgument();
1017 if (portName == null) {
1018 ci.println("Please enter Port Name");
1022 String vlan = ci.nextArgument();
1024 ci.println("Please enter Valid Vlan");
1028 Integer.parseInt(vlan);
1029 } catch (Exception e) {
1030 ci.println("Please enter Valid Vlan");
1035 Map<ConfigConstants, Object> configs = new HashMap<ConfigConstants, Object>();
1036 configs.put(ConfigConstants.TYPE, "VLAN");
1037 configs.put(ConfigConstants.VLAN, vlan);
1041 Node node = Node.fromString(nodeName);
1043 ci.println("Invalid Node");
1046 status = this.addPort(node, bridgeName, portName, configs);
1047 ci.println("Port creation status : "+status.toString());
1048 } catch (Throwable e) {
1049 // TODO Auto-generated catch block
1050 e.printStackTrace();
1051 ci.println("Failed to create Port "+portName+" in Bridge "+bridgeName);
1055 public void _addTunnel (CommandInterpreter ci) {
1056 String nodeName = ci.nextArgument();
1057 if (nodeName == null) {
1058 ci.println("Please enter Node Name");
1062 String bridgeName = ci.nextArgument();
1063 if (bridgeName == null) {
1064 ci.println("Please enter Bridge Name");
1068 String portName = ci.nextArgument();
1069 if (portName == null) {
1070 ci.println("Please enter Port Name");
1074 String tunnelType = ci.nextArgument();
1075 if (tunnelType == null) {
1076 ci.println("Please enter Tunnel Type");
1080 String remoteIp = ci.nextArgument();
1081 if (remoteIp == null) {
1082 ci.println("Please enter valid Remote IP Address");
1087 InetAddress.getByName(remoteIp);
1088 } catch (Exception e) {
1089 e.printStackTrace();
1090 ci.println("Please enter valid Remote IP Address");
1094 Map<ConfigConstants, Object> configs = new HashMap<ConfigConstants, Object>();
1095 configs.put(ConfigConstants.TYPE, "TUNNEL");
1096 configs.put(ConfigConstants.TUNNEL_TYPE, tunnelType);
1097 configs.put(ConfigConstants.DEST_IP, remoteIp);
1101 Node node = Node.fromString(nodeName);
1103 ci.println("Invalid Node");
1106 status = this.addPort(node, bridgeName, portName, configs);
1107 ci.println("Port creation status : "+status.toString());
1108 } catch (Throwable e) {
1109 // TODO Auto-generated catch block
1110 e.printStackTrace();
1111 ci.println("Failed to create Port "+portName+" in Bridge "+bridgeName);
1115 public void _printCache (CommandInterpreter ci) {
1116 String nodeName = ci.nextArgument();
1117 if (nodeName == null) {
1118 ci.println("Please enter Node Name");
1121 Node node = Node.fromString(nodeName);
1123 ci.println("Invalid Node");
1126 inventoryServiceInternal.printCache(node);
1129 public void _forceConnect (CommandInterpreter ci) {
1130 String force = ci.nextArgument();
1131 if (force.equalsIgnoreCase("YES")) forceConnect = true;
1132 else if (force.equalsIgnoreCase("NO")) forceConnect = false;
1133 else ci.println("Please enter YES or NO.");
1134 ci.println("Current ForceConnect State : "+forceConnect);
1139 public String getHelp() {
1140 StringBuffer help = new StringBuffer();
1141 help.append("---OVSDB CLI---\n");
1142 help.append("\t ovsconnect <ConnectionName> <ip-address> - Connect to OVSDB\n");
1143 help.append("\t addBridge <Node> <BridgeName> - Add Bridge\n");
1144 help.append("\t getBridgeDomains <Node> - Get Bridges\n");
1145 help.append("\t deleteBridgeDomain <Node> <BridgeName> - Delete a Bridge\n");
1146 help.append("\t addPort <Node> <BridgeName> <PortName> <type> <options pairs> - Add Port\n");
1147 help.append("\t deletePort <Node> <BridgeName> <PortName> - Delete Port\n");
1148 help.append("\t addPortVlan <Node> <BridgeName> <PortName> <vlan> - Add Port, Vlan\n");
1149 help.append("\t addTunnel <Node> <Bridge> <Port> <tunnel-type> <remote-ip> - Add Tunnel\n");
1150 help.append("\t printCache <Node> - Prints Table Cache");
1151 return help.toString();