Initial import of CFM and SOAM api

Change-Id: Icf5cc2d5fb34b75460e80e8cced0d70265bcd33b
diff --git a/apps/cfm/src/test/java/org/onosproject/cfm/impl/CfmResourceTest.java b/apps/cfm/src/test/java/org/onosproject/cfm/impl/CfmResourceTest.java
new file mode 100644
index 0000000..bcbaa9a
--- /dev/null
+++ b/apps/cfm/src/test/java/org/onosproject/cfm/impl/CfmResourceTest.java
@@ -0,0 +1,32 @@
+/*
+ * 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.impl;
+
+import org.glassfish.jersey.server.ResourceConfig;
+import org.onosproject.rest.resources.ResourceTest;
+
+/**
+ * Base class for CFM REST API tests.  Performs common configuration operations.
+ */
+public class CfmResourceTest extends ResourceTest {
+
+    /**
+     * Creates a new web-resource test.
+     */
+    public CfmResourceTest() {
+        super(ResourceConfig.forApplicationClass(CfmWebApplication.class));
+    }
+}
diff --git a/apps/cfm/src/test/java/org/onosproject/cfm/impl/MaWebResourceTest.java b/apps/cfm/src/test/java/org/onosproject/cfm/impl/MaWebResourceTest.java
new file mode 100644
index 0000000..6378ea9
--- /dev/null
+++ b/apps/cfm/src/test/java/org/onosproject/cfm/impl/MaWebResourceTest.java
@@ -0,0 +1,199 @@
+/*
+ * 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.impl;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.osgi.ServiceDirectory;
+import org.onlab.osgi.TestServiceDirectory;
+import org.onlab.packet.VlanId;
+import org.onlab.rest.BaseResource;
+import org.onosproject.cfm.CfmCodecContext;
+import org.onosproject.codec.CodecService;
+import org.onosproject.incubator.net.l2monitoring.cfm.Component;
+import org.onosproject.incubator.net.l2monitoring.cfm.DefaultComponent;
+import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceAssociation;
+import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceDomain;
+import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceAssociation;
+import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceDomain;
+import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdCharStr;
+import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort;
+import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId;
+import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr;
+import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId;
+import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
+import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMdService;
+
+import javax.ws.rs.InternalServerErrorException;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.Response;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.Optional;
+
+import static junit.framework.TestCase.fail;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+
+public class MaWebResourceTest extends CfmResourceTest {
+    private final CfmMdService mdService = createMock(CfmMdService.class);
+
+    private static final MdId MDNAME1 = MdIdCharStr.asMdId("md-1");
+    private static final MaIdShort MANAME1 = MaIdCharStr.asMaId("ma-1-1");
+
+    private MaintenanceAssociation ma1;
+
+    @Before
+    public void setUpTest() throws CfmConfigException {
+        CfmCodecContext context = new CfmCodecContext();
+        ServiceDirectory testDirectory = new TestServiceDirectory()
+                .add(CfmMdService.class, mdService)
+                .add(CodecService.class, context.codecManager());
+        BaseResource.setServiceDirectory(testDirectory);
+
+        ma1 = DefaultMaintenanceAssociation
+                .builder(MANAME1, MDNAME1.getNameLength())
+                .addToRemoteMepIdList(MepId.valueOf((short) 101))
+                .addToRemoteMepIdList(MepId.valueOf((short) 102))
+                .ccmInterval(MaintenanceAssociation.CcmInterval.INTERVAL_3MS)
+                .maNumericId((short) 1)
+                .addToComponentList(
+                        DefaultComponent.builder(1)
+                                .tagType(Component.TagType.VLAN_STAG)
+                                .mhfCreationType(Component.MhfCreationType.NONE)
+                                .idPermission(Component.IdPermissionType.MANAGE)
+                                .addToVidList(VlanId.vlanId((short) 1010))
+                                .build())
+                .build();
+    }
+
+    @Test
+    public void testGetMa() {
+
+        expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1))
+                .andReturn(Optional.ofNullable(ma1)).anyTimes();
+        replay(mdService);
+
+        final WebTarget wt = target();
+        final String response = wt.path("md/" + MDNAME1.mdName()
+                + "/ma/" + MANAME1.maName()).request().get(String.class);
+
+        assertThat(response, is("{\"ma\":" +
+                "{\"maName\":\"ma-1-1\"," +
+                "\"maNameType\":\"CHARACTERSTRING\"," +
+                "\"maNumericId\":1," +
+                "\"ccm-interval\":\"INTERVAL_3MS\"," +
+                "\"component-list\":[{\"component\":" +
+                    "{\"component-id\":1," +
+                    "\"vid-list\":[{\"vid\":\"1010\"}]," +
+                    "\"mhf-creation-type\":\"NONE\"," +
+                    "\"id-permission\":\"MANAGE\"," +
+                    "\"tag-type\":\"VLAN_STAG\"}}]," +
+                "\"rmep-list\":" +
+                    "[{\"rmep\":101}," +
+                    "{\"rmep\":102}]}}"));
+    }
+
+    @Test
+    public void testGetMaEmpty() throws IOException {
+        MaIdShort maId2 = MaIdCharStr.asMaId("ma-2");
+        expect(mdService
+                .getMaintenanceAssociation(MDNAME1, maId2))
+                .andReturn(Optional.empty()).anyTimes();
+        replay(mdService);
+
+        final WebTarget wt = target();
+        try {
+            final String response = wt.path("md/" + MDNAME1.mdName()
+                    + "/ma/" + maId2.maName()).request().get(String.class);
+            fail("Expected InternalServerErrorException, as MA is unknown");
+        } catch (InternalServerErrorException e) {
+            ByteArrayInputStream is = (ByteArrayInputStream) e.getResponse().getEntity();
+            BufferedReader br = new BufferedReader(new InputStreamReader(is));
+            String line = null;
+            StringBuffer sb = new StringBuffer();
+            while ((line = br.readLine()) != null) {
+                sb.append(line);
+            }
+
+            assertThat(sb.toString(), is("{ \"failure\":" +
+                    "\"java.lang.IllegalArgumentException: MA ma-2 not Found\" }"));
+        }
+    }
+
+    @Test
+    public void testDeleteMa() throws CfmConfigException {
+
+        expect(mdService.deleteMaintenanceAssociation(MDNAME1, MANAME1))
+                .andReturn(true).anyTimes();
+        replay(mdService);
+
+        final WebTarget wt = target();
+        final Response response = wt.path("md/" + MDNAME1.mdName()
+                + "/ma/" + MANAME1.maName()).request().delete();
+
+        assertEquals(200, response.getStatus());
+    }
+
+    @Test
+    public void testDeleteMaEmpty() throws CfmConfigException {
+        MaIdShort maId2 = MaIdCharStr.asMaId("ma-2");
+
+        expect(mdService.deleteMaintenanceAssociation(MDNAME1, maId2))
+                .andReturn(false).anyTimes();
+        replay(mdService);
+
+        final WebTarget wt = target();
+        final Response response = wt.path("md/" + MDNAME1.mdName()
+                + "/ma/" + maId2.maName()).request().delete();
+
+        assertEquals(304, response.getStatus());
+    }
+
+    @Test
+    public void testCreateMa() throws CfmConfigException {
+        MaintenanceDomain md1 = DefaultMaintenanceDomain
+                .builder(MDNAME1).mdLevel(MaintenanceDomain.MdLevel.LEVEL2).build();
+
+        expect(mdService.getMaintenanceDomain(MDNAME1))
+                .andReturn(Optional.ofNullable(md1)).anyTimes();
+        expect(mdService.createMaintenanceAssociation(MDNAME1, ma1))
+                .andReturn(false).anyTimes();
+        replay(mdService);
+
+        ObjectMapper mapper = new ObjectMapper();
+        CfmCodecContext context = new CfmCodecContext();
+        ObjectNode node = mapper.createObjectNode();
+        node.set("ma", context.codec(MaintenanceAssociation.class)
+                .encode(ma1, context));
+
+        final WebTarget wt = target();
+        final Response response = wt.path("md/" + MDNAME1.mdName()
+                + "/ma").request().post(Entity.json(node.toString()));
+
+        assertEquals(201, response.getStatus());
+    }
+}
diff --git a/apps/cfm/src/test/java/org/onosproject/cfm/impl/MdWebResourceTest.java b/apps/cfm/src/test/java/org/onosproject/cfm/impl/MdWebResourceTest.java
new file mode 100644
index 0000000..f4c9b5a
--- /dev/null
+++ b/apps/cfm/src/test/java/org/onosproject/cfm/impl/MdWebResourceTest.java
@@ -0,0 +1,193 @@
+/*
+ * 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.impl;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.osgi.ServiceDirectory;
+import org.onlab.osgi.TestServiceDirectory;
+import org.onlab.rest.BaseResource;
+import org.onosproject.cfm.CfmCodecContext;
+import org.onosproject.codec.CodecService;
+import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceDomain;
+import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceDomain;
+import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId;
+import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr;
+import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
+import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMdService;
+
+import javax.ws.rs.InternalServerErrorException;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.Response;
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+
+import static junit.framework.TestCase.fail;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+
+public class MdWebResourceTest extends CfmResourceTest {
+    private final CfmMdService mdService = createMock(CfmMdService.class);
+
+    private static final MdId MDNAME1 = MdIdCharStr.asMdId("md-1");
+    private static final MdId MDNAME2 = MdIdCharStr.asMdId("md-2");
+
+    private List<MaintenanceDomain> mdList;
+
+    @Before
+    public void setUpTest() throws CfmConfigException {
+        CfmCodecContext context = new CfmCodecContext();
+        ServiceDirectory testDirectory = new TestServiceDirectory()
+                .add(CfmMdService.class, mdService)
+                .add(CodecService.class, context.codecManager());
+        BaseResource.setServiceDirectory(testDirectory);
+
+        mdList = new ArrayList<>();
+
+        mdList.add(DefaultMaintenanceDomain.builder(MDNAME1)
+                .mdLevel(MaintenanceDomain.MdLevel.LEVEL1).build());
+        mdList.add(DefaultMaintenanceDomain.builder(MDNAME2)
+                .mdLevel(MaintenanceDomain.MdLevel.LEVEL2).build());
+    }
+
+    @Test
+    public void testGetMds() {
+        expect(mdService.getAllMaintenanceDomain()).andReturn(mdList).anyTimes();
+        replay(mdService);
+
+        final WebTarget wt = target();
+        final String response = wt.path("md").request().get(String.class);
+
+        assertThat(response, is("{\"mds\":[[" +
+                "{\"mdName\":\"md-1\",\"mdNameType\":\"CHARACTERSTRING\"," +
+                "\"mdLevel\":\"LEVEL1\",\"maList\":[]}," +
+                "{\"mdName\":\"md-2\",\"mdNameType\":\"CHARACTERSTRING\"," +
+                "\"mdLevel\":\"LEVEL2\",\"maList\":[]}]]}"));
+    }
+
+    @Test
+    public void testGetMdsEmpty() {
+        expect(mdService.getAllMaintenanceDomain())
+                            .andReturn(new ArrayList<>()).anyTimes();
+        replay(mdService);
+
+        final WebTarget wt = target();
+        final String response = wt.path("md").request().get(String.class);
+
+        assertThat(response, is("{\"mds\":[[]]}"));
+    }
+
+    @Test
+    public void testGetMd() {
+        expect(mdService.getMaintenanceDomain(MDNAME1))
+                .andReturn(Optional.ofNullable(mdList.get(0))).anyTimes();
+        replay(mdService);
+
+        final WebTarget wt = target();
+        final String response = wt.path("md/" + MDNAME1).request().get(String.class);
+
+        assertThat(response, is("{\"md\":" +
+                "{\"mdName\":\"md-1\",\"mdNameType\":\"CHARACTERSTRING\"," +
+                    "\"mdLevel\":\"LEVEL1\",\"maList\":[]}}"));
+    }
+
+    @Test
+    public void testGetMdEmpty() throws IOException {
+        final MdId mdName3 = MdIdCharStr.asMdId("md-3");
+        expect(mdService.getMaintenanceDomain(mdName3))
+                .andReturn(Optional.empty()).anyTimes();
+        replay(mdService);
+
+        final WebTarget wt = target();
+        try {
+            final String response = wt.path("md/" + mdName3).request().get(String.class);
+            fail("Expected InternalServerErrorException, as MD is unknown");
+        } catch (InternalServerErrorException e) {
+            ByteArrayInputStream is = (ByteArrayInputStream) e.getResponse().getEntity();
+            BufferedReader br = new BufferedReader(new InputStreamReader(is));
+            String line = null;
+            StringBuffer sb = new StringBuffer();
+            while ((line = br.readLine()) != null) {
+                sb.append(line);
+            }
+
+            assertThat(sb.toString(), is("{ \"failure\":" +
+                    "\"java.lang.IllegalArgumentException: MD md-3 not Found\" }"));
+        }
+    }
+
+    @Test
+    public void testDeleteMd() throws CfmConfigException {
+        expect(mdService.deleteMaintenanceDomain(MDNAME1))
+                .andReturn(true).anyTimes();
+        replay(mdService);
+
+        final WebTarget wt = target();
+        final Response response = wt.path("md/" + MDNAME1).request().delete();
+
+        assertEquals(200, response.getStatus());
+    }
+
+    @Test
+    public void testDeleteMdNotPresent() throws CfmConfigException {
+        expect(mdService.deleteMaintenanceDomain(MDNAME1))
+                .andReturn(false).anyTimes();
+        replay(mdService);
+
+        final WebTarget wt = target();
+        final Response response = wt.path("md/" + MDNAME1).request().delete();
+
+        assertEquals(304, response.getStatus());
+    }
+
+    @Test
+    public void testCreateMd() throws CfmConfigException {
+        MaintenanceDomain md3 = DefaultMaintenanceDomain
+                .builder(MdIdCharStr.asMdId("md-3"))
+                .mdLevel(MaintenanceDomain.MdLevel.LEVEL3)
+                .mdNumericId((short) 3)
+                .build();
+
+        expect(mdService.createMaintenanceDomain(mdList.get(1)))
+                .andReturn(false).anyTimes();
+        replay(mdService);
+
+        ObjectMapper mapper = new ObjectMapper();
+        CfmCodecContext context = new CfmCodecContext();
+        ObjectNode node = mapper.createObjectNode();
+        node.set("md", context.codec(MaintenanceDomain.class)
+                    .encode(mdList.get(1), context));
+
+
+        final WebTarget wt = target();
+        final Response response = wt.path("md")
+                .request().post(Entity.json(node.toString()));
+
+        assertEquals(201, response.getStatus());
+    }
+}
diff --git a/apps/cfm/src/test/java/org/onosproject/cfm/impl/MepWebResourceTest.java b/apps/cfm/src/test/java/org/onosproject/cfm/impl/MepWebResourceTest.java
new file mode 100644
index 0000000..4dd83db
--- /dev/null
+++ b/apps/cfm/src/test/java/org/onosproject/cfm/impl/MepWebResourceTest.java
@@ -0,0 +1,378 @@
+/*
+ * 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.impl;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.osgi.ServiceDirectory;
+import org.onlab.osgi.TestServiceDirectory;
+import org.onlab.packet.VlanId;
+import org.onlab.rest.BaseResource;
+import org.onosproject.cfm.CfmCodecContext;
+import org.onosproject.codec.CodecService;
+import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceAssociation;
+import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceDomain;
+import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMep;
+import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMepEntry;
+import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMepLbCreate;
+import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMepLtCreate;
+import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceAssociation;
+import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceDomain;
+import org.onosproject.incubator.net.l2monitoring.cfm.Mep;
+import org.onosproject.incubator.net.l2monitoring.cfm.MepEntry;
+import org.onosproject.incubator.net.l2monitoring.cfm.MepLbCreate;
+import org.onosproject.incubator.net.l2monitoring.cfm.MepLtCreate;
+import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdCharStr;
+import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort;
+import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId;
+import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr;
+import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId;
+import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
+import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMdService;
+import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMepService;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+
+import javax.ws.rs.InternalServerErrorException;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.Response;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.BitSet;
+import java.util.Collection;
+import java.util.Optional;
+
+import static junit.framework.TestCase.fail;
+import static org.easymock.EasyMock.*;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+
+public class MepWebResourceTest extends CfmResourceTest {
+    private final CfmMepService mepService = createMock(CfmMepService.class);
+    private final CfmMdService mdService = createMock(CfmMdService.class);
+
+    private static final MdId MDNAME1 = MdIdCharStr.asMdId("md-1");
+    private static final MaIdShort MANAME1 = MaIdCharStr.asMaId("ma-1-1");
+    private static final MepId MEPID1 = MepId.valueOf((short) 1);
+
+    private MepEntry mepEntry1 = null;
+
+    @Before
+    public void setUpTest() throws CfmConfigException {
+        CfmCodecContext context = new CfmCodecContext();
+        ServiceDirectory testDirectory = new TestServiceDirectory()
+                .add(CfmMepService.class, mepService)
+                .add(CfmMdService.class, mdService)
+                .add(CodecService.class, context.codecManager());
+        BaseResource.setServiceDirectory(testDirectory);
+
+        mepEntry1 = DefaultMepEntry.builder(
+                    MEPID1,
+                    DeviceId.deviceId("netconf:1.2.3.4:830"),
+                    PortNumber.portNumber(1),
+                    Mep.MepDirection.UP_MEP, MDNAME1, MANAME1)
+                .buildEntry();
+
+    }
+
+    @Test
+    public void testGetAllMepsForMaEmpty() throws CfmConfigException {
+
+        expect(mepService.getAllMeps(MDNAME1, MANAME1)).andReturn(null).anyTimes();
+        replay(mepService);
+
+        final WebTarget wt = target();
+        final String response = wt.path("md/" + MDNAME1.mdName() + "/ma/" +
+                MANAME1.maName() + "/mep").request().get(String.class);
+        assertThat(response, is("{\"meps\":[[]]}"));
+    }
+
+    @Test
+    public void testGetAllMepsForMa1Mep() throws CfmConfigException {
+        Collection<MepEntry> meps = new ArrayList<>();
+        meps.add(mepEntry1);
+
+        expect(mepService.getAllMeps(MDNAME1, MANAME1)).andReturn(meps).anyTimes();
+        replay(mepService);
+
+        final WebTarget wt = target();
+        final String response = wt.path("md/" + MDNAME1.mdName() + "/ma/" +
+                MANAME1.maName() + "/mep").request().get(String.class);
+
+        assertThat(response, is("{\"meps\":" +
+                "[[{" +
+                "\"mepId\":" + MEPID1.value() + "," +
+                "\"deviceId\":\"netconf:1.2.3.4:830\"," +
+                "\"port\":1," +
+                "\"direction\":\"UP_MEP\"," +
+                "\"mdName\":\"" + MDNAME1.mdName() + "\"," +
+                "\"maName\":\"" + MANAME1.maName() + "\"," +
+                "\"administrative-state\":false," +
+                "\"cci-enabled\":false," +
+                "\"remoteMeps\":[]}]]}"));
+    }
+
+    @Test
+    public void testGetMepValid() throws CfmConfigException {
+
+        expect(mepService.getMep(MDNAME1, MANAME1, MepId.valueOf((short) 1)))
+                .andReturn(mepEntry1).anyTimes();
+        replay(mepService);
+
+        final WebTarget wt = target();
+        final String response = wt.path("md/" + MDNAME1.mdName() + "/ma/" +
+                MANAME1.maName() + "/mep/" + MEPID1.value()).request().get(String.class);
+
+        assertThat(response, is("{\"mep\":" +
+                "{" +
+                "\"mepId\":" + MEPID1.value() + "," +
+                "\"deviceId\":\"netconf:1.2.3.4:830\"," +
+                "\"port\":1," +
+                "\"direction\":\"UP_MEP\"," +
+                "\"mdName\":\"" + MDNAME1.mdName() + "\"," +
+                "\"maName\":\"" + MANAME1.maName() + "\"," +
+                "\"administrative-state\":false," +
+                "\"cci-enabled\":false," +
+                "\"remoteMeps\":[]}}"));
+    }
+
+    @Test
+    public void testGetMepNotFound() throws CfmConfigException, IOException {
+
+        expect(mepService.getMep(MDNAME1, MANAME1, MepId.valueOf((short) 2)))
+                .andReturn(null).anyTimes();
+        replay(mepService);
+
+        final WebTarget wt = target();
+
+        try {
+            final String response = wt.path("md/" + MDNAME1.mdName() + "/ma/" +
+                    MANAME1.maName() + "/mep/" + 2).request().get(String.class);
+            fail("Expected exception to be thrown");
+        } catch (InternalServerErrorException e) {
+            ByteArrayInputStream is = (ByteArrayInputStream) e.getResponse().getEntity();
+            BufferedReader br = new BufferedReader(new InputStreamReader(is));
+            String line = null;
+            StringBuffer sb = new StringBuffer();
+            while ((line = br.readLine()) != null) {
+                sb.append(line);
+            }
+            assertEquals("{ \"failure\":\"MEP md-1/ma-1-1/2 not found\" }", sb.toString());
+        }
+    }
+
+    @Test
+    public void testDeleteMepValid() throws CfmConfigException {
+
+        expect(mepService.deleteMep(MDNAME1, MANAME1, MepId.valueOf((short) 1)))
+                .andReturn(true).anyTimes();
+        replay(mepService);
+
+        final WebTarget wt = target();
+        final Response response = wt.path("md/" + MDNAME1.mdName() + "/ma/" +
+                MANAME1.maName() + "/mep/" + MEPID1.value()).request().delete();
+
+        assertEquals("Expecting 200", 200, response.getStatus());
+    }
+
+    @Test
+    public void testDeleteMepNotFound() throws CfmConfigException {
+
+        expect(mepService.deleteMep(MDNAME1, MANAME1, MepId.valueOf((short) 2)))
+                .andReturn(false).anyTimes();
+        replay(mepService);
+
+        final WebTarget wt = target();
+        final Response response = wt.path("md/" + MDNAME1.mdName() + "/ma/" +
+                MANAME1.maName() + "/mep/2").request().delete();
+
+        assertEquals("Expecting 304", 304, response.getStatus());
+    }
+
+    @Test
+    public void testCreateMep() throws CfmConfigException, IOException {
+        MepId mepId2 = MepId.valueOf((short) 2);
+        Mep mep2 = DefaultMep.builder(
+                mepId2,
+                DeviceId.deviceId("netconf:2.2.3.4:830"),
+                PortNumber.portNumber(2),
+                Mep.MepDirection.UP_MEP, MDNAME1, MANAME1).build();
+
+        MaintenanceAssociation ma1 = DefaultMaintenanceAssociation
+                .builder(MANAME1, MDNAME1.getNameLength()).build();
+
+        expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1))
+                                .andReturn(Optional.ofNullable(ma1)).anyTimes();
+        replay(mdService);
+        expect(mepService.createMep(MDNAME1, MANAME1, mep2))
+                                .andReturn(true).anyTimes();
+        replay(mepService);
+
+        ObjectMapper mapper = new ObjectMapper();
+        CfmCodecContext context = new CfmCodecContext();
+        ObjectNode node = mapper.createObjectNode();
+        node.set("mep", context.codec(Mep.class).encode(mep2, context));
+
+        final WebTarget wt = target();
+        final Response response = wt.path("md/" + MDNAME1.mdName() + "/ma/" +
+                MANAME1.maName() + "/mep")
+                .request()
+                .post(Entity.json(node.toString()));
+
+        assertEquals("Expecting 201", 201, response.getStatus());
+    }
+
+    @Test
+    public void testCreateMepAlreadyExists() throws CfmConfigException, IOException {
+        MepId mepId3 = MepId.valueOf((short) 3);
+        Mep mep3 = DefaultMep.builder(
+                    mepId3,
+                    DeviceId.deviceId("netconf:3.2.3.4:830"),
+                    PortNumber.portNumber(3),
+                    Mep.MepDirection.UP_MEP, MDNAME1, MANAME1)
+                .cciEnabled(true)
+                .ccmLtmPriority(Mep.Priority.PRIO3)
+                .administrativeState(false)
+                .primaryVid(VlanId.vlanId((short) 3))
+                .defectAbsentTime(Duration.ofMinutes(2))
+                .defectPresentTime(Duration.ofMinutes(3))
+                .fngAddress(Mep.FngAddress.notSpecified())
+                .lowestFaultPriorityDefect(Mep.LowestFaultDefect.ALL_DEFECTS)
+                .build();
+
+        MaintenanceAssociation ma1 = DefaultMaintenanceAssociation
+                .builder(MANAME1, MDNAME1.getNameLength()).build();
+
+        expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1))
+                .andReturn(Optional.ofNullable(ma1)).anyTimes();
+        replay(mdService);
+        expect(mepService.createMep(MDNAME1, MANAME1, mep3))
+                .andReturn(false).anyTimes();
+        replay(mepService);
+
+        ObjectMapper mapper = new ObjectMapper();
+        CfmCodecContext context = new CfmCodecContext();
+        ObjectNode node = mapper.createObjectNode();
+        node.set("mep", context.codec(Mep.class).encode(mep3, context));
+
+        final WebTarget wt = target();
+        final Response response = wt.path("md/" + MDNAME1.mdName() + "/ma/" +
+                MANAME1.maName() + "/mep")
+                .request()
+                .post(Entity.json(node.toString()));
+
+        assertEquals("Expecting 304", 304, response.getStatus());
+    }
+
+    @Test
+    public void testTransmitLoopback() throws CfmConfigException {
+        MepLbCreate mepLbCreate1 = DefaultMepLbCreate
+                .builder(MEPID1)
+                .numberMessages(20)
+                .dataTlvHex("AA:BB:CC:DD")
+                .vlanDropEligible(true)
+                .build();
+
+        MaintenanceAssociation ma1 = DefaultMaintenanceAssociation
+                .builder(MANAME1, MDNAME1.getNameLength()).build();
+        MaintenanceDomain md1 = DefaultMaintenanceDomain.builder(MDNAME1)
+                .addToMaList(ma1).build();
+        expect(mdService.getMaintenanceDomain(MDNAME1))
+                        .andReturn(Optional.ofNullable(md1)).anyTimes();
+        expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1))
+                        .andReturn(Optional.ofNullable(ma1)).anyTimes();
+        replay(mdService);
+
+        ObjectMapper mapper = new ObjectMapper();
+        CfmCodecContext context = new CfmCodecContext();
+        ObjectNode node = mapper.createObjectNode();
+        node.set("loopback", context.codec(MepLbCreate.class).encode(mepLbCreate1, context));
+
+        final WebTarget wt = target();
+        final Response response = wt.path("md/" + MDNAME1.mdName() + "/ma/" +
+                MANAME1.maName() + "/mep/" + MEPID1.value() + "/transmit-loopback")
+                .request()
+                .put(Entity.json(node.toString()));
+
+        assertEquals("Expecting 202", 202, response.getStatus());
+    }
+
+    @Test
+    public void testAbortLoopback() throws CfmConfigException {
+
+        MepLbCreate mepLbCreate1 = DefaultMepLbCreate.builder(MEPID1).build();
+
+        MaintenanceAssociation ma1 = DefaultMaintenanceAssociation
+                .builder(MANAME1, MDNAME1.getNameLength()).build();
+        MaintenanceDomain md1 = DefaultMaintenanceDomain.builder(MDNAME1)
+                .addToMaList(ma1).build();
+        expect(mdService.getMaintenanceDomain(MDNAME1))
+                .andReturn(Optional.ofNullable(md1)).anyTimes();
+        expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1))
+                .andReturn(Optional.ofNullable(ma1)).anyTimes();
+        replay(mdService);
+
+        final WebTarget wt = target();
+        final Response response = wt.path("md/" + MDNAME1.mdName() + "/ma/" +
+                MANAME1.maName() + "/mep/" + MEPID1.value() + "/abort-loopback")
+                .request()
+                .put(Entity.json(""));
+
+        assertEquals("Expecting 202", 202, response.getStatus());
+
+    }
+
+    @Test
+    public void testTransmitLinktrace() throws CfmConfigException {
+        MepLtCreate mepLtCreate1 = DefaultMepLtCreate
+                .builder(MEPID1)
+                .defaultTtl((short) 20)
+                .transmitLtmFlags(BitSet.valueOf(new byte[]{1}))
+                .build();
+
+        MaintenanceAssociation ma1 = DefaultMaintenanceAssociation
+                .builder(MANAME1, MDNAME1.getNameLength()).build();
+        MaintenanceDomain md1 = DefaultMaintenanceDomain.builder(MDNAME1)
+                .addToMaList(ma1).build();
+        expect(mdService.getMaintenanceDomain(MDNAME1))
+                .andReturn(Optional.ofNullable(md1)).anyTimes();
+        expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1))
+                .andReturn(Optional.ofNullable(ma1)).anyTimes();
+        replay(mdService);
+
+        ObjectMapper mapper = new ObjectMapper();
+        CfmCodecContext context = new CfmCodecContext();
+        ObjectNode node = mapper.createObjectNode();
+        node.set("linktrace", context.codec(MepLtCreate.class).encode(mepLtCreate1, context));
+
+        final WebTarget wt = target();
+        final Response response = wt.path("md/" + MDNAME1.mdName() + "/ma/" +
+                MANAME1.maName() + "/mep/" + MEPID1.value() + "/transmit-linktrace")
+                .request()
+                .put(Entity.json(node.toString()));
+
+        assertEquals("Expecting 202", 202, response.getStatus());
+    }
+}