/** * Create an {@link IpNetwork} instance which represents the given IP * address. * * @param iaddr An {@link InetAddress} instance. * @return An {@link IpNetwork} instance which represents the given IP * address. Note that {@code null} is returned if {@code iaddr} * is {@code null}. * @throws IllegalArgumentException * The given IP address is invalid. */ public static final IpNetwork create(InetAddress iaddr) { return create(iaddr, 0); }
/** * Return a string representation of IP network. * * <p> * If this instance represents a specific IP host, an IP address of the * host without prefix length is returned. Otherwise a string which * represents the IP network in CIDR notation is returned. * </p> * * @return A string representation of IP network. */ public final String getText() { int max = getMaxPrefix(); return (prefixLength == max) ? getHostAddress() : getCidrText(); }
/** * Return a string representation of this object. * * @return A string representation of this object. */ @Override public final String toString() { String name = getClass().getSimpleName(); StringBuilder builder = new StringBuilder(name); return builder.append('[').append(getText()).append(']').toString(); } }
/** * Return an {@link InetAddress} instance which represents the IP network * address. * * @return An {@link InetAddress} instance. */ public final InetAddress getInetAddress() { InetAddress iaddr = inetAddress; if (iaddr == null) { iaddr = getInetAddress(getBytes()); inetAddress = iaddr; } return iaddr; }
List<VtnFlowMatch> matchList = new ArrayList<VtnFlowMatch>(); VlanId vlanId = new VlanId(0); IpNetwork ipaddrSrc = IpNetwork.create(InetAddress.getByName(addressSrc)); IpNetwork ipaddrDst = IpNetwork.create(InetAddress.getByName(addressDst)); MacAddress macAddress = null; VtnInetMatch match = new VtnInetMatchBuilder() .setDestinationNetwork(ipaddrDst.getIpPrefix()) .setSourceNetwork(ipaddrSrc.getIpPrefix()) .setProtocol((short)1) .setDscp(null)
/** * Create an {@link IpNetwork} instance which represents the IP network * specified by the given {@link Address} instance. * * @param addr An {@link Address} instance which represents the * IP address. * @return An {@link IpNetwork} instance which represents the IP network * specified by {@code addr}. Note that {@code null} is returned * if {@code addr} is {@code null} or it does not contain valid * value. * @throws IllegalArgumentException * The given {@link Address} instance is invalid. */ public static final IpNetwork create(Address addr) { if (addr instanceof Ipv4) { Ipv4Prefix ipv4 = ((Ipv4)addr).getIpv4Address(); return (ipv4 == null) ? null : create(ipv4.getValue()); } else if (addr == null) { return null; } throw getUnsupportedAddressException(addr); }
/** * Ensure that the given {@link IpNetwork} instance represents an * IPv4 address. * * @param ipn An {@link IpNetwork} instance. * @return {@code ipn} casted as {@link Ip4Network} if {@code ipn} * represents an IPv4 address. Otherwise {@code null}. */ public static Ip4Network toIp4Address(IpNetwork ipn) { return (ipn instanceof Ip4Network && ipn.isAddress()) ? (Ip4Network)ipn : null; }
/** * Return a {@link VtnInetMatchBuilder} instance which contains the flow * condition configured in this instance. * * @return A {@link VtnInetMatchBuilder} instance. */ public final VtnInetMatchBuilder toVtnInetMatchBuilder() { VtnInetMatchBuilder builder = new VtnInetMatchBuilder(); if (sourceNetwork != null) { builder.setSourceNetwork(sourceNetwork.getIpPrefix()); } if (destinationNetwork != null) { builder.setDestinationNetwork(destinationNetwork.getIpPrefix()); } if (dscp != null) { builder.setDscp(new Dscp(dscp)); } return builder.setProtocol(protocol); }
/** * {@inheritDoc} */ @Override protected final void appendContents(StringBuilder builder) { builder.append("addr=").append(address.getHostAddress()); super.appendContents(builder); }
/** * Return a string representation of this network address in CIDR notation. * * @return A string representation of this network address. * @deprecated Only for JAXB. Use {@link #getCidrText()} instead. */ @XmlValue public final String getCidrValue() { return getCidrText(); }
/** * Construct a new instance. * * @param cidr A string representation of IP network in CIDR notation. * @throws NullPointerException * {@code cidr} is {@code null}. * @throws IllegalArgumentException * The given string is invalid. */ private InetAddressPrefix(String cidr) { String text = cidr.trim(); int pos = text.indexOf((int)CIDR_SEPARATOR); String host; if (pos < 0) { host = text; prefix = 0; } else { host = text.substring(0, pos); try { String pstr = text.substring(pos + 1, text.length()); prefix = Integer.parseInt(pstr); } catch (RuntimeException e) { throw new IllegalArgumentException( "Invalid CIDR prefix: " + text, e); } } address = getInetAddress(host); }
/** * Determine whether the given IP header matches the condition for * IP address configured in this instance. * * @param ctx A {@link FlowMatchContext} instance. * @param iph An {@link InetHeader} instance. * @return {@code true} only if the given IP header matches all the * conditions for IP address configured in this instance. */ private boolean matchAddress(FlowMatchContext ctx, InetHeader iph) { // Check the source IP address. if (sourceNetwork != null) { ctx.addMatchField(FlowMatchType.IP_SRC); if (!sourceNetwork.contains(iph.getSourceAddress())) { return false; } } // Check the destination IP address. if (destinationNetwork != null) { ctx.addMatchField(FlowMatchType.IP_DST); if (!destinationNetwork.contains(iph.getDestinationAddress())) { return false; } } return true; }
/** * {@inheritDoc} */ @Override protected final void verifyImpl() throws RpcException { if (address == null) { String msg = getErrorMessage("IP address"); throw RpcException.getNullArgumentException(msg); } if (!address.isAddress()) { String msg = getErrorMessage("Netmask cannot be specified", address); throw RpcException.getBadArgumentException(msg); } }
/** * {@inheritDoc} */ @Override public void setMatch(MatchBuilder builder) { super.setMatch(builder); Ipv4MatchBuilder imatch; IpNetwork ipn = getSourceNetwork(); if (ipn == null) { imatch = null; } else { IpPrefix ipp = ipn.getIpPrefix(); imatch = create((Ipv4MatchBuilder)null). setIpv4Source(ipp.getIpv4Prefix()); } ipn = getDestinationNetwork(); if (ipn != null) { IpPrefix ipp = ipn.getIpPrefix(); imatch = create(imatch).setIpv4Destination(ipp.getIpv4Prefix()); } if (imatch != null) { builder.setLayer3Match(imatch.build()); } }
/** * Create an {@link IpNetwork} instance which represents the IP network * specified by CIDR notation. * * @param cidr A string representation of the IP network in CIDR notation. * Note that zero prefix means "no mask". So zero prefix is * treated as if the maximum prefix length is specified. * @return An {@link IpNetwork} instance which represents the IP network * specified by {@code cidr}. Note that {@code null} is returned * if {@code cidr} is {@code null}. * @throws IllegalArgumentException * The given network address is invalid. */ public static final IpNetwork create(String cidr) { if (cidr == null) { return null; } InetAddressPrefix ipfx = new InetAddressPrefix(cidr); return create(ipfx.getAddress(), ipfx.getPrefix()); }
/** * Store strings used to construct flow condition key. * * @param builder A {@link StringBuilder} instance which contains strings * used to construct flow condition key. */ public final void setConditionKey(StringBuilder builder) { String sep = (builder.length() == 0) ? "" : VTNMatch.COND_KEY_SEPARATOR; if (sourceNetwork != null) { builder.append(sep).append(FlowMatchType.IP_SRC).append('='). append(sourceNetwork.getText()); sep = VTNMatch.COND_KEY_SEPARATOR; } if (destinationNetwork != null) { builder.append(sep).append(FlowMatchType.IP_DST).append('='). append(destinationNetwork.getText()); sep = VTNMatch.COND_KEY_SEPARATOR; } if (protocol != null) { builder.append(sep).append(FlowMatchType.IP_PROTO).append('='). append(protocol); sep = VTNMatch.COND_KEY_SEPARATOR; } if (dscp != null) { builder.append(sep).append(FlowMatchType.IP_DSCP).append('='). append(dscp); } }
/** * {@inheritDoc} */ @Override public Set<IpNetwork> getIpNetworkSet() { Set<IpNetwork> ipSet = ipAddressSet; if (ipSet == null) { ipSet = new HashSet<>(); ipAddressSet = ipSet; List<IpAddress> ipaddrs = theEntry.getIpAddresses(); if (!MiscUtils.isEmpty(ipaddrs)) { for (IpAddress ip: ipaddrs) { ipSet.add(IpNetwork.create(ip)); } } } return ipSet; }
/** * Construct a new {@link IpNetwork} instance which represents the IP * network specified by the given {@link IpPrefix} instance. * * @param type A class which specifies the type of {@link IpNetwork} * instance. * @param ipp An {@link IpPrefix} instance. * @param desc A brief description about the specified IP network. * @return An {@link IpNetwork} instance or {@code null}. * @throws RpcException * The given parameter is invalid. */ private IpNetwork getIpNetwork(Class<? extends IpNetwork> type, IpPrefix ipp, String desc) throws RpcException { try { IpNetwork ipn = IpNetwork.create(ipp); if (ipn != null && !type.isInstance(ipn)) { throw new IllegalArgumentException( "Unexpected IP prefix type"); } return ipn; } catch (RuntimeException e) { String msg = MiscUtils.joinColon(ipp, e.getMessage()); throw invalidInetAddress(msg, desc, e); } }
/** * Construct a new instance. * * @param act A {@link VtnIpaddrActionFields} instance. * @param ord An integer which determines the order of flow actions * in a flow entry. * @throws RpcException An invalid argument is specified. */ protected VTNInetAddrAction(VtnIpaddrActionFields act, Integer ord) throws RpcException { super(ord); if (act != null) { address = IpNetwork.create(act.getAddress()); } verify(); }
/** * Create an {@link IpNetwork} instance which represents the IP network * specified by the given {@link IpPrefix} instance. * * @param ipp An {@link IpPrefix} instance which represents the IP * network. * @return An {@link IpNetwork} instance which represents the IP network * specified by {@code ipp}. Note that {@code null} is returned * if {@code ipp} is {@code null} or it does not contain valid * value. * @throws IllegalArgumentException * The given {@link IpPrefix} instance is invalid. */ public static final IpNetwork create(IpPrefix ipp) { if (ipp != null) { Ipv4Prefix ipv4 = ipp.getIpv4Prefix(); if (ipv4 != null) { return create(ipv4.getValue()); } if (ipp.getIpv6Prefix() != null) { throw new IllegalArgumentException( "Unsupported IP prefix: " + ipp); } } return null; }