use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.object.Of in project netvirt by opendaylight.
the class RouterDpnChangeListener method removeSNATFromDPN.
// TODO Clean up the exception handling
@SuppressWarnings("checkstyle:IllegalCatch")
void removeSNATFromDPN(BigInteger dpnId, String routerName, long routerId, long routerVpnId, Uuid extNetworkId, WriteTransaction removeFlowInvTx) {
// irrespective of naptswitch or non-naptswitch, SNAT default miss entry need to be removed
// remove miss entry to NAPT switch
// if naptswitch elect new switch and install Snat flows and remove those flows in oldnaptswitch
Collection<String> externalIpCache = NatUtil.getExternalIpsForRouter(dataBroker, routerId);
ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker, routerName, extNetworkId);
if (extNwProvType == null) {
return;
}
// Get the external IP labels other than VXLAN provider type. Since label is not applicable for VXLAN
Map<String, Long> externalIpLabel;
if (extNwProvType == ProviderTypes.VXLAN) {
externalIpLabel = null;
} else {
externalIpLabel = NatUtil.getExternalIpsLabelForRouter(dataBroker, routerId);
}
BigInteger naptSwitch = NatUtil.getPrimaryNaptfromRouterName(dataBroker, routerName);
if (naptSwitch == null || naptSwitch.equals(BigInteger.ZERO)) {
LOG.error("removeSNATFromDPN : No naptSwitch is selected for router {}", routerName);
return;
}
try {
boolean naptStatus = naptSwitchHA.isNaptSwitchDown(routerName, routerId, dpnId, naptSwitch, routerVpnId, externalIpCache, removeFlowInvTx);
if (!naptStatus) {
LOG.debug("removeSNATFromDPN: Switch with DpnId {} is not naptSwitch for router {}", dpnId, routerName);
long groupId = NatUtil.createGroupId(NatUtil.getGroupIdKey(routerName), idManager);
FlowEntity flowEntity = null;
try {
flowEntity = naptSwitchHA.buildSnatFlowEntity(dpnId, routerName, groupId, routerVpnId, NatConstants.DEL_FLOW);
if (flowEntity == null) {
LOG.error("removeSNATFromDPN : Failed to populate flowentity for router:{} " + "with dpnId:{} groupId:{}", routerName, dpnId, groupId);
return;
}
LOG.debug("removeSNATFromDPN : Removing default SNAT miss entry flow entity {}", flowEntity);
mdsalManager.removeFlowToTx(flowEntity, removeFlowInvTx);
} catch (Exception ex) {
LOG.error("removeSNATFromDPN : Failed to remove default SNAT miss entry flow entity {}", flowEntity, ex);
return;
}
LOG.debug("removeSNATFromDPN : Removed default SNAT miss entry flow for dpnID {} with routername {}", dpnId, routerName);
// remove group
GroupEntity groupEntity = null;
try {
groupEntity = MDSALUtil.buildGroupEntity(dpnId, groupId, routerName, GroupTypes.GroupAll, Collections.emptyList());
LOG.info("removeSNATFromDPN : Removing NAPT GroupEntity:{}", groupEntity);
mdsalManager.removeGroup(groupEntity);
} catch (Exception ex) {
LOG.error("removeSNATFromDPN : Failed to remove group entity {}", groupEntity, ex);
return;
}
LOG.debug("removeSNATFromDPN : Removed default SNAT miss entry flow for dpnID {} with routerName {}", dpnId, routerName);
} else {
naptSwitchHA.removeSnatFlowsInOldNaptSwitch(routerName, routerId, naptSwitch, externalIpLabel, removeFlowInvTx);
// remove table 26 flow ppointing to table46
FlowEntity flowEntity = null;
try {
flowEntity = naptSwitchHA.buildSnatFlowEntityForNaptSwitch(dpnId, routerName, routerVpnId, NatConstants.DEL_FLOW);
if (flowEntity == null) {
LOG.error("removeSNATFromDPN : Failed to populate flowentity for router {} with dpnId {}", routerName, dpnId);
return;
}
LOG.debug("removeSNATFromDPN : Removing default SNAT miss entry flow entity for router {} with " + "dpnId {} in napt switch {}", routerName, dpnId, naptSwitch);
mdsalManager.removeFlowToTx(flowEntity, removeFlowInvTx);
} catch (Exception ex) {
LOG.error("removeSNATFromDPN : Failed to remove default SNAT miss entry flow entity {}", flowEntity, ex);
return;
}
LOG.debug("removeSNATFromDPN : Removed default SNAT miss entry flow for dpnID {} with routername {}", dpnId, routerName);
// best effort to check IntExt model
naptSwitchHA.bestEffortDeletion(routerId, routerName, externalIpLabel, removeFlowInvTx);
}
} catch (Exception ex) {
LOG.error("removeSNATFromDPN : Exception while handling naptSwitch down for router {}", routerName, ex);
}
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.object.Of in project netvirt by opendaylight.
the class InterfaceStateEventListener method getRouterIdForPort.
// TODO Clean up the exception handling
@SuppressWarnings("checkstyle:IllegalCatch")
private String getRouterIdForPort(String interfaceName) {
String routerName = null;
VpnInterface vpnInterface = null;
try {
vpnInterface = NatUtil.getConfiguredVpnInterface(dataBroker, interfaceName);
} catch (Exception ex) {
LOG.error("getRouterIdForPort : Unable to process for interface {} as it is not configured", interfaceName, ex);
}
if (vpnInterface != null) {
// getVpnName
if (vpnInterface.getVpnInstanceNames() == null) {
LOG.debug("getRouterIdForPort : vpnName not found for vpnInterface {} of port {}", vpnInterface, interfaceName);
} else {
for (VpnInstanceNames vpnInstance : vpnInterface.getVpnInstanceNames()) {
String vpnName = vpnInstance.getVpnName();
try {
routerName = NatUtil.getRouterIdfromVpnInstance(dataBroker, vpnName);
} catch (Exception e) {
LOG.error("getRouterIdForPort : Unable to get routerId for vpnName {}", vpnName, e);
}
if (routerName != null) {
// check router is associated to external network
if (NatUtil.isSnatEnabledForRouterId(dataBroker, routerName)) {
LOG.debug("getRouterIdForPort : Retreived Router Id {} for vpnname {} " + "associated to interface {}", routerName, vpnName, interfaceName);
return routerName;
} else {
LOG.warn("getRouterIdForPort : Interface {} associated to routerId {} is not " + "associated to external network", interfaceName, routerName);
}
} else {
LOG.warn("getRouterIdForPort : Router is not associated to vpnname {} for interface {}", vpnName, interfaceName);
}
}
}
} else {
LOG.debug("getRouterIdForPort : Interface {} is not a vpninterface", interfaceName);
}
return null;
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.object.Of in project netvirt by opendaylight.
the class InterfaceStateEventListener method removeSnatEntriesForPort.
// TODO Clean up the exception handling
@SuppressWarnings("checkstyle:IllegalCatch")
private void removeSnatEntriesForPort(String interfaceName, String routerName) {
Long routerId = NatUtil.getVpnId(dataBroker, routerName);
if (routerId == NatConstants.INVALID_ID) {
LOG.error("removeSnatEntriesForPort : routerId not found for routername {}", routerName);
return;
}
BigInteger naptSwitch = getNaptSwitchforRouter(dataBroker, routerName);
if (naptSwitch == null || naptSwitch.equals(BigInteger.ZERO)) {
LOG.error("removeSnatEntriesForPort : NaptSwitch is not elected for router {} with Id {}", routerName, routerId);
return;
}
// getInternalIp for port
List<String> fixedIps = getFixedIpsForPort(interfaceName);
if (fixedIps == null) {
LOG.warn("removeSnatEntriesForPort : Internal Ips not found for InterfaceName {} in router {} with id {}", interfaceName, routerName, routerId);
return;
}
for (String internalIp : fixedIps) {
LOG.debug("removeSnatEntriesForPort : Internal Ip retrieved for interface {} is {} in router with Id {}", interfaceName, internalIp, routerId);
IpPort ipPort = NatUtil.getInternalIpPortInfo(dataBroker, routerId, internalIp);
if (ipPort == null) {
LOG.debug("removeSnatEntriesForPort : no snatint-ip-port-map found for ip:{}", internalIp);
continue;
}
for (IntIpProtoType protoType : ipPort.getIntIpProtoType()) {
ProtocolTypes protocol = protoType.getProtocol();
for (Integer portnum : protoType.getPorts()) {
// build and remove the flow in outbound table
try {
removeNatFlow(naptSwitch, NwConstants.OUTBOUND_NAPT_TABLE, routerId, internalIp, portnum);
} catch (Exception ex) {
LOG.error("removeSnatEntriesForPort : Failed to remove snat flow for internalIP {} with " + "Port {} protocol {} for routerId {} in OUTBOUNDTABLE of NaptSwitch {}", internalIp, portnum, protocol, routerId, naptSwitch, ex);
}
// Get the external IP address and the port from the model
NAPTEntryEvent.Protocol proto = protocol.toString().equals(ProtocolTypes.TCP.toString()) ? NAPTEntryEvent.Protocol.TCP : NAPTEntryEvent.Protocol.UDP;
IpPortExternal ipPortExternal = NatUtil.getExternalIpPortMap(dataBroker, routerId, internalIp, String.valueOf(portnum), proto);
if (ipPortExternal == null) {
LOG.error("removeSnatEntriesForPort : Mapping for internalIp {} with port {} is not found in " + "router with Id {}", internalIp, portnum, routerId);
return;
}
String externalIpAddress = ipPortExternal.getIpAddress();
Integer portNumber = ipPortExternal.getPortNum();
// build and remove the flow in inboundtable
try {
removeNatFlow(naptSwitch, NwConstants.INBOUND_NAPT_TABLE, routerId, externalIpAddress, portNumber);
} catch (Exception ex) {
LOG.error("removeSnatEntriesForPort : Failed to remove snat flow internalIP {} with " + "Port {} protocol {} for routerId {} in INBOUNDTABLE of naptSwitch {}", externalIpAddress, portNumber, protocol, routerId, naptSwitch, ex);
}
String internalIpPort = internalIp + ":" + portnum;
// delete the entry from IntExtIpPortMap DS
try {
naptManager.removeFromIpPortMapDS(routerId, internalIpPort, proto);
naptManager.removePortFromPool(internalIpPort, externalIpAddress);
} catch (Exception ex) {
LOG.error("removeSnatEntriesForPort : releaseIpExtPortMapping failed, Removal of " + "ipportmap {} for router {} failed", internalIpPort, routerId, ex);
}
}
}
// delete the entry from SnatIntIpPortMap DS
LOG.debug("removeSnatEntriesForPort : Removing InternalIp:{} on router {}", internalIp, routerId);
naptManager.removeFromSnatIpPortDS(routerId, internalIp);
}
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.object.Of in project netvirt by opendaylight.
the class NAPTSwitchSelector method selectNewNAPTSwitch.
BigInteger selectNewNAPTSwitch(String routerName) {
LOG.info("selectNewNAPTSwitch : Select a new NAPT switch for router {}", routerName);
Map<BigInteger, Integer> naptSwitchWeights = constructNAPTSwitches();
List<BigInteger> routerSwitches = getDpnsForVpn(routerName);
if (routerSwitches.isEmpty()) {
LOG.warn("selectNewNAPTSwitch : Delaying NAPT switch selection due to no dpns scenario for router {}", routerName);
return BigInteger.ZERO;
}
Set<SwitchWeight> switchWeights = new TreeSet<>();
for (BigInteger dpn : routerSwitches) {
if (naptSwitchWeights.get(dpn) != null) {
switchWeights.add(new SwitchWeight(dpn, naptSwitchWeights.get(dpn)));
} else {
switchWeights.add(new SwitchWeight(dpn, 0));
}
}
BigInteger primarySwitch;
if (!switchWeights.isEmpty()) {
LOG.debug("selectNewNAPTSwitch : Current switch weights for router {} - {}", routerName, switchWeights);
RouterToNaptSwitchBuilder routerToNaptSwitchBuilder = new RouterToNaptSwitchBuilder().setRouterName(routerName);
SwitchWeight firstSwitchWeight = switchWeights.iterator().next();
primarySwitch = firstSwitchWeight.getSwitch();
RouterToNaptSwitch id = routerToNaptSwitchBuilder.setPrimarySwitchId(primarySwitch).build();
MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION, getNaptSwitchesIdentifier(routerName), id);
LOG.debug("selectNewNAPTSwitch : successful addition of RouterToNaptSwitch to napt-switches container");
return primarySwitch;
} else {
primarySwitch = BigInteger.ZERO;
LOG.debug("selectNewNAPTSwitch : switchWeights empty, primarySwitch: {} ", primarySwitch);
return primarySwitch;
}
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.object.Of in project netvirt by opendaylight.
the class NaptFlowRemovedEventHandler method onFlowRemoved.
@Override
public void onFlowRemoved(FlowRemoved flowRemoved) {
/*
If the removed flow is from the OUTBOUND NAPT table :
1) Get the ActionInfo of the flow.
2) From the ActionInfo of the flow get the internal IP address, port and the protocol.
3) Get the Metadata matching info of the flow.
4) From the Metadata matching info of the flow get router ID.
5) Querry the container intext-ip-port-map using the router ID
and the internal IP address, port to get the external IP address, port
6) Instantiate an NaptEntry event and populate the external IP address, port and the router ID.
7) Place the NaptEntry event to the queue.
*/
short tableId = flowRemoved.getTableId();
RemovedFlowReason removedReasonFlag = flowRemoved.getReason();
if (tableId == NwConstants.OUTBOUND_NAPT_TABLE && RemovedFlowReason.OFPRRIDLETIMEOUT.equals(removedReasonFlag)) {
LOG.info("onFlowRemoved : triggered for table-{} entry", tableId);
// Get the internal internal IP address and the port number from the IPv4 match.
Ipv4Prefix internalIpv4Address = null;
Layer3Match layer3Match = flowRemoved.getMatch().getLayer3Match();
if (layer3Match instanceof Ipv4Match) {
Ipv4Match internalIpv4Match = (Ipv4Match) layer3Match;
internalIpv4Address = internalIpv4Match.getIpv4Source();
}
if (internalIpv4Address == null) {
LOG.error("onFlowRemoved : Matching internal IP is null while retrieving the " + "value from the Outbound NAPT flow");
return;
}
// Get the internal IP as a string
String internalIpv4AddressAsString = internalIpv4Address.getValue();
String[] internalIpv4AddressParts = internalIpv4AddressAsString.split("/");
String internalIpv4HostAddress = null;
if (internalIpv4AddressParts.length >= 1) {
internalIpv4HostAddress = internalIpv4AddressParts[0];
}
// Get the protocol from the layer4 match
NAPTEntryEvent.Protocol protocol = null;
Integer internalPortNumber = null;
Layer4Match layer4Match = flowRemoved.getMatch().getLayer4Match();
if (layer4Match instanceof TcpMatch) {
TcpMatchFields tcpMatchFields = (TcpMatchFields) layer4Match;
internalPortNumber = tcpMatchFields.getTcpSourcePort().getValue();
protocol = NAPTEntryEvent.Protocol.TCP;
} else if (layer4Match instanceof UdpMatch) {
UdpMatchFields udpMatchFields = (UdpMatchFields) layer4Match;
internalPortNumber = udpMatchFields.getUdpSourcePort().getValue();
protocol = NAPTEntryEvent.Protocol.UDP;
}
if (protocol == null) {
LOG.error("onFlowRemoved : Matching protocol is null while retrieving the value " + "from the Outbound NAPT flow");
return;
}
// Get the router ID from the metadata.
Long routerId;
BigInteger metadata = flowRemoved.getMatch().getMetadata().getMetadata();
if (MetaDataUtil.getNatRouterIdFromMetadata(metadata) != 0) {
routerId = MetaDataUtil.getNatRouterIdFromMetadata(metadata);
} else {
LOG.error("onFlowRemoved : Null exception while retrieving routerId");
return;
}
final String internalIpPortKey = routerId + NatConstants.COLON_SEPARATOR + internalIpv4HostAddress + NatConstants.COLON_SEPARATOR + internalPortNumber;
// Get the external IP address and the port from the model
IpPortExternal ipPortExternal = NatUtil.getExternalIpPortMap(dataBroker, routerId, internalIpv4HostAddress, internalPortNumber.toString(), protocol);
if (ipPortExternal == null) {
LOG.error("onFlowRemoved : IpPortExternal not found, BGP vpn might be " + "associated with router");
// router must be associated with BGP vpn ID
long bgpVpnId = routerId;
LOG.debug("onFlowRemoved : BGP VPN ID {}", bgpVpnId);
String vpnName = NatUtil.getRouterName(dataBroker, bgpVpnId);
String routerName = NatUtil.getRouterIdfromVpnInstance(dataBroker, vpnName);
if (routerName == null) {
LOG.error("onFlowRemoved : Unable to find router for VpnName {}", vpnName);
return;
}
routerId = NatUtil.getVpnId(dataBroker, routerName);
LOG.debug("onFlowRemoved : Router ID {}", routerId);
ipPortExternal = NatUtil.getExternalIpPortMap(dataBroker, routerId, internalIpv4HostAddress, internalPortNumber.toString(), protocol);
if (ipPortExternal == null) {
LOG.error("onFlowRemoved : IpPortExternal is null while queried from the " + "model for routerId {}", routerId);
return;
}
}
String externalIpAddress = ipPortExternal.getIpAddress();
int externalPortNumber = ipPortExternal.getPortNum();
// Create an NAPT event and place it in the queue.
NAPTEntryEvent naptEntryEvent = new NAPTEntryEvent(externalIpAddress, externalPortNumber, routerId, NAPTEntryEvent.Operation.DELETE, protocol, null, false, null);
naptEventdispatcher.addFlowRemovedNaptEvent(naptEntryEvent);
// Get the DPN ID from the Node
InstanceIdentifier<Node> nodeRef = flowRemoved.getNode().getValue().firstIdentifierOf(Node.class);
String dpn = nodeRef.firstKeyOf(Node.class).getId().getValue();
BigInteger dpnId = getDpnId(dpn);
String switchFlowRef = NatUtil.getNaptFlowRef(dpnId, tableId, String.valueOf(routerId), internalIpv4HostAddress, internalPortNumber);
// Inform the MDSAL manager to inform about the flow removal.
LOG.debug("onFlowRemoved : DPN ID {}, Metadata {}, SwitchFlowRef {}, " + "internalIpv4HostAddress{}", dpnId, routerId, switchFlowRef, internalIpv4AddressAsString);
FlowEntity snatFlowEntity = NatUtil.buildFlowEntity(dpnId, tableId, switchFlowRef);
long startTime = System.currentTimeMillis();
mdsalManager.removeFlow(snatFlowEntity);
LOG.debug("onFlowRemoved : Elapsed time fo deleting table-{} flow for snat ({}) session:{}ms", tableId, internalIpPortKey, (System.currentTimeMillis() - startTime));
// Remove the SourceIP:Port key from the Napt packet handler map.
naptPacketInHandler.removeIncomingPacketMap(internalIpPortKey);
// Remove the mapping of internal fixed ip/port to external ip/port from the datastore.
SessionAddress internalSessionAddress = new SessionAddress(internalIpv4HostAddress, internalPortNumber);
naptManager.releaseIpExtPortMapping(routerId, internalSessionAddress, protocol);
LOG.info("onFlowRemoved : exit");
} else {
LOG.debug("onFlowRemoved : Received flow removed notification due to flowdelete from switch for flowref");
}
}
Aggregations