use of org.onosproject.kubevirtnetworking.api.KubevirtIpPool in project onos by opennetworkinglab.
the class KubevirtListIpAddressCommand method doExecute.
@Override
protected void doExecute() throws Exception {
KubevirtNetworkService service = get(KubevirtNetworkService.class);
if (networkId == null) {
error("No network identifier was specified");
return;
}
KubevirtNetwork network = service.network(networkId);
if (network == null) {
print("No network was found with the given network ID");
return;
}
KubevirtIpPool pool = network.ipPool();
if (pool == null) {
print("No IP pool was found with the given network ID");
return;
}
String format = genFormatString(ImmutableList.of(CLI_IP_ADDRESS_LENGTH, CLI_IP_ADDRESS_AVAILABILITY));
print(format, "IP Address", "Availability");
List<IpAddress> sortedAllocatedIps = new ArrayList<>(pool.allocatedIps());
Collections.sort(sortedAllocatedIps);
for (IpAddress ip : sortedAllocatedIps) {
print(format, ip.toString(), "[ X ]");
}
List<IpAddress> sortedAvailableIps = new ArrayList<>(pool.availableIps());
Collections.sort(sortedAvailableIps);
for (IpAddress ip : sortedAvailableIps) {
print(format, ip.toString(), "[ O ]");
}
}
use of org.onosproject.kubevirtnetworking.api.KubevirtIpPool in project onos by opennetworkinglab.
the class KubevirtNetworkJsonMatcher method matchesSafely.
@Override
protected boolean matchesSafely(JsonNode jsonNode, Description description) {
// check network ID
String jsonNetworkId = jsonNode.get(NETWORK_ID).asText();
String networkId = network.networkId();
if (!jsonNetworkId.equals(networkId)) {
description.appendText("network ID was " + jsonNetworkId);
return false;
}
// check type
String jsonType = jsonNode.get(TYPE).asText();
String type = network.type().name();
if (!jsonType.equals(type)) {
description.appendText("network type was " + jsonType);
return false;
}
// check name
String jsonName = jsonNode.get(NAME).asText();
String name = network.name();
if (!jsonName.equals(name)) {
description.appendText("network name was " + jsonName);
return false;
}
// check MTU
int jsonMtu = jsonNode.get(MTU).asInt();
int mtu = network.mtu();
if (jsonMtu != mtu) {
description.appendText("network MTU was " + jsonMtu);
return false;
}
// check gateway IP
String jsonGatewayIp = jsonNode.get(GATEWAY_IP).asText();
String gatewayIp = network.gatewayIp().toString();
if (!jsonGatewayIp.equals(gatewayIp)) {
description.appendText("gateway IP was " + jsonGatewayIp);
return false;
}
// check default route
boolean jsonDefaultRoute = jsonNode.get(DEFAULT_ROUTE).asBoolean();
boolean defaultRoute = network.defaultRoute();
if (jsonDefaultRoute != defaultRoute) {
description.appendText("Default route was " + jsonDefaultRoute);
return false;
}
// check CIDR
String jsonCidr = jsonNode.get(CIDR).asText();
String cidr = network.cidr();
if (!jsonCidr.equals(cidr)) {
description.appendText("CIDR was " + jsonCidr);
return false;
}
// check segment ID
JsonNode jsonSegmentId = jsonNode.get(SEGMENT_ID);
if (jsonSegmentId != null) {
String segmentId = network.segmentId();
if (!jsonSegmentId.asText().equals(segmentId)) {
description.appendText("segment ID was " + jsonSegmentId.asText());
return false;
}
}
// check ip pool
JsonNode jsonIpPool = jsonNode.get(IP_POOL);
if (jsonIpPool != null) {
KubevirtIpPool ipPool = network.ipPool();
KubevirtIpPoolJsonMatcher ipPoolMatcher = KubevirtIpPoolJsonMatcher.matchesKubevirtIpPool(ipPool);
if (ipPoolMatcher.matches(jsonIpPool)) {
return true;
} else {
description.appendText("IP pool was " + jsonIpPool.toString());
return false;
}
}
// check host routes
JsonNode jsonHostRoutes = jsonNode.get(HOST_ROUTES);
if (jsonHostRoutes != null) {
if (jsonHostRoutes.size() != network.hostRoutes().size()) {
description.appendText("host routes size was " + jsonHostRoutes.size());
return false;
}
for (KubevirtHostRoute hostRoute : network.hostRoutes()) {
boolean routeFound = false;
for (int routeIndex = 0; routeIndex < jsonHostRoutes.size(); routeIndex++) {
KubevirtHostRouteJsonMatcher routeMatcher = KubevirtHostRouteJsonMatcher.matchesKubevirtHostRoute(hostRoute);
if (routeMatcher.matches(jsonHostRoutes.get(routeIndex))) {
routeFound = true;
break;
}
}
if (!routeFound) {
description.appendText("Host route not found " + hostRoute.toString());
return false;
}
}
}
// check dnses
JsonNode jsonDnses = jsonNode.get(DNSES);
if (jsonDnses != null) {
if (jsonDnses.size() != network.dnses().size()) {
description.appendText("DNSes size was " + jsonDnses.size());
return false;
}
for (IpAddress dns : network.dnses()) {
boolean dnsFound = false;
for (int dnsIndex = 0; dnsIndex < jsonDnses.size(); dnsIndex++) {
String jsonDns = jsonDnses.get(dnsIndex).asText();
if (jsonDns.equals(dns.toString())) {
dnsFound = true;
break;
}
}
if (!dnsFound) {
description.appendText("DNS not found " + dns.toString());
return false;
}
}
}
return true;
}
use of org.onosproject.kubevirtnetworking.api.KubevirtIpPool 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();
}
use of org.onosproject.kubevirtnetworking.api.KubevirtIpPool 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.KubevirtIpPool in project onos by opennetworkinglab.
the class KubevirtNetworkWebResourceTest method setUpTest.
/**
* Sets up the global values for all the tests.
*/
@Before
public void setUpTest() {
final CodecManager codecService = new CodecManager();
codecService.activate();
codecService.registerCodec(KubevirtHostRoute.class, new KubevirtHostRouteCodec());
codecService.registerCodec(KubevirtIpPool.class, new KubevirtIpPoolCodec());
codecService.registerCodec(KubevirtNetwork.class, new KubevirtNetworkCodec());
ServiceDirectory testDirectory = new TestServiceDirectory().add(KubevirtNetworkAdminService.class, mockAdminService).add(CodecService.class, codecService);
setServiceDirectory(testDirectory);
network = DefaultKubevirtNetwork.builder().networkId("network").name("network").type(KubevirtNetwork.Type.FLAT).cidr("10.10.10.0/24").mtu(1500).gatewayIp(IpAddress.valueOf("10.10.10.1")).defaultRoute(true).ipPool(new KubevirtIpPool(IpAddress.valueOf("10.10.10.100"), IpAddress.valueOf("10.10.10.200"))).build();
}
Aggregations