1 package org.opendaylight.ovsdb.plugin;
3 import java.math.BigInteger;
4 import java.net.InetAddress;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.HashMap;
8 import java.util.LinkedHashMap;
12 import org.eclipse.osgi.framework.console.CommandInterpreter;
13 import org.eclipse.osgi.framework.console.CommandProvider;
14 import org.opendaylight.controller.sal.connection.ConnectionConstants;
15 import org.opendaylight.controller.sal.core.Node;
16 import org.opendaylight.controller.sal.core.NodeConnector;
17 import org.opendaylight.controller.sal.networkconfig.bridgedomain.ConfigConstants;
18 import org.opendaylight.controller.sal.networkconfig.bridgedomain.IPluginInBridgeDomainConfigService;
19 import org.opendaylight.controller.sal.utils.Status;
20 import org.opendaylight.controller.sal.utils.StatusCode;
21 import org.opendaylight.ovsdb.lib.database.OVSInstance;
22 import org.opendaylight.ovsdb.lib.database.OvsdbType;
23 import org.opendaylight.ovsdb.lib.message.TransactBuilder;
24 import org.opendaylight.ovsdb.lib.message.operations.InsertOperation;
25 import org.opendaylight.ovsdb.lib.message.operations.MutateOperation;
26 import org.opendaylight.ovsdb.lib.message.operations.Operation;
27 import org.opendaylight.ovsdb.lib.message.operations.OperationResult;
28 import org.opendaylight.ovsdb.lib.message.operations.UpdateOperation;
29 import org.opendaylight.ovsdb.lib.notation.Condition;
30 import org.opendaylight.ovsdb.lib.notation.Function;
31 import org.opendaylight.ovsdb.lib.notation.Mutation;
32 import org.opendaylight.ovsdb.lib.notation.Mutator;
33 import org.opendaylight.ovsdb.lib.notation.OvsDBMap;
34 import org.opendaylight.ovsdb.lib.notation.OvsDBSet;
35 import org.opendaylight.ovsdb.lib.notation.UUID;
36 import org.opendaylight.ovsdb.lib.table.Bridge;
37 import org.opendaylight.ovsdb.lib.table.Capability;
38 import org.opendaylight.ovsdb.lib.table.Controller;
39 import org.opendaylight.ovsdb.lib.table.Interface;
40 import org.opendaylight.ovsdb.lib.table.Manager;
41 import org.opendaylight.ovsdb.lib.table.Mirror;
42 import org.opendaylight.ovsdb.lib.table.NetFlow;
43 import org.opendaylight.ovsdb.lib.table.Open_vSwitch;
44 import org.opendaylight.ovsdb.lib.table.Port;
45 import org.opendaylight.ovsdb.lib.table.Qos;
46 import org.opendaylight.ovsdb.lib.table.Queue;
47 import org.opendaylight.ovsdb.lib.table.SFlow;
48 import org.opendaylight.ovsdb.lib.table.SSL;
49 import org.opendaylight.ovsdb.lib.table.internal.Table;
50 import org.osgi.framework.BundleContext;
51 import org.osgi.framework.FrameworkUtil;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 import com.fasterxml.jackson.databind.ObjectMapper;
56 import com.google.common.util.concurrent.ListenableFuture;
58 public class ConfigurationService implements IPluginInBridgeDomainConfigService, OVSDBConfigService,
61 private static final Logger logger = LoggerFactory
62 .getLogger(ConfigurationService.class);
64 IConnectionServiceInternal connectionService;
65 InventoryServiceInternal inventoryServiceInternal;
66 boolean forceConnect = false;
72 * Function called by the dependency manager when at least one dependency
73 * become unsatisfied or when the component is shutting down because for
74 * example bundle is being stopped.
81 * Function called by dependency manager after "init ()" is called and after
82 * the services provided by the class are registered in the service registry
86 registerWithOSGIConsole();
89 private void registerWithOSGIConsole() {
90 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass())
92 bundleContext.registerService(CommandProvider.class.getName(), this,
97 * Function called by the dependency manager before the services exported by
98 * the component are unregistered, this will be followed by a "destroy ()"
105 public void setConnectionServiceInternal(IConnectionServiceInternal connectionService) {
106 this.connectionService = connectionService;
109 public void unsetConnectionServiceInternal(IConnectionServiceInternal connectionService) {
110 if (this.connectionService == connectionService) {
111 this.connectionService = null;
115 public void setInventoryServiceInternal(InventoryServiceInternal inventoryServiceInternal) {
116 this.inventoryServiceInternal = inventoryServiceInternal;
119 public void unsetInventoryServiceInternal(InventoryServiceInternal inventoryServiceInternal) {
120 if (this.inventoryServiceInternal == inventoryServiceInternal) {
121 this.inventoryServiceInternal = null;
125 private Connection getConnection (Node node) {
126 Connection connection = connectionService.getConnection(node);
127 if (connection == null || !connection.getChannel().isActive()) {
136 * @param node Node serving this configuration service
137 * @param bridgeConnectorIdentifier String representation of a Bridge Connector
138 * @return Bridge Connector configurations
141 public Status createBridgeDomain(Node node, String bridgeIdentifier,
142 Map<ConfigConstants, Object> configs) throws Throwable {
144 if (connectionService == null) {
145 logger.error("Couldn't refer to the ConnectionService");
146 return new Status(StatusCode.NOSERVICE);
149 Connection connection = this.getConnection(node);
150 if (connection == null) {
151 return new Status(StatusCode.NOSERVICE, "Connection to ovsdb-server not available");
154 Map<String, Table<?>> ovsTable = inventoryServiceInternal.getTableCache(node, Open_vSwitch.NAME.getName());
155 String newBridge = "new_bridge";
156 String newInterface = "new_interface";
157 String newPort = "new_port";
158 String newSwitch = "new_switch";
160 Operation addSwitchRequest = null;
162 if(ovsTable != null){
163 String ovsTableUUID = (String) ovsTable.keySet().toArray()[0];
164 UUID bridgeUuidPair = new UUID(newBridge);
165 Mutation bm = new Mutation("bridges", Mutator.INSERT, bridgeUuidPair);
166 List<Mutation> mutations = new ArrayList<Mutation>();
169 UUID uuid = new UUID(ovsTableUUID);
170 Condition condition = new Condition("_uuid", Function.EQUALS, uuid);
171 List<Condition> where = new ArrayList<Condition>();
172 where.add(condition);
173 addSwitchRequest = new MutateOperation(Open_vSwitch.NAME.getName(), where, mutations);
176 Open_vSwitch ovsTableRow = new Open_vSwitch();
177 OvsDBSet<UUID> bridges = new OvsDBSet<UUID>();
178 UUID bridgeUuidPair = new UUID(newBridge);
179 bridges.add(bridgeUuidPair);
180 ovsTableRow.setBridges(bridges);
181 addSwitchRequest = new InsertOperation(Open_vSwitch.NAME.getName(), newSwitch, ovsTableRow);
184 Bridge bridgeRow = new Bridge();
185 bridgeRow.setName(bridgeIdentifier);
186 OvsDBSet<UUID> ports = new OvsDBSet<UUID>();
187 UUID port = new UUID(newPort);
189 bridgeRow.setPorts(ports);
190 InsertOperation addBridgeRequest = new InsertOperation(Bridge.NAME.getName(), newBridge, bridgeRow);
192 Port portRow = new Port();
193 portRow.setName(bridgeIdentifier);
194 OvsDBSet<UUID> interfaces = new OvsDBSet<UUID>();
195 UUID interfaceid = new UUID(newInterface);
196 interfaces.add(interfaceid);
197 portRow.setInterfaces(interfaces);
198 InsertOperation addPortRequest = new InsertOperation(Port.NAME.getName(), newPort, portRow);
200 Interface interfaceRow = new Interface();
201 interfaceRow.setName(bridgeIdentifier);
202 interfaceRow.setType("internal");
203 InsertOperation addIntfRequest = new InsertOperation(Interface.NAME.getName(), newInterface, interfaceRow);
205 /* Update config version */
206 String ovsTableUUID = (String) ovsTable.keySet().toArray()[0];
207 Mutation bm = new Mutation("next_cfg", Mutator.SUM, 1);
208 List<Mutation> mutations = new ArrayList<Mutation>();
211 UUID uuid = new UUID(ovsTableUUID);
212 Condition condition = new Condition("_uuid", Function.EQUALS, uuid);
213 List<Condition> where = new ArrayList<Condition>();
214 where.add(condition);
215 MutateOperation updateCfgVerRequest = new MutateOperation(Open_vSwitch.NAME.getName(), where, mutations);
217 TransactBuilder transaction = new TransactBuilder();
218 transaction.addOperations(new ArrayList<Operation>(
219 Arrays.asList(addSwitchRequest,
223 updateCfgVerRequest)));
225 ListenableFuture<List<OperationResult>> transResponse = connection.getRpc().transact(transaction);
226 List<OperationResult> tr = transResponse.get();
227 List<Operation> requests = transaction.getRequests();
228 Status status = new Status(StatusCode.SUCCESS);
229 for (int i = 0; i < tr.size() ; i++) {
230 if (i < requests.size()) requests.get(i).setResult(tr.get(i));
231 if (tr.get(i) != null && tr.get(i).getError() != null && tr.get(i).getError().trim().length() > 0) {
232 OperationResult result = tr.get(i);
233 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
237 if (tr.size() > requests.size()) {
238 OperationResult result = tr.get(tr.size()-1);
239 logger.error("Error creating Bridge : {}\n Error : {}\n Details : {}", bridgeIdentifier,
241 result.getDetails());
242 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
244 if (status.isSuccess()) {
245 setBridgeOFController(node, bridgeIdentifier);
248 } catch(Exception e){
249 logger.error("Error in createBridgeDomain(): ",e);
251 return new Status(StatusCode.INTERNALERROR);
255 * Create a Port Attached to a Bridge
256 * Ex. ovs-vsctl add-port br0 vif0
257 * @param node Node serving this configuration service
258 * @param bridgeDomainIdentifier String representation of a Bridge Domain
259 * @param portIdentifier String representation of a user defined Port Name
262 public Status addPort(Node node, String bridgeIdentifier, String portIdentifier,
263 Map<ConfigConstants, Object> configs) {
265 if (connectionService == null) {
266 logger.error("Couldn't refer to the ConnectionService");
267 return new Status(StatusCode.NOSERVICE);
269 Connection connection = this.getConnection(node);
270 if (connection == null) {
271 return new Status(StatusCode.NOSERVICE, "Connection to ovsdb-server not available");
273 if (connection != null) {
274 Map<String, Table<?>> brTable = inventoryServiceInternal.getTableCache(node, Bridge.NAME.getName());
275 String newBridge = "new_bridge";
276 String newInterface = "new_interface";
277 String newPort = "new_port";
280 Operation addBrMutRequest = null;
281 String brUuid = null;
282 for (String uuid : brTable.keySet()) {
283 Bridge bridge = (Bridge) brTable.get(uuid);
284 if (bridge.getName().contains(bridgeIdentifier)) {
289 UUID brUuidPair = new UUID(newPort);
290 Mutation bm = new Mutation("ports", Mutator.INSERT, brUuidPair);
291 List<Mutation> mutations = new ArrayList<Mutation>();
294 UUID uuid = new UUID(brUuid);
295 Condition condition = new Condition("_uuid", Function.EQUALS, uuid);
296 List<Condition> where = new ArrayList<Condition>();
297 where.add(condition);
298 addBrMutRequest = new MutateOperation(Bridge.NAME.getName(), where, mutations);
300 OvsDBMap<String, String> options = null;
302 OvsDBSet<BigInteger> tags = null;
303 if (configs != null) {
304 type = (String) configs.get(ConfigConstants.TYPE);
305 Map<String, String> customConfigs = (Map<String, String>) configs.get(ConfigConstants.CUSTOM);
306 if (customConfigs != null) {
307 options = new OvsDBMap<String, String>();
308 for (String customConfig : customConfigs.keySet()) {
309 options.put(customConfig, customConfigs.get(customConfig));
314 Interface interfaceRow = new Interface();
315 interfaceRow.setName(portIdentifier);
318 if (type.equalsIgnoreCase(OvsdbType.PortType.TUNNEL.name())) {
319 interfaceRow.setType((String)configs.get(ConfigConstants.TUNNEL_TYPE));
320 if (options == null) options = new OvsDBMap<String, String>();
321 options.put("remote_ip", (String)configs.get(ConfigConstants.DEST_IP));
322 } else if (type.equalsIgnoreCase(OvsdbType.PortType.VLAN.name())) {
323 tags = new OvsDBSet<BigInteger>();
324 tags.add(BigInteger.valueOf(Integer.parseInt((String)configs.get(ConfigConstants.VLAN))));
325 } else if (type.equalsIgnoreCase(OvsdbType.PortType.PATCH.name())) {
326 interfaceRow.setType(type.toLowerCase());
329 if (options != null) {
330 interfaceRow.setOptions(options);
333 InsertOperation addIntfRequest = new InsertOperation(Interface.NAME.getName(),
334 newInterface, interfaceRow);
336 Port portRow = new Port();
337 portRow.setName(portIdentifier);
338 if (tags != null) portRow.setTag(tags);
339 OvsDBSet<UUID> interfaces = new OvsDBSet<UUID>();
340 UUID interfaceid = new UUID(newInterface);
341 interfaces.add(interfaceid);
342 portRow.setInterfaces(interfaces);
343 InsertOperation addPortRequest = new InsertOperation(Port.NAME.getName(), newPort, portRow);
345 TransactBuilder transaction = new TransactBuilder();
346 transaction.addOperations(new ArrayList<Operation>
347 (Arrays.asList(addBrMutRequest, addPortRequest, addIntfRequest)));
349 ListenableFuture<List<OperationResult>> transResponse = connection.getRpc().transact(transaction);
350 List<OperationResult> tr = transResponse.get();
351 List<Operation> requests = transaction.getRequests();
352 Status status = new Status(StatusCode.SUCCESS);
353 for (int i = 0; i < tr.size() ; i++) {
354 if (i < requests.size()) requests.get(i).setResult(tr.get(i));
355 if (tr.get(i).getError() != null && tr.get(i).getError().trim().length() > 0) {
356 OperationResult result = tr.get(i);
357 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
361 if (tr.size() > requests.size()) {
362 OperationResult result = tr.get(tr.size()-1);
363 logger.error("Error creating Bridge : {}\n Error : {}\n Details : {}", bridgeIdentifier,
365 result.getDetails());
366 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
370 return new Status(StatusCode.INTERNALERROR);
372 } catch(Exception e){
373 logger.error("Error in addPort()",e);
375 return new Status(StatusCode.INTERNALERROR);
379 * Implements the OVS Connection for Managers
381 * @param node Node serving this configuration service
382 * @param String with IP and connection types
384 @SuppressWarnings("unchecked")
385 public boolean setManager(Node node, String managerip) throws Throwable{
387 if (connectionService == null) {
388 logger.error("Couldn't refer to the ConnectionService");
391 Connection connection = this.getConnection(node);
392 if (connection == null) {
396 if (connection != null) {
397 String newmanager = "new_manager";
399 OVSInstance instance = OVSInstance.monitorOVS(connection);
401 Map ovsoutter = new LinkedHashMap();
402 Map ovsinner = new LinkedHashMap();
403 ArrayList ovsalist1 = new ArrayList();
404 ArrayList ovsalist2 = new ArrayList();
405 ArrayList ovsalist3 = new ArrayList();
406 ArrayList ovsalist4 = new ArrayList();
409 ovsoutter.put("where", ovsalist1);
410 ovsalist1.add(ovsalist2);
411 ovsalist2.add("_uuid");
413 ovsalist2.add(ovsalist3);
414 ovsalist3.add("uuid");
415 ovsalist3.add(instance.getUuid());
416 ovsoutter.put("op", "update");
417 ovsoutter.put("table", "Open_vSwitch");
418 ovsoutter.put("row", ovsinner);
419 ovsinner.put("manager_options", ovsalist4);
420 ovsalist4.add("named-uuid");
421 ovsalist4.add(newmanager);
423 Map mgroutside = new LinkedHashMap();
424 Map mgrinside = new LinkedHashMap();
426 //Manager Table Insert
427 mgroutside.put("uuid-name", newmanager);
428 mgroutside.put("op", "insert");
429 mgroutside.put("table","Manager");
430 mgroutside.put("row", mgrinside);
431 mgrinside.put("target", managerip);
433 Object[] params = {"Open_vSwitch", ovsoutter, mgroutside};
434 OvsdbMessage msg = new OvsdbMessage("transact", params);
436 //connection.sendMessage(msg);
440 logger.error("Error in setManager(): ",e);
446 public Status addBridgeDomainConfig(Node node, String bridgeIdentfier,
447 Map<ConfigConstants, Object> configs) {
448 String mgmt = (String)configs.get(ConfigConstants.MGMT);
451 if (setManager(node, mgmt)) return new Status(StatusCode.SUCCESS);
452 } catch (Throwable e) {
453 // TODO Auto-generated catch block
455 return new Status(StatusCode.INTERNALERROR);
458 return new Status(StatusCode.BADREQUEST);
462 public Status addPortConfig(Node node, String bridgeIdentifier, String portIdentifier,
463 Map<ConfigConstants, Object> configs) {
464 // TODO Auto-generated method stub
469 public Status deletePort(Node node, String bridgeIdentifier, String portIdentifier) {
472 if (connectionService == null) {
473 logger.error("Couldn't refer to the ConnectionService");
474 return new Status(StatusCode.NOSERVICE);
477 Connection connection = this.getConnection(node);
478 if (connection == null) {
479 return new Status(StatusCode.NOSERVICE, "Connection to ovsdb-server not available");
482 Map<String, Table<?>> brTable = inventoryServiceInternal.getTableCache(node, Bridge.NAME.getName());
483 Map<String, Table<?>> portTable = inventoryServiceInternal.getTableCache(node, Port.NAME.getName());
484 Operation delPortRequest = null;
485 String brUuid = null;
486 String portUuid = null;
488 for (String uuid : brTable.keySet()) {
489 Bridge bridge = (Bridge) brTable.get(uuid);
490 if (bridge.getName().contains(bridgeIdentifier)) {
495 if(portTable != null){
496 for (String uuid : portTable.keySet()) {
497 Port port = (Port) portTable.get(uuid);
498 if (port.getName().contains(portIdentifier)) {
504 UUID portUuidPair = new UUID(portUuid);
505 Mutation bm = new Mutation("ports", Mutator.DELETE, portUuidPair);
506 List<Mutation> mutations = new ArrayList<Mutation>();
509 UUID uuid = new UUID(brUuid);
510 Condition condition = new Condition("_uuid", Function.EQUALS, uuid);
511 List<Condition> where = new ArrayList<Condition>();
512 where.add(condition);
513 delPortRequest = new MutateOperation(Bridge.NAME.getName(), where, mutations);
515 TransactBuilder transaction = new TransactBuilder();
516 transaction.addOperations(new ArrayList<Operation>(Arrays.asList(delPortRequest)));
518 ListenableFuture<List<OperationResult>> transResponse = connection.getRpc().transact(transaction);
519 List<OperationResult> tr = transResponse.get();
520 List<Operation> requests = transaction.getRequests();
521 Status status = new Status(StatusCode.SUCCESS);
522 for (int i = 0; i < tr.size() ; i++) {
523 if (i < requests.size()) requests.get(i).setResult(tr.get(i));
524 if (tr.get(i).getError() != null && tr.get(i).getError().trim().length() > 0) {
525 OperationResult result = tr.get(i);
526 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
530 if (tr.size() > requests.size()) {
531 OperationResult result = tr.get(tr.size()-1);
532 logger.error("Error creating Bridge : {}\n Error : {}\n Details : {}", bridgeIdentifier,
534 result.getDetails());
535 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
538 } catch(Exception e){
539 logger.error("Error in deletePort()",e);
541 return new Status(StatusCode.INTERNALERROR);
545 public Node getBridgeDomainNode(Node node, String bridgeIdentifier) {
546 // TODO Auto-generated method stub
551 public Map<ConfigConstants, Object> getPortConfigs(Node node, String bridgeIdentifier,
552 String portIdentifier) {
553 // TODO Auto-generated method stub
558 public Status removeBridgeDomainConfig(Node node, String bridgeIdentifier,
559 Map<ConfigConstants, Object> configs) {
560 // TODO Auto-generated method stub
565 public Status removePortConfig(Node node, String bridgeIdentifier, String portIdentifier,
566 Map<ConfigConstants, Object> configs) {
567 // TODO Auto-generated method stub
572 public Status deleteBridgeDomain(Node node, String bridgeIdentifier) {
575 if (connectionService == null) {
576 logger.error("Couldn't refer to the ConnectionService");
577 return new Status(StatusCode.NOSERVICE);
579 Connection connection = this.getConnection(node);
580 if (connection == null) {
581 return new Status(StatusCode.NOSERVICE, "Connection to ovsdb-server not available");
583 Map<String, Table<?>> ovsTable = inventoryServiceInternal.getTableCache(node, Open_vSwitch.NAME.getName());
584 Map<String, Table<?>> brTable = inventoryServiceInternal.getTableCache(node, Bridge.NAME.getName());
585 Operation delBrRequest = null;
586 String ovsUuid = null;
587 String brUuid = null;
589 if (brTable != null) {
590 for (String uuid : brTable.keySet()) {
591 Bridge bridge = (Bridge) brTable.get(uuid);
592 if (bridge.getName().contains(bridgeIdentifier)) {
597 if (ovsTable != null) {
598 ovsUuid = (String) ovsTable.keySet().toArray()[0];
600 UUID bridgeUuidPair = new UUID(brUuid);
601 Mutation bm = new Mutation("bridges", Mutator.DELETE, bridgeUuidPair);
602 List<Mutation> mutations = new ArrayList<Mutation>();
605 UUID uuid = new UUID(ovsUuid);
606 Condition condition = new Condition("_uuid", Function.EQUALS, uuid);
607 List<Condition> where = new ArrayList<Condition>();
608 where.add(condition);
609 delBrRequest = new MutateOperation(Open_vSwitch.NAME.getName(), where, mutations);
611 TransactBuilder transaction = new TransactBuilder();
612 transaction.addOperations(new ArrayList<Operation>(Arrays.asList(delBrRequest)));
614 ListenableFuture<List<OperationResult>> transResponse = connection.getRpc().transact(transaction);
615 List<OperationResult> tr = transResponse.get();
616 List<Operation> requests = transaction.getRequests();
617 Status status = new Status(StatusCode.SUCCESS);
618 for (int i = 0; i < tr.size(); i++) {
619 if (i < requests.size()) requests.get(i).setResult(tr.get(i));
620 if (tr.get(i) != null && tr.get(i).getError() != null && tr.get(i).getError().trim().length() > 0) {
621 OperationResult result = tr.get(i);
622 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
626 if (tr.size() > requests.size()) {
627 OperationResult result = tr.get(tr.size() - 1);
628 logger.error("Error deleting Bridge : {}\n Error : {}\n Details : {}",
629 bridgeIdentifier, result.getError(), result.getDetails());
630 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
633 } catch (Exception e) {
634 logger.error("Error in deleteBridgeDomain(): ",e);
636 return new Status(StatusCode.INTERNALERROR);
640 public Map<ConfigConstants, Object> getBridgeDomainConfigs(Node node, String bridgeIdentifier) {
641 // TODO Auto-generated method stub
646 public List<String> getBridgeDomains(Node node) {
647 List<String> brlist = new ArrayList<String>();
648 Map<String, Table<?>> brTableCache = inventoryServiceInternal.getTableCache(node, Bridge.NAME.getName());
649 if(brTableCache != null){
650 for (String uuid : brTableCache.keySet()) {
651 Bridge bridge = (Bridge) brTableCache.get(uuid);
652 brlist.add(bridge.getName());
659 public NodeConnector getNodeConnector(Node arg0, String arg1, String arg2) {
663 Boolean setBridgeOFController(Node node, String bridgeIdentifier) {
664 if (connectionService == null) {
665 logger.error("Couldn't refer to the ConnectionService");
670 Map<String, Table<?>> brTableCache = inventoryServiceInternal.getTableCache(node, Bridge.NAME.getName());
671 for (String uuid : brTableCache.keySet()) {
672 Bridge bridge = (Bridge)brTableCache.get(uuid);
673 if (bridge.getName().contains(bridgeIdentifier)) {
674 return connectionService.setOFController(node, uuid);
677 } catch(Exception e) {
678 logger.error("Error in setBridgeOFController()",e);
684 public Status insertRow(Node node, String tableName, String parent_uuid, Table<?> row) {
685 logger.info("tableName : {}, parent_uuid : {} Row : {}", tableName, parent_uuid, row.toString());
686 Status statusWithUUID = null;
688 // Schema based Table handling will help fix this static Table handling.
690 if (row.getTableName().getName().equalsIgnoreCase("Bridge")) {
691 statusWithUUID = insertBridgeRow(node, parent_uuid, (Bridge)row);
693 else if (row.getTableName().getName().equalsIgnoreCase("Capbility")) {
694 statusWithUUID = insertCapabilityRow(node, parent_uuid, (Capability)row);
696 else if (row.getTableName().getName().equalsIgnoreCase("Controller")) {
697 statusWithUUID = insertControllerRow(node, parent_uuid, (Controller)row);
699 else if (row.getTableName().getName().equalsIgnoreCase("Interface")) {
700 statusWithUUID = insertInterfaceRow(node, parent_uuid, (Interface)row);
702 else if (row.getTableName().getName().equalsIgnoreCase("Manager")) {
703 statusWithUUID = insertManagerRow(node, parent_uuid, (Manager)row);
705 else if (row.getTableName().getName().equalsIgnoreCase("Mirror")) {
706 statusWithUUID = insertMirrorRow(node, parent_uuid, (Mirror)row);
708 else if (row.getTableName().getName().equalsIgnoreCase("NetFlow")) {
709 statusWithUUID = insertNetFlowRow(node, parent_uuid, (NetFlow)row);
711 else if (row.getTableName().getName().equalsIgnoreCase("Open_vSwitch")) {
712 statusWithUUID = insertOpen_vSwitchRow(node, (Open_vSwitch)row);
714 else if (row.getTableName().getName().equalsIgnoreCase("Port")) {
715 statusWithUUID = insertPortRow(node, parent_uuid, (Port)row);
717 else if (row.getTableName().getName().equalsIgnoreCase("QoS")) {
718 statusWithUUID = insertQosRow(node, parent_uuid, (Qos)row);
720 else if (row.getTableName().getName().equalsIgnoreCase("Queue")) {
721 statusWithUUID = insertQueueRow(node, parent_uuid, (Queue)row);
723 else if (row.getTableName().getName().equalsIgnoreCase("sFlow")) {
724 statusWithUUID = insertSflowRow(node, parent_uuid, (SFlow)row);
726 else if (row.getTableName().getName().equalsIgnoreCase("SSL")) {
727 statusWithUUID = insertSSLRow(node, parent_uuid, (SSL)row);
729 return statusWithUUID;
734 public Status updateRow (Node node, String tableName, String parentUUID, String rowUUID, Table<?> row) {
736 if (connectionService == null) {
737 logger.error("Couldn't refer to the ConnectionService");
738 return new Status(StatusCode.NOSERVICE);
741 Connection connection = this.getConnection(node);
742 if (connection == null) {
743 return new Status(StatusCode.NOSERVICE, "Connection to ovsdb-server not available");
746 Map<String, Table<?>> ovsTable = inventoryServiceInternal.getTableCache(node, Open_vSwitch.NAME.getName());
748 if (ovsTable == null) {
749 return new Status(StatusCode.NOTFOUND, "There are no Open_vSwitch instance in the Open_vSwitch table");
752 UUID uuid = new UUID(rowUUID);
753 Condition condition = new Condition("_uuid", Function.EQUALS, uuid);
754 List<Condition> where = new ArrayList<Condition>();
755 where.add(condition);
756 Operation updateRequest = new UpdateOperation(tableName, where, row);
758 TransactBuilder transaction = new TransactBuilder();
759 transaction.addOperations(new ArrayList<Operation>(
760 Arrays.asList(updateRequest)));
762 ListenableFuture<List<OperationResult>> transResponse = connection.getRpc().transact(transaction);
763 List<OperationResult> tr = transResponse.get();
764 List<Operation> requests = transaction.getRequests();
765 Status status = new Status(StatusCode.SUCCESS);
766 for (int i = 0; i < tr.size() ; i++) {
767 if (i < requests.size()) requests.get(i).setResult(tr.get(i));
768 if (tr.get(i) != null && tr.get(i).getError() != null && tr.get(i).getError().trim().length() > 0) {
769 OperationResult result = tr.get(i);
770 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
774 if (tr.size() > requests.size()) {
775 OperationResult result = tr.get(tr.size()-1);
776 logger.error("Error Updating Row : {}/{}\n Error : {}\n Details : {}", tableName, row,
778 result.getDetails());
779 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
781 if (status.isSuccess()) {
782 status = new Status(StatusCode.SUCCESS);
785 } catch(Exception e){
786 logger.error("Error in updateRow(): ",e);
788 return new Status(StatusCode.INTERNALERROR);
792 public Status deleteRow(Node node, String tableName, String uuid) {
793 if (tableName.equalsIgnoreCase("Bridge")) {
794 return deleteBridgeRow(node, uuid);
796 else if (tableName.equalsIgnoreCase("Capbility")) {
797 return deleteCapabilityRow(node, uuid);
799 else if (tableName.equalsIgnoreCase("Controller")) {
800 return deleteControllerRow(node, uuid);
802 else if (tableName.equalsIgnoreCase("Interface")) {
803 return deleteInterfaceRow(node, uuid);
805 else if (tableName.equalsIgnoreCase("Manager")) {
806 return deleteManagerRow(node, uuid);
808 else if (tableName.equalsIgnoreCase("Mirror")) {
809 return deleteMirrorRow(node, uuid);
811 else if (tableName.equalsIgnoreCase("NetFlow")) {
812 return deleteNetFlowRow(node, uuid);
814 else if (tableName.equalsIgnoreCase("Open_vSwitch")) {
815 return deleteOpen_vSwitchRow(node, uuid);
817 else if (tableName.equalsIgnoreCase("Port")) {
818 return deletePortRow(node, uuid);
820 else if (tableName.equalsIgnoreCase("QoS")) {
821 return deleteQosRow(node, uuid);
823 else if (tableName.equalsIgnoreCase("Queue")) {
824 return deleteQueueRow(node, uuid);
826 else if (tableName.equalsIgnoreCase("sFlow")) {
827 return deleteSflowRow(node, uuid);
829 else if (tableName.equalsIgnoreCase("SSL")) {
830 return deleteSSLRow(node, uuid);
832 return new Status(StatusCode.NOTFOUND, "Table "+tableName+" not supported");
836 public Map<String, Table<?>> getRows(Node node, String tableName) throws Exception{
838 if (inventoryServiceInternal == null) {
839 throw new Exception("Inventory Service is Unavailable.");
841 Map<String, Table<?>> ovsTable = inventoryServiceInternal.getTableCache(node, tableName);
843 } catch(Exception e){
844 throw new Exception("Unable to read table due to "+e.getMessage());
849 public Table<?> getRow(Node node, String tableName, String uuid) throws Exception {
851 if (inventoryServiceInternal == null) {
852 throw new Exception("Inventory Service is Unavailable.");
854 Map<String, Table<?>> ovsTable = inventoryServiceInternal.getTableCache(node, tableName);
855 if (ovsTable == null) return null;
856 return ovsTable.get(uuid);
857 } catch(Exception e){
858 throw new Exception("Unable to read table due to "+e.getMessage());
863 public String getSerializedRows(Node node, String tableName) throws Exception{
865 Map<String, Table<?>> ovsTable = this.getRows(node, tableName);
866 if (ovsTable == null) return null;
867 ObjectMapper mapper = new ObjectMapper();
868 return mapper.writeValueAsString(ovsTable);
869 } catch(Exception e){
870 throw new Exception("Unable to read table due to "+e.getMessage());
875 public String getSerializedRow(Node node, String tableName, String uuid) throws Exception {
877 Table<?> row = this.getRow(node, tableName, uuid);
878 if (row == null) return null;
879 ObjectMapper mapper = new ObjectMapper();
880 return mapper.writeValueAsString(row);
881 } catch(Exception e){
882 throw new Exception("Unable to read table due to "+e.getMessage());
887 public List<String> getTables(Node node) {
888 // TODO Auto-generated method stub
892 private Status insertBridgeRow(Node node, String open_VSwitch_uuid, Bridge bridgeRow) {
894 String insertErrorMsg = "bridge";
895 String rowName=bridgeRow.getName();
898 Map<String, Table<?>> ovsTable = inventoryServiceInternal.getTableCache(node, Open_vSwitch.NAME.getName());
900 if (ovsTable == null) {
901 return new Status(StatusCode.NOTFOUND, "There are no Open_vSwitch instance in the Open_vSwitch table");
904 String newBridge = "new_bridge";
906 Operation addSwitchRequest = null;
908 String ovsTableUUID = open_VSwitch_uuid;
909 if (ovsTableUUID == null) ovsTableUUID = (String) ovsTable.keySet().toArray()[0];
910 UUID bridgeUuidPair = new UUID(newBridge);
911 Mutation bm = new Mutation("bridges", Mutator.INSERT, bridgeUuidPair);
912 List<Mutation> mutations = new ArrayList<Mutation>();
915 UUID uuid = new UUID(ovsTableUUID);
916 Condition condition = new Condition("_uuid", Function.EQUALS, uuid);
917 List<Condition> where = new ArrayList<Condition>();
918 where.add(condition);
919 addSwitchRequest = new MutateOperation(Open_vSwitch.NAME.getName(), where, mutations);
921 InsertOperation addBridgeRequest = new InsertOperation(Bridge.NAME.getName(), newBridge, bridgeRow);
923 TransactBuilder transaction = new TransactBuilder();
924 transaction.addOperations(new ArrayList<Operation>(
925 Arrays.asList(addSwitchRequest,
928 int bridgeInsertIndex = transaction.getRequests().indexOf(addBridgeRequest);
930 return _insertTableRow(node,transaction,bridgeInsertIndex,insertErrorMsg,rowName);
932 } catch(Exception e){
933 logger.error("Error in insertBridgeRow(): ",e);
935 return new Status(StatusCode.INTERNALERROR);
939 private Status insertPortRow(Node node, String bridge_uuid, Port portRow) {
941 String insertErrorMsg = "port";
942 String rowName=portRow.getName();
945 Map<String, Table<?>> brTable = inventoryServiceInternal.getTableCache(node, Bridge.NAME.getName());
946 if (brTable == null || brTable.get(bridge_uuid) == null) {
947 return new Status(StatusCode.NOTFOUND, "Bridge with UUID "+bridge_uuid+" Not found");
949 String newPort = "new_port";
950 UUID portUUID = new UUID(newPort);
951 Mutation bm = new Mutation("ports", Mutator.INSERT, portUUID);
952 List<Mutation> mutations = new ArrayList<Mutation>();
955 UUID uuid = new UUID(bridge_uuid);
956 Condition condition = new Condition("_uuid", Function.EQUALS, uuid);
957 List<Condition> where = new ArrayList<Condition>();
958 where.add(condition);
959 Operation addBrMutRequest = new MutateOperation(Bridge.NAME.getName(), where, mutations);
961 // Default OVS schema is to have 1 or more interface part of Bridge. Hence it is mandatory to
962 // Insert an Interface in a Port add case :-(.
964 String newInterface = "new_interface";
965 Interface interfaceRow = new Interface();
966 interfaceRow.setName(portRow.getName());
967 InsertOperation addIntfRequest = new InsertOperation(Interface.NAME.getName(),
968 newInterface, interfaceRow);
970 OvsDBSet<UUID> interfaces = new OvsDBSet<UUID>();
971 UUID interfaceid = new UUID(newInterface);
972 interfaces.add(interfaceid);
973 portRow.setInterfaces(interfaces);
975 InsertOperation addPortRequest = new InsertOperation(Port.NAME.getName(), newPort, portRow);
977 TransactBuilder transaction = new TransactBuilder();
978 transaction.addOperations(new ArrayList<Operation>
979 (Arrays.asList(addBrMutRequest, addPortRequest, addIntfRequest)));
980 int portInsertIndex = transaction.getRequests().indexOf(addPortRequest);
982 return _insertTableRow(node,transaction,portInsertIndex,insertErrorMsg,rowName);
984 } catch (Exception e) {
985 logger.error("Error in insertPortRow(): ",e);
987 return new Status(StatusCode.INTERNALERROR);
990 private Status insertInterfaceRow(Node node, String port_uuid, Interface interfaceRow) {
992 String insertErrorMsg = "interface";
993 String rowName=interfaceRow.getName();
997 // Interface table must have entry in Port table, checking port table for port
998 Map<String, Table<?>> portTable = inventoryServiceInternal.getTableCache(node, Port.NAME.getName());
999 if (portTable == null || portTable.get(port_uuid) == null) {
1000 return new Status(StatusCode.NOTFOUND, "Port with UUID "+port_uuid+" Not found");
1002 // MUTATOR, need to insert the interface UUID to LIST of interfaces in PORT TABLE for port_uuid
1003 String newInterface = "new_interface";
1004 UUID interfaceUUID = new UUID(newInterface);
1005 Mutation portTableMutation = new Mutation("interfaces", Mutator.INSERT, interfaceUUID); // field name to append is "interfaces"
1006 List<Mutation> mutations = new ArrayList<Mutation>();
1007 mutations.add(portTableMutation);
1009 // Create the Operation which will be used in Transact to perform the PORT TABLE mutation
1010 UUID uuid = new UUID(port_uuid);
1011 Condition condition = new Condition("_uuid", Function.EQUALS, uuid);
1012 List<Condition> where = new ArrayList<Condition>();
1013 where.add(condition);
1014 Operation addPortMutationRequest = new MutateOperation(Port.NAME.getName(), where, mutations);
1016 // Create the interface row request
1017 InsertOperation addIntfRequest = new InsertOperation(Interface.NAME.getName(),newInterface, interfaceRow);
1019 // Transaction to insert/modify tables - validate using "sudo ovsdb-client dump" on host running OVSDB process
1020 TransactBuilder transaction = new TransactBuilder();
1021 transaction.addOperations(new ArrayList<Operation>(Arrays.asList(addIntfRequest,addPortMutationRequest)));
1023 // Check the results. Iterates over the results of the Array of transaction Operations, and reports STATUS
1024 int interfaceInsertIndex = transaction.getRequests().indexOf(addIntfRequest);
1026 return _insertTableRow(node,transaction,interfaceInsertIndex,insertErrorMsg,rowName);
1028 } catch (Exception e) {
1029 logger.error("Error in insertInterfaceRow(): ",e);
1031 return new Status(StatusCode.INTERNALERROR);
1034 private Status insertOpen_vSwitchRow(Node node, Open_vSwitch row) {
1035 return new Status(StatusCode.NOTIMPLEMENTED, "Insert operation for this Table is not implemented yet.");
1038 private Status insertControllerRow(Node node, String bridge_uuid, Controller row) {
1040 String insertErrorMsg = "controller";
1041 String rowName=row.getTableName().toString();
1045 Map<String, Table<?>> brTable = inventoryServiceInternal.getTableCache(node, Bridge.NAME.getName());
1046 if (brTable == null || brTable.get(bridge_uuid) == null) {
1047 return new Status(StatusCode.NOTFOUND, "Bridge with UUID "+bridge_uuid+" Not found");
1050 Map<String, Table<?>> controllerCache = inventoryServiceInternal.getTableCache(node, Controller.NAME.getName());
1052 String uuid_name = "new_controller";
1053 boolean controllerExists = false;
1054 if (controllerCache != null) {
1055 for (String uuid : controllerCache.keySet()) {
1056 Controller controller = (Controller)controllerCache.get(uuid);
1057 if (controller.getTarget().equals(row.getTarget())) {
1059 controllerExists = true;
1065 UUID controllerUUID = new UUID(uuid_name);
1066 Mutation bm = new Mutation("controller", Mutator.INSERT, controllerUUID);
1067 List<Mutation> mutations = new ArrayList<Mutation>();
1070 UUID uuid = new UUID(bridge_uuid);
1071 Condition condition = new Condition("_uuid", Function.EQUALS, uuid);
1072 List<Condition> where = new ArrayList<Condition>();
1073 where.add(condition);
1074 Operation addBrMutRequest = new MutateOperation(Bridge.NAME.getName(), where, mutations);
1075 InsertOperation addControllerRequest = null;
1077 TransactBuilder transaction = new TransactBuilder();
1078 transaction.addOperation(addBrMutRequest);
1079 int portInsertIndex = -1;
1080 if (!controllerExists) {
1081 addControllerRequest = new InsertOperation(Controller.NAME.getName(), uuid_name, row);
1082 transaction.addOperation(addControllerRequest);
1083 portInsertIndex = transaction.getRequests().indexOf(addControllerRequest);
1086 return _insertTableRow(node,transaction,portInsertIndex,insertErrorMsg,rowName);
1088 } catch (Exception e) {
1089 logger.error("Error in insertControllerRow(): ",e);
1091 return new Status(StatusCode.INTERNALERROR);
1094 private Status insertSSLRow(Node node, String parent_uuid, SSL row) {
1095 return new Status(StatusCode.NOTIMPLEMENTED, "Insert operation for this Table is not implemented yet.");
1098 private Status insertSflowRow(Node node, String parent_uuid, SFlow row) {
1099 return new Status(StatusCode.NOTIMPLEMENTED, "Insert operation for this Table is not implemented yet.");
1102 private Status insertQueueRow(Node node, String parent_uuid, Queue row) {
1103 return new Status(StatusCode.NOTIMPLEMENTED, "Insert operation for this Table is not implemented yet.");
1106 private Status insertQosRow(Node node, String parent_uuid, Qos row) {
1107 return new Status(StatusCode.NOTIMPLEMENTED, "Insert operation for this Table is not implemented yet.");
1110 private Status insertNetFlowRow(Node node, String parent_uuid, NetFlow row) {
1111 return new Status(StatusCode.NOTIMPLEMENTED, "Insert operation for this Table is not implemented yet.");
1114 private Status insertMirrorRow(Node node, String parent_uuid, Mirror row) {
1115 return new Status(StatusCode.NOTIMPLEMENTED, "Insert operation for this Table is not implemented yet.");
1118 private Status insertManagerRow(Node node, String parent_uuid, Manager row) {
1119 return new Status(StatusCode.NOTIMPLEMENTED, "Insert operation for this Table is not implemented yet.");
1122 private Status insertCapabilityRow(Node node, String parent_uuid, Capability row) {
1123 return new Status(StatusCode.NOTIMPLEMENTED, "Insert operation for this Table is not implemented yet.");
1126 private Status _insertTableRow(Node node, TransactBuilder transaction, Integer insertIndex, String insertErrorMsg,String rowName){
1129 //Check for connection before calling RPC to perform transaction
1130 if (connectionService == null) {
1131 logger.error("Couldn't refer to the ConnectionService");
1132 return new Status(StatusCode.NOSERVICE);
1135 Connection connection = this.getConnection(node);
1136 if (connection == null) {
1137 return new Status(StatusCode.NOSERVICE, "Connection to ovsdb-server not available");
1140 ListenableFuture<List<OperationResult>> transResponse = connection.getRpc().transact(transaction);
1141 List<OperationResult> tr = transResponse.get();
1142 List<Operation> requests = transaction.getRequests();
1143 Status status = new Status(StatusCode.SUCCESS);
1144 for (int i = 0; i < tr.size() ; i++) {
1145 if (i < requests.size()) requests.get(i).setResult(tr.get(i));
1146 if (tr.get(i) != null && tr.get(i).getError() != null && tr.get(i).getError().trim().length() > 0) {
1147 OperationResult result = tr.get(i);
1148 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
1152 if (tr.size() > requests.size()) {
1153 OperationResult result = tr.get(tr.size()-1);
1154 logger.error("Error creating {} : {}\n Error : {}\n Details : {}", insertErrorMsg,
1157 result.getDetails());
1158 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
1160 if (status.isSuccess()) {
1161 UUID bridgeUUID = tr.get(insertIndex).getUuid();
1162 status = new Status(StatusCode.SUCCESS, bridgeUUID.toString());
1165 } catch(Exception e){
1166 logger.error("Error in _insertTableRow(): ",e);
1168 return new Status(StatusCode.INTERNALERROR);
1172 private Status deleteBridgeRow(Node node, String uuid) {
1173 // Set up variables for generic _deleteTableRow()
1174 String parentTableName=Open_vSwitch.NAME.getName();
1175 String childTableName=Bridge.NAME.getName();
1176 String parentColumn = "bridges";
1178 return _deleteTableRow(node,uuid,parentTableName,childTableName,parentColumn);
1181 private Status deletePortRow(Node node, String uuid) {
1182 // Set up variables for generic _deleteTableRow()
1183 String parentTableName=Bridge.NAME.getName();
1184 String childTableName=Port.NAME.getName();
1185 String parentColumn = "ports";
1187 return _deleteTableRow(node,uuid,parentTableName,childTableName,parentColumn);
1190 private Status deleteInterfaceRow(Node node, String uuid) {
1191 // Since Port<-Interface tables have a 1:n relationship, need to test if this is the last interface
1192 // assigned to a port before attempting delete.
1193 Map<String, Table<?>> portTable = inventoryServiceInternal.getTableCache(node, Port.NAME.getName());
1194 Map<String, Table<?>> interfaceTable = inventoryServiceInternal.getTableCache(node, Interface.NAME.getName());
1195 // Check that the UUID exists
1196 if (portTable == null || interfaceTable == null || uuid == null || interfaceTable.get(uuid) == null) {
1197 return new Status(StatusCode.NOTFOUND, "");
1200 // Need to check if this is the last interface for that port. Cannot delete last interface.
1201 UUID compareUuid = new UUID(uuid);
1202 for (int i=0 ; i < portTable.values().size(); i++){
1203 Port port = (Port)portTable.values().toArray()[i];
1204 if ((port.getInterfaces().size() == 1) && (port.getInterfaces().contains(compareUuid))){
1205 return new Status(StatusCode.BADREQUEST, "Cannot delete last interface from port");
1209 // Since the above past, it's safe to use the generic _deleteTableRow method
1210 // Set up variables for generic _deleteTableRow()
1211 String parentTableName=Port.NAME.getName();
1212 String childTableName=Interface.NAME.getName();
1213 String parentColumn = "interfaces";
1215 return _deleteTableRow(node,uuid,parentTableName,childTableName,parentColumn);
1218 private Status deleteControllerRow(Node node, String uuid) {
1219 // Set up variables for generic _deleteTableRow()
1220 String parentTableName=Bridge.NAME.getName();
1221 String childTableName=Controller.NAME.getName();
1222 String parentColumn = "controller";
1224 return _deleteTableRow(node,uuid,parentTableName,childTableName,parentColumn);
1227 private Status deleteOpen_vSwitchRow(Node node, String uuid) {
1228 return new Status(StatusCode.NOTIMPLEMENTED, "delete operation for this Table is not implemented yet.");
1231 private Status deleteSSLRow(Node node, String uuid) {
1232 return new Status(StatusCode.NOTIMPLEMENTED, "delete operation for this Table is not implemented yet.");
1235 private Status deleteSflowRow(Node node, String uuid) {
1236 return new Status(StatusCode.NOTIMPLEMENTED, "delete operation for this Table is not implemented yet.");
1239 private Status deleteQueueRow(Node node, String uuid) {
1240 return new Status(StatusCode.NOTIMPLEMENTED, "delete operation for this Table is not implemented yet.");
1243 private Status deleteQosRow(Node node, String uuid) {
1244 return new Status(StatusCode.NOTIMPLEMENTED, "delete operation for this Table is not implemented yet.");
1247 private Status deleteNetFlowRow(Node node, String uuid) {
1248 return new Status(StatusCode.NOTIMPLEMENTED, "delete operation for this Table is not implemented yet.");
1251 private Status deleteMirrorRow(Node node, String uuid) {
1252 return new Status(StatusCode.NOTIMPLEMENTED, "delete operation for this Table is not implemented yet.");
1255 private Status deleteManagerRow(Node node, String uuid) {
1256 return new Status(StatusCode.NOTIMPLEMENTED, "delete operation for this Table is not implemented yet.");
1259 private Status deleteCapabilityRow(Node node, String uuid) {
1260 return new Status(StatusCode.NOTIMPLEMENTED, "delete operation for this Table is not implemented yet.");
1262 private Status _deleteTableRow(Node node,String uuid,String parentTableName, String childTableName, String parentColumn) {
1264 // Check there is a connectionService
1265 if (connectionService == null) {
1266 logger.error("Couldn't refer to the ConnectionService");
1267 return new Status(StatusCode.NOSERVICE);
1270 // Establish the connection
1271 Connection connection = this.getConnection(node);
1272 if (connection == null) {
1273 return new Status(StatusCode.NOSERVICE, "Connection to ovsdb-server not available");
1276 // Ports have a 0:n relationship with Bridges, so need to MUTATE BRIDGE row and DELETE PORT row
1277 Map<String, Table<?>> parentTable = inventoryServiceInternal.getTableCache(node, parentTableName);
1278 Map<String, Table<?>> childTable = inventoryServiceInternal.getTableCache(node, childTableName);
1280 // Check that the UUID exists
1281 if (parentTable == null || childTable == null || uuid == null || childTable.get(uuid) == null) {
1282 return new Status(StatusCode.NOTFOUND, "");
1285 // Initialise the actual request var
1286 Operation delRequest = null;
1288 // Prepare the mutator to remove the port UUID from the "ports" list in the BRIDGE TABLE
1289 UUID rowUuid = new UUID(uuid);
1290 Mutation mutator = new Mutation(parentColumn, Mutator.DELETE, rowUuid);
1291 List<Mutation> mutations = new ArrayList<Mutation>();
1292 mutations.add(mutator);
1294 Status status = new Status(StatusCode.SUCCESS);
1296 // INCLUDES condition ensures that it captures all rows in the parent table (ie duplicates) that have the child UUID
1297 Condition condition = new Condition(parentColumn, Function.INCLUDES, rowUuid);
1298 List<Condition> where = new ArrayList<Condition>();
1299 where.add(condition);
1300 delRequest = new MutateOperation(parentTableName, where, mutations);
1302 TransactBuilder transaction = new TransactBuilder();
1303 transaction.addOperations(new ArrayList<Operation>(Arrays.asList(delRequest)));
1305 // This executes the transaction.
1306 ListenableFuture<List<OperationResult>> transResponse = connection.getRpc().transact(transaction);
1308 // Pull the responses
1309 List<OperationResult> tr = transResponse.get();
1310 List<Operation> requests = transaction.getRequests();
1312 for (int i = 0; i < tr.size(); i++) {
1313 if (i < requests.size()) requests.get(i).setResult(tr.get(i));
1314 if (tr.get(i) != null && tr.get(i).getError() != null && tr.get(i).getError().trim().length() > 0) {
1315 OperationResult result = tr.get(i);
1316 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
1320 if (tr.size() > requests.size()) {
1321 OperationResult result = tr.get(tr.size() - 1);
1322 logger.error("Error deleting: {}\n Error : {}\n Details : {}",
1323 uuid, result.getError(), result.getDetails());
1324 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
1327 } catch (Exception e) {
1328 logger.error("Error in _deleteTableRow",e);
1330 return new Status(StatusCode.INTERNALERROR);
1333 public void _ovsconnect (CommandInterpreter ci) {
1334 String bridgeName = ci.nextArgument();
1335 if (bridgeName == null) {
1336 ci.println("Please enter Bridge Name");
1340 String ovsdbserver = ci.nextArgument();
1341 if (ovsdbserver == null) {
1342 ci.println("Please enter valid IP-Address");
1346 InetAddress.getByName(ovsdbserver);
1347 } catch (Exception e) {
1348 e.printStackTrace();
1349 ci.println("Please enter valid IP-Address");
1352 String port = ci.nextArgument();
1357 ci.println("connecting to ovsdb server : "+ovsdbserver+":"+port+" ... ");
1358 Map<ConnectionConstants, String> params = new HashMap<ConnectionConstants, String>();
1359 params.put(ConnectionConstants.ADDRESS, ovsdbserver);
1360 params.put(ConnectionConstants.PORT, port);
1361 Node node = connectionService.connect(bridgeName, params);
1362 if (node != null) ci.println("Node Name: "+node.toString());
1363 else ci.println("Could not connect to Node");
1366 public void _addBridge (CommandInterpreter ci) {
1367 String nodeName = ci.nextArgument();
1368 if (nodeName == null) {
1369 ci.println("Please enter Node Name");
1372 String bridgeName = ci.nextArgument();
1373 if (bridgeName == null) {
1374 ci.println("Please enter Bridge Name");
1380 Node node = Node.fromString(nodeName);
1382 ci.println("Invalid Node");
1385 status = this.createBridgeDomain(node, bridgeName, null);
1386 ci.println("Bridge creation status : "+status.toString());
1387 } catch (Throwable e) {
1388 // TODO Auto-generated catch block
1389 e.printStackTrace();
1390 ci.println("Failed to create Bridge "+bridgeName);
1394 public void _getBridgeDomains (CommandInterpreter ci) {
1395 String nodeName = ci.nextArgument();
1396 if (nodeName == null) {
1397 ci.println("Please enter Node Name");
1402 List<String> brlist = new ArrayList<String>();
1404 Node node = Node.fromString(nodeName);
1405 brlist = this.getBridgeDomains(node);
1407 ci.println("Invalid Node");
1410 ci.println("Existing Bridges: "+brlist.toString());
1411 } catch (Throwable e) {
1412 e.printStackTrace();
1413 ci.println("Failed to list Bridges");
1417 public void _deleteBridgeDomain (CommandInterpreter ci) {
1418 String nodeName = ci.nextArgument();
1419 if (nodeName == null) {
1420 ci.println("Please enter Node Name");
1423 String bridgeName = ci.nextArgument();
1424 if (bridgeName == null) {
1425 ci.println("Please enter Bridge Name");
1430 Node node = Node.fromString(nodeName);
1432 ci.println("Invalid Node");
1435 status = this.deleteBridgeDomain(node, bridgeName);
1436 ci.println("Bridge deletion status : "+status.toString());
1437 } catch (Throwable e) {
1438 e.printStackTrace();
1439 ci.println("Failed to delete Bridge "+bridgeName);
1443 public void _addPort (CommandInterpreter ci) {
1444 String nodeName = ci.nextArgument();
1445 if (nodeName == null) {
1446 ci.println("Please enter Node Name");
1450 String bridgeName = ci.nextArgument();
1451 if (bridgeName == null) {
1452 ci.println("Please enter Bridge Name");
1456 String portName = ci.nextArgument();
1457 if (portName == null) {
1458 ci.println("Please enter Port Name");
1462 String type = ci.nextArgument();
1464 Map<String, String> configs = new HashMap<String, String>();
1466 String configKey = ci.nextArgument();
1467 if (configKey == null) break;
1468 String configValue = ci.nextArgument();
1469 if (configValue == null) break;
1470 configs.put(configKey, configValue);
1473 Map<ConfigConstants, Object> customConfigs = null;
1475 customConfigs = new HashMap<ConfigConstants, Object>();
1476 customConfigs.put(ConfigConstants.TYPE, type);
1479 if (configs.size() > 0) {
1480 if (customConfigs == null) customConfigs = new HashMap<ConfigConstants, Object>();
1481 customConfigs.put(ConfigConstants.CUSTOM, configs);
1482 ci.println(customConfigs.toString());
1486 Node node = Node.fromString(nodeName);
1488 ci.println("Invalid Node");
1491 status = this.addPort(node, bridgeName, portName, customConfigs);
1492 ci.println("Port creation status : "+status.toString());
1493 } catch (Throwable e) {
1494 // TODO Auto-generated catch block
1495 e.printStackTrace();
1496 ci.println("Failed to create Port "+portName+" in Bridge "+bridgeName);
1500 public void _deletePort (CommandInterpreter ci) {
1501 String nodeName = ci.nextArgument();
1502 if (nodeName == null) {
1503 ci.println("Please enter Node Name");
1507 String bridgeName = ci.nextArgument();
1508 if (bridgeName == null) {
1509 ci.println("Please enter Bridge Name");
1513 String portName = ci.nextArgument();
1514 if (portName == null) {
1515 ci.println("Please enter Port Name");
1521 Node node = Node.fromString(nodeName);
1523 ci.println("Invalid Node");
1526 status = this.deletePort(node, bridgeName, portName);
1527 ci.println("Port deletion status : "+status.toString());
1528 } catch (Throwable e) {
1529 // TODO Auto-generated catch block
1530 e.printStackTrace();
1531 ci.println("Failed to delete Port "+portName+" in Bridge "+bridgeName);
1535 public void _addPortVlan (CommandInterpreter ci) {
1536 String nodeName = ci.nextArgument();
1537 if (nodeName == null) {
1538 ci.println("Please enter Node Name");
1542 String bridgeName = ci.nextArgument();
1543 if (bridgeName == null) {
1544 ci.println("Please enter Bridge Name");
1548 String portName = ci.nextArgument();
1549 if (portName == null) {
1550 ci.println("Please enter Port Name");
1554 String vlan = ci.nextArgument();
1556 ci.println("Please enter Valid Vlan");
1560 Integer.parseInt(vlan);
1561 } catch (Exception e) {
1562 ci.println("Please enter Valid Vlan");
1567 Map<ConfigConstants, Object> configs = new HashMap<ConfigConstants, Object>();
1568 configs.put(ConfigConstants.TYPE, "VLAN");
1569 configs.put(ConfigConstants.VLAN, vlan);
1573 Node node = Node.fromString(nodeName);
1575 ci.println("Invalid Node");
1578 status = this.addPort(node, bridgeName, portName, configs);
1579 ci.println("Port creation status : "+status.toString());
1580 } catch (Throwable e) {
1581 // TODO Auto-generated catch block
1582 e.printStackTrace();
1583 ci.println("Failed to create Port "+portName+" in Bridge "+bridgeName);
1587 public void _addTunnel (CommandInterpreter ci) {
1588 String nodeName = ci.nextArgument();
1589 if (nodeName == null) {
1590 ci.println("Please enter Node Name");
1594 String bridgeName = ci.nextArgument();
1595 if (bridgeName == null) {
1596 ci.println("Please enter Bridge Name");
1600 String portName = ci.nextArgument();
1601 if (portName == null) {
1602 ci.println("Please enter Port Name");
1606 String tunnelType = ci.nextArgument();
1607 if (tunnelType == null) {
1608 ci.println("Please enter Tunnel Type");
1612 String remoteIp = ci.nextArgument();
1613 if (remoteIp == null) {
1614 ci.println("Please enter valid Remote IP Address");
1619 InetAddress.getByName(remoteIp);
1620 } catch (Exception e) {
1621 e.printStackTrace();
1622 ci.println("Please enter valid Remote IP Address");
1626 Map<ConfigConstants, Object> configs = new HashMap<ConfigConstants, Object>();
1627 configs.put(ConfigConstants.TYPE, "TUNNEL");
1628 configs.put(ConfigConstants.TUNNEL_TYPE, tunnelType);
1629 configs.put(ConfigConstants.DEST_IP, remoteIp);
1633 Node node = Node.fromString(nodeName);
1635 ci.println("Invalid Node");
1638 status = this.addPort(node, bridgeName, portName, configs);
1639 ci.println("Port creation status : "+status.toString());
1640 } catch (Throwable e) {
1641 // TODO Auto-generated catch block
1642 e.printStackTrace();
1643 ci.println("Failed to create Port "+portName+" in Bridge "+bridgeName);
1647 public void _printCache (CommandInterpreter ci) {
1648 String nodeName = ci.nextArgument();
1649 if (nodeName == null) {
1650 ci.println("Please enter Node Name");
1653 Node node = Node.fromString(nodeName);
1655 ci.println("Invalid Node");
1658 inventoryServiceInternal.printCache(node);
1661 public void _forceConnect (CommandInterpreter ci) {
1662 String force = ci.nextArgument();
1663 if (force.equalsIgnoreCase("YES")) forceConnect = true;
1664 else if (force.equalsIgnoreCase("NO")) forceConnect = false;
1665 else ci.println("Please enter YES or NO.");
1666 ci.println("Current ForceConnect State : "+forceConnect);
1671 public String getHelp() {
1672 StringBuffer help = new StringBuffer();
1673 help.append("---OVSDB CLI---\n");
1674 help.append("\t ovsconnect <ConnectionName> <ip-address> - Connect to OVSDB\n");
1675 help.append("\t addBridge <Node> <BridgeName> - Add Bridge\n");
1676 help.append("\t getBridgeDomains <Node> - Get Bridges\n");
1677 help.append("\t deleteBridgeDomain <Node> <BridgeName> - Delete a Bridge\n");
1678 help.append("\t addPort <Node> <BridgeName> <PortName> <type> <options pairs> - Add Port\n");
1679 help.append("\t deletePort <Node> <BridgeName> <PortName> - Delete Port\n");
1680 help.append("\t addPortVlan <Node> <BridgeName> <PortName> <vlan> - Add Port, Vlan\n");
1681 help.append("\t addTunnel <Node> <Bridge> <Port> <tunnel-type> <remote-ip> - Add Tunnel\n");
1682 help.append("\t printCache <Node> - Prints Table Cache");
1683 return help.toString();