use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.flowspec.flowspec.type.port._case.Ports in project netvirt by opendaylight.
the class NetvirtProviderTest method getLogicalInterfacesFromNeutronNetwork.
@Test
public void getLogicalInterfacesFromNeutronNetwork() {
// Network doesnt exist
NeutronNetworkBuilder nwBuilder = new NeutronNetworkBuilder();
nwBuilder.setNetworkUuid(NW_UUID_NOEXIST_STR);
List<String> interfaces = netvirtProvider.getLogicalInterfacesFromNeutronNetwork(nwBuilder.build());
assertTrue(interfaces.isEmpty());
// Network exists, subnet list empty
createNetworkMap(NW_UUID_STR);
nwBuilder = new NeutronNetworkBuilder();
nwBuilder.setNetworkUuid(NW_UUID_STR);
interfaces = netvirtProvider.getLogicalInterfacesFromNeutronNetwork(nwBuilder.build());
assertTrue(interfaces.isEmpty());
// Network exists, subnet does not exist
createNetworkMap(NW_UUID_STR, SUBNET_UUID_STR, false, EMPTY_STR);
nwBuilder = new NeutronNetworkBuilder();
nwBuilder.setNetworkUuid(NW_UUID_STR);
interfaces = netvirtProvider.getLogicalInterfacesFromNeutronNetwork(nwBuilder.build());
assertTrue(interfaces.isEmpty());
// Network exists, subnet exists, no ports
createNetworkMap(NW_UUID_STR, SUBNET_UUID_STR, true, EMPTY_STR);
nwBuilder = new NeutronNetworkBuilder();
nwBuilder.setNetworkUuid(NW_UUID_STR);
interfaces = netvirtProvider.getLogicalInterfacesFromNeutronNetwork(nwBuilder.build());
assertTrue(interfaces.isEmpty());
// Network exists, subnet exists, port exists
createNetworkMap(NW_UUID_STR, SUBNET_UUID_STR, true, PORT_UUID_STR);
nwBuilder = new NeutronNetworkBuilder();
nwBuilder.setNetworkUuid(NW_UUID_STR);
interfaces = netvirtProvider.getLogicalInterfacesFromNeutronNetwork(nwBuilder.build());
assertFalse(interfaces.isEmpty());
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.flowspec.flowspec.type.port._case.Ports in project netvirt by opendaylight.
the class ExternalNetworksChangeListener method associateExternalNetworkWithVPN.
private void associateExternalNetworkWithVPN(Networks network, WriteTransaction writeFlowInvTx) {
List<Uuid> routerIds = network.getRouterIds();
for (Uuid routerId : routerIds) {
// long router = NatUtil.getVpnId(dataBroker, routerId.getValue());
InstanceIdentifier<RouterPorts> routerPortsId = NatUtil.getRouterPortsId(routerId.getValue());
Optional<RouterPorts> optRouterPorts = MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, routerPortsId);
if (!optRouterPorts.isPresent()) {
LOG.debug("associateExternalNetworkWithVPN : Could not read Router Ports data object with id: {} " + "to handle associate ext nw {}", routerId, network.getId());
continue;
}
RouterPorts routerPorts = optRouterPorts.get();
List<Ports> interfaces = routerPorts.getPorts();
for (Ports port : interfaces) {
String portName = port.getPortName();
BigInteger dpnId = NatUtil.getDpnForInterface(interfaceManager, portName);
if (dpnId.equals(BigInteger.ZERO)) {
LOG.debug("associateExternalNetworkWithVPN : DPN not found for {}, " + "skip handling of ext nw {} association", portName, network.getId());
continue;
}
List<InternalToExternalPortMap> intExtPortMapList = port.getInternalToExternalPortMap();
for (InternalToExternalPortMap ipMap : intExtPortMapList) {
// remove all VPN related entries
floatingIpListener.createNATFlowEntries(dpnId, portName, routerId.getValue(), network.getId(), ipMap, writeFlowInvTx);
}
}
}
// SNAT
for (Uuid routerId : routerIds) {
LOG.debug("associateExternalNetworkWithVPN() : for routerId {}", routerId);
Uuid networkId = network.getId();
if (networkId == null) {
LOG.error("associateExternalNetworkWithVPN : networkId is null for the router ID {}", routerId);
return;
}
final String vpnName = network.getVpnid().getValue();
if (vpnName == null) {
LOG.error("associateExternalNetworkWithVPN : No VPN associated with ext nw {} for router {}", networkId, routerId);
return;
}
BigInteger dpnId = new BigInteger("0");
InstanceIdentifier<RouterToNaptSwitch> routerToNaptSwitch = NatUtil.buildNaptSwitchRouterIdentifier(routerId.getValue());
Optional<RouterToNaptSwitch> rtrToNapt = MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, routerToNaptSwitch);
if (rtrToNapt.isPresent()) {
dpnId = rtrToNapt.get().getPrimarySwitchId();
}
LOG.debug("associateExternalNetworkWithVPN : got primarySwitch as dpnId{} ", dpnId);
if (dpnId == null || dpnId.equals(BigInteger.ZERO)) {
LOG.warn("associateExternalNetworkWithVPN : primary napt Switch not found for router {} on dpn: {}", routerId, dpnId);
return;
}
Long routerIdentifier = NatUtil.getVpnId(dataBroker, routerId.getValue());
InstanceIdentifierBuilder<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMapping> idBuilder = InstanceIdentifier.builder(IntextIpMap.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMapping.class, new org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMappingKey(routerIdentifier));
InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMapping> id = idBuilder.build();
Optional<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMapping> ipMapping = MDSALUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL, id);
if (ipMapping.isPresent()) {
List<IpMap> ipMaps = ipMapping.get().getIpMap();
for (IpMap ipMap : ipMaps) {
String externalIp = ipMap.getExternalIp();
LOG.debug("associateExternalNetworkWithVPN : Calling advToBgpAndInstallFibAndTsFlows for dpnId {}," + "vpnName {} and externalIp {}", dpnId, vpnName, externalIp);
if (natMode == NatMode.Controller) {
externalRouterListener.advToBgpAndInstallFibAndTsFlows(dpnId, NwConstants.INBOUND_NAPT_TABLE, vpnName, routerIdentifier, routerId.getValue(), externalIp, network.getId(), null, /* external-router */
writeFlowInvTx);
}
}
} else {
LOG.warn("associateExternalNetworkWithVPN : No ipMapping present fot the routerId {}", routerId);
}
long vpnId = NatUtil.getVpnId(dataBroker, vpnName);
// Install 47 entry to point to 21
if (natMode == NatMode.Controller) {
externalRouterListener.installNaptPfibEntriesForExternalSubnets(routerId.getValue(), dpnId, writeFlowInvTx);
if (vpnId != -1) {
LOG.debug("associateExternalNetworkWithVPN : Calling externalRouterListener installNaptPfibEntry " + "for dpnId {} and vpnId {}", dpnId, vpnId);
externalRouterListener.installNaptPfibEntry(dpnId, vpnId, writeFlowInvTx);
}
}
}
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.flowspec.flowspec.type.port._case.Ports in project netvirt by opendaylight.
the class NatRpcServiceImpl method constructNatInformation.
private List<RouterNat> constructNatInformation(Uuid routerUuid, long routerId) {
String neutronRouterName = NatUtil.getNeutronRouterNamebyUuid(dataBroker, routerUuid);
RouterNatBuilder natRouterBuilder = new RouterNatBuilder();
natRouterBuilder.setRouterUuid(routerUuid);
natRouterBuilder.setRouterName(neutronRouterName);
IpPortMapping ipPortMapping = NatUtil.getIportMapping(dataBroker, routerId);
if (ipPortMapping == null) {
LOG.warn("constructNatInformation : No SNAT IP Mapping found for router-uuid {}", routerUuid.getValue());
} else {
// Capturing SNAT information
List<SnatIpMapping> snatIpMapping = new ArrayList<SnatIpMapping>();
for (IntextIpProtocolType protocolType : ipPortMapping.getIntextIpProtocolType()) {
for (IpPortMap ipPortMap : protocolType.getIpPortMap()) {
String[] internalPortMap = ipPortMap.getIpPortInternal().split(NwConstants.MACADDR_SEP);
SnatIpMappingBuilder natIpMappingBuilder = new SnatIpMappingBuilder().setInternalIp(internalPortMap[0]).setInternalPort(internalPortMap[1]).setExternalIp(ipPortMap.getIpPortExternal().getIpAddress()).setExternalPort(ipPortMap.getIpPortExternal().getPortNum().toString()).setProtocol(protocolType.getProtocol().getName());
snatIpMapping.add(natIpMappingBuilder.build());
}
}
natRouterBuilder.setSnatIpMapping(snatIpMapping);
}
// Capturing DNAT information
List<DnatIpMapping> dnatIpMapping = new ArrayList<DnatIpMapping>();
List<Ports> fipPorts = NatUtil.getFloatingIpPortsForRouter(dataBroker, routerUuid);
if (fipPorts.isEmpty()) {
LOG.warn("constructNatInformation : No DNAT IP Mapping found for router-uuid {}", routerUuid.getValue());
} else {
for (Ports fipPort : fipPorts) {
List<InternalToExternalPortMap> ipMapping = fipPort.getInternalToExternalPortMap();
for (InternalToExternalPortMap fipMap : ipMapping) {
DnatIpMappingBuilder natIpMappingBuilder = new DnatIpMappingBuilder().setExternalIp(fipMap.getExternalIp()).setInternalIp(fipMap.getInternalIp());
dnatIpMapping.add(natIpMappingBuilder.build());
}
}
natRouterBuilder.setDnatIpMapping(dnatIpMapping);
}
List<RouterNat> natRouterList = new ArrayList<RouterNat>();
natRouterList.add(natRouterBuilder.build());
return natRouterList;
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.flowspec.flowspec.type.port._case.Ports in project netvirt by opendaylight.
the class NaptManager method getExternalAddressMapping.
/**
* method to get external ip/port mapping when provided with internal ip/port pair
* If already a mapping exist for the given input, then the existing mapping is returned
* instead of overwriting with new ip/port pair.
*
* @param segmentId - Router ID
* @param sourceAddress - internal ip address/port pair
* @param protocol - TCP/UDP
* @return external ip address/port
*/
// TODO Clean up the exception handling
@SuppressWarnings("checkstyle:IllegalCatch")
public SessionAddress getExternalAddressMapping(long segmentId, SessionAddress sourceAddress, NAPTEntryEvent.Protocol protocol) {
LOG.debug("getExternalAddressMapping : called with segmentId {}, internalIp {} and port {}", segmentId, sourceAddress.getIpAddress(), sourceAddress.getPortNumber());
/*
1. Get Internal IP, Port in IP:Port format
2. Inside DB with routerId get the list of entries and check if it matches with existing IP:Port
3. If True return SessionAddress of ExternalIp and Port
4. Else check ip Map and Form the ExternalIp and Port and update DB and then return ExternalIp and Port
*/
// SessionAddress externalIpPort = new SessionAddress();
String internalIpPort = sourceAddress.getIpAddress() + ":" + sourceAddress.getPortNumber();
// First check existing Port Map.
SessionAddress existingIpPort = checkIpPortMap(segmentId, internalIpPort, protocol);
if (existingIpPort != null) {
// populate externalIpPort from IpPortMap and return
LOG.debug("getExternalAddressMapping : successfully returning existingIpPort as {} and {}", existingIpPort.getIpAddress(), existingIpPort.getPortNumber());
return existingIpPort;
} else {
// Now check in ip-map
String externalIp = checkIpMap(segmentId, sourceAddress.getIpAddress());
if (externalIp == null) {
LOG.error("getExternalAddressMapping : Unexpected error, internal to external " + "ip map does not exist");
return null;
} else {
/* Logic assuming internalIp is always ip and not subnet
* case 1: externalIp is ip
* a) goto externalIp pool and getPort and return
* b) else return error
* case 2: externalIp is subnet
* a) Take first externalIp and goto that Pool and getPort
* if port -> return
* else Take second externalIp and create that Pool and getPort
* if port ->return
* else
* Continue same with third externalIp till we exhaust subnet
* b) Nothing worked return error
*/
SubnetUtils externalIpSubnet;
List<String> allIps = new ArrayList<>();
String subnetPrefix = "/" + String.valueOf(NatConstants.DEFAULT_PREFIX);
boolean extSubnetFlag = false;
if (!externalIp.contains(subnetPrefix)) {
extSubnetFlag = true;
externalIpSubnet = new SubnetUtils(externalIp);
allIps = Arrays.asList(externalIpSubnet.getInfo().getAllAddresses());
LOG.debug("getExternalAddressMapping : total count of externalIps available {}", externalIpSubnet.getInfo().getAddressCount());
} else {
LOG.debug("getExternalAddressMapping : getExternalAddress single ip case");
if (externalIp.contains(subnetPrefix)) {
// remove /32 what we got from checkIpMap
externalIp = externalIp.substring(0, externalIp.indexOf(subnetPrefix));
}
allIps.add(externalIp);
}
boolean nextExtIpFlag = false;
for (String extIp : allIps) {
LOG.info("getExternalAddressMapping : Looping externalIPs with externalIP now as {}", extIp);
if (nextExtIpFlag) {
createNaptPortPool(extIp);
LOG.debug("getExternalAddressMapping : Created Pool for next Ext IP {}", extIp);
}
AllocateIdInput getIdInput = new AllocateIdInputBuilder().setPoolName(extIp).setIdKey(internalIpPort).build();
try {
Future<RpcResult<AllocateIdOutput>> result = idManager.allocateId(getIdInput);
RpcResult<AllocateIdOutput> rpcResult;
if (result != null && result.get().isSuccessful()) {
LOG.debug("getExternalAddressMapping : Got id from idManager");
rpcResult = result.get();
} else {
LOG.error("getExternalAddressMapping : getExternalAddressMapping, idManager could not " + "allocate id retry if subnet");
if (!extSubnetFlag) {
LOG.error("getExternalAddressMapping : getExternalAddressMapping returning null " + "for single IP case, may be ports exhausted");
return null;
}
LOG.debug("getExternalAddressMapping : Could be ports exhausted case, " + "try with another externalIP if possible");
nextExtIpFlag = true;
continue;
}
int extPort = rpcResult.getResult().getIdValue().intValue();
// Write to ip-port-map before returning
IpPortExternalBuilder ipExt = new IpPortExternalBuilder();
IpPortExternal ipPortExt = ipExt.setIpAddress(extIp).setPortNum(extPort).build();
IpPortMap ipm = new IpPortMapBuilder().setKey(new IpPortMapKey(internalIpPort)).setIpPortInternal(internalIpPort).setIpPortExternal(ipPortExt).build();
LOG.debug("getExternalAddressMapping : writing into ip-port-map with " + "externalIP {} and port {}", ipPortExt.getIpAddress(), ipPortExt.getPortNum());
try {
MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION, getIpPortMapIdentifier(segmentId, internalIpPort, protocol), ipm);
} catch (UncheckedExecutionException uee) {
LOG.error("getExternalAddressMapping : Failed to write into ip-port-map with exception", uee);
}
// Write to snat-internal-ip-port-info
String internalIpAddress = sourceAddress.getIpAddress();
int ipPort = sourceAddress.getPortNumber();
ProtocolTypes protocolType = NatUtil.getProtocolType(protocol);
List<Integer> portList = new ArrayList<>(NatUtil.getInternalIpPortListInfo(dataBroker, segmentId, internalIpAddress, protocolType));
portList.add(ipPort);
IntIpProtoTypeBuilder builder = new IntIpProtoTypeBuilder();
IntIpProtoType intIpProtocolType = builder.setKey(new IntIpProtoTypeKey(protocolType)).setPorts(portList).build();
try {
MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION, NatUtil.buildSnatIntIpPortIdentifier(segmentId, internalIpAddress, protocolType), intIpProtocolType);
} catch (Exception ex) {
LOG.error("getExternalAddressMapping : Failed to write into snat-internal-ip-port-info " + "with exception", ex);
}
SessionAddress externalIpPort = new SessionAddress(extIp, extPort);
LOG.debug("getExternalAddressMapping : successfully returning externalIP {} " + "and port {}", externalIpPort.getIpAddress(), externalIpPort.getPortNumber());
return externalIpPort;
} catch (InterruptedException | ExecutionException e) {
LOG.error("getExternalAddressMapping : Exception caught", e);
return null;
}
}
// end of for loop
}
// end of else ipmap present
}
// end of else check ipmap
LOG.error("getExternalAddressMapping : Unable to handle external IP address and port mapping with segmentId {}," + "internalIp {} and internalPort {}", segmentId, sourceAddress.getIpAddress(), sourceAddress.getPortNumber());
return null;
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.flowspec.destination.flowspec.flowspec.type.port._case.Ports in project netvirt by opendaylight.
the class DhcpPktHandler method onPacketReceived.
// TODO: Handle this in a separate thread
@Override
public void onPacketReceived(PacketReceived packet) {
if (!config.isControllerDhcpEnabled()) {
return;
}
Class<? extends PacketInReason> pktInReason = packet.getPacketInReason();
short tableId = packet.getTableId().getValue();
if ((tableId == NwConstants.DHCP_TABLE || tableId == NwConstants.DHCP_TABLE_EXTERNAL_TUNNEL) && isPktInReasonSendtoCtrl(pktInReason)) {
byte[] inPayload = packet.getPayload();
Ethernet ethPkt = new Ethernet();
try {
ethPkt.deserialize(inPayload, 0, inPayload.length * NetUtils.NUM_BITS_IN_A_BYTE);
} catch (PacketException e) {
LOG.warn("Failed to decode DHCP Packet.", e);
LOG.trace("Received packet {}", packet);
return;
}
DHCP pktIn;
pktIn = getDhcpPktIn(ethPkt);
if (pktIn != null) {
LOG.trace("DHCPPkt received: {}", pktIn);
LOG.trace("Received Packet: {}", packet);
BigInteger metadata = packet.getMatch().getMetadata().getMetadata();
long portTag = MetaDataUtil.getLportFromMetadata(metadata).intValue();
String macAddress = DHCPUtils.byteArrayToString(ethPkt.getSourceMACAddress());
BigInteger tunnelId = packet.getMatch().getTunnel() == null ? null : packet.getMatch().getTunnel().getTunnelId();
String interfaceName = getInterfaceNameFromTag(portTag);
InterfaceInfo interfaceInfo = interfaceManager.getInterfaceInfoFromOperationalDataStore(interfaceName);
if (interfaceInfo == null) {
LOG.error("Failed to get interface info for interface name {}", interfaceName);
return;
}
Port port;
if (tunnelId != null) {
port = dhcpExternalTunnelManager.readVniMacToPortCache(tunnelId, macAddress);
} else {
port = getNeutronPort(interfaceName);
}
Subnet subnet = getNeutronSubnet(port);
String serverMacAddress = interfaceInfo.getMacAddress();
String serverIp = null;
if (subnet != null) {
java.util.Optional<SubnetToDhcpPort> dhcpPortData = DhcpServiceUtils.getSubnetDhcpPortData(broker, subnet.getUuid().getValue());
/* If enable_dhcp_service flag was enabled and an ODL network DHCP Port data was made available use
* the ports Fixed IP as server IP for DHCP communication.
*/
if (dhcpPortData.isPresent()) {
serverIp = dhcpPortData.get().getPortFixedip();
serverMacAddress = dhcpPortData.get().getPortMacaddress();
} else {
// DHCP Neutron Port not found for this network
LOG.error("Neutron DHCP port is not available for the Subnet {} and port {}.", subnet.getUuid(), port.getUuid());
return;
}
}
DHCP replyPkt = handleDhcpPacket(pktIn, interfaceName, macAddress, port, subnet, serverIp);
if (replyPkt == null) {
LOG.warn("Unable to construct reply packet for interface name {}", interfaceName);
return;
}
byte[] pktOut = getDhcpPacketOut(replyPkt, ethPkt, serverMacAddress);
sendPacketOut(pktOut, interfaceInfo.getDpId(), interfaceName, tunnelId);
}
}
}
Aggregations