@Test public void testCreateUpdate() throws Exception { List<SxpBindingFields> add = new ArrayList<>(), dell = new ArrayList<>(); dell.add(getBinding(10000, "192.168.0.1/32")); add.add(getBinding(20000, "2001::1/64")); add.add(getBinding(20000, "10.10.10.10/30")); dell.add(getBinding(30000, "2002::1/128")); add.add(getBinding(40000, "11.11.11.0/29")); add.add(getBinding(65000, "172.168.1.0/28")); List<CapabilityType> capabilityTypes = new ArrayList<>(); capabilityTypes.add(CapabilityType.Ipv4Unicast); capabilityTypes.add(CapabilityType.Ipv6Unicast); ByteBuf message = MessageFactory.createUpdate(dell, add, nodeId, capabilityTypes, null); byte[] result = new byte[] {0, 0, 0, 108, 0, 0, 0, 3, 80, 13, 5, 32, -64, -88, 0, 1, 80, 14, 17, -128, 32, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 16, 16, 4, -64, -88, 0, 1, 16, 17, 2, 78, 32, 80, 11, 5, 30, 10, 10, 10, 10, 80, 12, 9, 64, 32, 1, 0, 0, 0, 0, 0, 0, 16, 16, 4, -64, -88, 0, 1, 16, 17, 2, -100, 64, 80, 11, 5, 29, 11, 11, 11, 0, 16, 16, 4, -64, -88, 0, 1, 16, 17, 2, -3, -24, 80, 11, 5, 28, -84, -88, 1, 0}; assertArrayEquals(result, toBytes(message)); }
public static <R extends SxpBindingFields, T extends SxpBindingFields> ByteBuf createUpdate(List<R> deleteBindings, List<T> addBindings, NodeId nodeId, List<CapabilityType> capabilities, SxpBindingFilter bindingFilter) throws SecurityGroupTagValueException, AttributeVariantException { AttributeList attributes = new AttributeList(); List<IpPrefix> ipv4Prefixes = new ArrayList<>(); List<IpPrefix> ipv6Prefixes = new ArrayList<>(); if (deleteBindings != null && !deleteBindings.isEmpty()) { deleteBindings.forEach(b -> { if (b.getIpPrefix().getIpv4Prefix() != null) { ipv4Prefixes.add(b.getIpPrefix()); } else if (b.getIpPrefix().getIpv6Prefix() != null) { ipv6Prefixes.add(b.getIpPrefix()); } }); if (!ipv4Prefixes.isEmpty()) { attributes.add(AttributeFactory.createIpv4DeletePrefix(ipv4Prefixes, AttributeFactory.NONTRANSITIVE_COMPACT)); ipv4Prefixes.clear(); } if (!ipv6Prefixes.isEmpty()) { attributes.add(AttributeFactory.createIpv6DeletePrefix(ipv6Prefixes, AttributeFactory.NONTRANSITIVE_COMPACT)); ipv6Prefixes.clear(); } } if (addBindings != null && !addBindings.isEmpty()) { Sgt sgt = null; PeerSequence peerSequence = null; for (T binding : addBindings) { if (bindingFilter != null && bindingFilter.apply(binding)) { continue; } if ((!binding.getPeerSequence().equals(peerSequence) || !binding.getSecurityGroupTag().equals(sgt))) { if (!ipv4Prefixes.isEmpty() && sgt != null && peerSequence != null) { attributes.add(AttributeFactory.createIpv4AddPrefix(ipv4Prefixes, capabilities.contains( CapabilityType.Ipv4Unicast) ? AttributeFactory.NONTRANSITIVE_COMPACT : AttributeFactory.OPTIONAL_COMPACT)); ipv4Prefixes.clear(); } if (!ipv6Prefixes.isEmpty() && sgt != null && peerSequence != null) { attributes.add(AttributeFactory.createIpv6AddPrefix(ipv6Prefixes, capabilities.contains( CapabilityType.Ipv6Unicast) ? AttributeFactory.NONTRANSITIVE_COMPACT : AttributeFactory.OPTIONAL_COMPACT)); ipv6Prefixes.clear(); } List<NodeId> peers = NodeIdConv.getPeerSequence(binding.getPeerSequence()); if (nodeId != null) { peers.add(0, nodeId); } attributes.add(AttributeFactory.createPeerSequence(peers)); attributes.add(AttributeFactory.createSourceGroupTag(binding.getSecurityGroupTag().getValue())); sgt = binding.getSecurityGroupTag(); peerSequence = binding.getPeerSequence(); } if (binding.getIpPrefix().getIpv4Prefix() != null) { ipv4Prefixes.add(binding.getIpPrefix()); } else if (binding.getIpPrefix().getIpv6Prefix() != null) { ipv6Prefixes.add(binding.getIpPrefix()); } } if (!ipv4Prefixes.isEmpty()) { attributes.add(AttributeFactory.createIpv4AddPrefix(ipv4Prefixes, capabilities.contains( CapabilityType.Ipv4Unicast) ? AttributeFactory.NONTRANSITIVE_COMPACT : AttributeFactory.OPTIONAL_COMPACT)); ipv4Prefixes.clear(); } if (!ipv6Prefixes.isEmpty()) { attributes.add(AttributeFactory.createIpv6AddPrefix(ipv6Prefixes, capabilities.contains( CapabilityType.Ipv6Unicast) ? AttributeFactory.NONTRANSITIVE_COMPACT : AttributeFactory.OPTIONAL_COMPACT)); ipv6Prefixes.clear(); } } return getMessage(MessageType.Update, attributes.toBytes()); }
MessageFactory { public static <R extends SxpBindingFields, T extends SxpBindingFields> ByteBuf createUpdate(List<R> deleteBindings, List<T> addBindings, NodeId nodeId, List<CapabilityType> capabilities, SxpBindingFilter bindingFilter) throws SecurityGroupTagValueException, AttributeVariantException { AttributeList attributes = new AttributeList(); List<IpPrefix> ipv4Prefixes = new ArrayList<>(); List<IpPrefix> ipv6Prefixes = new ArrayList<>(); if (deleteBindings != null && !deleteBindings.isEmpty()) { deleteBindings.forEach(b -> { if (b.getIpPrefix().getIpv4Prefix() != null) { ipv4Prefixes.add(b.getIpPrefix()); } else if (b.getIpPrefix().getIpv6Prefix() != null) { ipv6Prefixes.add(b.getIpPrefix()); } }); if (!ipv4Prefixes.isEmpty()) { attributes.add(AttributeFactory.createIpv4DeletePrefix(ipv4Prefixes, AttributeFactory.NONTRANSITIVE_COMPACT)); ipv4Prefixes.clear(); } if (!ipv6Prefixes.isEmpty()) { attributes.add(AttributeFactory.createIpv6DeletePrefix(ipv6Prefixes, AttributeFactory.NONTRANSITIVE_COMPACT)); ipv6Prefixes.clear(); } } if (addBindings != null && !addBindings.isEmpty()) { Sgt sgt = null; PeerSequence peerSequence = null; for (T binding : addBindings) { if (bindingFilter != null && bindingFilter.apply(binding)) { continue; } if ((!binding.getPeerSequence().equals(peerSequence) || !binding.getSecurityGroupTag().equals(sgt))) { if (!ipv4Prefixes.isEmpty() && sgt != null && peerSequence != null) { attributes.add(AttributeFactory.createIpv4AddPrefix(ipv4Prefixes, capabilities.contains( CapabilityType.Ipv4Unicast) ? AttributeFactory.NONTRANSITIVE_COMPACT : AttributeFactory.OPTIONAL_COMPACT)); ipv4Prefixes.clear(); } if (!ipv6Prefixes.isEmpty() && sgt != null && peerSequence != null) { attributes.add(AttributeFactory.createIpv6AddPrefix(ipv6Prefixes, capabilities.contains( CapabilityType.Ipv6Unicast) ? AttributeFactory.NONTRANSITIVE_COMPACT : AttributeFactory.OPTIONAL_COMPACT)); ipv6Prefixes.clear(); } List<NodeId> peers = NodeIdConv.getPeerSequence(binding.getPeerSequence()); if (nodeId != null) { peers.add(0, nodeId); } attributes.add(AttributeFactory.createPeerSequence(peers)); attributes.add(AttributeFactory.createSourceGroupTag(binding.getSecurityGroupTag().getValue())); sgt = binding.getSecurityGroupTag(); peerSequence = binding.getPeerSequence(); } if (binding.getIpPrefix().getIpv4Prefix() != null) { ipv4Prefixes.add(binding.getIpPrefix()); } else if (binding.getIpPrefix().getIpv6Prefix() != null) { ipv6Prefixes.add(binding.getIpPrefix()); } } if (!ipv4Prefixes.isEmpty()) { attributes.add(AttributeFactory.createIpv4AddPrefix(ipv4Prefixes, capabilities.contains( CapabilityType.Ipv4Unicast) ? AttributeFactory.NONTRANSITIVE_COMPACT : AttributeFactory.OPTIONAL_COMPACT)); ipv4Prefixes.clear(); } if (!ipv6Prefixes.isEmpty()) { attributes.add(AttributeFactory.createIpv6AddPrefix(ipv6Prefixes, capabilities.contains( CapabilityType.Ipv6Unicast) ? AttributeFactory.NONTRANSITIVE_COMPACT : AttributeFactory.OPTIONAL_COMPACT)); ipv6Prefixes.clear(); } } return getMessage(MessageType.Update, attributes.toBytes()); } }
MessageFactory { public static <R extends SxpBindingFields, T extends SxpBindingFields> ByteBuf createUpdate(List<R> deleteBindings, List<T> addBindings, NodeId nodeId, List<CapabilityType> capabilities, SxpBindingFilter bindingFilter) throws SecurityGroupTagValueException, AttributeVariantException { AttributeList attributes = new AttributeList(); List<IpPrefix> ipv4Prefixes = new ArrayList<>(); List<IpPrefix> ipv6Prefixes = new ArrayList<>(); if (deleteBindings != null && !deleteBindings.isEmpty()) { deleteBindings.forEach(b -> { if (b.getIpPrefix().getIpv4Prefix() != null) { ipv4Prefixes.add(b.getIpPrefix()); } else if (b.getIpPrefix().getIpv6Prefix() != null) { ipv6Prefixes.add(b.getIpPrefix()); } }); if (!ipv4Prefixes.isEmpty()) { attributes.add(AttributeFactory.createIpv4DeletePrefix(ipv4Prefixes, AttributeFactory.NONTRANSITIVE_COMPACT)); ipv4Prefixes.clear(); } if (!ipv6Prefixes.isEmpty()) { attributes.add(AttributeFactory.createIpv6DeletePrefix(ipv6Prefixes, AttributeFactory.NONTRANSITIVE_COMPACT)); ipv6Prefixes.clear(); } } if (addBindings != null && !addBindings.isEmpty()) { Sgt sgt = null; PeerSequence peerSequence = null; for (T binding : addBindings) { if (bindingFilter != null && bindingFilter.apply(binding)) { continue; } if ((!binding.getPeerSequence().equals(peerSequence) || !binding.getSecurityGroupTag().equals(sgt))) { if (!ipv4Prefixes.isEmpty() && sgt != null && peerSequence != null) { attributes.add(AttributeFactory.createIpv4AddPrefix(ipv4Prefixes, capabilities.contains( CapabilityType.Ipv4Unicast) ? AttributeFactory.NONTRANSITIVE_COMPACT : AttributeFactory.OPTIONAL_COMPACT)); ipv4Prefixes.clear(); } if (!ipv6Prefixes.isEmpty() && sgt != null && peerSequence != null) { attributes.add(AttributeFactory.createIpv6AddPrefix(ipv6Prefixes, capabilities.contains( CapabilityType.Ipv6Unicast) ? AttributeFactory.NONTRANSITIVE_COMPACT : AttributeFactory.OPTIONAL_COMPACT)); ipv6Prefixes.clear(); } List<NodeId> peers = NodeIdConv.getPeerSequence(binding.getPeerSequence()); if (nodeId != null) { peers.add(0, nodeId); } attributes.add(AttributeFactory.createPeerSequence(peers)); attributes.add(AttributeFactory.createSourceGroupTag(binding.getSecurityGroupTag().getValue())); sgt = binding.getSecurityGroupTag(); peerSequence = binding.getPeerSequence(); } if (binding.getIpPrefix().getIpv4Prefix() != null) { ipv4Prefixes.add(binding.getIpPrefix()); } else if (binding.getIpPrefix().getIpv6Prefix() != null) { ipv6Prefixes.add(binding.getIpPrefix()); } } if (!ipv4Prefixes.isEmpty()) { attributes.add(AttributeFactory.createIpv4AddPrefix(ipv4Prefixes, capabilities.contains( CapabilityType.Ipv4Unicast) ? AttributeFactory.NONTRANSITIVE_COMPACT : AttributeFactory.OPTIONAL_COMPACT)); ipv4Prefixes.clear(); } if (!ipv6Prefixes.isEmpty()) { attributes.add(AttributeFactory.createIpv6AddPrefix(ipv6Prefixes, capabilities.contains( CapabilityType.Ipv6Unicast) ? AttributeFactory.NONTRANSITIVE_COMPACT : AttributeFactory.OPTIONAL_COMPACT)); ipv6Prefixes.clear(); } } return getMessage(MessageType.Update, attributes.toBytes()); }  }
MessageFactory { public static <R extends SxpBindingFields, T extends SxpBindingFields> ByteBuf createUpdate(List<R> deleteBindings, List<T> addBindings, NodeId nodeId, List<CapabilityType> capabilities, SxpBindingFilter bindingFilter) throws SecurityGroupTagValueException, AttributeVariantException { AttributeList attributes = new AttributeList(); List<IpPrefix> ipv4Prefixes = new ArrayList<>(); List<IpPrefix> ipv6Prefixes = new ArrayList<>(); if (deleteBindings != null && !deleteBindings.isEmpty()) { deleteBindings.forEach(b -> { if (b.getIpPrefix().getIpv4Prefix() != null) { ipv4Prefixes.add(b.getIpPrefix()); } else if (b.getIpPrefix().getIpv6Prefix() != null) { ipv6Prefixes.add(b.getIpPrefix()); } }); if (!ipv4Prefixes.isEmpty()) { attributes.add(AttributeFactory.createIpv4DeletePrefix(ipv4Prefixes, AttributeFactory.NONTRANSITIVE_COMPACT)); ipv4Prefixes.clear(); } if (!ipv6Prefixes.isEmpty()) { attributes.add(AttributeFactory.createIpv6DeletePrefix(ipv6Prefixes, AttributeFactory.NONTRANSITIVE_COMPACT)); ipv6Prefixes.clear(); } } if (addBindings != null && !addBindings.isEmpty()) { Sgt sgt = null; PeerSequence peerSequence = null; for (T binding : addBindings) { if (bindingFilter != null && bindingFilter.apply(binding)) { continue; } if ((!binding.getPeerSequence().equals(peerSequence) || !binding.getSecurityGroupTag().equals(sgt))) { if (!ipv4Prefixes.isEmpty() && sgt != null && peerSequence != null) { attributes.add(AttributeFactory.createIpv4AddPrefix(ipv4Prefixes, capabilities.contains( CapabilityType.Ipv4Unicast) ? AttributeFactory.NONTRANSITIVE_COMPACT : AttributeFactory.OPTIONAL_COMPACT)); ipv4Prefixes.clear(); } if (!ipv6Prefixes.isEmpty() && sgt != null && peerSequence != null) { attributes.add(AttributeFactory.createIpv6AddPrefix(ipv6Prefixes, capabilities.contains( CapabilityType.Ipv6Unicast) ? AttributeFactory.NONTRANSITIVE_COMPACT : AttributeFactory.OPTIONAL_COMPACT)); ipv6Prefixes.clear(); } List<NodeId> peers = NodeIdConv.getPeerSequence(binding.getPeerSequence()); if (nodeId != null) { peers.add(0, nodeId); } attributes.add(AttributeFactory.createPeerSequence(peers)); attributes.add(AttributeFactory.createSourceGroupTag(binding.getSecurityGroupTag().getValue())); sgt = binding.getSecurityGroupTag(); peerSequence = binding.getPeerSequence(); } if (binding.getIpPrefix().getIpv4Prefix() != null) { ipv4Prefixes.add(binding.getIpPrefix()); } else if (binding.getIpPrefix().getIpv6Prefix() != null) { ipv6Prefixes.add(binding.getIpPrefix()); } } if (!ipv4Prefixes.isEmpty()) { attributes.add(AttributeFactory.createIpv4AddPrefix(ipv4Prefixes, capabilities.contains( CapabilityType.Ipv4Unicast) ? AttributeFactory.NONTRANSITIVE_COMPACT : AttributeFactory.OPTIONAL_COMPACT)); ipv4Prefixes.clear(); } if (!ipv6Prefixes.isEmpty()) { attributes.add(AttributeFactory.createIpv6AddPrefix(ipv6Prefixes, capabilities.contains( CapabilityType.Ipv6Unicast) ? AttributeFactory.NONTRANSITIVE_COMPACT : AttributeFactory.OPTIONAL_COMPACT)); ipv6Prefixes.clear(); } } return getMessage(MessageType.Update, attributes.toBytes()); }  static ByteBuf createError(ErrorCode errorCode, ErrorSubCode errorSubCode, byte[] data); static ByteBuf createKeepalive(); static ByteBuf createOpen(Version version, ConnectionMode nodeMode, NodeId nodeID, int holdTimeMinAcceptable); static ByteBuf createOpen(Version version, ConnectionMode nodeMode, NodeId nodeID, int holdTimeMin,
            int holdTimeMax); static ByteBuf createOpenResp(Version version, ConnectionMode nodeMode, NodeId nodeID); static ByteBuf createOpenResp(Version version, ConnectionMode nodeMode, NodeId nodeID,
            int holdTimeMinAcceptable); static ByteBuf createOpenResp(Version version, ConnectionMode nodeMode, NodeId nodeID, int holdTimeMin,
            int holdTimeMax); static ByteBuf createPurgeAll(); static ByteBuf createUpdate(List<R> deleteBindings,
            List<T> addBindings, NodeId nodeId, List<CapabilityType> capabilities, SxpBindingFilter bindingFilter); static Notification decodeErrorMessage(byte[] payload); static Notification decodeKeepalive(byte[] payload); static Notification decodeOpen(byte[] payload); static Notification decodeOpenResp(byte[] payload); static Version extractVersion(final byte[] payload); static Notification decodePurgeAll(byte[] payload); static Notification decodeUpdate(byte[] payload); static Notification parse(Version version, ByteBuf request); static String toString(byte[] message); static String toString(ByteBuf message); static String toString(Notification message); static List<CapabilityType> decodeCapabilities(OpenMessage message); }
MessageFactory { public static <R extends SxpBindingFields, T extends SxpBindingFields> ByteBuf createUpdate(List<R> deleteBindings, List<T> addBindings, NodeId nodeId, List<CapabilityType> capabilities, SxpBindingFilter bindingFilter) throws SecurityGroupTagValueException, AttributeVariantException { AttributeList attributes = new AttributeList(); List<IpPrefix> ipv4Prefixes = new ArrayList<>(); List<IpPrefix> ipv6Prefixes = new ArrayList<>(); if (deleteBindings != null && !deleteBindings.isEmpty()) { deleteBindings.forEach(b -> { if (b.getIpPrefix().getIpv4Prefix() != null) { ipv4Prefixes.add(b.getIpPrefix()); } else if (b.getIpPrefix().getIpv6Prefix() != null) { ipv6Prefixes.add(b.getIpPrefix()); } }); if (!ipv4Prefixes.isEmpty()) { attributes.add(AttributeFactory.createIpv4DeletePrefix(ipv4Prefixes, AttributeFactory.NONTRANSITIVE_COMPACT)); ipv4Prefixes.clear(); } if (!ipv6Prefixes.isEmpty()) { attributes.add(AttributeFactory.createIpv6DeletePrefix(ipv6Prefixes, AttributeFactory.NONTRANSITIVE_COMPACT)); ipv6Prefixes.clear(); } } if (addBindings != null && !addBindings.isEmpty()) { Sgt sgt = null; PeerSequence peerSequence = null; for (T binding : addBindings) { if (bindingFilter != null && bindingFilter.apply(binding)) { continue; } if ((!binding.getPeerSequence().equals(peerSequence) || !binding.getSecurityGroupTag().equals(sgt))) { if (!ipv4Prefixes.isEmpty() && sgt != null && peerSequence != null) { attributes.add(AttributeFactory.createIpv4AddPrefix(ipv4Prefixes, capabilities.contains( CapabilityType.Ipv4Unicast) ? AttributeFactory.NONTRANSITIVE_COMPACT : AttributeFactory.OPTIONAL_COMPACT)); ipv4Prefixes.clear(); } if (!ipv6Prefixes.isEmpty() && sgt != null && peerSequence != null) { attributes.add(AttributeFactory.createIpv6AddPrefix(ipv6Prefixes, capabilities.contains( CapabilityType.Ipv6Unicast) ? AttributeFactory.NONTRANSITIVE_COMPACT : AttributeFactory.OPTIONAL_COMPACT)); ipv6Prefixes.clear(); } List<NodeId> peers = NodeIdConv.getPeerSequence(binding.getPeerSequence()); if (nodeId != null) { peers.add(0, nodeId); } attributes.add(AttributeFactory.createPeerSequence(peers)); attributes.add(AttributeFactory.createSourceGroupTag(binding.getSecurityGroupTag().getValue())); sgt = binding.getSecurityGroupTag(); peerSequence = binding.getPeerSequence(); } if (binding.getIpPrefix().getIpv4Prefix() != null) { ipv4Prefixes.add(binding.getIpPrefix()); } else if (binding.getIpPrefix().getIpv6Prefix() != null) { ipv6Prefixes.add(binding.getIpPrefix()); } } if (!ipv4Prefixes.isEmpty()) { attributes.add(AttributeFactory.createIpv4AddPrefix(ipv4Prefixes, capabilities.contains( CapabilityType.Ipv4Unicast) ? AttributeFactory.NONTRANSITIVE_COMPACT : AttributeFactory.OPTIONAL_COMPACT)); ipv4Prefixes.clear(); } if (!ipv6Prefixes.isEmpty()) { attributes.add(AttributeFactory.createIpv6AddPrefix(ipv6Prefixes, capabilities.contains( CapabilityType.Ipv6Unicast) ? AttributeFactory.NONTRANSITIVE_COMPACT : AttributeFactory.OPTIONAL_COMPACT)); ipv6Prefixes.clear(); } } return getMessage(MessageType.Update, attributes.toBytes()); }  static ByteBuf createError(ErrorCode errorCode, ErrorSubCode errorSubCode, byte[] data); static ByteBuf createKeepalive(); static ByteBuf createOpen(Version version, ConnectionMode nodeMode, NodeId nodeID, int holdTimeMinAcceptable); static ByteBuf createOpen(Version version, ConnectionMode nodeMode, NodeId nodeID, int holdTimeMin,
            int holdTimeMax); static ByteBuf createOpenResp(Version version, ConnectionMode nodeMode, NodeId nodeID); static ByteBuf createOpenResp(Version version, ConnectionMode nodeMode, NodeId nodeID,
            int holdTimeMinAcceptable); static ByteBuf createOpenResp(Version version, ConnectionMode nodeMode, NodeId nodeID, int holdTimeMin,
            int holdTimeMax); static ByteBuf createPurgeAll(); static ByteBuf createUpdate(List<R> deleteBindings,
            List<T> addBindings, NodeId nodeId, List<CapabilityType> capabilities, SxpBindingFilter bindingFilter); static Notification decodeErrorMessage(byte[] payload); static Notification decodeKeepalive(byte[] payload); static Notification decodeOpen(byte[] payload); static Notification decodeOpenResp(byte[] payload); static Version extractVersion(final byte[] payload); static Notification decodePurgeAll(byte[] payload); static Notification decodeUpdate(byte[] payload); static Notification parse(Version version, ByteBuf request); static String toString(byte[] message); static String toString(ByteBuf message); static String toString(Notification message); static List<CapabilityType> decodeCapabilities(OpenMessage message);  }