use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev200120.RouterId in project netvirt by opendaylight.
the class FloatingIPListener method updateOperationalDS.
static void updateOperationalDS(DataBroker dataBroker, String routerId, String interfaceName, Uint32 label, String internalIp, String externalIp) {
LOG.info("updateOperationalDS : Updating operational DS for floating ip config : {} with label {}", internalIp, label);
InstanceIdentifier<Ports> portsId = NatUtil.getPortsIdentifier(routerId, interfaceName);
Optional<Ports> optPorts = SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(dataBroker, LogicalDatastoreType.OPERATIONAL, portsId);
InternalToExternalPortMap intExtPortMap = new InternalToExternalPortMapBuilder().withKey(new InternalToExternalPortMapKey(internalIp)).setInternalIp(internalIp).setExternalIp(externalIp).setLabel(label).build();
if (optPorts.isPresent()) {
LOG.debug("updateOperationalDS : Ports {} entry already present. Updating intExtPortMap for internal ip {}", interfaceName, internalIp);
MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.OPERATIONAL, portsId.child(InternalToExternalPortMap.class, new InternalToExternalPortMapKey(internalIp)), intExtPortMap);
} else {
LOG.debug("updateOperationalDS : Adding Ports entry {} along with intExtPortMap {}", interfaceName, internalIp);
List<InternalToExternalPortMap> intExtPortMapList = new ArrayList<>();
intExtPortMapList.add(intExtPortMap);
Ports ports = new PortsBuilder().withKey(new PortsKey(interfaceName)).setPortName(interfaceName).setInternalToExternalPortMap(intExtPortMapList).build();
MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.OPERATIONAL, portsId, ports);
}
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev200120.RouterId in project netvirt by opendaylight.
the class NaptEventHandler method prepareAndSendPacketOut.
private void prepareAndSendPacketOut(NAPTEntryEvent naptEntryEvent, Uint32 routerId, String sourceIPPortKey) {
// Send Packetout - tcp or udp packets which got punted to controller.
Uint64 metadata = naptEntryEvent.getPacketReceived().getMatch().getMetadata().getMetadata();
byte[] inPayload = naptEntryEvent.getPacketReceived().getPayload();
Ethernet ethPkt = new Ethernet();
if (inPayload != null) {
try {
ethPkt.deserialize(inPayload, 0, inPayload.length * Byte.SIZE);
} catch (PacketException e) {
NaptPacketInHandler.removeIncomingPacketMap(sourceIPPortKey);
LOG.error("prepareAndSendPacketOut : Failed to decode Packet", e);
return;
}
}
long portTag = MetaDataUtil.getLportFromMetadata(metadata).intValue();
LOG.debug("prepareAndSendPacketOut : portTag from incoming packet is {}", portTag);
List<ActionInfo> actionInfos = new ArrayList<>();
String interfaceName = getInterfaceNameFromTag(portTag);
Uint64 dpnID = null;
int portNum = -1;
if (interfaceName != null) {
LOG.debug("prepareAndSendPacketOut : interfaceName fetched from portTag is {}", interfaceName);
org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface = null;
int vlanId = 0;
iface = interfaceManager.getInterfaceInfoFromConfigDataStore(interfaceName);
if (iface == null) {
NaptPacketInHandler.removeIncomingPacketMap(sourceIPPortKey);
LOG.error("prepareAndSendPacketOut : Unable to read interface {} from config DataStore", interfaceName);
return;
}
IfL2vlan ifL2vlan = iface.augmentation(IfL2vlan.class);
if (ifL2vlan != null && ifL2vlan.getVlanId() != null) {
vlanId = ifL2vlan.getVlanId().getValue() == null ? 0 : ifL2vlan.getVlanId().getValue().toJava();
}
InterfaceInfo infInfo = interfaceManager.getInterfaceInfoFromOperationalDataStore(interfaceName);
if (infInfo == null) {
LOG.error("prepareAndSendPacketOut : error in getting interfaceInfo from Operation DS");
return;
}
dpnID = infInfo.getDpId();
portNum = infInfo.getPortNo();
if (ethPkt.getEtherType() != (short) NwConstants.ETHTYPE_802_1Q) {
// VLAN Access port
LOG.debug("prepareAndSendPacketOut : vlanId is {}", vlanId);
if (vlanId != 0) {
// Push vlan
actionInfos.add(new ActionPushVlan(0));
actionInfos.add(new ActionSetFieldVlanVid(1, vlanId));
} else {
LOG.debug("prepareAndSendPacketOut : No vlanId {}, may be untagged", vlanId);
}
} else {
// VLAN Trunk Port
LOG.debug("prepareAndSendPacketOut : This is VLAN Trunk port case - need not do VLAN tagging again");
}
} else {
// This case will be hit for packets send from non-napt switch.
LOG.info("prepareAndSendPacketOut : interfaceName is not available.Retrieve from packet received");
NodeConnectorId nodeId = naptEntryEvent.getPacketReceived().getMatch().getInPort();
portNum = Integer.parseInt(nodeId.getValue());
LOG.debug("prepareAndSendPacketOut : in_port portNum : {}", portNum);
// List<PathArgument> dpnNodes = naptEntryEvent.getPacketReceived().getIngress().getValue().getPath();
Iterable<PathArgument> outArgs = naptEntryEvent.getPacketReceived().getIngress().getValue().getPathArguments();
PathArgument pathArgument = Iterables.get(outArgs, 2);
LOG.debug("prepareAndSendPacketOut : pathArgument : {}", pathArgument);
InstanceIdentifier.IdentifiableItem<?, ?> item = Arguments.checkInstanceOf(pathArgument, InstanceIdentifier.IdentifiableItem.class);
NodeConnectorKey key = Arguments.checkInstanceOf(item.getKey(), NodeConnectorKey.class);
LOG.info("prepareAndSendPacketOut : NodeConnectorKey key : {}", key.getId().getValue());
String dpnKey = key.getId().getValue();
if (dpnKey.contains(NatConstants.COLON_SEPARATOR)) {
dpnID = Uint64.valueOf(dpnKey.split(NatConstants.COLON_SEPARATOR)[1]).intern();
}
}
byte[] pktOut = buildNaptPacketOut(ethPkt);
if (pktOut != null) {
String routerName = NatUtil.getRouterName(dataBroker, routerId);
Uint64 tunId = NatUtil.getTunnelIdForNonNaptToNaptFlow(dataBroker, natOverVxlanUtil, elanManager, idManager, routerId, routerName);
LOG.info("sendNaptPacketOut for ({}:{}) on dpnId {} portNum {} tunId {}", naptEntryEvent.getIpAddress(), naptEntryEvent.getPortNumber(), dpnID, portNum, tunId);
sendNaptPacketOut(pktOut, dpnID, portNum, actionInfos, tunId);
} else {
LOG.warn("prepareAndSendPacketOut : Unable to send Packet Out");
}
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev200120.RouterId 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")
@Nullable
public SessionAddress getExternalAddressMapping(Uint32 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;
}
// 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;
}
/* 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);
}
Uint32 extPort = NatUtil.getUniqueId(idManager, extIp, internalIpPort);
if (extPort == NatConstants.INVALID_ID) {
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;
}
// Write to ip-port-map before returning
IpPortExternalBuilder ipExt = new IpPortExternalBuilder();
IpPortExternal ipPortExt = ipExt.setIpAddress(extIp).setPortNum(extPort.intValue()).build();
IpPortMap ipm = new IpPortMapBuilder().withKey(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);
final ReentrantLock lock = lockFor(segmentId, internalIpAddress, protocolType);
lock.lock();
try {
List<Uint16> portList = new ArrayList<>(NatUtil.getInternalIpPortListInfo(dataBroker, segmentId, internalIpAddress, protocolType));
portList.add(Uint16.valueOf(ipPort));
IntIpProtoTypeBuilder builder = new IntIpProtoTypeBuilder();
IntIpProtoType intIpProtocolType = builder.withKey(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);
}
} finally {
lock.unlock();
}
SessionAddress externalIpPort = new SessionAddress(extIp, extPort.intValue());
LOG.debug("getExternalAddressMapping : successfully returning externalIP {} " + "and port {}", externalIpPort.getIpAddress(), externalIpPort.getPortNumber());
return externalIpPort;
}
// end of for loop
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.bmp.monitor.rev200120.RouterId in project netvirt by opendaylight.
the class NaptManager method initialiseExternalCounter.
protected void initialiseExternalCounter(Routers routers, Uint32 routerId) {
LOG.debug("initialiseExternalCounter : Initialise External IPs counter");
// update the new counter value for this externalIp
for (ExternalIps externalIp : routers.nonnullExternalIps().values()) {
String[] ipSplit = externalIp.getIpAddress().split("/");
String extIp = ipSplit[0];
String extPrefix = Short.toString(NatConstants.DEFAULT_PREFIX);
if (ipSplit.length == 2) {
extPrefix = ipSplit[1];
}
extIp = extIp + "/" + extPrefix;
initialiseNewExternalIpCounter(routerId, extIp);
}
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.rev200120.RouterId in project netvirt by opendaylight.
the class ExternalRoutersListener method remove.
@Override
public void remove(InstanceIdentifier<Routers> identifier, Routers router) {
if (natMode != NatMode.Controller) {
return;
}
LOG.trace("remove : Router delete method");
if (identifier == null || router == null) {
LOG.error("remove : returning without processing since routers is null");
return;
}
String routerName = router.getRouterName();
coordinator.enqueueJob(NatConstants.NAT_DJC_PREFIX + router.key(), () -> Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, tx -> {
LOG.info("remove : Removing default NAT route from FIB on all dpns part of router {} ", routerName);
Uint32 routerId = NatUtil.getVpnId(dataBroker, routerName);
if (routerId == NatConstants.INVALID_ID) {
LOG.error("remove : Remove external router event - Invalid routerId for routerName {}", routerName);
return;
}
Uint32 bgpVpnId = NatConstants.INVALID_ID;
Uuid bgpVpnUuid = NatUtil.getVpnForRouter(dataBroker, routerName);
if (bgpVpnUuid != null) {
bgpVpnId = NatUtil.getVpnId(dataBroker, bgpVpnUuid.getValue());
}
addOrDelDefFibRouteToSNAT(routerName, routerId, bgpVpnId, bgpVpnUuid, false, tx);
Uuid networkUuid = router.getNetworkId();
Uint64 primarySwitchId = NatUtil.getPrimaryNaptfromRouterName(dataBroker, routerName);
if (primarySwitchId == null || primarySwitchId.equals(Uint64.ZERO)) {
// No NAPT switch for external router, probably because the router is not attached to
// any
// internal networks
LOG.debug("No NAPT switch for router {}, check if router is attached to any internal " + "network", routerName);
return;
} else {
Collection<String> externalIps = NatUtil.getExternalIpsForRouter(dataBroker, routerId);
final String vpnName = NatUtil.getAssociatedVPN(dataBroker, networkUuid);
handleDisableSnat(router, networkUuid, externalIps, true, vpnName, primarySwitchId, routerId, tx);
}
if (NatUtil.releaseId(idManager, NatConstants.ODL_VNI_POOL_NAME, routerName) == NatConstants.INVALID_ID) {
LOG.error("remove: Unable to release VNI for router - {}", routerName);
}
})), NatConstants.NAT_DJC_MAX_RETRIES);
}
Aggregations