Initial import of CFM and SOAM api

Change-Id: Icf5cc2d5fb34b75460e80e8cced0d70265bcd33b
diff --git a/incubator/net/src/test/java/org/onosproject/incubator/net/l2monitoring/cfm/impl/CfmMdManagerTest.java b/incubator/net/src/test/java/org/onosproject/incubator/net/l2monitoring/cfm/impl/CfmMdManagerTest.java
new file mode 100644
index 0000000..a0de513
--- /dev/null
+++ b/incubator/net/src/test/java/org/onosproject/incubator/net/l2monitoring/cfm/impl/CfmMdManagerTest.java
@@ -0,0 +1,386 @@
+/*
+ * 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.incubator.net.l2monitoring.cfm.impl;
+
+import com.google.common.collect.Sets;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.junit.TestUtils;
+import org.onlab.packet.IpAddress;
+import org.onosproject.cluster.ClusterServiceAdapter;
+import org.onosproject.cluster.ControllerNode;
+import org.onosproject.cluster.DefaultControllerNode;
+import org.onosproject.cluster.NodeId;
+import org.onosproject.common.event.impl.TestEventDispatcher;
+import org.onosproject.core.CoreServiceAdapter;
+import org.onosproject.core.DefaultApplicationId;
+import org.onosproject.core.IdGenerator;
+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.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.mastership.MastershipServiceAdapter;
+import org.onosproject.net.DeviceId;
+import org.onosproject.store.service.TestStorageService;
+
+import java.util.Collection;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicLong;
+
+import static junit.framework.TestCase.assertFalse;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.onosproject.net.NetTestTools.injectEventDispatcher;
+
+/**
+ * CFM MD Manager test.
+ */
+public class CfmMdManagerTest {
+    private static final NodeId NID_LOCAL = new NodeId("local");
+    private static final IpAddress LOCALHOST = IpAddress.valueOf("127.0.0.1");
+
+    private DistributedMdStore mdStore;
+    private CfmMdService service;
+    private CfmMdManager manager;
+
+    @Before
+    public void setup() throws Exception, CfmConfigException {
+        mdStore = new DistributedMdStore();
+
+        MaintenanceAssociation maTest11 = DefaultMaintenanceAssociation
+                .builder(MaIdCharStr.asMaId("test-ma-1-1"), 9)
+                .ccmInterval(MaintenanceAssociation.CcmInterval.INTERVAL_10MIN)
+                .maNumericId((short) 1)
+                .addToRemoteMepIdList(MepId.valueOf((short) 101))
+                .addToRemoteMepIdList(MepId.valueOf((short) 102))
+                .addToComponentList(
+                        DefaultComponent.builder(1)
+                                .tagType(Component.TagType.VLAN_CTAG)
+                                .build())
+                .build();
+
+        MaintenanceAssociation maTest12 = DefaultMaintenanceAssociation
+                .builder(MaIdCharStr.asMaId("test-ma-1-2"), 9)
+                .ccmInterval(MaintenanceAssociation.CcmInterval.INTERVAL_10MIN)
+                .maNumericId((short) 2)
+                .addToRemoteMepIdList(MepId.valueOf((short) 201))
+                .addToRemoteMepIdList(MepId.valueOf((short) 202))
+                .addToComponentList(
+                        DefaultComponent.builder(2)
+                                .tagType(Component.TagType.VLAN_CTAG)
+                                .build())
+                .build();
+
+        MaintenanceDomain mdTest1 = DefaultMaintenanceDomain
+                .builder(MdIdCharStr.asMdId("test-md-1"))
+                .mdLevel(MaintenanceDomain.MdLevel.LEVEL1)
+                .mdNumericId((short) 1)
+                .addToMaList(maTest11)
+                .addToMaList(maTest12)
+                .build();
+
+        TestUtils.setField(mdStore, "storageService", new TestStorageService());
+        TestUtils.setField(mdStore, "clusterService", new CfmMdManagerTest.TestClusterService());
+        TestUtils.setField(mdStore, "mastershipService", new CfmMdManagerTest.TestMastershipService());
+        mdStore.activate();
+        mdStore.createUpdateMaintenanceDomain(mdTest1);
+
+        manager = new CfmMdManager();
+        manager.store = mdStore;
+        service = manager;
+        TestUtils.setField(manager, "storageService", new TestStorageService());
+        TestUtils.setField(manager, "coreService", new TestCoreService());
+        injectEventDispatcher(manager, new TestEventDispatcher());
+
+        manager.appId = new CfmMdManagerTest.TestApplicationId(0, "CfmMdManagerTest");
+        manager.activate();
+    }
+
+    @After
+    public void tearDown() {
+
+        manager.deactivate();
+        injectEventDispatcher(manager, null);
+
+    }
+
+    @Test
+    public void testGetAllMaintenanceDomain() {
+        Collection<MaintenanceDomain> mdList = service.getAllMaintenanceDomain();
+        assertEquals(1, mdList.size());
+
+        MaintenanceDomain md = mdList.iterator().next();
+        assertEquals(1, md.mdNumericId());
+
+        assertEquals(2, md.maintenanceAssociationList().size());
+
+        md.maintenanceAssociationList().iterator().forEachRemaining(ma ->
+                assertTrue(ma.maId().maName().endsWith(String.valueOf(ma.maNumericId())))
+        );
+    }
+
+    @Test
+    public void testGetMaintenanceDomain() {
+        Optional<MaintenanceDomain> md =
+                service.getMaintenanceDomain(MdIdCharStr.asMdId("test-md-1"));
+        assertTrue(md.isPresent());
+
+        assertEquals(1, md.get().mdNumericId());
+
+        assertEquals(2, md.get().maintenanceAssociationList().size());
+
+
+
+        //Now try an invalid name
+        Optional<MaintenanceDomain> mdInvalid =
+                service.getMaintenanceDomain(MdIdCharStr.asMdId("test-md-3"));
+        assertFalse(mdInvalid.isPresent());
+    }
+
+    @Test
+    public void testDeleteMaintenanceDomain() {
+        try {
+            assertTrue(service.deleteMaintenanceDomain(
+                    MdIdCharStr.asMdId("test-md-1")));
+        } catch (CfmConfigException e) {
+            fail("Should not have thrown exception: " + e.getMessage());
+        }
+
+        //Now try an invalid name
+        try {
+            assertFalse(service.deleteMaintenanceDomain(
+                    MdIdCharStr.asMdId("test-md-3")));
+        } catch (CfmConfigException e) {
+            fail("Should not have thrown exception: " + e.getMessage());
+        }
+    }
+
+    @Test
+    public void testCreateMaintenanceDomain() throws CfmConfigException {
+
+        MaintenanceAssociation maTest21 = DefaultMaintenanceAssociation
+                .builder(MaIdCharStr.asMaId("test-ma-2-1"), 9)
+                .ccmInterval(MaintenanceAssociation.CcmInterval.INTERVAL_1S)
+                .maNumericId((short) 1)
+                .addToRemoteMepIdList(MepId.valueOf((short) 101))
+                .addToRemoteMepIdList(MepId.valueOf((short) 102))
+                .addToComponentList(
+                        DefaultComponent.builder(1)
+                                .tagType(Component.TagType.VLAN_STAG)
+                                .build())
+                .build();
+
+        MaintenanceDomain mdTest2 = DefaultMaintenanceDomain
+                .builder(MdIdCharStr.asMdId("test-md-2"))
+                .mdLevel(MaintenanceDomain.MdLevel.LEVEL2)
+                .mdNumericId((short) 2)
+                .addToMaList(maTest21)
+                .build();
+
+
+        //We expect false here because there should have been no previous value
+        //with that ID in the store
+        assertFalse(service.createMaintenanceDomain(mdTest2));
+    }
+
+    @Test
+    public void testGetAllMaintenanceAssociation() {
+        Collection<MaintenanceAssociation> maListMd1 =
+            service.getAllMaintenanceAssociation(
+                    MdIdCharStr.asMdId("test-md-1"));
+
+        assertEquals(2, maListMd1.size());
+
+        maListMd1.iterator().forEachRemaining(ma ->
+            assertTrue(ma.maId().maName().endsWith(String.valueOf(ma.maNumericId())))
+        );
+
+        //Now try with an invalid name
+        try {
+            service.getAllMaintenanceAssociation(
+                    MdIdCharStr.asMdId("test-md-2"));
+        } catch (IllegalArgumentException e) {
+            assertEquals("Unknown MD test-md-2", e.getMessage());
+        }
+    }
+
+    @Test
+    public void testGetMaintenanceAssociation() {
+        Optional<MaintenanceAssociation> ma =
+                service.getMaintenanceAssociation(
+                        MdIdCharStr.asMdId("test-md-1"),
+                        MaIdCharStr.asMaId("test-ma-1-2"));
+
+        assertTrue(ma.isPresent());
+
+        //Now try an invalid MD Name
+        try {
+            service.getMaintenanceAssociation(
+                    MdIdCharStr.asMdId("test-md-2"),
+                    MaIdCharStr.asMaId("test-ma-1-2"));
+        } catch (IllegalArgumentException e) {
+            assertEquals("Unknown MD test-md-2", e.getMessage());
+        }
+
+        //Now try an invalid MA Name with a valid MD Name
+        try {
+            Optional<MaintenanceAssociation> maInvalid =
+                    service.getMaintenanceAssociation(
+                    MdIdCharStr.asMdId("test-md-1"),
+                    MaIdCharStr.asMaId("test-ma-1-3"));
+            assertFalse(maInvalid.isPresent());
+        } catch (IllegalArgumentException e) {
+            fail("Should not throw exception: " + e.getMessage());
+        }
+    }
+
+    @Test
+    public void testDeleteMaintenanceAssociation() throws CfmConfigException {
+        assertTrue(service.deleteMaintenanceAssociation(
+                MdIdCharStr.asMdId("test-md-1"),
+                MaIdCharStr.asMaId("test-ma-1-2")));
+
+        //Now check it has actually been removed
+        Collection<MaintenanceAssociation> maListUpdated =
+                service.getAllMaintenanceAssociation(
+                        MdIdCharStr.asMdId("test-md-1"));
+        assertEquals(1, maListUpdated.size());
+        maListUpdated.stream().findFirst().ifPresent(ma ->
+                assertEquals("test-ma-1-1", ma.maId().maName())
+        );
+
+        //Now try with an invalid mdName
+        try {
+            service.deleteMaintenanceAssociation(
+                    MdIdCharStr.asMdId("test-md-2"),
+                    MaIdCharStr.asMaId("test-ma-1-2"));
+        } catch (IllegalArgumentException e) {
+            assertEquals("Unknown MD: test-md-2", e.getMessage());
+        }
+
+        //Now try with an invalid maName
+        try {
+            assertFalse(service.deleteMaintenanceAssociation(
+                            MdIdCharStr.asMdId("test-md-1"),
+                            MaIdCharStr.asMaId("test-ma-1-3")));
+        } catch (IllegalArgumentException e) {
+            fail("Should not throw exception: " + e.getMessage());
+        }
+    }
+
+    @Test
+    public void testCreateMaintenanceAssociation() throws CfmConfigException {
+        MaintenanceAssociation maTest41 = DefaultMaintenanceAssociation
+                .builder(MaIdCharStr.asMaId("test-ma-1-4"), 9)
+                .ccmInterval(MaintenanceAssociation.CcmInterval.INTERVAL_1S)
+                .maNumericId((short) 4)
+                .addToRemoteMepIdList(MepId.valueOf((short) 401))
+                .addToRemoteMepIdList(MepId.valueOf((short) 402))
+                .addToComponentList(
+                        DefaultComponent.builder(4)
+                                .tagType(Component.TagType.VLAN_STAG)
+                                .build())
+                .build();
+
+        //Should return false, as this MA did not exist before
+        assertFalse(service.createMaintenanceAssociation(
+                            MdIdCharStr.asMdId("test-md-1"), maTest41));
+        assertEquals(3, service.getAllMaintenanceAssociation(
+                                MdIdCharStr.asMdId("test-md-1")).size());
+
+        //Now try with an invalid mdName
+        try {
+            service.createMaintenanceAssociation(
+                    MdIdCharStr.asMdId("test-md-2"), maTest41);
+        } catch (IllegalArgumentException e) {
+            assertEquals("Unknown MD: test-md-2", e.getMessage());
+        }
+
+        //Now try replacing an MA with some new values or Remote MEP
+        MaintenanceAssociation maTest11 = DefaultMaintenanceAssociation
+                .builder(MaIdCharStr.asMaId("test-ma-1-1"), 9)
+                .ccmInterval(MaintenanceAssociation.CcmInterval.INTERVAL_10MIN)
+                .maNumericId((short) 1)
+                .addToRemoteMepIdList(MepId.valueOf((short) 111)) //Changed
+                .addToRemoteMepIdList(MepId.valueOf((short) 112)) //Changed
+                .addToComponentList(
+                        DefaultComponent.builder(1)
+                                .tagType(Component.TagType.VLAN_CTAG)
+                                .build())
+                .build();
+
+        //Should return true, as this MA did exist before
+        assertTrue(service.createMaintenanceAssociation(
+                MdIdCharStr.asMdId("test-md-1"), maTest11));
+        assertEquals(3, service.getAllMaintenanceAssociation(
+                MdIdCharStr.asMdId("test-md-1")).size());
+    }
+
+
+    public class TestApplicationId extends DefaultApplicationId {
+        public TestApplicationId(int id, String name) {
+            super(id, name);
+        }
+    }
+
+    private final class TestClusterService extends ClusterServiceAdapter {
+
+        ControllerNode local = new DefaultControllerNode(NID_LOCAL, LOCALHOST);
+
+        @Override
+        public ControllerNode getLocalNode() {
+            return local;
+        }
+
+        @Override
+        public Set<ControllerNode> getNodes() {
+            return Sets.newHashSet();
+        }
+
+    }
+
+    private class TestMastershipService extends MastershipServiceAdapter {
+        @Override
+        public NodeId getMasterFor(DeviceId deviceId) {
+            return NID_LOCAL;
+        }
+    }
+
+    private class TestCoreService extends CoreServiceAdapter {
+
+        @Override
+        public IdGenerator getIdGenerator(String topic) {
+            return new IdGenerator() {
+                private AtomicLong counter = new AtomicLong(0);
+
+                @Override
+                public long getNewId() {
+                    return counter.getAndIncrement();
+                }
+            };
+        }
+    }
+}
diff --git a/incubator/net/src/test/java/org/onosproject/incubator/net/l2monitoring/cfm/impl/CfmMepManagerTest.java b/incubator/net/src/test/java/org/onosproject/incubator/net/l2monitoring/cfm/impl/CfmMepManagerTest.java
new file mode 100644
index 0000000..a825858
--- /dev/null
+++ b/incubator/net/src/test/java/org/onosproject/incubator/net/l2monitoring/cfm/impl/CfmMepManagerTest.java
@@ -0,0 +1,381 @@
+/*
+ * 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.incubator.net.l2monitoring.cfm.impl;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.junit.TestUtils;
+import org.onlab.packet.ChassisId;
+import org.onosproject.common.event.impl.TestEventDispatcher;
+import org.onosproject.core.CoreServiceAdapter;
+import org.onosproject.core.IdGenerator;
+import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceAssociation;
+import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMep;
+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.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.CfmMepProgrammable;
+import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMepService;
+import org.onosproject.incubator.net.l2monitoring.soam.SoamDmProgrammable;
+import org.onosproject.incubator.net.l2monitoring.soam.impl.TestSoamDmProgrammable;
+import org.onosproject.net.AbstractProjectableModel;
+import org.onosproject.net.AnnotationKeys;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.DefaultDevice;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.device.DeviceDescriptionDiscovery;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.driver.Behaviour;
+import org.onosproject.net.driver.DefaultDriver;
+import org.onosproject.net.driver.Driver;
+import org.onosproject.net.driver.DriverService;
+import org.onosproject.net.provider.ProviderId;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.atomic.AtomicLong;
+
+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.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.onosproject.net.NetTestTools.injectEventDispatcher;
+
+/**
+ * CFM MEP Manager test.
+ */
+public class CfmMepManagerTest {
+    private static final String TEST_MFR = "testMfr";
+    private static final String TEST_HW_VERSION = "testHwVersion";
+    private static final String TEST_SW_VERSION = "testSwVersion";
+    private static final String TEST_SN = "testSn";
+    private static final String TEST_DRIVER = "testDriver";
+    public static final String TEST_DRIVER_3 = "testDriver3";
+    public static final String TEST_SW_3 = "testSw3";
+    private final CfmMdService mdService = createMock(CfmMdService.class);
+    private final DeviceService deviceService = createMock(DeviceService.class);
+    private final DriverService driverService = createMock(DriverService.class);
+
+    private CfmMepService mepService;
+    private CfmMepManager mepManager;
+
+    protected static final MdId MDNAME1 = MdIdCharStr.asMdId("md-1");
+    protected static final MaIdShort MANAME1 = MaIdCharStr.asMaId("ma-1-1");
+
+    private MaintenanceAssociation ma1;
+    protected static final MepId MEPID1 = MepId.valueOf((short) 10);
+    protected static final MepId MEPID2 = MepId.valueOf((short) 20);
+    protected static final DeviceId DEVICE_ID1 = DeviceId.deviceId("netconf:1.2.3.4:830");
+    protected static final DeviceId DEVICE_ID2 = DeviceId.deviceId("netconf:2.2.3.4:830");
+
+    private Mep mep1;
+    private Mep mep2;
+
+    private Device device1;
+    private Device device2;
+
+    private Driver testDriver;
+
+    @Before
+    public void setup() throws CfmConfigException {
+        mepManager = new CfmMepManager();
+
+        ma1 = DefaultMaintenanceAssociation.builder(MANAME1, MDNAME1.getNameLength()).build();
+
+        TestUtils.setField(mepManager, "coreService", new TestCoreService());
+        TestUtils.setField(mepManager, "deviceService", deviceService);
+        TestUtils.setField(mepManager, "cfmMdService", mdService);
+        injectEventDispatcher(mepManager, new TestEventDispatcher());
+
+        mepService = mepManager;
+        mepManager.activate();
+
+        mep1 = DefaultMep.builder(MEPID1, DEVICE_ID1, PortNumber.P0,
+                Mep.MepDirection.UP_MEP, MDNAME1, MANAME1).build();
+        mep2 = DefaultMep.builder(MEPID2, DEVICE_ID2, PortNumber.portNumber(2),
+                Mep.MepDirection.UP_MEP, MDNAME1, MANAME1).build();
+        List<Mep> mepList = new ArrayList<>();
+        mepList.add(mep1);
+        mepList.add(mep2);
+        TestUtils.setField(mepManager, "mepCollection", mepList);
+
+        device1 = new DefaultDevice(
+                ProviderId.NONE, DEVICE_ID1, Device.Type.SWITCH,
+                TEST_MFR, TEST_HW_VERSION, TEST_SW_VERSION, TEST_SN,
+                new ChassisId(1),
+                DefaultAnnotations.builder().set(AnnotationKeys.DRIVER, TEST_DRIVER).build());
+
+        device2 = new DefaultDevice(
+                ProviderId.NONE, DEVICE_ID2, Device.Type.SWITCH,
+                TEST_MFR, TEST_HW_VERSION, TEST_SW_VERSION, TEST_SN,
+                new ChassisId(2),
+                DefaultAnnotations.builder().set(AnnotationKeys.DRIVER, TEST_DRIVER).build());
+
+        AbstractProjectableModel.setDriverService(null, driverService);
+
+        Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours = new HashMap<>();
+        behaviours.put(DeviceDescriptionDiscovery.class, TestDeviceDiscoveryBehavior.class);
+        behaviours.put(CfmMepProgrammable.class, TestCfmMepProgrammable.class);
+        behaviours.put(SoamDmProgrammable.class, TestSoamDmProgrammable.class);
+
+        testDriver = new DefaultDriver(
+                TEST_DRIVER, new ArrayList<Driver>(),
+                TEST_MFR, TEST_HW_VERSION, TEST_SW_VERSION,
+                behaviours, new HashMap<>());
+    }
+
+
+    @After
+    public void tearDown() {
+//        mepManager.deactivate();
+    }
+
+    @Test
+    public void testGetAllMeps() throws CfmConfigException {
+
+        expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1))
+                .andReturn(Optional.ofNullable(ma1))
+                .anyTimes();
+        replay(mdService);
+
+        expect(deviceService.getDevice(DEVICE_ID1)).andReturn(device1).anyTimes();
+        expect(deviceService.getDevice(DEVICE_ID2)).andReturn(device2).anyTimes();
+        replay(deviceService);
+
+        expect(driverService.getDriver(TEST_DRIVER)).andReturn(testDriver).anyTimes();
+        replay(driverService);
+
+        Collection<MepEntry> mepEntries = mepManager.getAllMeps(MDNAME1, MANAME1);
+
+        assertEquals(2, mepEntries.size());
+    }
+
+    @Test
+    public void testGetMep() throws CfmConfigException {
+
+        expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1))
+                .andReturn(Optional.ofNullable(ma1))
+                .anyTimes();
+        replay(mdService);
+
+        expect(deviceService.getDevice(DEVICE_ID1)).andReturn(device1).anyTimes();
+        replay(deviceService);
+
+        expect(driverService.getDriver(TEST_DRIVER)).andReturn(testDriver).anyTimes();
+        replay(driverService);
+
+        MepEntry mepEntry = mepManager.getMep(MDNAME1, MANAME1, MEPID1);
+
+        assertEquals(MEPID1.value(), mepEntry.mepId().value());
+    }
+
+    @Test
+    public void testGetMepMissing() {
+
+        expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1))
+                .andReturn(Optional.ofNullable(ma1))
+                .anyTimes();
+        replay(mdService);
+
+        expect(deviceService.getDevice(DEVICE_ID1)).andReturn(null).anyTimes();
+        replay(deviceService);
+
+        expect(driverService.getDriver(TEST_DRIVER)).andReturn(testDriver).anyTimes();
+        replay(driverService);
+
+        try {
+            mepManager.getMep(MDNAME1, MANAME1, MEPID1);
+            fail("Expecting CfmConfigException because device does not exist");
+        } catch (CfmConfigException e) {
+            assertEquals("Device not found netconf:1.2.3.4:830", e.getMessage());
+        }
+    }
+
+    @Test
+    public void testDeleteMep() throws CfmConfigException {
+        expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1))
+                .andReturn(Optional.ofNullable(ma1))
+                .anyTimes();
+        replay(mdService);
+
+        expect(deviceService.getDevice(DEVICE_ID1)).andReturn(device1).anyTimes();
+        replay(deviceService);
+
+        expect(driverService.getDriver(TEST_DRIVER)).andReturn(testDriver).anyTimes();
+        replay(driverService);
+
+        assertTrue(mepManager.deleteMep(MDNAME1, MANAME1, MEPID1));
+    }
+
+    @Test
+    public void testCreateMep() throws CfmConfigException {
+        expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1))
+                .andReturn(Optional.ofNullable(ma1))
+                .anyTimes();
+        replay(mdService);
+
+        expect(deviceService.getDevice(DEVICE_ID1)).andReturn(device1).anyTimes();
+        replay(deviceService);
+
+        expect(driverService.getDriver(TEST_DRIVER)).andReturn(testDriver).anyTimes();
+        replay(driverService);
+
+        MepId mepId3 = MepId.valueOf((short) 3);
+        Mep mep3 = DefaultMep.builder(mepId3, DEVICE_ID1, PortNumber.portNumber(1),
+                Mep.MepDirection.UP_MEP, MDNAME1, MANAME1).build();
+
+        assertTrue(mepManager.createMep(MDNAME1, MANAME1, mep3));
+    }
+
+    @Test
+    public void testCreateMepBehaviorNotSupported() throws CfmConfigException {
+        final DeviceId deviceId3 = DeviceId.deviceId("netconf:3.2.3.4:830");
+
+        Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours = new HashMap<>();
+        behaviours.put(DeviceDescriptionDiscovery.class, TestDeviceDiscoveryBehavior.class);
+
+        Driver testDriver3 = new DefaultDriver(
+                TEST_DRIVER_3, new ArrayList<Driver>(),
+                TEST_MFR, TEST_HW_VERSION, TEST_SW_3,
+                behaviours, new HashMap<>());
+
+        Device device3 = new DefaultDevice(
+                ProviderId.NONE, deviceId3, Device.Type.SWITCH,
+                TEST_MFR, TEST_HW_VERSION, TEST_SW_3, TEST_SN,
+                new ChassisId(2),
+                DefaultAnnotations.builder().set(AnnotationKeys.DRIVER, TEST_DRIVER_3).build());
+
+        expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1))
+                .andReturn(Optional.ofNullable(ma1))
+                .anyTimes();
+        replay(mdService);
+
+        expect(deviceService.getDevice(deviceId3)).andReturn(device3).anyTimes();
+        replay(deviceService);
+
+        expect(driverService.getDriver(TEST_DRIVER_3)).andReturn(testDriver3).anyTimes();
+        replay(driverService);
+
+        MepId mepId3 = MepId.valueOf((short) 3);
+        Mep mep3 = DefaultMep.builder(mepId3, deviceId3, PortNumber.portNumber(1),
+                Mep.MepDirection.UP_MEP, MDNAME1, MANAME1).build();
+
+        try {
+            mepManager.createMep(MDNAME1, MANAME1, mep3);
+            fail("Expecting CfmConfigException because driver does not support behavior");
+        } catch (CfmConfigException e) {
+            assertEquals("Device netconf:3.2.3.4:830 does not support " +
+                    "CfmMepProgrammable behaviour.", e.getMessage());
+        }
+    }
+
+    @Test
+    public void testTransmitLoopback() {
+        expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1))
+                .andReturn(Optional.ofNullable(ma1))
+                .anyTimes();
+        replay(mdService);
+
+        expect(deviceService.getDevice(DEVICE_ID1)).andReturn(device1).anyTimes();
+        replay(deviceService);
+
+        expect(driverService.getDriver(TEST_DRIVER)).andReturn(testDriver).anyTimes();
+        replay(driverService);
+
+        MepLbCreate lbCreate = DefaultMepLbCreate.builder(MepId.valueOf((short) 11)).build();
+        try {
+            mepService.transmitLoopback(MDNAME1, MANAME1, MEPID1, lbCreate);
+        } catch (CfmConfigException e) {
+            fail("Not expecting an exception");
+        }
+    }
+
+    @Test
+    public void testAbortLoopback() {
+        expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1))
+                .andReturn(Optional.ofNullable(ma1))
+                .anyTimes();
+        replay(mdService);
+
+        expect(deviceService.getDevice(DEVICE_ID1)).andReturn(device1).anyTimes();
+        replay(deviceService);
+
+        expect(driverService.getDriver(TEST_DRIVER)).andReturn(testDriver).anyTimes();
+        replay(driverService);
+
+        try {
+            mepService.abortLoopback(MDNAME1, MANAME1, MEPID1);
+        } catch (CfmConfigException e) {
+            fail("Not expecting an exception");
+        }
+    }
+
+    @Test
+    public void testTransmitLinktrace() throws CfmConfigException {
+        expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1))
+                .andReturn(Optional.ofNullable(ma1))
+                .anyTimes();
+        replay(mdService);
+
+        expect(deviceService.getDevice(DEVICE_ID1)).andReturn(device1).anyTimes();
+        replay(deviceService);
+
+        expect(driverService.getDriver(TEST_DRIVER)).andReturn(testDriver).anyTimes();
+        replay(driverService);
+
+        MepLtCreate ltCreate = DefaultMepLtCreate.builder(MepId.valueOf((short) 11)).build();
+        try {
+            mepService.transmitLinktrace(MDNAME1, MANAME1, MEPID1, ltCreate);
+        } catch (UnsupportedOperationException e) {
+            assertEquals("Not yet implemented", e.getMessage());
+        }
+    }
+
+    private class TestCoreService extends CoreServiceAdapter {
+
+        @Override
+        public IdGenerator getIdGenerator(String topic) {
+            return new IdGenerator() {
+                private AtomicLong counter = new AtomicLong(0);
+
+                @Override
+                public long getNewId() {
+                    return counter.getAndIncrement();
+                }
+            };
+        }
+    }
+}
diff --git a/incubator/net/src/test/java/org/onosproject/incubator/net/l2monitoring/cfm/impl/TestCfmMepProgrammable.java b/incubator/net/src/test/java/org/onosproject/incubator/net/l2monitoring/cfm/impl/TestCfmMepProgrammable.java
new file mode 100644
index 0000000..8d0f765
--- /dev/null
+++ b/incubator/net/src/test/java/org/onosproject/incubator/net/l2monitoring/cfm/impl/TestCfmMepProgrammable.java
@@ -0,0 +1,95 @@
+/*
+ * 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.incubator.net.l2monitoring.cfm.impl;
+
+import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMep;
+import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMepEntry;
+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.MaIdShort;
+import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId;
+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.CfmMepProgrammable;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import static org.onosproject.incubator.net.l2monitoring.cfm.impl.CfmMepManagerTest.*;
+
+/**
+ * A dummy implementation of the CfmMepProgrammable for test purposes.
+ */
+public class TestCfmMepProgrammable extends AbstractHandlerBehaviour implements CfmMepProgrammable {
+
+    private List<Mep> deviceMepList;
+
+    public TestCfmMepProgrammable() throws CfmConfigException {
+        deviceMepList = new ArrayList<>();
+
+        deviceMepList.add(DefaultMep.builder(MEPID1, DEVICE_ID1, PortNumber.P0,
+                Mep.MepDirection.UP_MEP, MDNAME1, MANAME1).build());
+        deviceMepList.add(DefaultMep.builder(MEPID2, DEVICE_ID2, PortNumber.portNumber(2),
+                Mep.MepDirection.UP_MEP, MDNAME1, MANAME1).build());
+    }
+
+    @Override
+    public Collection<MepEntry> getAllMeps(MdId mdName, MaIdShort maName) throws CfmConfigException {
+        return null;
+    }
+
+    @Override
+    public MepEntry getMep(MdId mdName, MaIdShort maName, MepId mepId) throws CfmConfigException {
+        for (Mep mep:deviceMepList) {
+            if (mep.mdId().equals(mdName) && mep.maId().equals(maName) && mep.mepId().equals(mepId)) {
+                return DefaultMepEntry.builder(mep).buildEntry();
+            }
+        }
+        return null;
+    }
+
+    @Override
+    public boolean deleteMep(MdId mdName, MaIdShort maName, MepId mepId) throws CfmConfigException {
+        return true;
+    }
+
+    @Override
+    public boolean createMep(MdId mdName, MaIdShort maName, Mep mep) throws CfmConfigException {
+        return true;
+    }
+
+    @Override
+    public void transmitLoopback(MdId mdName, MaIdShort maName, MepId mepId, MepLbCreate lbCreate)
+            throws CfmConfigException {
+
+    }
+
+    @Override
+    public void abortLoopback(MdId mdName, MaIdShort maName, MepId mepId) throws CfmConfigException {
+
+    }
+
+    @Override
+    public void transmitLinktrace(MdId mdName, MaIdShort maName, MepId mepId, MepLtCreate ltCreate)
+            throws CfmConfigException {
+
+    }
+}
diff --git a/incubator/net/src/test/java/org/onosproject/incubator/net/l2monitoring/cfm/impl/TestDeviceDiscoveryBehavior.java b/incubator/net/src/test/java/org/onosproject/incubator/net/l2monitoring/cfm/impl/TestDeviceDiscoveryBehavior.java
new file mode 100644
index 0000000..01d3d01
--- /dev/null
+++ b/incubator/net/src/test/java/org/onosproject/incubator/net/l2monitoring/cfm/impl/TestDeviceDiscoveryBehavior.java
@@ -0,0 +1,37 @@
+/*
+ * 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.incubator.net.l2monitoring.cfm.impl;
+
+import org.onosproject.net.device.DeviceDescription;
+import org.onosproject.net.device.DeviceDescriptionDiscovery;
+import org.onosproject.net.device.PortDescription;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+
+import java.util.List;
+
+public class TestDeviceDiscoveryBehavior extends AbstractHandlerBehaviour
+        implements DeviceDescriptionDiscovery {
+
+    @Override
+    public DeviceDescription discoverDeviceDetails() {
+        return null;
+    }
+
+    @Override
+    public List<PortDescription> discoverPortDetails() {
+        return null;
+    }
+}