Hyunsun Moon
Committed by Gerrit Code Review

CORD-208 Implement additional ovsdb client methods

- Create a bridge with data path id
- Create a tunnel port with some options

Change-Id: I9fbf6322272f9151c0fc904d1c681e5ffb793be0
...@@ -28,6 +28,7 @@ import org.onosproject.ovsdb.rfc.operations.Operation; ...@@ -28,6 +28,7 @@ import org.onosproject.ovsdb.rfc.operations.Operation;
28 import org.onosproject.ovsdb.rfc.schema.DatabaseSchema; 28 import org.onosproject.ovsdb.rfc.schema.DatabaseSchema;
29 29
30 import java.util.List; 30 import java.util.List;
31 +import java.util.Map;
31 import java.util.Set; 32 import java.util.Set;
32 33
33 /** 34 /**
...@@ -50,6 +51,17 @@ public interface OvsdbClientService extends OvsdbRPC { ...@@ -50,6 +51,17 @@ public interface OvsdbClientService extends OvsdbRPC {
50 void createTunnel(IpAddress srcIp, IpAddress dstIp); 51 void createTunnel(IpAddress srcIp, IpAddress dstIp);
51 52
52 /** 53 /**
54 + * Creates a tunnel port with given options.
55 + *
56 + * @param bridgeName bridge name
57 + * @param portName port name
58 + * @param tunnelType tunnel type
59 + * @param options tunnel options
60 + * @return true if tunnel creation is successful, false otherwise
61 + */
62 + boolean createTunnel(String bridgeName, String portName, String tunnelType, Map<String, String> options);
63 +
64 + /**
53 * Drops the configuration for the tunnel. 65 * Drops the configuration for the tunnel.
54 * 66 *
55 * @param srcIp source IP address 67 * @param srcIp source IP address
...@@ -72,6 +84,17 @@ public interface OvsdbClientService extends OvsdbRPC { ...@@ -72,6 +84,17 @@ public interface OvsdbClientService extends OvsdbRPC {
72 void createBridge(String bridgeName); 84 void createBridge(String bridgeName);
73 85
74 /** 86 /**
87 + * Creates a bridge with given name and dpid.
88 + * Sets the bridge's controller with given controllers.
89 + *
90 + * @param bridgeName bridge name
91 + * @param dpid data path id
92 + * @param controllers controllers
93 + * @return true if bridge creation is successful, false otherwise
94 + */
95 + boolean createBridge(String bridgeName, String dpid, List<ControllerInfo> controllers);
96 +
97 + /**
75 * Drops a bridge. 98 * Drops a bridge.
76 * 99 *
77 * @param bridgeName bridge name 100 * @param bridgeName bridge name
......
...@@ -73,6 +73,8 @@ import org.slf4j.LoggerFactory; ...@@ -73,6 +73,8 @@ import org.slf4j.LoggerFactory;
73 73
74 import java.net.InetSocketAddress; 74 import java.net.InetSocketAddress;
75 import java.util.ArrayList; 75 import java.util.ArrayList;
76 +import java.util.Arrays;
77 +import java.util.HashMap;
76 import java.util.HashSet; 78 import java.util.HashSet;
77 import java.util.Iterator; 79 import java.util.Iterator;
78 import java.util.List; 80 import java.util.List;
...@@ -481,6 +483,52 @@ public class DefaultOvsdbClient ...@@ -481,6 +483,52 @@ public class DefaultOvsdbClient
481 log.info("Create bridge success"); 483 log.info("Create bridge success");
482 } 484 }
483 485
486 + @Override
487 + public boolean createBridge(String bridgeName, String dpid, List<ControllerInfo> controllers) {
488 +
489 + DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME);
490 + String ovsUuid = getOvsUuid(OvsdbConstant.DATABASENAME);
491 +
492 + if (dbSchema == null || ovsUuid == null) {
493 + log.warn("Couldn't find database Open_vSwitch");
494 + return false;
495 + }
496 +
497 + String bridgeUuid = getBridgeUuid(bridgeName);
498 + if (bridgeUuid != null) {
499 + log.warn("Bridge {} is already exist", bridgeName);
500 + // remove existing one and re-create?
501 + return false;
502 + }
503 +
504 + Bridge bridge = (Bridge) TableGenerator.createTable(dbSchema, OvsdbTable.BRIDGE);
505 + bridge.setName(bridgeName);
506 +
507 + Set<String> failMode = new HashSet<>(Arrays.asList("secure"));
508 + bridge.setFailMode(failMode);
509 +
510 + Set<String> protocols = new HashSet<>(Arrays.asList(OvsdbConstant.OPENFLOW13));
511 + bridge.setProtocols(protocols);
512 +
513 + Map<String, String> options = new HashMap<>();
514 + options.put("datapath-id", dpid);
515 + bridge.setOtherConfig(options);
516 +
517 + bridgeUuid = insertConfig(OvsdbConstant.BRIDGE, "_uuid",
518 + OvsdbConstant.DATABASENAME, "bridges",
519 + ovsUuid, bridge.getRow());
520 +
521 + if (bridgeUuid != null) {
522 + createPort(bridgeName, bridgeName);
523 + } else {
524 + log.warn("Failed to create bridge {} on {}", bridgeName, nodeId.toString());
525 + return false;
526 + }
527 +
528 + setControllersWithUUID(UUID.uuid(bridgeUuid), controllers);
529 + return true;
530 + }
531 +
484 /** 532 /**
485 * Sets the bridge's controller automatically. 533 * Sets the bridge's controller automatically.
486 * <p/> 534 * <p/>
...@@ -648,6 +696,50 @@ public class DefaultOvsdbClient ...@@ -648,6 +696,50 @@ public class DefaultOvsdbClient
648 } 696 }
649 697
650 @Override 698 @Override
699 + public boolean createTunnel(String bridgeName, String portName, String tunnelType, Map<String, String> options) {
700 +
701 + String bridgeUuid = getBridgeUuid(bridgeName);
702 + if (bridgeUuid == null) {
703 + log.warn("Couldn't find bridge {} in {}", bridgeName, nodeId.getIpAddress());
704 + return false;
705 + }
706 +
707 + if (getPortUuid(portName, bridgeUuid) != null) {
708 + log.warn("Port {} already exists", portName);
709 + // remove existing one and re-create?
710 + return false;
711 + }
712 +
713 + ArrayList<Operation> operations = Lists.newArrayList();
714 + DatabaseSchema dbSchema = schema.get(OvsdbConstant.DATABASENAME);
715 +
716 + // insert a new port to the port table
717 + Port port = (Port) TableGenerator.createTable(dbSchema, OvsdbTable.PORT);
718 + port.setName(portName);
719 + Insert portInsert = new Insert(dbSchema.getTableSchema("Port"), "Port", port.getRow());
720 + portInsert.getRow().put("interfaces", UUID.uuid("Interface"));
721 + operations.add(portInsert);
722 +
723 + // update the bridge table
724 + Condition condition = ConditionUtil.equals("_uuid", UUID.uuid(bridgeUuid));
725 + Mutation mutation = MutationUtil.insert("ports", UUID.uuid("Port"));
726 + List<Condition> conditions = new ArrayList<>(Arrays.asList(condition));
727 + List<Mutation> mutations = new ArrayList<>(Arrays.asList(mutation));
728 + operations.add(new Mutate(dbSchema.getTableSchema("Bridge"), conditions, mutations));
729 +
730 + // insert a tunnel interface
731 + Interface intf = (Interface) TableGenerator.createTable(dbSchema, OvsdbTable.INTERFACE);
732 + intf.setName(portName);
733 + intf.setType(tunnelType);
734 + intf.setOptions(options);
735 + Insert intfInsert = new Insert(dbSchema.getTableSchema("Interface"), "Interface", intf.getRow());
736 + operations.add(intfInsert);
737 +
738 + transactConfig(OvsdbConstant.DATABASENAME, operations);
739 + return true;
740 + }
741 +
742 + @Override
651 public void dropTunnel(IpAddress srcIp, IpAddress dstIp) { 743 public void dropTunnel(IpAddress srcIp, IpAddress dstIp) {
652 String bridgeName = OvsdbConstant.INTEGRATION_BRIDGE; 744 String bridgeName = OvsdbConstant.INTEGRATION_BRIDGE;
653 String portName = getTunnelName(OvsdbConstant.TYPEVXLAN, dstIp); 745 String portName = getTunnelName(OvsdbConstant.TYPEVXLAN, dstIp);
......
...@@ -34,6 +34,7 @@ import org.onosproject.ovsdb.rfc.operations.Operation; ...@@ -34,6 +34,7 @@ import org.onosproject.ovsdb.rfc.operations.Operation;
34 import org.onosproject.ovsdb.rfc.schema.DatabaseSchema; 34 import org.onosproject.ovsdb.rfc.schema.DatabaseSchema;
35 35
36 import java.util.List; 36 import java.util.List;
37 +import java.util.Map;
37 import java.util.Set; 38 import java.util.Set;
38 39
39 /** 40 /**
...@@ -52,6 +53,11 @@ public class OvsdbClientServiceAdapter implements OvsdbClientService { ...@@ -52,6 +53,11 @@ public class OvsdbClientServiceAdapter implements OvsdbClientService {
52 } 53 }
53 54
54 @Override 55 @Override
56 + public boolean createTunnel(String bridgeName, String portName, String tunnelType, Map<String, String> options) {
57 + return true;
58 + }
59 +
60 + @Override
55 public void dropTunnel(IpAddress srcIp, IpAddress dstIp) { 61 public void dropTunnel(IpAddress srcIp, IpAddress dstIp) {
56 62
57 } 63 }
...@@ -67,6 +73,11 @@ public class OvsdbClientServiceAdapter implements OvsdbClientService { ...@@ -67,6 +73,11 @@ public class OvsdbClientServiceAdapter implements OvsdbClientService {
67 } 73 }
68 74
69 @Override 75 @Override
76 + public boolean createBridge(String bridgeName, String dpid, List<ControllerInfo> controllers) {
77 + return true;
78 + }
79 +
80 + @Override
70 public void dropBridge(String bridgeName) { 81 public void dropBridge(String bridgeName) {
71 82
72 } 83 }
......