@JsonCreator private static @Nonnull SelfDescribingObject create( @JsonProperty(PROP_NAME) String name, @JsonProperty(PROP_SCHEMA) Schema schema, @JsonProperty(PROP_VALUE) Object value) { return new SelfDescribingObject(name, schema, value); }
@Test public void deserialize() throws IOException { ObjectNode json = BatfishObjectMapper.mapper().createObjectNode(); json.put(SelfDescribingObject.PROP_NAME, "meaningoflife"); json.set( SelfDescribingObject.PROP_SCHEMA, BatfishObjectMapper.mapper().valueToTree(Schema.INTEGER)); json.put(SelfDescribingObject.PROP_VALUE, 42); String strObject = json.toString(); SelfDescribingObject sdObject = BatfishObjectMapper.mapper().readValue(strObject, SelfDescribingObject.class); assertThat(sdObject.getName(), equalTo("meaningoflife")); assertThat(sdObject.getSchema(), equalTo(Schema.INTEGER)); assertThat(sdObject.getValue(), equalTo(42)); }
@Test public void testGetTypedValue() { Ip ip = Ip.parse("1.1.1.1"); SelfDescribingObject ipObject = new SelfDescribingObject(Schema.IP, ip); assertThat(ipObject.getTypedValue(), equalTo(ip)); } }
@VisibleForTesting static SelfDescribingObject getRemoteIp(@Nonnull BgpPeerConfig peer) { if (peer instanceof BgpActivePeerConfig) { return new SelfDescribingObject(Schema.IP, ((BgpActivePeerConfig) peer).getPeerAddress()); } if (peer instanceof BgpPassivePeerConfig) { return new SelfDescribingObject(Schema.PREFIX, ((BgpPassivePeerConfig) peer).getPeerPrefix()); } throw new IllegalArgumentException( String.format("Peer is neither Active nor Passive: %s", peer)); }
@VisibleForTesting static SelfDescribingObject getRemoteAs(@Nonnull BgpPeerConfig peer) { if (peer instanceof BgpActivePeerConfig) { return new SelfDescribingObject(Schema.LONG, ((BgpActivePeerConfig) peer).getRemoteAs()); } if (peer instanceof BgpPassivePeerConfig) { return new SelfDescribingObject( Schema.list(Schema.LONG), ((BgpPassivePeerConfig) peer).getRemoteAs()); } throw new IllegalArgumentException( String.format("Peer is neither Active nor Passive: %s", peer)); }
/** Returns what is actually inserted into the answer */ static SelfDescribingObject insertedObject(Object obj, String structType) { Schema targetSchema = NamedStructureSpecifier.JAVA_MAP.get(structType).getSchema(); return new SelfDescribingObject( targetSchema, PropertySpecifier.convertTypeIfNeeded(obj, targetSchema)); } }
@Test public void testEquals() { SelfDescribingObject group1Elem1 = new SelfDescribingObject("a", Schema.BOOLEAN, false); SelfDescribingObject group1Elem2 = new SelfDescribingObject("a", Schema.BOOLEAN, false); // different value SelfDescribingObject group2 = new SelfDescribingObject("a", Schema.BOOLEAN, true); // different schema SelfDescribingObject group3 = new SelfDescribingObject("a", Schema.NODE, null); // different name SelfDescribingObject group4 = new SelfDescribingObject("b", Schema.BOOLEAN, false); new EqualsTester() .addEqualityGroup(group1Elem1, group1Elem2) .addEqualityGroup(group2) .addEqualityGroup(group3) .addEqualityGroup(group4) .testEquals(); }
@Test public void getRemoteAsPassivePeer() { BgpPassivePeerConfig passivePeerConfig = BgpPassivePeerConfig.builder().setRemoteAs(ImmutableList.of(100L)).build(); assertThat( getRemoteAs(passivePeerConfig), equalTo(new SelfDescribingObject(Schema.list(Schema.LONG), ImmutableList.of(100L)))); } }
@Test public void getRemoteAsActivePeer() { BgpActivePeerConfig activePeerConfig = BgpActivePeerConfig.builder().setRemoteAs(100L).build(); assertThat(getRemoteAs(activePeerConfig), equalTo(new SelfDescribingObject(Schema.LONG, 100L))); }
@Test public void testColumnComparatorSelfDescribingObject() { String col = "col1"; ColumnMetadata columnMetadata = new ColumnMetadata(col, Schema.SELF_DESCRIBING, "colDesc"); Comparator<Row> comparator = _manager.columnComparator(columnMetadata); Row r1 = Row.of(col, new SelfDescribingObject(Schema.STRING, "a")); Row r2 = Row.of(col, new SelfDescribingObject(Schema.STRING, "b")); assertThat(comparator.compare(r1, r2), lessThan(0)); }
private static @Nonnull Row buildActivePeerRow( BgpPeerConfigId activeId, BgpActivePeerConfig activePeer, SessionType type, ConfiguredSessionStatus status, Map<String, ColumnMetadata> metadataMap, ValueGraph<BgpPeerConfigId, BgpSessionProperties> bgpTopology, Map<String, Configuration> configurations) { Node remoteNode = null; if (status == UNIQUE_MATCH) { String remoteNodeName = bgpTopology.adjacentNodes(activeId).iterator().next().getHostname(); remoteNode = new Node(remoteNodeName); } Ip localIp = activePeer.getLocalIp(); NodeInterfacePair localInterface = getInterface(configurations.get(activeId.getHostname()), localIp); return Row.builder(metadataMap) .put(COL_CONFIGURED_STATUS, status) .put(COL_LOCAL_INTERFACE, localInterface) .put(COL_LOCAL_AS, activePeer.getLocalAs()) .put(COL_LOCAL_IP, activePeer.getLocalIp()) .put(COL_NODE, new Node(activeId.getHostname())) .put(COL_REMOTE_AS, new SelfDescribingObject(Schema.LONG, activePeer.getRemoteAs())) .put(COL_REMOTE_NODE, remoteNode) .put(COL_REMOTE_IP, new SelfDescribingObject(Schema.IP, activePeer.getPeerAddress())) .put(COL_SESSION_TYPE, type) .put(COL_VRF, activeId.getVrfName()) .build(); }
private static @Nonnull Row buildActivePeerRow( BgpPeerConfigId activeId, BgpActivePeerConfig activePeer, SessionType type, SessionStatus status, Map<String, ColumnMetadata> metadataMap, ValueGraph<BgpPeerConfigId, BgpSessionProperties> configuredBgpTopology, Map<String, Configuration> configurations) { Node remoteNode = null; if (status != NOT_COMPATIBLE) { String remoteNodeName = configuredBgpTopology.adjacentNodes(activeId).iterator().next().getHostname(); remoteNode = new Node(remoteNodeName); } Ip localIp = activePeer.getLocalIp(); NodeInterfacePair localInterface = getInterface(configurations.get(activeId.getHostname()), localIp); return Row.builder(metadataMap) .put(COL_ESTABLISHED_STATUS, status) .put(COL_LOCAL_INTERFACE, localInterface) .put(COL_LOCAL_AS, activePeer.getLocalAs()) .put(COL_LOCAL_IP, activePeer.getLocalIp()) .put(COL_NODE, new Node(activeId.getHostname())) .put(COL_REMOTE_AS, new SelfDescribingObject(Schema.LONG, activePeer.getRemoteAs())) .put(COL_REMOTE_NODE, remoteNode) .put(COL_REMOTE_IP, new SelfDescribingObject(Schema.IP, activePeer.getPeerAddress())) .put(COL_SESSION_TYPE, type) .put(COL_VRF, activeId.getVrfName()) .build(); }
private static @Nonnull Row buildPassivePeerWithoutRemoteRow( Map<String, ColumnMetadata> metadataMap, BgpPeerConfigId passiveId, BgpPassivePeerConfig passivePeer, ConfiguredSessionStatus status) { return Row.builder(metadataMap) .put(COL_CONFIGURED_STATUS, status) .put(COL_LOCAL_INTERFACE, null) .put(COL_LOCAL_AS, passivePeer.getLocalAs()) .put(COL_LOCAL_IP, passivePeer.getLocalIp()) .put(COL_NODE, new Node(passiveId.getHostname())) .put( COL_REMOTE_AS, new SelfDescribingObject(Schema.list(Schema.LONG), passivePeer.getRemoteAs())) .put(COL_REMOTE_NODE, null) .put(COL_REMOTE_IP, new SelfDescribingObject(Schema.PREFIX, passivePeer.getPeerPrefix())) .put(COL_SESSION_TYPE, SessionType.UNSET) .put(COL_VRF, passiveId.getVrfName()) .build(); }
private static @Nonnull Row buildPassivePeerWithoutRemoteRow( Map<String, ColumnMetadata> metadataMap, BgpPeerConfigId passiveId, BgpPassivePeerConfig passivePeer, SessionStatus status) { return Row.builder(metadataMap) .put(COL_ESTABLISHED_STATUS, status) .put(COL_LOCAL_INTERFACE, null) .put(COL_LOCAL_AS, passivePeer.getLocalAs()) .put(COL_LOCAL_IP, passivePeer.getLocalIp()) .put(COL_NODE, new Node(passiveId.getHostname())) .put( COL_REMOTE_AS, new SelfDescribingObject(Schema.list(Schema.LONG), passivePeer.getRemoteAs())) .put(COL_REMOTE_NODE, null) .put(COL_REMOTE_IP, new SelfDescribingObject(Schema.PREFIX, passivePeer.getPeerPrefix())) .put(COL_SESSION_TYPE, SessionType.UNSET) .put(COL_VRF, passiveId.getVrfName()) .build(); }
@Test public void getRemoteIpActivePeer() { Ip ip = Ip.parse("1.1.1.1"); BgpActivePeerConfig activePeerConfig = BgpActivePeerConfig.builder().setPeerAddress(ip).build(); assertThat(getRemoteIp(activePeerConfig), equalTo(new SelfDescribingObject(Schema.IP, ip))); }
private static @Nonnull Row buildDynamicMatchRow( Map<String, ColumnMetadata> metadataMap, BgpPeerConfigId passiveId, BgpPassivePeerConfig passivePeer, BgpPeerConfigId activeId, BgpActivePeerConfig activePeer, Map<String, Configuration> configurations) { SessionType type = BgpSessionProperties.getSessionType(activePeer); Ip localIp = activePeer.getPeerAddress(); NodeInterfacePair localInterface = getInterface(configurations.get(passiveId.getHostname()), localIp); return Row.builder(metadataMap) .put(COL_CONFIGURED_STATUS, DYNAMIC_MATCH) .put(COL_LOCAL_INTERFACE, localInterface) .put(COL_LOCAL_AS, passivePeer.getLocalAs()) .put(COL_LOCAL_IP, localIp) .put(COL_NODE, new Node(passiveId.getHostname())) .put(COL_REMOTE_AS, new SelfDescribingObject(Schema.LONG, activePeer.getLocalAs())) .put(COL_REMOTE_NODE, new Node(activeId.getHostname())) .put(COL_REMOTE_IP, new SelfDescribingObject(Schema.IP, activePeer.getLocalIp())) .put(COL_SESSION_TYPE, type) .put(COL_VRF, passiveId.getVrfName()) .build(); }
private static @Nonnull Row buildDynamicMatchRow( Map<String, ColumnMetadata> metadataMap, BgpPeerConfigId passiveId, BgpPassivePeerConfig passivePeer, BgpPeerConfigId activeId, BgpActivePeerConfig activePeer, boolean established, Map<String, Configuration> configurations) { SessionType type = BgpSessionProperties.getSessionType(activePeer); SessionStatus status = established ? ESTABLISHED : NOT_ESTABLISHED; Ip localIp = activePeer.getPeerAddress(); NodeInterfacePair localInterface = getInterface(configurations.get(passiveId.getHostname()), localIp); return Row.builder(metadataMap) .put(COL_ESTABLISHED_STATUS, status) .put(COL_LOCAL_INTERFACE, localInterface) .put(COL_LOCAL_AS, passivePeer.getLocalAs()) .put(COL_LOCAL_IP, localIp) .put(COL_NODE, new Node(passiveId.getHostname())) .put(COL_REMOTE_AS, new SelfDescribingObject(Schema.LONG, activePeer.getLocalAs())) .put(COL_REMOTE_NODE, new Node(activeId.getHostname())) .put(COL_REMOTE_IP, new SelfDescribingObject(Schema.IP, activePeer.getLocalIp())) .put(COL_SESSION_TYPE, type) .put(COL_VRF, passiveId.getVrfName()) .build(); }
@Test public void getRemoteIpPassivePeer() { Prefix prefix = Prefix.create(Ip.parse("1.1.1.1"), 23); BgpPassivePeerConfig passivePeerConfig = BgpPassivePeerConfig.builder().setPeerPrefix(prefix).build(); assertThat( getRemoteIp(passivePeerConfig), equalTo(new SelfDescribingObject(Schema.PREFIX, prefix))); } }
.put(COL_NODE, node) .put(COL_VRF, "v") .put(COL_REMOTE_IP, new SelfDescribingObject(Schema.IP, Ip.parse("2.2.2.2"))) .put(getColumnName(LOCAL_IP), Ip.parse("1.1.1.1")) .build()); .put( COL_REMOTE_IP, new SelfDescribingObject(Schema.PREFIX, Prefix.create(Ip.parse("3.3.3.0"), 24))) .put(getColumnName(LOCAL_IP), Ip.parse("1.1.1.2")) .build());
.put(COL_NODE, node) .put(COL_VRF, "v") .put(COL_REMOTE_IP, new SelfDescribingObject(Schema.IP, Ip.parse("2.2.2.2"))) .put(getColumnName(LOCAL_AS), 100L) .put(getColumnName(REMOTE_AS), new SelfDescribingObject(Schema.LONG, 200L)) .put(getColumnName(LOCAL_IP), Ip.parse("1.1.1.1")) .put(getColumnName(IS_PASSIVE), false) .put( COL_REMOTE_IP, new SelfDescribingObject(Schema.PREFIX, Prefix.create(Ip.parse("3.3.3.0"), 24))) .put( getColumnName(REMOTE_AS), new SelfDescribingObject(Schema.list(Schema.LONG), ImmutableList.of(300L))) .put(getColumnName(LOCAL_IP), Ip.parse("1.1.1.2")) .put(getColumnName(IS_PASSIVE), true)