use of org.onosproject.net.flow.FlowRuleService in project onos by opennetworkinglab.
the class TelemetryVflowListCommand method doExecute.
@Override
protected void doExecute() {
CoreService coreService = get(CoreService.class);
FlowRuleService flowService = get(FlowRuleService.class);
ApplicationId appId = coreService.getAppId(OPENSTACK_TELEMETRY_APP_ID);
List<FlowEntry> flows = Lists.newArrayList(flowService.getFlowEntriesById(appId));
print(FORMAT, "SrcIp", "SrcPort", "DstIp", "DstPort", "Protocol");
for (FlowEntry entry : flows) {
TrafficSelector selector = entry.selector();
IpPrefix srcIp = ((IPCriterion) selector.getCriterion(IPV4_SRC)).ip();
IpPrefix dstIp = ((IPCriterion) selector.getCriterion(IPV4_DST)).ip();
TpPort srcPort = TpPort.tpPort(0);
TpPort dstPort = TpPort.tpPort(0);
String protocolStr = "ANY";
Criterion ipProtocolCriterion = selector.getCriterion(IP_PROTO);
if (ipProtocolCriterion != null) {
short protocol = ((IPProtocolCriterion) selector.getCriterion(IP_PROTO)).protocol();
if (protocol == PROTOCOL_TCP) {
srcPort = ((TcpPortCriterion) selector.getCriterion(TCP_SRC)).tcpPort();
dstPort = ((TcpPortCriterion) selector.getCriterion(TCP_DST)).tcpPort();
protocolStr = TCP;
}
if (protocol == PROTOCOL_UDP) {
srcPort = ((UdpPortCriterion) selector.getCriterion(UDP_SRC)).udpPort();
dstPort = ((UdpPortCriterion) selector.getCriterion(UDP_SRC)).udpPort();
protocolStr = UDP;
}
}
print(FORMAT, srcIp.toString(), srcPort.toString(), dstIp.toString(), dstPort.toString(), protocolStr);
}
}
use of org.onosproject.net.flow.FlowRuleService in project onos by opennetworkinglab.
the class FlowRuleProgrammableServerImpl method getFlowEntries.
@Override
public Collection<FlowEntry> getFlowEntries() {
DeviceId deviceId = getDeviceId();
checkNotNull(deviceId, MSG_DEVICE_ID_NULL);
// Expected FlowEntries installed through ONOS
FlowRuleService flowService = getHandler().get(FlowRuleService.class);
Iterable<FlowEntry> flowEntries = flowService.getFlowEntries(deviceId);
// Hit the path that provides the server's flow rules
InputStream response = null;
try {
response = getController().get(deviceId, URL_RULE_MANAGEMENT, JSON);
} catch (ProcessingException pEx) {
log.error("Failed to get NIC flow entries from device: {}", deviceId);
return Collections.EMPTY_LIST;
}
// Load the JSON into objects
ObjectMapper mapper = new ObjectMapper();
ObjectNode objNode = null;
try {
Map<String, Object> jsonMap = mapper.readValue(response, Map.class);
JsonNode jsonNode = mapper.convertValue(jsonMap, JsonNode.class);
objNode = (ObjectNode) jsonNode;
} catch (IOException ioEx) {
log.error("Failed to get NIC flow entries from device: {}", deviceId);
return Collections.EMPTY_LIST;
}
if (objNode == null) {
log.error("Failed to get NIC flow entries from device: {}", deviceId);
return Collections.EMPTY_LIST;
}
JsonNode scsNode = objNode.path(PARAM_RULES);
// Here we store the trully installed rules
Collection<FlowEntry> actualFlowEntries = Sets.<FlowEntry>newConcurrentHashSet();
for (JsonNode scNode : scsNode) {
String scId = get(scNode, PARAM_ID);
String rxFilter = get(scNode.path(PARAM_NIC_RX_FILTER), PARAM_NIC_RX_METHOD);
// Only Flow-based RxFilter is permitted
if (RxFilter.getByName(rxFilter) != RxFilter.FLOW) {
log.warn("Device with Rx filter {} is not managed by this driver", rxFilter.toString().toUpperCase());
continue;
}
// Each device might have multiple NICs
for (JsonNode nicNode : scNode.path(PARAM_NICS)) {
JsonNode cpusNode = nicNode.path(PARAM_CPUS);
// Each NIC can dispatch to multiple CPU cores
for (JsonNode cpuNode : cpusNode) {
String cpuId = get(cpuNode, PARAM_ID);
JsonNode rulesNode = cpuNode.path(PARAM_RULES);
// Multiple rules might correspond to each CPU core
for (JsonNode ruleNode : rulesNode) {
long ruleId = ruleNode.path(PARAM_ID).asLong();
String ruleContent = get(ruleNode, PARAM_RULE_CONTENT);
// Search for this rule ID in ONOS's store
FlowRule r = findRuleInFlowEntries(flowEntries, ruleId);
// Local rule, not present in the controller => Ignore
if (r == null) {
continue;
// Rule trully present in the data plane => Add
} else {
actualFlowEntries.add(new DefaultFlowEntry(r, FlowEntry.FlowEntryState.ADDED, 0, 0, 0));
}
}
}
}
}
return actualFlowEntries;
}
use of org.onosproject.net.flow.FlowRuleService in project onos by opennetworkinglab.
the class FlowsWebResource method getPendingFlows.
/**
* Gets all pending flow entries. Returns array of all pending flow rules in the system.
*
* @return 200 OK with a collection of flows
* @onos.rsModel FlowEntries
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("pending")
public Response getPendingFlows() {
ObjectNode root = mapper().createObjectNode();
ArrayNode flowsNode = root.putArray(FLOWS);
FlowRuleService service = get(FlowRuleService.class);
Iterable<Device> devices = get(DeviceService.class).getDevices();
for (Device device : devices) {
Iterable<FlowEntry> flowEntries = service.getFlowEntries(device.id());
if (flowEntries != null) {
for (FlowEntry entry : flowEntries) {
if ((entry.state() == FlowEntry.FlowEntryState.PENDING_ADD) || (entry.state() == FlowEntry.FlowEntryState.PENDING_REMOVE)) {
flowsNode.add(codec(FlowEntry.class).encode(entry, this));
}
}
}
}
return ok(root).build();
}
use of org.onosproject.net.flow.FlowRuleService in project onos by opennetworkinglab.
the class FlowsWebResource method removeFlowByAppId.
/**
* Removes flow rules by application ID.
* Removes a collection of flow rules generated by the given application.
*
* @param appId application identifier
* @return 204 NO CONTENT
*/
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Path("application/{appId}")
public Response removeFlowByAppId(@PathParam("appId") String appId) {
FlowRuleService service = get(FlowRuleService.class);
ApplicationService appService = get(ApplicationService.class);
ApplicationId idInstant = nullIsNotFound(appService.getId(appId), APP_ID_NOT_FOUND);
service.removeFlowRulesById(idInstant);
return Response.noContent().build();
}
use of org.onosproject.net.flow.FlowRuleService in project onos by opennetworkinglab.
the class FlowsWebResource method deleteFlowByDeviceIdAndFlowId.
/**
* Removes flow rule. Removes the specified flow rule.
*
* @param deviceId device identifier
* @param flowId flow rule identifier
* @return 204 NO CONTENT
*/
@DELETE
@Path("{deviceId}/{flowId}")
public Response deleteFlowByDeviceIdAndFlowId(@PathParam("deviceId") String deviceId, @PathParam("flowId") long flowId) {
FlowRuleService service = get(FlowRuleService.class);
Iterable<FlowEntry> flowEntries = service.getFlowEntries(DeviceId.deviceId(deviceId));
if (!flowEntries.iterator().hasNext()) {
throw new ItemNotFoundException(DEVICE_NOT_FOUND);
}
StreamSupport.stream(flowEntries.spliterator(), false).filter(entry -> entry.id().value() == flowId).forEach(service::removeFlowRules);
return Response.noContent().build();
}
Aggregations