use of org.onosproject.kubevirtnetworking.api.KubevirtNetwork in project onos by opennetworkinglab.
the class KubevirtNetworkingUtil method getPorts.
/**
* Obtains the kubevirt port from kubevirt VMI.
*
* @param nodeService kubevirt node service
* @param networks set of existing kubevirt networks
* @param resource VMI definition
* @return kubevirt ports attached to the VMI
*/
public static Set<KubevirtPort> getPorts(KubevirtNodeService nodeService, Set<KubevirtNetwork> networks, String resource) {
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode json = mapper.readTree(resource);
JsonNode statusJson = json.get(STATUS);
ArrayNode interfacesJson = (ArrayNode) statusJson.get(INTERFACES);
String vmName = parseResourceName(resource);
KubevirtPort.Builder builder = DefaultKubevirtPort.builder();
String nodeName = parseVmiNodeName(resource);
if (nodeName != null && nodeService.node(nodeName) != null) {
builder.deviceId(nodeService.node(nodeName).intgBridge());
}
if (interfacesJson == null) {
return ImmutableSet.of();
}
Set<KubevirtPort> ports = new HashSet<>();
for (JsonNode interfaceJson : interfacesJson) {
JsonNode jsonName = interfaceJson.get(NAME);
// interface, we skip inspect this interface
if (jsonName == null) {
continue;
}
String name = jsonName.asText();
KubevirtNetwork network = networks.stream().filter(n -> (NETWORK_PREFIX + n.name()).equals(name) || (n.name() + "-net").equals(name)).findAny().orElse(null);
if (network != null) {
// FIXME: we do not update IP address, as learning IP address
// requires much more time due to the lag from VM agent
String mac = interfaceJson.get(MAC).asText();
builder.vmName(vmName).macAddress(MacAddress.valueOf(mac)).networkId(network.networkId());
ports.add(builder.build());
}
}
return ports;
} catch (IOException e) {
log.error("Failed to parse port info from VMI object", e);
}
return ImmutableSet.of();
}
use of org.onosproject.kubevirtnetworking.api.KubevirtNetwork in project onos by opennetworkinglab.
the class KubevirtNetworkWebResource method hasNetwork.
/**
* Checks whether the network exists with given network id.
*
* @param id network identifier
* @return 200 OK with true/false result
*/
@GET
@Path("exist/{id}")
public Response hasNetwork(@PathParam("id") String id) {
log.trace(String.format(MESSAGE, "QUERY " + id));
KubevirtNetworkAdminService service = get(KubevirtNetworkAdminService.class);
ObjectNode root = mapper().createObjectNode();
KubevirtNetwork network = service.network(id);
if (network == null) {
root.put(RESULT, false);
} else {
root.put(RESULT, true);
}
return Response.ok(root).build();
}
use of org.onosproject.kubevirtnetworking.api.KubevirtNetwork in project onos by opennetworkinglab.
the class KubevirtNetworkWebResource method updateNetwork.
/**
* Updates the network with the specified identifier.
*
* @param id network identifier
* @param input network JSON input stream
* @return 200 OK with the updated network, 400 BAD_REQUEST if the requested
* network does not exist
* @onos.rsModel KubevirtNetwork
*/
@PUT
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response updateNetwork(@PathParam("id") String id, InputStream input) {
log.trace(String.format(MESSAGE, "UPDATED"));
KubevirtNetworkAdminService service = get(KubevirtNetworkAdminService.class);
try {
ObjectNode jsonTree = readTreeFromStream(mapper(), input);
JsonNode specifiedNetworkId = jsonTree.get("networkId");
if (specifiedNetworkId != null && !specifiedNetworkId.asText().equals(id)) {
throw new IllegalArgumentException(NETWORK_INVALID);
}
final KubevirtNetwork network = codec(KubevirtNetwork.class).decode(jsonTree, this);
service.updateNetwork(network);
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
return Response.ok().build();
}
use of org.onosproject.kubevirtnetworking.api.KubevirtNetwork in project onos by opennetworkinglab.
the class KubevirtNetworkWebResource method getNetworkById.
/**
* Returns the network with the specified identifier.
*
* @param id network identifier
* @return 200 OK with a network, 404 not found
* @onos.rsModel KubevirtNetwork
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{id}")
public Response getNetworkById(@PathParam("id") String id) {
KubevirtNetworkAdminService service = get(KubevirtNetworkAdminService.class);
final KubevirtNetwork network = nullIsNotFound(service.network(id), NETWORK_NOT_FOUND + id);
return ok(codec(KubevirtNetwork.class).encode(network, this)).build();
}
use of org.onosproject.kubevirtnetworking.api.KubevirtNetwork in project onos by opennetworkinglab.
the class KubevirtNetworkCodecTest method testKubevirtNetworkDecode.
/**
* Tests the kubevirt network decoding.
*
* @throws IOException io exception
*/
@Test
public void testKubevirtNetworkDecode() throws IOException {
KubevirtNetwork network = getKubevirtNetwork("KubevirtNetwork.json");
assertThat(network.networkId(), is("network-1"));
assertThat(network.name(), is("network-1"));
assertThat(network.type().name(), is("FLAT"));
assertThat(network.cidr(), is("10.10.0.0/24"));
assertThat(network.gatewayIp().toString(), is("10.10.0.1"));
assertThat(network.defaultRoute(), is(true));
assertThat(network.ipPool().start().toString(), is("10.10.10.100"));
assertThat(network.ipPool().end().toString(), is("10.10.10.200"));
assertThat(network.dnses().size(), is(1));
KubevirtHostRoute route = network.hostRoutes().stream().findFirst().orElse(null);
assertThat(route, is(new KubevirtHostRoute(IpPrefix.valueOf("10.10.10.0/24"), IpAddress.valueOf("10.10.10.1"))));
}
Aggregations