Mahesh Poojary S
Committed by Mahesh Poojary Huawei

[ONOS-3114] Changes on Flow Classifier Manager

Change-Id: I5cdbe2c0c9769d16381322bb6c952cdc37ecccfc
......@@ -24,49 +24,59 @@ import org.onosproject.vtnrsc.FlowClassifierId;
public interface FlowClassifierService {
/**
* Store Flow Classifier.
* Check whether Flow Classifier is present based on given Flow Classifier
* Id.
*
* @param flowClassifier Flow Classifier
* @return true if adding Flow Classifier into store is success otherwise return false.
* @param id flow classifier identifier
* @return true if flow classifier is present otherwise return false
*/
boolean createFlowClassifier(FlowClassifier flowClassifier);
boolean exists(FlowClassifierId id);
/**
* Return the existing collection of Flow Classifier.
* Returns the number of flow classifiers known to the system.
*
* @return Flow Classifier collections.
* @return number of flow classifiers
*/
Iterable<FlowClassifier> getFlowClassifiers();
int getFlowClassifierCount();
/**
* Store Flow Classifier.
*
* @param flowClassifier flow classifier
* @return true if adding flow classifier into store is success otherwise
* return false
*/
boolean createFlowClassifier(FlowClassifier flowClassifier);
/**
* Check whether Flow Classifier is present based on given Flow Classifier Id.
* Return the existing collection of Flow Classifier.
*
* @param id Flow Classifier.
* @return true if Flow Classifier is present otherwise return false.
* @return flow classifier collections
*/
boolean hasFlowClassifier(FlowClassifierId id);
Iterable<FlowClassifier> getFlowClassifiers();
/**
* Retrieve the Flow Classifier based on given Flow Classifier id.
*
* @param id Flow Classifier Id.
* @return Flow Classifier if present otherwise returns null.
* @param id flow classifier identifier
* @return flow classifier if present otherwise returns null
*/
FlowClassifier getFlowClassifier(FlowClassifierId id);
/**
* Update Flow Classifier based on given Flow Classifier Id.
*
* @param flowClassifier Flow Classifier.
* @return true if update is success otherwise return false.
* @param flowClassifier flow classifier
* @return true if flow classifier update is success otherwise return false
*/
boolean updateFlowClassifier(FlowClassifier flowClassifier);
/**
* Remove Flow Classifier from store based on given Flow Classifier Id.
*
* @param id Flow Classifier Id.
* @return true if Flow Classifier removal is success otherwise return false.
* @param id flow classifier identifier
* @return true if flow classifier removal is success otherwise return
* false
*/
boolean removeFlowClassifier(FlowClassifierId id);
}
......
......@@ -73,28 +73,19 @@ public class FlowClassifierManager implements FlowClassifierService {
}
@Override
public boolean createFlowClassifier(FlowClassifier flowClassifier) {
log.debug("createFlowClassifier");
checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL);
FlowClassifierId id = flowClassifier.flowClassifierId();
flowClassifierStore.put(id, flowClassifier);
if (!flowClassifierStore.containsKey(id)) {
log.debug("Flow Classifier creation is failed whose identifier is {}.", id.toString());
return false;
}
return true;
public boolean exists(FlowClassifierId id) {
checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
return flowClassifierStore.containsKey(id);
}
@Override
public Iterable<FlowClassifier> getFlowClassifiers() {
return ImmutableList.copyOf(flowClassifierStore.values());
public int getFlowClassifierCount() {
return flowClassifierStore.size();
}
@Override
public boolean hasFlowClassifier(FlowClassifierId id) {
checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
return flowClassifierStore.containsKey(id);
public Iterable<FlowClassifier> getFlowClassifiers() {
return ImmutableList.copyOf(flowClassifierStore.values());
}
@Override
......@@ -104,10 +95,36 @@ public class FlowClassifierManager implements FlowClassifierService {
}
@Override
public boolean updateFlowClassifier(FlowClassifier flowClassifier) {
public boolean createFlowClassifier(FlowClassifier flowClassifier) {
log.debug("createFlowClassifier");
checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL);
FlowClassifierId id = flowClassifier.flowClassifierId();
flowClassifierStore.put(id, flowClassifier);
if (!flowClassifierStore.containsKey(id)) {
log.debug("Flow Classifier creation is failed whose identifier is {}.", id.toString());
return false;
}
return true;
}
@Override
public boolean updateFlowClassifier(FlowClassifier flowClassifier) {
checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL);
if (!flowClassifierStore.containsKey(flowClassifier.flowClassifierId())) {
log.debug("The flowClassifier is not exist whose identifier was {} ", flowClassifier.flowClassifierId()
.toString());
return false;
}
flowClassifierStore.put(flowClassifier.flowClassifierId(), flowClassifier);
if (!flowClassifier.equals(flowClassifierStore.get(flowClassifier.flowClassifierId()))) {
log.debug("Updation of flowClassifier is failed whose identifier was {} ", flowClassifier
.flowClassifierId().toString());
return false;
}
return true;
}
......
......@@ -15,7 +15,6 @@
*/
package org.onosproject.vtnweb.resources;
import static javax.ws.rs.core.Response.Status.NOT_FOUND;
import static javax.ws.rs.core.Response.Status.OK;
import static org.onlab.util.Tools.nullIsNotFound;
......@@ -54,7 +53,6 @@ public class FlowClassifierWebResource extends AbstractWebResource {
private final Logger log = LoggerFactory.getLogger(FlowClassifierWebResource.class);
final FlowClassifierService service = get(FlowClassifierService.class);
public static final String FLOW_CLASSIFIER_NOT_FOUND = "Flow classifier not found";
/**
......@@ -65,7 +63,7 @@ public class FlowClassifierWebResource extends AbstractWebResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getFlowClassifiers() {
final Iterable<FlowClassifier> flowClassifiers = service.getFlowClassifiers();
Iterable<FlowClassifier> flowClassifiers = get(FlowClassifierService.class).getFlowClassifiers();
ObjectNode result = new ObjectMapper().createObjectNode();
ArrayNode flowClassifierEntry = result.putArray("flow_classifiers");
if (flowClassifiers != null) {
......@@ -79,19 +77,16 @@ public class FlowClassifierWebResource extends AbstractWebResource {
/**
* Get details of a flow classifier.
*
* @param id flow classifier id
* @param id
* flow classifier id
* @return 200 OK , 404 if given identifier does not exist
*/
@GET
@Path("{flow_id}")
@Produces(MediaType.APPLICATION_JSON)
public Response getFlowClassifier(@PathParam("flow_id") String id) {
if (!service.hasFlowClassifier(FlowClassifierId.of(id))) {
return Response.status(NOT_FOUND).entity(FLOW_CLASSIFIER_NOT_FOUND).build();
}
FlowClassifier flowClassifier = nullIsNotFound(service.getFlowClassifier(FlowClassifierId.of(id)),
FLOW_CLASSIFIER_NOT_FOUND);
FlowClassifier flowClassifier = nullIsNotFound(
get(FlowClassifierService.class).getFlowClassifier(FlowClassifierId.of(id)), FLOW_CLASSIFIER_NOT_FOUND);
ObjectNode result = new ObjectMapper().createObjectNode();
result.set("flow_classifier", new FlowClassifierCodec().encode(flowClassifier, this));
......@@ -102,7 +97,8 @@ public class FlowClassifierWebResource extends AbstractWebResource {
/**
* Creates and stores a new flow classifier.
*
* @param stream flow classifier from JSON
* @param stream
* flow classifier from JSON
* @return status of the request - CREATED if the JSON is correct,
* BAD_REQUEST if the JSON is invalid
*/
......@@ -116,7 +112,8 @@ public class FlowClassifierWebResource extends AbstractWebResource {
JsonNode flow = jsonTree.get("flow_classifier");
FlowClassifier flowClassifier = new FlowClassifierCodec().decode((ObjectNode) flow, this);
Boolean issuccess = nullIsNotFound(service.createFlowClassifier(flowClassifier), FLOW_CLASSIFIER_NOT_FOUND);
Boolean issuccess = nullIsNotFound(get(FlowClassifierService.class).createFlowClassifier(flowClassifier),
FLOW_CLASSIFIER_NOT_FOUND);
return Response.status(OK).entity(issuccess.toString()).build();
} catch (IOException ex) {
log.error("Exception while creating flow classifier {}.", ex.toString());
......@@ -127,8 +124,10 @@ public class FlowClassifierWebResource extends AbstractWebResource {
/**
* Update details of a flow classifier.
*
* @param id flow classifier id
* @param stream InputStream
* @param id
* flow classifier id
* @param stream
* InputStream
* @return 200 OK, 404 if given identifier does not exist
*/
@PUT
......@@ -141,7 +140,8 @@ public class FlowClassifierWebResource extends AbstractWebResource {
JsonNode jsonTree = mapper().readTree(stream);
JsonNode flow = jsonTree.get("flow_classifier");
FlowClassifier flowClassifier = new FlowClassifierCodec().decode((ObjectNode) flow, this);
Boolean result = nullIsNotFound(service.updateFlowClassifier(flowClassifier), FLOW_CLASSIFIER_NOT_FOUND);
Boolean result = nullIsNotFound(get(FlowClassifierService.class).updateFlowClassifier(flowClassifier),
FLOW_CLASSIFIER_NOT_FOUND);
return Response.status(OK).entity(result.toString()).build();
} catch (IOException e) {
log.error("Update flow classifier failed because of exception {}.", e.toString());
......@@ -152,14 +152,16 @@ public class FlowClassifierWebResource extends AbstractWebResource {
/**
* Delete details of a flow classifier.
*
* @param id flow classifier id
* @param id
* flow classifier id
*/
@Path("{flow_id}")
@DELETE
public void deleteFlowClassifier(@PathParam("flow_id") String id) {
log.debug("Deletes flow classifier by identifier {}.", id);
FlowClassifierId flowClassifierId = FlowClassifierId.of(id);
Boolean issuccess = nullIsNotFound(service.removeFlowClassifier(flowClassifierId), FLOW_CLASSIFIER_NOT_FOUND);
Boolean issuccess = nullIsNotFound(get(FlowClassifierService.class).removeFlowClassifier(flowClassifierId),
FLOW_CLASSIFIER_NOT_FOUND);
}
}
......
......@@ -227,7 +227,7 @@ public class FlowClassifierResourceTest extends VtnResourceTest {
final Set<FlowClassifier> flowClassifiers = new HashSet<>();
flowClassifiers.add(flowClassifier1);
expect(flowClassifierService.hasFlowClassifier(anyObject())).andReturn(true).anyTimes();
expect(flowClassifierService.exists(anyObject())).andReturn(true).anyTimes();
expect(flowClassifierService.getFlowClassifier(anyObject())).andReturn(flowClassifier1).anyTimes();
replay(flowClassifierService);
......