SfcOvsRpc implements ServiceFunctionForwarderOvsService { @Override public Future<RpcResult<CreateOvsBridgeOutput>> createOvsBridge(CreateOvsBridgeInput input) { Preconditions.checkNotNull(input, "create-ovs-bridge RPC input must be not null!"); Preconditions.checkNotNull(input.getOvsNode(), "create-ovs-bridge RPC input container ovs-node must be not null!"); RpcResultBuilder<CreateOvsBridgeOutput> rpcResultBuilder; NodeId nodeId = null; OvsNode ovsNode = input.getOvsNode(); if (ovsNode.getPort() != null && ovsNode.getIp() != null) { nodeId = new NodeId(OVSDB_NODE_PREFIX + ovsNode.getIp().getIpv4Address().getValue() + ":" + ovsNode.getPort().getValue()); } else if (ovsNode.getIp() != null) { IpAddress ipAddress = new IpAddress(ovsNode.getIp().getValue()); Node node = SfcOvsUtil.getManagerNodeByIp(ipAddress, executor); if (node != null) { nodeId = node.getNodeId(); } } if (nodeId != null) { InstanceIdentifier<Node> nodeIID = SfcOvsUtil.buildOvsdbNodeIID(nodeId); OvsdbBridgeAugmentationBuilder ovsdbBridgeBuilder = new OvsdbBridgeAugmentationBuilder(); ovsdbBridgeBuilder.setBridgeName(new OvsdbBridgeName(input.getName())); ovsdbBridgeBuilder.setManagedBy(new OvsdbNodeRef(nodeIID)); Object[] methodParams = {ovsdbBridgeBuilder.build()}; SfcOvsDataStoreAPI sfcOvsDataStoreAPI = new SfcOvsDataStoreAPI(SfcOvsDataStoreAPI.Method.PUT_OVSDB_BRIDGE, methodParams); if ((boolean) SfcOvsUtil.submitCallable(sfcOvsDataStoreAPI, executor)) { rpcResultBuilder = RpcResultBuilder.success(new CreateOvsBridgeOutputBuilder().setResult(true).build()); } else { String message = "Error writing OVS Bridge: '" + input.getName() + "' into OVSDB Configuration DataStore."; rpcResultBuilder = RpcResultBuilder.<CreateOvsBridgeOutput>failed() .withError(RpcError.ErrorType.APPLICATION, message); } } else { String message = "Error writing OVS Bridge: '" + input.getName() + "' into OVSDB Configuration DataStore (cannot determine parent NodeId)."; rpcResultBuilder = RpcResultBuilder.<CreateOvsBridgeOutput>failed() .withError(RpcError.ErrorType.APPLICATION, message); } return Futures.immediateFuture(rpcResultBuilder.build()); }  @Override Future<RpcResult<CreateOvsBridgeOutput>> createOvsBridge(CreateOvsBridgeInput input);  }
@Test public void testCreateOvsBridgeNullNode() throws Exception { createOvsBridgeInputBuilder = new CreateOvsBridgeInputBuilder(); ovsNodeBuilder = new OvsNodeBuilder(); sfcOvsRpcObject = new SfcOvsRpc(); nodeBuilder = new NodeBuilder(); ovsNodeBuilder.setIp(new IpAddress(new Ipv4Address(ipAddressString))); createOvsBridgeInputBuilder.setOvsNode(ovsNodeBuilder.build()); futureResult = sfcOvsRpcObject.createOvsBridge(createOvsBridgeInputBuilder.build()); assertNotNull("Must not be null", futureResult); assertNull("Result must be null", futureResult.get().getResult()); assertFalse("Must be false", futureResult.get().isSuccessful()); }
@Test public void testCreateOvsBridgeNullNodeId() throws Exception { createOvsBridgeInputBuilder = new CreateOvsBridgeInputBuilder(); ovsNodeBuilder = new OvsNodeBuilder(); sfcOvsRpcObject = new SfcOvsRpc(); nodeBuilder = new NodeBuilder(); nodeBuilder.setNodeId(new NodeId("NodeId")); ovsNodeBuilder.setIp(new IpAddress(new Ipv4Address(ipAddressString))); createOvsBridgeInputBuilder.setName(testName).setOvsNode(ovsNodeBuilder.build()); ovsNodeBuilder.setIp(new IpAddress(new Ipv4Address(ipAddressString))); createOvsBridgeInputBuilder.setName(testName).setOvsNode(ovsNodeBuilder.build()); PowerMockito.stub(PowerMockito.method(SfcOvsUtil.class, "getManagerNodeByIp")).toReturn(nodeBuilder.build()); PowerMockito.stub(PowerMockito.method(SfcOvsUtil.class, "submitCallable")).toReturn(false); futureResult = sfcOvsRpcObject.createOvsBridge(createOvsBridgeInputBuilder.build()); assertNotNull("Must not be null", futureResult); assertNull("Result must be null", futureResult.get().getResult()); assertFalse("Must be false", futureResult.get().isSuccessful()); }
@Test public void testCreateOvsBridgeFalseResult() throws Exception { createOvsBridgeInputBuilder = new CreateOvsBridgeInputBuilder(); ovsNodeBuilder = new OvsNodeBuilder(); sfcOvsRpcObject = new SfcOvsRpc(); ovsNodeBuilder.setIp(new IpAddress(new Ipv4Address(ipAddressString))).setPort(new PortNumber(portNumber)); createOvsBridgeInputBuilder.setName(testName).setOvsNode(ovsNodeBuilder.build()); ovsNodeBuilder.setIp(new IpAddress(new Ipv4Address(ipAddressString))).setPort(new PortNumber(portNumber)); createOvsBridgeInputBuilder.setName(testName).setOvsNode(ovsNodeBuilder.build()); PowerMockito.stub(PowerMockito.method(SfcOvsUtil.class, "submitCallable")).toReturn(false); futureResult = sfcOvsRpcObject.createOvsBridge(createOvsBridgeInputBuilder.build()); assertNotNull("Must not be null", futureResult); assertNull("Result must be null", futureResult.get().getResult()); assertFalse("Must be false", futureResult.get().isSuccessful()); }
@Test public void testCreateOvsBridgeTrueResult() throws Exception { createOvsBridgeInputBuilder = new CreateOvsBridgeInputBuilder(); ovsNodeBuilder = new OvsNodeBuilder(); sfcOvsRpcObject = new SfcOvsRpc(); ovsNodeBuilder.setIp(new IpAddress(new Ipv4Address(ipAddressString))).setPort(new PortNumber(portNumber)); createOvsBridgeInputBuilder.setOvsNode(ovsNodeBuilder.build()).setName(testName); createOvsBridgeInputBuilder.setOvsNode(ovsNodeBuilder.build()).setName(testName); PowerMockito.stub(PowerMockito.method(SfcOvsUtil.class, "submitCallable")).toReturn(true); futureResult = sfcOvsRpcObject.createOvsBridge(createOvsBridgeInputBuilder.build()); assertNotNull("Must not be null", futureResult); assertTrue("Result must be null", futureResult.get().getResult().isResult()); assertTrue("Must be true", futureResult.get().isSuccessful()); }