+ public Status modifySubnet(SubnetConfig conf) {
+ // Sanity check
+ if (conf == null) {
+ return new Status(StatusCode.BADREQUEST, "Invalid Subnet configuration: null");
+ }
+
+ // Valid configuration check
+ Status status = conf.validate();
+ if (!status.isSuccess()) {
+ log.warn(status.getDescription());
+ return status;
+ }
+
+ // If a subnet configuration with this name does not exist, consider this is a creation
+ SubnetConfig target = subnetsConfigList.get(conf.getName());
+ if (target == null) {
+ return this.addSubnet(conf);
+ }
+
+ // No change
+ if (target.equals(conf)) {
+ return new Status(StatusCode.SUCCESS);
+ }
+
+ // Check not allowed modifications
+ if (!target.getSubnet().equals(conf.getSubnet())) {
+ return new Status(StatusCode.BADREQUEST, "IP address change is not allowed");
+ }
+
+ // Derive the set of node connectors that are being removed
+ Set<NodeConnector> toRemove = target.getNodeConnectors();
+ toRemove.removeAll(conf.getNodeConnectors());
+ List<String> nodeConnectorStrings = null;
+ if (!toRemove.isEmpty()) {
+ nodeConnectorStrings = new ArrayList<String>();
+ for (NodeConnector nc : toRemove) {
+ nodeConnectorStrings.add(nc.toString());
+ }
+ status = this.removePortsFromSubnet(conf.getName(), nodeConnectorStrings);
+ if (!status.isSuccess()) {
+ return status;
+ }
+ }
+
+ // Derive the set of node connectors that are being added
+ Set<NodeConnector> toAdd = conf.getNodeConnectors();
+ toAdd.removeAll(target.getNodeConnectors());
+ if (!toAdd.isEmpty()) {
+ List<String> nodeConnectorStringRemoved = nodeConnectorStrings;
+ nodeConnectorStrings = new ArrayList<String>();
+ for (NodeConnector nc : toAdd) {
+ nodeConnectorStrings.add(nc.toString());
+ }
+ status = this.addPortsToSubnet(conf.getName(), nodeConnectorStrings);
+ if (!status.isSuccess()) {
+ // If any port was removed, add it back as a best recovery effort
+ if (!toRemove.isEmpty()) {
+ this.addPortsToSubnet(conf.getName(), nodeConnectorStringRemoved);
+ }
+ return status;
+ }
+ }
+
+ // Update Configuration
+ subnetsConfigList.put(conf.getName(), conf);
+
+ return new Status(StatusCode.SUCCESS);
+ }
+
+ @Override
+ public Status addPortsToSubnet(String name, List<String> switchPorts) {
+ if (name == null) {
+ return new Status(StatusCode.BADREQUEST, "Null subnet name");
+ }