[ONOS-7262] Cfm improvements to allow RMeps and Mds and Mas to be added and deleted

Change-Id: Ibffb13d046bfb29dbe88de7b558c95fbf9db046d
diff --git a/apps/cfm/src/main/java/org/onosproject/cfm/rest/CfmWebApplication.java b/apps/cfm/src/main/java/org/onosproject/cfm/rest/CfmWebApplication.java
index 13157f2..b32174e 100644
--- a/apps/cfm/src/main/java/org/onosproject/cfm/rest/CfmWebApplication.java
+++ b/apps/cfm/src/main/java/org/onosproject/cfm/rest/CfmWebApplication.java
@@ -32,6 +32,7 @@
                 MdWebResource.class,
                 MaWebResource.class,
                 MepWebResource.class,
+                DeviceMepWebResource.class,
                 DmWebResource.class,
                 LmWebResource.class);
     }
diff --git a/apps/cfm/src/main/java/org/onosproject/cfm/rest/DeviceMepWebResource.java b/apps/cfm/src/main/java/org/onosproject/cfm/rest/DeviceMepWebResource.java
new file mode 100644
index 0000000..5fbed6d
--- /dev/null
+++ b/apps/cfm/src/main/java/org/onosproject/cfm/rest/DeviceMepWebResource.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * 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.cfm.rest;
+
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import org.onosproject.incubator.net.l2monitoring.cfm.Mep;
+import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
+import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMepService;
+import org.onosproject.net.DeviceId;
+import org.onosproject.rest.AbstractWebResource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+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.util.Collection;
+
+/**
+ * Layer 2 CFM Maintenance Association Endpoint (MEP) by Device web resource.
+ */
+@Path("device")
+public class DeviceMepWebResource extends AbstractWebResource {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    /**
+     * Get all MEPs by Device Id. The device should support Meps
+     *
+     * @param deviceId The id of a device.
+     * @return 200 OK with a list of MEPS or 500 on error
+     */
+    @GET
+    @Path("{device_id}")
+    @Produces(MediaType.APPLICATION_JSON)
+    @Consumes(MediaType.APPLICATION_JSON)
+    public Response getAllMepsForDevice(@PathParam("device_id") String deviceId) {
+        DeviceId deviceIdObj = DeviceId.deviceId(deviceId);
+        log.debug("GET all Meps called for Device {}", deviceIdObj);
+        try {
+            Collection<Mep> mepCollection = get(CfmMepService.class)
+                    .getAllMepsByDevice(deviceIdObj);
+            ArrayNode an = mapper().createArrayNode();
+            an.add(codec(Mep.class).encode(mepCollection, this));
+            return ok(mapper().createObjectNode().set("meps", an)).build();
+        } catch (CfmConfigException e) {
+            log.error("Get all Meps on device {} failed because of exception",
+                    deviceIdObj, e);
+            return Response.serverError().entity("{ \"failure\":\"" + e.toString() + "\" }").build();
+        }
+    }
+}
diff --git a/apps/cfm/src/main/java/org/onosproject/cfm/rest/MdWebResource.java b/apps/cfm/src/main/java/org/onosproject/cfm/rest/MdWebResource.java
index c642f4a..0e523a7 100644
--- a/apps/cfm/src/main/java/org/onosproject/cfm/rest/MdWebResource.java
+++ b/apps/cfm/src/main/java/org/onosproject/cfm/rest/MdWebResource.java
@@ -82,6 +82,7 @@
         log.debug("GET called for MD {}", mdName);
         try {
             MaintenanceDomain md = get(CfmMdService.class)
+                    //FIXME Handle other types of name constructs e.g. DomainName
                     .getMaintenanceDomain(MdIdCharStr.asMdId(mdName))
                     .orElseThrow(() -> new IllegalArgumentException(
                             "MD " + mdName + " not Found"));
diff --git a/apps/cfm/src/main/java/org/onosproject/cfm/rest/MepWebResource.java b/apps/cfm/src/main/java/org/onosproject/cfm/rest/MepWebResource.java
index fa8c728..52d4150 100644
--- a/apps/cfm/src/main/java/org/onosproject/cfm/rest/MepWebResource.java
+++ b/apps/cfm/src/main/java/org/onosproject/cfm/rest/MepWebResource.java
@@ -18,6 +18,7 @@
 import java.io.InputStream;
 import java.net.URI;
 import java.util.Collection;
+import java.util.Optional;
 
 import javax.ws.rs.Consumes;
 import javax.ws.rs.DELETE;
@@ -145,7 +146,7 @@
             MdId mdId = MdIdCharStr.asMdId(mdName);
             MaIdShort maId = MaIdCharStr.asMaId(maName);
             boolean deleted = get(CfmMepService.class)
-                    .deleteMep(mdId, maId, MepId.valueOf(mepIdShort));
+                    .deleteMep(mdId, maId, MepId.valueOf(mepIdShort), Optional.empty());
             if (!deleted) {
                 return Response.notModified(mdName + "/" + maName + "/" +
                         mepIdShort + " did not exist").build();
@@ -187,8 +188,8 @@
 
             Mep mep = ((MepCodec) mepCodec).decode((ObjectNode) cfg, this, mdName, maName);
 
-            Boolean issuccess = get(CfmMepService.class).createMep(mdId, maId, mep);
-            if (!issuccess) {
+            Boolean didNotExist = get(CfmMepService.class).createMep(mdId, maId, mep);
+            if (!didNotExist) {
                 return Response.notModified(mdName + "/" + ma.maId() + "/" + mep.mepId() +
                         " already exists").build();
             }