use of org.onosproject.kubevirtnetworking.api.KubevirtNetwork in project onos by opennetworkinglab.
the class KubevirtNetworkCodecTest method testKubevirtNetworkEncode.
/**
* Tests the kubevirt network encoding.
*/
@Test
public void testKubevirtNetworkEncode() {
KubevirtHostRoute hostRoute1 = new KubevirtHostRoute(IpPrefix.valueOf("10.10.10.0/24"), IpAddress.valueOf("20.20.20.1"));
KubevirtHostRoute hostRoute2 = new KubevirtHostRoute(IpPrefix.valueOf("20.20.20.0/24"), IpAddress.valueOf("10.10.10.1"));
KubevirtIpPool ipPool = new KubevirtIpPool(IpAddress.valueOf("10.10.10.100"), IpAddress.valueOf("10.10.10.200"));
KubevirtNetwork network = DefaultKubevirtNetwork.builder().networkId("net-1").name("net-1").type(KubevirtNetwork.Type.FLAT).gatewayIp(IpAddress.valueOf("10.10.10.1")).defaultRoute(true).mtu(1500).cidr("10.10.10.0/24").hostRoutes(ImmutableSet.of(hostRoute1, hostRoute2)).ipPool(ipPool).dnses(ImmutableSet.of(IpAddress.valueOf("8.8.8.8"))).build();
ObjectNode networkJson = kubevirtNetworkCodec.encode(network, context);
assertThat(networkJson, matchesKubevirtNetwork(network));
}
use of org.onosproject.kubevirtnetworking.api.KubevirtNetwork in project onos by opennetworkinglab.
the class KubevirtNetworkCodecTest method getKubevirtNetwork.
private KubevirtNetwork getKubevirtNetwork(String resourceName) throws IOException {
InputStream jsonStream = KubevirtNetworkCodecTest.class.getResourceAsStream(resourceName);
JsonNode json = context.mapper().readTree(jsonStream);
MatcherAssert.assertThat(json, notNullValue());
KubevirtNetwork network = kubevirtNetworkCodec.decode((ObjectNode) json, context);
assertThat(network, notNullValue());
return network;
}
use of org.onosproject.kubevirtnetworking.api.KubevirtNetwork in project onos by opennetworkinglab.
the class KubevirtNetworkWebResource method createNetwork.
/**
* Creates a network from the JSON input stream.
*
* @param input network JSON input stream
* @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
* is invalid or duplicated network already exists
* @onos.rsModel KubevirtNetwork
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createNetwork(InputStream input) {
log.trace(String.format(MESSAGE, "CREATE"));
KubevirtNetworkAdminService service = get(KubevirtNetworkAdminService.class);
URI location;
try {
ObjectNode jsonTree = readTreeFromStream(mapper(), input);
final KubevirtNetwork network = codec(KubevirtNetwork.class).decode(jsonTree, this);
service.createNetwork(network);
location = new URI(network.networkId());
} catch (IOException | URISyntaxException e) {
throw new IllegalArgumentException(e);
}
return Response.created(location).build();
}
use of org.onosproject.kubevirtnetworking.api.KubevirtNetwork in project onos by opennetworkinglab.
the class KubevirtNetworkWebResource method getNetworks.
/**
* Returns set of all networks.
*
* @return 200 OK with set of all networks
* @onos.rsModel KubevirtNetworks
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getNetworks() {
KubevirtNetworkAdminService service = get(KubevirtNetworkAdminService.class);
final Iterable<KubevirtNetwork> networks = service.networks();
return ok(encodeArray(KubevirtNetwork.class, "networks", networks)).build();
}
use of org.onosproject.kubevirtnetworking.api.KubevirtNetwork in project onos by opennetworkinglab.
the class KubevirtNetworkCodec method decode.
@Override
public KubevirtNetwork decode(ObjectNode json, CodecContext context) {
if (json == null || !json.isObject()) {
return null;
}
String networkId = nullIsIllegal(json.get(NETWORK_ID).asText(), NETWORK_ID + MISSING_MESSAGE);
String type = nullIsIllegal(json.get(TYPE).asText(), TYPE + MISSING_MESSAGE);
String name = nullIsIllegal(json.get(NAME).asText(), NAME + MISSING_MESSAGE);
Integer mtu = nullIsIllegal(json.get(MTU).asInt(), MTU + MISSING_MESSAGE);
String gatewayIp = nullIsIllegal(json.get(GATEWAY_IP).asText(), GATEWAY_IP + MISSING_MESSAGE);
boolean defaultRoute = nullIsIllegal(json.get(DEFAULT_ROUTE).asBoolean(), DEFAULT_ROUTE + MISSING_MESSAGE);
String cidr = nullIsIllegal(json.get(CIDR).asText(), CIDR + MISSING_MESSAGE);
KubevirtNetwork.Builder networkBuilder = DefaultKubevirtNetwork.builder().networkId(networkId).type(KubevirtNetwork.Type.valueOf(type)).name(name).mtu(mtu).gatewayIp(IpAddress.valueOf(gatewayIp)).defaultRoute(defaultRoute).cidr(cidr);
if (!type.equals(KubevirtNetwork.Type.FLAT.name())) {
JsonNode segmentIdJson = json.get(SEGMENT_ID);
if (segmentIdJson != null) {
networkBuilder.segmentId(segmentIdJson.asText());
}
}
JsonNode ipPoolJson = json.get(IP_POOL);
if (ipPoolJson != null) {
final JsonCodec<KubevirtIpPool> ipPoolCodec = context.codec(KubevirtIpPool.class);
networkBuilder.ipPool(ipPoolCodec.decode((ObjectNode) ipPoolJson.deepCopy(), context));
}
// parse host routes
Set<KubevirtHostRoute> hostRoutes = new HashSet<>();
JsonNode hostRoutesJson = json.get(HOST_ROUTES);
if (hostRoutesJson != null) {
final JsonCodec<KubevirtHostRoute> hostRouteCodec = context.codec(KubevirtHostRoute.class);
IntStream.range(0, hostRoutesJson.size()).forEach(i -> {
ObjectNode routeJson = get(hostRoutesJson, i);
hostRoutes.add(hostRouteCodec.decode(routeJson, context));
});
}
networkBuilder.hostRoutes(hostRoutes);
// parse DNSes
Set<IpAddress> dnses = new HashSet<>();
JsonNode dnsesJson = json.get(DNSES);
if (dnsesJson != null) {
for (int i = 0; i < dnsesJson.size(); i++) {
JsonNode dnsJson = dnsesJson.get(i);
if (dnsJson != null) {
dnses.add(IpAddress.valueOf(dnsJson.asText()));
}
}
}
networkBuilder.dnses(dnses);
log.trace("Network is {}", networkBuilder.build().toString());
return networkBuilder.build();
}
Aggregations