@Override public Network implement( Network network, NetworkOffering offering, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapcityException { assert (network.getState() == State.Implementing) : "Why are we implementing " + network; long dcId = dest.getDataCenter().getId(); NetworkType nwType = dest.getDataCenter().getNetworkType(); // get physical network id Long physicalNetworkId = network.getPhysicalNetworkId(); // physical network id can be null in Guest Network in Basic zone, so // locate the physical network if (physicalNetworkId == null) { physicalNetworkId = _networkModel.findPhysicalNetworkId(dcId, offering.getTags(), offering.getTrafficType()); } PhysicalNetworkVO physnet = _physicalNetworkDao.findById(physicalNetworkId); if (!canHandle(offering, nwType, physnet)) { s_logger.debug("Refusing to design this network"); return null; } NetworkVO implemented = (NetworkVO) super.implement(network, offering, dest, context); if (network.getGateway() != null) { implemented.setGateway(network.getGateway()); } if (network.getCidr() != null) { implemented.setCidr(network.getCidr()); } String name = network.getName(); if (name == null || name.isEmpty()) { name = ((NetworkVO) network).getUuid(); } // do we need to create switch right now? implemented.setBroadcastDomainType(BroadcastDomainType.Vswitch); return implemented; }
@Override public Network implement( final Network network, final NetworkOffering offering, final DeployDestination dest, final ReservationContext context) throws InsufficientVirtualNetworkCapacityException { assert network.getState() == State.Implementing : "Why are we implementing " + network; final long dcId = dest.getDataCenter().getId(); Long physicalNetworkId = network.getPhysicalNetworkId(); // physical network id can be null in Guest Network in Basic zone, so locate the physical // network if (physicalNetworkId == null) { physicalNetworkId = networkModel.findPhysicalNetworkId(dcId, offering.getTags(), offering.getTrafficType()); } final NetworkVO implemented = new NetworkVO( network.getTrafficType(), network.getMode(), network.getBroadcastDomainType(), network.getNetworkOfferingId(), State.Allocated, network.getDataCenterId(), physicalNetworkId, offering.getRedundantRouter()); if (network.getGateway() != null) { implemented.setGateway(network.getGateway()); } if (network.getCidr() != null) { implemented.setCidr(network.getCidr()); } // Name is either the given name or the uuid String name = network.getName(); if (name == null || name.isEmpty()) { name = ((NetworkVO) network).getUuid(); } if (name.length() > MAX_NAME_LENGTH) { name = name.substring(0, MAX_NAME_LENGTH - 1); } final List<NiciraNvpDeviceVO> devices = niciraNvpDao.listByPhysicalNetwork(physicalNetworkId); if (devices.isEmpty()) { s_logger.error("No NiciraNvp Controller on physical network " + physicalNetworkId); return null; } final NiciraNvpDeviceVO niciraNvpDevice = devices.get(0); final HostVO niciraNvpHost = hostDao.findById(niciraNvpDevice.getHostId()); hostDao.loadDetails(niciraNvpHost); final String transportzoneuuid = niciraNvpHost.getDetail("transportzoneuuid"); final String transportzoneisotype = niciraNvpHost.getDetail("transportzoneisotype"); final CreateLogicalSwitchCommand cmd = new CreateLogicalSwitchCommand( transportzoneuuid, transportzoneisotype, name, context.getDomain().getName() + "-" + context.getAccount().getAccountName()); final CreateLogicalSwitchAnswer answer = (CreateLogicalSwitchAnswer) agentMgr.easySend(niciraNvpHost.getId(), cmd); if (answer == null || !answer.getResult()) { s_logger.error("CreateLogicalSwitchCommand failed"); return null; } try { implemented.setBroadcastUri(new URI("lswitch", answer.getLogicalSwitchUuid(), null)); implemented.setBroadcastDomainType(BroadcastDomainType.Lswitch); s_logger.info( "Implemented OK, network linked to = " + implemented.getBroadcastUri().toString()); } catch (final URISyntaxException e) { s_logger.error( "Unable to store logical switch id in broadcast uri, uuid = " + implemented.getUuid(), e); return null; } return implemented; }
@Override public Network implement( Network config, NetworkOffering offering, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapcityException { assert (config.getState() == State.Implementing) : "Why are we implementing " + config; if (Boolean.parseBoolean(_configDao.getValue(Config.OvsTunnelNetwork.key()))) { return null; } if (!_networkModel.networkIsConfiguredForExternalNetworking( config.getDataCenterId(), config.getId())) { return super.implement(config, offering, dest, context); } DataCenter zone = dest.getDataCenter(); NetworkVO implemented = new NetworkVO( config.getTrafficType(), config.getMode(), config.getBroadcastDomainType(), config.getNetworkOfferingId(), State.Allocated, config.getDataCenterId(), config.getPhysicalNetworkId()); // Get a vlan tag int vlanTag; if (config.getBroadcastUri() == null) { String vnet = _dcDao.allocateVnet( zone.getId(), config.getPhysicalNetworkId(), config.getAccountId(), context.getReservationId()); try { vlanTag = Integer.parseInt(vnet); } catch (NumberFormatException e) { throw new CloudRuntimeException( "Obtained an invalid guest vlan tag. Exception: " + e.getMessage()); } implemented.setBroadcastUri(BroadcastDomainType.Vlan.toUri(vlanTag)); ActionEventUtils.onCompletedActionEvent( UserContext.current().getCallerUserId(), config.getAccountId(), EventVO.LEVEL_INFO, EventTypes.EVENT_ZONE_VLAN_ASSIGN, "Assigned Zone Vlan: " + vnet + " Network Id: " + config.getId(), 0); } else { vlanTag = Integer.parseInt(config.getBroadcastUri().getHost()); implemented.setBroadcastUri(config.getBroadcastUri()); } // Determine the new gateway and CIDR String[] oldCidr = config.getCidr().split("/"); String oldCidrAddress = oldCidr[0]; int cidrSize = Integer.parseInt(oldCidr[1]); long newCidrAddress = (NetUtils.ip2Long(oldCidrAddress)); // if the implementing network is for vpc, no need to generate newcidr, use the cidr that came // from super cidr if (config.getVpcId() != null) { implemented.setGateway(config.getGateway()); implemented.setCidr(config.getCidr()); implemented.setState(State.Implemented); } else { // Determine the offset from the lowest vlan tag int offset = getVlanOffset(config.getPhysicalNetworkId(), vlanTag); cidrSize = getGloballyConfiguredCidrSize(); // If the offset has more bits than there is room for, return null long bitsInOffset = 32 - Integer.numberOfLeadingZeros(offset); if (bitsInOffset > (cidrSize - 8)) { throw new CloudRuntimeException( "The offset " + offset + " needs " + bitsInOffset + " bits, but only have " + (cidrSize - 8) + " bits to work with."); } newCidrAddress = (NetUtils.ip2Long(oldCidrAddress) & 0xff000000) | (offset << (32 - cidrSize)); implemented.setGateway(NetUtils.long2Ip(newCidrAddress + 1)); implemented.setCidr(NetUtils.long2Ip(newCidrAddress) + "/" + cidrSize); implemented.setState(State.Implemented); } // Mask the Ipv4 address of all nics that use this network with the new guest VLAN offset List<NicVO> nicsInNetwork = _nicDao.listByNetworkId(config.getId()); for (NicVO nic : nicsInNetwork) { if (nic.getIp4Address() != null) { long ipMask = getIpMask(nic.getIp4Address(), cidrSize); nic.setIp4Address(NetUtils.long2Ip(newCidrAddress | ipMask)); _nicDao.persist(nic); } } // Mask the destination address of all port forwarding rules in this network with the new guest // VLAN offset List<PortForwardingRuleVO> pfRulesInNetwork = _pfRulesDao.listByNetwork(config.getId()); for (PortForwardingRuleVO pfRule : pfRulesInNetwork) { if (pfRule.getDestinationIpAddress() != null) { long ipMask = getIpMask(pfRule.getDestinationIpAddress().addr(), cidrSize); String maskedDestinationIpAddress = NetUtils.long2Ip(newCidrAddress | ipMask); pfRule.setDestinationIpAddress(new Ip(maskedDestinationIpAddress)); _pfRulesDao.update(pfRule.getId(), pfRule); } } return implemented; }