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; ...@@ -24,49 +24,59 @@ import org.onosproject.vtnrsc.FlowClassifierId;
24 public interface FlowClassifierService { 24 public interface FlowClassifierService {
25 25
26 /** 26 /**
27 - * Store Flow Classifier. 27 + * Check whether Flow Classifier is present based on given Flow Classifier
28 + * Id.
28 * 29 *
29 - * @param flowClassifier Flow Classifier 30 + * @param id flow classifier identifier
30 - * @return true if adding Flow Classifier into store is success otherwise return false. 31 + * @return true if flow classifier is present otherwise return false
31 */ 32 */
32 - boolean createFlowClassifier(FlowClassifier flowClassifier); 33 + boolean exists(FlowClassifierId id);
33 34
34 /** 35 /**
35 - * Return the existing collection of Flow Classifier. 36 + * Returns the number of flow classifiers known to the system.
36 * 37 *
37 - * @return Flow Classifier collections. 38 + * @return number of flow classifiers
38 */ 39 */
39 - Iterable<FlowClassifier> getFlowClassifiers(); 40 + int getFlowClassifierCount();
41 +
42 + /**
43 + * Store Flow Classifier.
44 + *
45 + * @param flowClassifier flow classifier
46 + * @return true if adding flow classifier into store is success otherwise
47 + * return false
48 + */
49 + boolean createFlowClassifier(FlowClassifier flowClassifier);
40 50
41 /** 51 /**
42 - * Check whether Flow Classifier is present based on given Flow Classifier Id. 52 + * Return the existing collection of Flow Classifier.
43 * 53 *
44 - * @param id Flow Classifier. 54 + * @return flow classifier collections
45 - * @return true if Flow Classifier is present otherwise return false.
46 */ 55 */
47 - boolean hasFlowClassifier(FlowClassifierId id); 56 + Iterable<FlowClassifier> getFlowClassifiers();
48 57
49 /** 58 /**
50 * Retrieve the Flow Classifier based on given Flow Classifier id. 59 * Retrieve the Flow Classifier based on given Flow Classifier id.
51 * 60 *
52 - * @param id Flow Classifier Id. 61 + * @param id flow classifier identifier
53 - * @return Flow Classifier if present otherwise returns null. 62 + * @return flow classifier if present otherwise returns null
54 */ 63 */
55 FlowClassifier getFlowClassifier(FlowClassifierId id); 64 FlowClassifier getFlowClassifier(FlowClassifierId id);
56 65
57 /** 66 /**
58 * Update Flow Classifier based on given Flow Classifier Id. 67 * Update Flow Classifier based on given Flow Classifier Id.
59 * 68 *
60 - * @param flowClassifier Flow Classifier. 69 + * @param flowClassifier flow classifier
61 - * @return true if update is success otherwise return false. 70 + * @return true if flow classifier update is success otherwise return false
62 */ 71 */
63 boolean updateFlowClassifier(FlowClassifier flowClassifier); 72 boolean updateFlowClassifier(FlowClassifier flowClassifier);
64 73
65 /** 74 /**
66 * Remove Flow Classifier from store based on given Flow Classifier Id. 75 * Remove Flow Classifier from store based on given Flow Classifier Id.
67 * 76 *
68 - * @param id Flow Classifier Id. 77 + * @param id flow classifier identifier
69 - * @return true if Flow Classifier removal is success otherwise return false. 78 + * @return true if flow classifier removal is success otherwise return
79 + * false
70 */ 80 */
71 boolean removeFlowClassifier(FlowClassifierId id); 81 boolean removeFlowClassifier(FlowClassifierId id);
72 } 82 }
......
...@@ -73,28 +73,19 @@ public class FlowClassifierManager implements FlowClassifierService { ...@@ -73,28 +73,19 @@ public class FlowClassifierManager implements FlowClassifierService {
73 } 73 }
74 74
75 @Override 75 @Override
76 - public boolean createFlowClassifier(FlowClassifier flowClassifier) { 76 + public boolean exists(FlowClassifierId id) {
77 - log.debug("createFlowClassifier"); 77 + checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
78 - checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL); 78 + return flowClassifierStore.containsKey(id);
79 - FlowClassifierId id = flowClassifier.flowClassifierId();
80 -
81 - flowClassifierStore.put(id, flowClassifier);
82 - if (!flowClassifierStore.containsKey(id)) {
83 - log.debug("Flow Classifier creation is failed whose identifier is {}.", id.toString());
84 - return false;
85 - }
86 - return true;
87 } 79 }
88 80
89 @Override 81 @Override
90 - public Iterable<FlowClassifier> getFlowClassifiers() { 82 + public int getFlowClassifierCount() {
91 - return ImmutableList.copyOf(flowClassifierStore.values()); 83 + return flowClassifierStore.size();
92 } 84 }
93 85
94 @Override 86 @Override
95 - public boolean hasFlowClassifier(FlowClassifierId id) { 87 + public Iterable<FlowClassifier> getFlowClassifiers() {
96 - checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL); 88 + return ImmutableList.copyOf(flowClassifierStore.values());
97 - return flowClassifierStore.containsKey(id);
98 } 89 }
99 90
100 @Override 91 @Override
...@@ -104,10 +95,36 @@ public class FlowClassifierManager implements FlowClassifierService { ...@@ -104,10 +95,36 @@ public class FlowClassifierManager implements FlowClassifierService {
104 } 95 }
105 96
106 @Override 97 @Override
107 - public boolean updateFlowClassifier(FlowClassifier flowClassifier) { 98 + public boolean createFlowClassifier(FlowClassifier flowClassifier) {
99 + log.debug("createFlowClassifier");
108 checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL); 100 checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL);
109 FlowClassifierId id = flowClassifier.flowClassifierId(); 101 FlowClassifierId id = flowClassifier.flowClassifierId();
102 +
110 flowClassifierStore.put(id, flowClassifier); 103 flowClassifierStore.put(id, flowClassifier);
104 + if (!flowClassifierStore.containsKey(id)) {
105 + log.debug("Flow Classifier creation is failed whose identifier is {}.", id.toString());
106 + return false;
107 + }
108 + return true;
109 + }
110 +
111 + @Override
112 + public boolean updateFlowClassifier(FlowClassifier flowClassifier) {
113 + checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL);
114 +
115 + if (!flowClassifierStore.containsKey(flowClassifier.flowClassifierId())) {
116 + log.debug("The flowClassifier is not exist whose identifier was {} ", flowClassifier.flowClassifierId()
117 + .toString());
118 + return false;
119 + }
120 +
121 + flowClassifierStore.put(flowClassifier.flowClassifierId(), flowClassifier);
122 +
123 + if (!flowClassifier.equals(flowClassifierStore.get(flowClassifier.flowClassifierId()))) {
124 + log.debug("Updation of flowClassifier is failed whose identifier was {} ", flowClassifier
125 + .flowClassifierId().toString());
126 + return false;
127 + }
111 return true; 128 return true;
112 } 129 }
113 130
......
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
15 */ 15 */
16 package org.onosproject.vtnweb.resources; 16 package org.onosproject.vtnweb.resources;
17 17
18 -import static javax.ws.rs.core.Response.Status.NOT_FOUND;
19 import static javax.ws.rs.core.Response.Status.OK; 18 import static javax.ws.rs.core.Response.Status.OK;
20 import static org.onlab.util.Tools.nullIsNotFound; 19 import static org.onlab.util.Tools.nullIsNotFound;
21 20
...@@ -54,7 +53,6 @@ public class FlowClassifierWebResource extends AbstractWebResource { ...@@ -54,7 +53,6 @@ public class FlowClassifierWebResource extends AbstractWebResource {
54 53
55 private final Logger log = LoggerFactory.getLogger(FlowClassifierWebResource.class); 54 private final Logger log = LoggerFactory.getLogger(FlowClassifierWebResource.class);
56 55
57 - final FlowClassifierService service = get(FlowClassifierService.class);
58 public static final String FLOW_CLASSIFIER_NOT_FOUND = "Flow classifier not found"; 56 public static final String FLOW_CLASSIFIER_NOT_FOUND = "Flow classifier not found";
59 57
60 /** 58 /**
...@@ -65,7 +63,7 @@ public class FlowClassifierWebResource extends AbstractWebResource { ...@@ -65,7 +63,7 @@ public class FlowClassifierWebResource extends AbstractWebResource {
65 @GET 63 @GET
66 @Produces(MediaType.APPLICATION_JSON) 64 @Produces(MediaType.APPLICATION_JSON)
67 public Response getFlowClassifiers() { 65 public Response getFlowClassifiers() {
68 - final Iterable<FlowClassifier> flowClassifiers = service.getFlowClassifiers(); 66 + Iterable<FlowClassifier> flowClassifiers = get(FlowClassifierService.class).getFlowClassifiers();
69 ObjectNode result = new ObjectMapper().createObjectNode(); 67 ObjectNode result = new ObjectMapper().createObjectNode();
70 ArrayNode flowClassifierEntry = result.putArray("flow_classifiers"); 68 ArrayNode flowClassifierEntry = result.putArray("flow_classifiers");
71 if (flowClassifiers != null) { 69 if (flowClassifiers != null) {
...@@ -79,19 +77,16 @@ public class FlowClassifierWebResource extends AbstractWebResource { ...@@ -79,19 +77,16 @@ public class FlowClassifierWebResource extends AbstractWebResource {
79 /** 77 /**
80 * Get details of a flow classifier. 78 * Get details of a flow classifier.
81 * 79 *
82 - * @param id flow classifier id 80 + * @param id
81 + * flow classifier id
83 * @return 200 OK , 404 if given identifier does not exist 82 * @return 200 OK , 404 if given identifier does not exist
84 */ 83 */
85 @GET 84 @GET
86 @Path("{flow_id}") 85 @Path("{flow_id}")
87 @Produces(MediaType.APPLICATION_JSON) 86 @Produces(MediaType.APPLICATION_JSON)
88 public Response getFlowClassifier(@PathParam("flow_id") String id) { 87 public Response getFlowClassifier(@PathParam("flow_id") String id) {
89 - 88 + FlowClassifier flowClassifier = nullIsNotFound(
90 - if (!service.hasFlowClassifier(FlowClassifierId.of(id))) { 89 + get(FlowClassifierService.class).getFlowClassifier(FlowClassifierId.of(id)), FLOW_CLASSIFIER_NOT_FOUND);
91 - return Response.status(NOT_FOUND).entity(FLOW_CLASSIFIER_NOT_FOUND).build();
92 - }
93 - FlowClassifier flowClassifier = nullIsNotFound(service.getFlowClassifier(FlowClassifierId.of(id)),
94 - FLOW_CLASSIFIER_NOT_FOUND);
95 90
96 ObjectNode result = new ObjectMapper().createObjectNode(); 91 ObjectNode result = new ObjectMapper().createObjectNode();
97 result.set("flow_classifier", new FlowClassifierCodec().encode(flowClassifier, this)); 92 result.set("flow_classifier", new FlowClassifierCodec().encode(flowClassifier, this));
...@@ -102,7 +97,8 @@ public class FlowClassifierWebResource extends AbstractWebResource { ...@@ -102,7 +97,8 @@ public class FlowClassifierWebResource extends AbstractWebResource {
102 /** 97 /**
103 * Creates and stores a new flow classifier. 98 * Creates and stores a new flow classifier.
104 * 99 *
105 - * @param stream flow classifier from JSON 100 + * @param stream
101 + * flow classifier from JSON
106 * @return status of the request - CREATED if the JSON is correct, 102 * @return status of the request - CREATED if the JSON is correct,
107 * BAD_REQUEST if the JSON is invalid 103 * BAD_REQUEST if the JSON is invalid
108 */ 104 */
...@@ -116,7 +112,8 @@ public class FlowClassifierWebResource extends AbstractWebResource { ...@@ -116,7 +112,8 @@ public class FlowClassifierWebResource extends AbstractWebResource {
116 JsonNode flow = jsonTree.get("flow_classifier"); 112 JsonNode flow = jsonTree.get("flow_classifier");
117 113
118 FlowClassifier flowClassifier = new FlowClassifierCodec().decode((ObjectNode) flow, this); 114 FlowClassifier flowClassifier = new FlowClassifierCodec().decode((ObjectNode) flow, this);
119 - Boolean issuccess = nullIsNotFound(service.createFlowClassifier(flowClassifier), FLOW_CLASSIFIER_NOT_FOUND); 115 + Boolean issuccess = nullIsNotFound(get(FlowClassifierService.class).createFlowClassifier(flowClassifier),
116 + FLOW_CLASSIFIER_NOT_FOUND);
120 return Response.status(OK).entity(issuccess.toString()).build(); 117 return Response.status(OK).entity(issuccess.toString()).build();
121 } catch (IOException ex) { 118 } catch (IOException ex) {
122 log.error("Exception while creating flow classifier {}.", ex.toString()); 119 log.error("Exception while creating flow classifier {}.", ex.toString());
...@@ -127,8 +124,10 @@ public class FlowClassifierWebResource extends AbstractWebResource { ...@@ -127,8 +124,10 @@ public class FlowClassifierWebResource extends AbstractWebResource {
127 /** 124 /**
128 * Update details of a flow classifier. 125 * Update details of a flow classifier.
129 * 126 *
130 - * @param id flow classifier id 127 + * @param id
131 - * @param stream InputStream 128 + * flow classifier id
129 + * @param stream
130 + * InputStream
132 * @return 200 OK, 404 if given identifier does not exist 131 * @return 200 OK, 404 if given identifier does not exist
133 */ 132 */
134 @PUT 133 @PUT
...@@ -141,7 +140,8 @@ public class FlowClassifierWebResource extends AbstractWebResource { ...@@ -141,7 +140,8 @@ public class FlowClassifierWebResource extends AbstractWebResource {
141 JsonNode jsonTree = mapper().readTree(stream); 140 JsonNode jsonTree = mapper().readTree(stream);
142 JsonNode flow = jsonTree.get("flow_classifier"); 141 JsonNode flow = jsonTree.get("flow_classifier");
143 FlowClassifier flowClassifier = new FlowClassifierCodec().decode((ObjectNode) flow, this); 142 FlowClassifier flowClassifier = new FlowClassifierCodec().decode((ObjectNode) flow, this);
144 - Boolean result = nullIsNotFound(service.updateFlowClassifier(flowClassifier), FLOW_CLASSIFIER_NOT_FOUND); 143 + Boolean result = nullIsNotFound(get(FlowClassifierService.class).updateFlowClassifier(flowClassifier),
144 + FLOW_CLASSIFIER_NOT_FOUND);
145 return Response.status(OK).entity(result.toString()).build(); 145 return Response.status(OK).entity(result.toString()).build();
146 } catch (IOException e) { 146 } catch (IOException e) {
147 log.error("Update flow classifier failed because of exception {}.", e.toString()); 147 log.error("Update flow classifier failed because of exception {}.", e.toString());
...@@ -152,14 +152,16 @@ public class FlowClassifierWebResource extends AbstractWebResource { ...@@ -152,14 +152,16 @@ public class FlowClassifierWebResource extends AbstractWebResource {
152 /** 152 /**
153 * Delete details of a flow classifier. 153 * Delete details of a flow classifier.
154 * 154 *
155 - * @param id flow classifier id 155 + * @param id
156 + * flow classifier id
156 */ 157 */
157 @Path("{flow_id}") 158 @Path("{flow_id}")
158 @DELETE 159 @DELETE
159 public void deleteFlowClassifier(@PathParam("flow_id") String id) { 160 public void deleteFlowClassifier(@PathParam("flow_id") String id) {
160 log.debug("Deletes flow classifier by identifier {}.", id); 161 log.debug("Deletes flow classifier by identifier {}.", id);
161 FlowClassifierId flowClassifierId = FlowClassifierId.of(id); 162 FlowClassifierId flowClassifierId = FlowClassifierId.of(id);
162 - Boolean issuccess = nullIsNotFound(service.removeFlowClassifier(flowClassifierId), FLOW_CLASSIFIER_NOT_FOUND); 163 + Boolean issuccess = nullIsNotFound(get(FlowClassifierService.class).removeFlowClassifier(flowClassifierId),
164 + FLOW_CLASSIFIER_NOT_FOUND);
163 165
164 } 166 }
165 } 167 }
......
...@@ -227,7 +227,7 @@ public class FlowClassifierResourceTest extends VtnResourceTest { ...@@ -227,7 +227,7 @@ public class FlowClassifierResourceTest extends VtnResourceTest {
227 final Set<FlowClassifier> flowClassifiers = new HashSet<>(); 227 final Set<FlowClassifier> flowClassifiers = new HashSet<>();
228 flowClassifiers.add(flowClassifier1); 228 flowClassifiers.add(flowClassifier1);
229 229
230 - expect(flowClassifierService.hasFlowClassifier(anyObject())).andReturn(true).anyTimes(); 230 + expect(flowClassifierService.exists(anyObject())).andReturn(true).anyTimes();
231 expect(flowClassifierService.getFlowClassifier(anyObject())).andReturn(flowClassifier1).anyTimes(); 231 expect(flowClassifierService.getFlowClassifier(anyObject())).andReturn(flowClassifier1).anyTimes();
232 replay(flowClassifierService); 232 replay(flowClassifierService);
233 233
......