use of org.onosproject.kubevirtnetworking.api.KubevirtRouter in project onos by opennetworkinglab.
the class KubevirtNetworkingUtil method getRouterSnatIpAddress.
/**
* Returns the snat ip address with specified router.
*
* @param routerService router service
* @param internalNetworkId internal network id which is associated with the router
* @return snat ip address if exist, null otherwise
*/
public static IpAddress getRouterSnatIpAddress(KubevirtRouterService routerService, String internalNetworkId) {
KubevirtRouter router = routerService.routers().stream().filter(r -> r.internal().contains(internalNetworkId)).findAny().orElse(null);
if (router == null) {
return null;
}
String routerSnatIp = router.external().keySet().stream().findAny().orElse(null);
if (routerSnatIp == null) {
return null;
}
return Ip4Address.valueOf(routerSnatIp);
}
use of org.onosproject.kubevirtnetworking.api.KubevirtRouter in project onos by opennetworkinglab.
the class KubevirtRouterCodec method decode.
@Override
public KubevirtRouter decode(ObjectNode json, CodecContext context) {
if (json == null || !json.isObject()) {
return null;
}
String name = nullIsIllegal(json.get(NAME).asText(), NAME + MISSING_MESSAGE);
String vrouterMac = nullIsIllegal(json.get(MAC_ADDRESS).asText(), MAC_ADDRESS + MISSING_MESSAGE);
KubevirtRouter.Builder builder = DefaultKubevirtRouter.builder().name(name).mac(MacAddress.valueOf(vrouterMac));
JsonNode descriptionJson = json.get(DESCRIPTION);
if (descriptionJson != null) {
builder.description(descriptionJson.asText());
}
JsonNode enableSnatJson = json.get(ENABLE_SNAT);
if (enableSnatJson != null) {
builder.enableSnat(enableSnatJson.asBoolean());
}
JsonNode electedGwJson = json.get(GATEWAY);
if (electedGwJson != null) {
builder.electedGateway(electedGwJson.asText());
}
ArrayNode internalJson = (ArrayNode) json.get(INTERNAL);
Set<String> internal = new HashSet<>();
if (internalJson != null) {
for (int i = 0; i < internalJson.size(); i++) {
internal.add(internalJson.get(i).asText());
}
builder.internal(internal);
}
ObjectNode externalJson = (ObjectNode) json.get(EXTERNAL);
if (externalJson != null) {
Map<String, String> external = ImmutableMap.of(externalJson.get(IP_ADDRESS).asText(), externalJson.get(NETWORK).asText());
builder.external(external);
}
ObjectNode peerRouterJson = (ObjectNode) json.get(PEER_ROUTER);
if (peerRouterJson != null) {
JsonNode ipJson = peerRouterJson.get(IP_ADDRESS);
JsonNode macJson = peerRouterJson.get(MAC_ADDRESS);
if (ipJson != null && macJson != null) {
IpAddress ip = IpAddress.valueOf(ipJson.asText());
MacAddress mac = MacAddress.valueOf(macJson.asText());
KubevirtPeerRouter peer = new KubevirtPeerRouter(ip, mac);
builder.peerRouter(peer);
}
// if mac address is not specified, we will not add mac address to peer router
if (ipJson != null && macJson == null) {
IpAddress ip = IpAddress.valueOf(ipJson.asText());
KubevirtPeerRouter peer = new KubevirtPeerRouter(ip, null);
builder.peerRouter(peer);
}
}
return builder.build();
}
use of org.onosproject.kubevirtnetworking.api.KubevirtRouter in project onos by opennetworkinglab.
the class KubevirtRouterCodecTest method testKubevirtRouterEncode.
/**
* Tests the kubevirt router encoding.
*/
@Test
public void testKubevirtRouterEncode() {
KubevirtPeerRouter peerRouter = new KubevirtPeerRouter(IpAddress.valueOf("10.10.10.10"), MacAddress.valueOf("11:22:33:44:55:66"));
KubevirtRouter router = DefaultKubevirtRouter.builder().name("router-1").enableSnat(true).mac(MacAddress.valueOf("11:22:33:44:55:66")).description("router-1").internal(ImmutableSet.of("vlan-1")).external(ImmutableMap.of("10.10.10.20", "flat-1")).peerRouter(peerRouter).electedGateway("gatewayNode").build();
ObjectNode routerJson = kubevirtRouterCodec.encode(router, context);
assertThat(routerJson, matchesKubevirtRouter(router));
}
use of org.onosproject.kubevirtnetworking.api.KubevirtRouter in project onos by opennetworkinglab.
the class KubevirtRouterCodecTest method getKubevirtRouter.
private KubevirtRouter getKubevirtRouter(String resourceName) throws IOException {
InputStream jsonStream = KubevirtRouterCodecTest.class.getResourceAsStream(resourceName);
JsonNode json = context.mapper().readTree(jsonStream);
MatcherAssert.assertThat(json, notNullValue());
KubevirtRouter router = kubevirtRouterCodec.decode((ObjectNode) json, context);
assertThat(router, notNullValue());
return router;
}
use of org.onosproject.kubevirtnetworking.api.KubevirtRouter in project onos by opennetworkinglab.
the class KubevirtRouterCodecTest method testKubevirtRouterDecode.
@Test
public void testKubevirtRouterDecode() throws IOException {
KubevirtRouter router = getKubevirtRouter("KubevirtRouter.json");
assertEquals("router-1", router.name());
assertEquals("Example Virtual Router", router.description());
assertTrue(router.enableSnat());
assertEquals("11:22:33:44:55:66", router.mac().toString());
assertEquals("192.168.10.5", router.external().keySet().stream().findAny().orElse(null));
assertEquals("external-network", router.external().get("192.168.10.5"));
assertTrue(router.internal().contains("vxlan-network-1"));
assertTrue(router.internal().contains("vxlan-network-2"));
assertEquals("192.168.10.1", router.peerRouter().ipAddress().toString());
assertEquals("gatewayNode", router.electedGateway());
}
Aggregations