use of org.midonet.client.resource.Bridge in project cloudstack by apache.
the class MidoNetElement method prepare.
@Override
public boolean prepare(Network network, NicProfile nic, VirtualMachineProfile vm, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
s_logger.debug("prepare called with network: " + network.toString() + " nic: " + nic.toString() + " vm: " + vm.toString());
if (!midoInNetwork(network)) {
return false;
}
if (nic.getTrafficType() == Networks.TrafficType.Guest && !canHandle(network, Service.StaticNat)) {
return false;
}
if (nic.getTrafficType() == Networks.TrafficType.Guest || nic.getTrafficType() == Networks.TrafficType.Public && nic.getBroadcastType() == Networks.BroadcastDomainType.Mido) {
Bridge netBridge = getOrCreateNetworkBridge(network);
if (nic.getTrafficType() == Networks.TrafficType.Public && vm.getVirtualMachine().getType() != VirtualMachine.Type.DomainRouter) {
// Get provider router
Router providerRouter = api.getRouter(_providerRouterId);
Port[] ports = getOrCreatePublicBridgePorts(nic, netBridge, providerRouter);
RouterPort providerDownlink = (RouterPort) ports[1];
// Set route from router to bridge for this particular IP. Prepare
// is called in both starting a new VM and restarting a VM, so the
// NIC may
boolean routeExists = false;
for (Route route : providerRouter.getRoutes(new MultivaluedMapImpl())) {
String ip4 = route.getDstNetworkAddr();
if (ip4 != null && ip4.equals(nic.getIPv4Address())) {
routeExists = true;
break;
}
}
if (!routeExists) {
providerRouter.addRoute().type("Normal").weight(100).srcNetworkAddr("0.0.0.0").srcNetworkLength(0).dstNetworkAddr(nic.getIPv4Address()).dstNetworkLength(32).nextHopPort(providerDownlink.getId()).nextHopGateway(null).create();
}
}
// Add port on bridge
// returns wrapper resource of port
BridgePort newPort = netBridge.addExteriorPort().create();
// Set MidoNet port VIF ID to UUID of nic
UUID nicUUID = getNicUUID(nic);
newPort.vifId(nicUUID).update();
}
return true;
}
use of org.midonet.client.resource.Bridge in project cloudstack by apache.
the class MidoNetElementTest method testAddDhcpEntry.
/*
* Test the standard case of addDhcpEntry with no errors.
*/
public void testAddDhcpEntry() {
//mockMgmt
MidonetApi api = mock(MidonetApi.class, RETURNS_DEEP_STUBS);
//mockDhcpHost
DhcpHost mockDhcpHost = mock(DhcpHost.class);
//mockHostCollection
ResourceCollection<DhcpHost> hosts = new ResourceCollection<DhcpHost>(new ArrayList<DhcpHost>());
//mockDhcpSubnet
DhcpSubnet mockSub = mock(DhcpSubnet.class);
when(mockSub.addDhcpHost()).thenReturn(mockDhcpHost);
when(mockSub.getDhcpHosts()).thenReturn(hosts);
//mockSubnetCollection
ResourceCollection mockSubnetCollection = mock(ResourceCollection.class);
when(mockSubnetCollection.get(anyInt())).thenReturn(mockSub);
//mockBridge
Bridge mockBridge = mock(Bridge.class);
when(api.addBridge().tenantId(anyString()).name(anyString()).create()).thenReturn(mockBridge);
when(mockBridge.getDhcpSubnets()).thenReturn(mockSubnetCollection);
//mockRouter
Router mockRouter = mock(Router.class);
when(api.addRouter().tenantId(anyString()).name(anyString()).create()).thenReturn(mockRouter);
//mockNetwork
Network mockNetwork = mock(Network.class);
when(mockNetwork.getAccountId()).thenReturn((long) 1);
when(mockNetwork.getGateway()).thenReturn("1.2.3.4");
when(mockNetwork.getCidr()).thenReturn("1.2.3.0/24");
when(mockNetwork.getId()).thenReturn((long) 2);
when(mockNetwork.getBroadcastDomainType()).thenReturn(Networks.BroadcastDomainType.Mido);
when(mockNetwork.getTrafficType()).thenReturn(Networks.TrafficType.Guest);
//mockAccountDao
AccountDao mockAccountDao = mock(AccountDao.class);
AccountVO mockAccountVO = mock(AccountVO.class);
when(mockAccountDao.findById(anyLong())).thenReturn(mockAccountVO);
when(mockAccountVO.getUuid()).thenReturn("1");
//mockNic
NicProfile mockNic = mock(NicProfile.class);
when(mockNic.getIPv4Address()).thenReturn("10.10.10.170");
when(mockNic.getMacAddress()).thenReturn("02:00:73:3e:00:01");
when(mockNic.getName()).thenReturn("Fake Name");
//mockVm
@SuppressWarnings("unchecked") VirtualMachineProfile mockVm = mock(VirtualMachineProfile.class);
when(mockVm.getType()).thenReturn(VirtualMachine.Type.User);
MidoNetElement elem = new MidoNetElement();
elem.setMidonetApi(api);
elem.setAccountDao(mockAccountDao);
boolean result = false;
try {
result = elem.addDhcpEntry(mockNetwork, mockNic, mockVm, null, null);
} catch (ConcurrentOperationException e) {
fail(e.getMessage());
} catch (InsufficientCapacityException e) {
fail(e.getMessage());
} catch (ResourceUnavailableException e) {
fail(e.getMessage());
}
assertEquals(result, true);
}
use of org.midonet.client.resource.Bridge in project cloudstack by apache.
the class MidoNetElement method getNetworkBridge.
private Bridge getNetworkBridge(long networkID, String accountUuid) {
MultivaluedMap qNetBridge = new MultivaluedMapImpl();
String networkUUIDStr = String.valueOf(networkID);
qNetBridge.add("tenant_id", accountUuid);
for (Bridge b : this.api.getBridges(qNetBridge)) {
if (b.getName().equals(networkUUIDStr)) {
return b;
}
}
return null;
}
use of org.midonet.client.resource.Bridge in project cloudstack by apache.
the class MidoNetElement method implement.
@Override
public boolean implement(Network network, NetworkOffering offering, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
s_logger.debug("implement called with network: " + network.toString());
if (!midoInNetwork(network)) {
return false;
}
if (network.getTrafficType() == Networks.TrafficType.Guest) {
// Create the Midonet bridge for this network
Bridge netBridge = getOrCreateNetworkBridge(network);
Router tenantRouter = getOrCreateGuestNetworkRouter(network);
// connect router and bridge
ensureBridgeConnectedToRouter(network, netBridge, tenantRouter);
}
return true;
}
use of org.midonet.client.resource.Bridge in project cloudstack by apache.
the class MidoNetVifDriver method plug.
@Override
public LibvirtVMDef.InterfaceDef plug(NicTO nic, String guestOsType, String nicAdapter) throws InternalErrorException, LibvirtException {
if (s_logger.isDebugEnabled()) {
s_logger.debug("nic=" + nic);
}
LibvirtVMDef.InterfaceDef intf = new LibvirtVMDef.InterfaceDef();
String trafficLabel = nic.getName();
if (nic.getBroadcastType() == Networks.BroadcastDomainType.Mido && (nic.getType() == Networks.TrafficType.Guest || nic.getType() == Networks.TrafficType.Public)) {
/*
* create the tap.
*/
String tapName = addTap();
/*
* grab the tenant id and the network id from the Broadcast URI.
* We need to pluck the values out of the String. The string
* should look like "mido://[tenant_id].[bridge_name]"
*/
MultivaluedMap qNet = new MultivaluedMapImpl();
String nicAuthority = nic.getBroadcastUri().getAuthority();
String tenantId = nicAuthority.split("\\.")[0];
qNet.add("tenant_id", tenantId);
String url = nicAuthority.split("\\.")[1];
String netName = url.split(":")[0];
MidonetApi api = new MidonetApi(_midoApiLocation);
api.enableLogging();
for (Bridge b : api.getBridges(qNet)) {
if (b.getName().equals(netName)) {
for (BridgePort p : b.getPorts()) {
UUID pvif = p.getVifId();
if (pvif != null && p.getVifId().toString().equals(nic.getUuid())) {
getMyHost(api).addHostInterfacePort().interfaceName(tapName).portId(p.getId()).create();
break;
}
}
}
}
intf.defEthernet(tapName, nic.getMac(), getGuestNicModel(guestOsType, nicAdapter), "");
} else {
throw new InternalErrorException("Only NICs of BroadcastDomain type Mido are supported by the MidoNetVifDriver");
}
return intf;
}
Aggregations