[ONOS-3618] Implement REST API for Meter query, insert, delete

* Implement encode & decode method for MeterBandCodec & MeterCodec
* Implement MetersWebResource
* Add unit test for MeterBandCodec & MeterCodec
* Add unit test for MetersWebResource
* Add meter insertion json example
* Add Swagger doc

Change-Id: I07284c6678c08b3cb9e109e86ffb2cf28bf36447
diff --git a/web/api/src/main/java/org/onosproject/rest/resources/CoreWebApplication.java b/web/api/src/main/java/org/onosproject/rest/resources/CoreWebApplication.java
index afce7f7..06248fa 100644
--- a/web/api/src/main/java/org/onosproject/rest/resources/CoreWebApplication.java
+++ b/web/api/src/main/java/org/onosproject/rest/resources/CoreWebApplication.java
@@ -28,21 +28,22 @@
     @Override
     public Set<Class<?>> getClasses() {
         return getClasses(ApiDocResource.class,
-                          ApplicationsWebResource.class,
-                          ComponentConfigWebResource.class,
-                          NetworkConfigWebResource.class,
-                          ClusterWebResource.class,
-                          DevicesWebResource.class,
-                          LinksWebResource.class,
-                          HostsWebResource.class,
-                          IntentsWebResource.class,
-                          FlowsWebResource.class,
-                          GroupsWebResource.class,
-                          TopologyWebResource.class,
-                          ConfigWebResource.class,
-                          PathsWebResource.class,
-                          StatisticsWebResource.class,
-                          MetricsWebResource.class
+                ApplicationsWebResource.class,
+                ComponentConfigWebResource.class,
+                NetworkConfigWebResource.class,
+                ClusterWebResource.class,
+                DevicesWebResource.class,
+                LinksWebResource.class,
+                HostsWebResource.class,
+                IntentsWebResource.class,
+                FlowsWebResource.class,
+                GroupsWebResource.class,
+                MetersWebResource.class,
+                TopologyWebResource.class,
+                ConfigWebResource.class,
+                PathsWebResource.class,
+                StatisticsWebResource.class,
+                MetricsWebResource.class
         );
     }
-}
+}
\ No newline at end of file
diff --git a/web/api/src/main/java/org/onosproject/rest/resources/MetersWebResource.java b/web/api/src/main/java/org/onosproject/rest/resources/MetersWebResource.java
new file mode 100644
index 0000000..801e46d
--- /dev/null
+++ b/web/api/src/main/java/org/onosproject/rest/resources/MetersWebResource.java
@@ -0,0 +1,200 @@
+/*
+ * Copyright 2014-2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.rest.resources;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.meter.DefaultMeterRequest;
+import org.onosproject.net.meter.Meter;
+import org.onosproject.net.meter.MeterId;
+import org.onosproject.net.meter.MeterRequest;
+import org.onosproject.net.meter.MeterService;
+import org.onosproject.rest.AbstractWebResource;
+import org.slf4j.Logger;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import static org.onlab.util.Tools.nullIsNotFound;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Query and program meter rules.
+ */
+@Path("meters")
+public class MetersWebResource extends AbstractWebResource {
+    private final Logger log = getLogger(getClass());
+    public static final String DEVICE_INVALID = "Invalid deviceId in meter creation request";
+
+    final MeterService meterService = get(MeterService.class);
+    final ObjectNode root = mapper().createObjectNode();
+    final ArrayNode metersNode = root.putArray("meters");
+
+    /**
+     * Returns all meters of all devices.
+     *
+     * @return array of all the meters in the system
+     * @onos.rsModel Meters
+     */
+    @GET
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response getMeters() {
+        final Iterable<Meter> meters = meterService.getAllMeters();
+        if (meters != null) {
+            meters.forEach(meter -> metersNode.add(codec(Meter.class).encode(meter, this)));
+        }
+        return ok(root).build();
+    }
+
+    /**
+     * Returns a meter by the meter id.
+     *
+     * @param deviceId device identifier
+     * @return array of all the groups in the system
+     * @onos.rsModel Meter
+     */
+    @GET
+    @Produces(MediaType.APPLICATION_JSON)
+    @Path("{deviceId}/{meterId}")
+    public Response getMeterByDeviceIdAndMeterId(@PathParam("deviceId") String deviceId,
+                                                 @PathParam("meterId") String meterId) {
+        DeviceId did = DeviceId.deviceId(deviceId);
+        MeterId mid = MeterId.meterId(Long.valueOf(meterId));
+
+        final Meter meter = nullIsNotFound(meterService.getMeter(did, mid),
+                "Meter is not found for " + mid.id());
+
+        metersNode.add(codec(Meter.class).encode(meter, this));
+        return ok(root).build();
+    }
+
+    /**
+     * Create new meter rule. Creates and installs a new meter rule for the
+     * specified device.
+     *
+     * @param deviceId device identifier
+     * @param stream   meter rule JSON
+     * @return status of the request - CREATED if the JSON is correct,
+     * BAD_REQUEST if the JSON is invalid
+     * @onos.rsModel MeterPost
+     */
+    @POST
+    @Path("{deviceId}")
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response createMeter(@PathParam("deviceId") String deviceId,
+                                InputStream stream) {
+        URI location;
+        try {
+            ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
+            JsonNode specifiedDeviceId = jsonTree.get("deviceId");
+
+            if (specifiedDeviceId != null &&
+                    !specifiedDeviceId.asText().equals(deviceId)) {
+                throw new IllegalArgumentException(DEVICE_INVALID);
+            }
+            jsonTree.put("deviceId", deviceId);
+            final Meter tmpMeter = codec(Meter.class).decode(jsonTree, this);
+            final MeterRequest meterRequest = meterToMeterRequest(tmpMeter, "ADD");
+            final Meter meter = meterService.submit(meterRequest);
+            location = new URI(Long.toString(meter.id().id()));
+        } catch (IOException | URISyntaxException ex) {
+            throw new IllegalArgumentException(ex);
+        }
+
+        return Response
+                .created(location)
+                .build();
+    }
+
+    /**
+     * Removes the specified meter.
+     *
+     * @param deviceId device identifier
+     * @param meterId  meter identifier
+     */
+    @DELETE
+    @Produces(MediaType.APPLICATION_JSON)
+    @Path("{deviceId}/{meterId}")
+    public void deleteMeterByDeviceIdAndMeterId(@PathParam("deviceId") String deviceId,
+                                                @PathParam("meterId") String meterId) {
+        DeviceId did = DeviceId.deviceId(deviceId);
+        MeterId mid = MeterId.meterId(Long.valueOf(meterId));
+        final Meter tmpMeter = meterService.getMeter(did, mid);
+        if (tmpMeter != null) {
+            final MeterRequest meterRequest = meterToMeterRequest(tmpMeter, "REMOVE");
+            meterService.withdraw(meterRequest, tmpMeter.id());
+        }
+    }
+
+    /**
+     * Convert a meter instance to meterRequest instance with a certain operation.
+     *
+     * @param meter     meter instance
+     * @param operation operation
+     * @return converted meterRequest instance
+     */
+    private MeterRequest meterToMeterRequest(Meter meter, String operation) {
+        MeterRequest.Builder builder;
+        MeterRequest meterRequest;
+
+        if (meter == null) {
+            return null;
+        }
+
+        if (meter.isBurst()) {
+            builder = DefaultMeterRequest.builder()
+                    .fromApp(meter.appId())
+                    .forDevice(meter.deviceId())
+                    .withUnit(meter.unit())
+                    .withBands(meter.bands())
+                    .burst();
+        } else {
+            builder = DefaultMeterRequest.builder()
+                    .fromApp(meter.appId())
+                    .forDevice(meter.deviceId())
+                    .withUnit(meter.unit())
+                    .withBands(meter.bands());
+        }
+
+        switch (operation) {
+            case "ADD":
+                meterRequest = builder.add();
+                break;
+            case "REMOVE":
+                meterRequest = builder.remove();
+                break;
+            default:
+                log.warn("Invalid operation {}.", operation);
+                return null;
+        }
+
+        return meterRequest;
+    }
+}
\ No newline at end of file
diff --git a/web/api/src/main/resources/definitions/Meter.json b/web/api/src/main/resources/definitions/Meter.json
new file mode 100644
index 0000000..e8fefe5
--- /dev/null
+++ b/web/api/src/main/resources/definitions/Meter.json
@@ -0,0 +1,113 @@
+{
+  "type": "object",
+  "title": "meter",
+  "required": [
+    "id",
+    "appId",
+    "deviceId",
+    "unit",
+    "burst",
+    "state",
+    "life",
+    "refCount",
+    "packets",
+    "bytes",
+    "bands"
+  ],
+  "properties": {
+    "id": {
+      "type": "string",
+      "example": "1"
+    },
+    "appId": {
+      "type": "string",
+      "example": "1"
+    },
+    "deviceId": {
+      "type": "string",
+      "example": "of:0000000000000001"
+    },
+    "unit": {
+      "type": "string",
+      "example": "KB_PER_SEC"
+    },
+    "burst": {
+      "type": "boolean",
+      "example": true
+    },
+    "state": {
+      "type": "string",
+      "example": "ADDED"
+    },
+    "life": {
+      "type": "integer",
+      "format": "int64",
+      "example": 0
+    },
+    "refCount": {
+      "type": "integer",
+      "format": "int64",
+      "example": 0
+    },
+    "packets": {
+      "type": "integer",
+      "format": "int64",
+      "example": 0
+    },
+    "bytes": {
+      "type": "integer",
+      "format": "int64",
+      "example": 0
+    },
+    "bands": {
+      "type": "array",
+      "xml": {
+        "name": "bands",
+        "wrapped": true
+      },
+      "items": {
+        "type": "object",
+        "title": "bands",
+        "required": [
+          "type",
+          "rate",
+          "burstSize",
+          "prec",
+          "packets",
+          "bytes"
+        ],
+        "properties": {
+          "type": {
+            "type": "string",
+            "example": "REMARK"
+          },
+          "rate": {
+            "type": "integer",
+            "format": "int64",
+            "example": 0
+          },
+          "burstSize": {
+            "type": "integer",
+            "format": "int64",
+            "example": 0
+          },
+          "prec": {
+            "type": "integer",
+            "format": "int16",
+            "example": 0
+          },
+          "packets": {
+            "type": "integer",
+            "format": "int64",
+            "example": 0
+          },
+          "bytes": {
+            "type": "integer",
+            "format": "int64",
+            "example": 0
+          }
+        }
+      }
+    }
+  }
+}
\ No newline at end of file
diff --git a/web/api/src/main/resources/definitions/MeterPost.json b/web/api/src/main/resources/definitions/MeterPost.json
new file mode 100644
index 0000000..58ae9db
--- /dev/null
+++ b/web/api/src/main/resources/definitions/MeterPost.json
@@ -0,0 +1,67 @@
+{
+  "type": "object",
+  "title": "meter",
+  "required": [
+    "id",
+    "deviceId",
+    "unit",
+    "burst",
+    "bands"
+  ],
+  "properties": {
+    "id": {
+      "type": "string",
+      "example": "1"
+    },
+    "deviceId": {
+      "type": "string",
+      "example": "of:0000000000000001"
+    },
+    "unit": {
+      "type": "string",
+      "example": "KB_PER_SEC"
+    },
+    "burst": {
+      "type": "boolean",
+      "example": true
+    },
+    "bands": {
+      "type": "array",
+      "xml": {
+        "name": "bands",
+        "wrapped": true
+      },
+      "items": {
+        "type": "object",
+        "title": "bands",
+        "required": [
+          "type",
+          "rate",
+          "burstSize",
+          "prec"
+        ],
+        "properties": {
+          "type": {
+            "type": "string",
+            "example": "REMARK"
+          },
+          "rate": {
+            "type": "integer",
+            "format": "int64",
+            "example": "0"
+          },
+          "burstSize": {
+            "type": "integer",
+            "format": "int64",
+            "example": "0"
+          },
+          "prec": {
+            "type": "integer",
+            "format": "int16",
+            "example": "0"
+          }
+        }
+      }
+    }
+  }
+}
\ No newline at end of file
diff --git a/web/api/src/main/resources/definitions/Meters.json b/web/api/src/main/resources/definitions/Meters.json
new file mode 100644
index 0000000..0e7488b
--- /dev/null
+++ b/web/api/src/main/resources/definitions/Meters.json
@@ -0,0 +1,129 @@
+{
+  "type": "object",
+  "title": "meters",
+  "required": [
+    "meters"
+  ],
+  "properties": {
+    "groups": {
+      "type": "array",
+      "xml": {
+        "name": "meters",
+        "wrapped": true
+      },
+      "items": {
+        "type": "object",
+        "title": "meter",
+        "required": [
+          "id",
+          "appId",
+          "deviceId",
+          "unit",
+          "burst",
+          "state",
+          "life",
+          "refCount",
+          "packets",
+          "bytes",
+          "bands"
+        ],
+        "properties": {
+          "id": {
+            "type": "string",
+            "example": "1"
+          },
+          "appId": {
+            "type": "string",
+            "example": "1"
+          },
+          "deviceId": {
+            "type": "string",
+            "example": "of:0000000000000001"
+          },
+          "unit": {
+            "type": "string",
+            "example": "KB_PER_SEC"
+          },
+          "burst": {
+            "type": "boolean",
+            "example": true
+          },
+          "state": {
+            "type": "string",
+            "example": "ADDED"
+          },
+          "life": {
+            "type": "integer",
+            "format": "int64",
+            "example": 0
+          },
+          "refCount": {
+            "type": "integer",
+            "format": "int64",
+            "example": 0
+          },
+          "packets": {
+            "type": "integer",
+            "format": "int64",
+            "example": 0
+          },
+          "bytes": {
+            "type": "integer",
+            "format": "int64",
+            "example": 0
+          },
+          "bands": {
+            "type": "array",
+            "xml": {
+              "name": "bands",
+              "wrapped": true
+            },
+            "items": {
+              "type": "object",
+              "title": "bands",
+              "required": [
+                "type",
+                "rate",
+                "burstSize",
+                "prec",
+                "packets",
+                "bytes"
+              ],
+              "properties": {
+                "type": {
+                  "type": "string",
+                  "example": "REMARK"
+                },
+                "rate": {
+                  "type": "integer",
+                  "format": "int64",
+                  "example": 0
+                },
+                "burstSize": {
+                  "type": "integer",
+                  "format": "int64",
+                  "example": 0
+                },
+                "prec": {
+                  "type": "integer",
+                  "format": "int16",
+                  "example": 0
+                },
+                "packets": {
+                  "type": "integer",
+                  "format": "int64",
+                  "example": 0
+                },
+                "bytes": {
+                  "type": "integer",
+                  "format": "int64",
+                  "example": 0
+                }
+              }
+            }
+          }
+        }
+      }
+    }
+  }
+}
\ No newline at end of file