@Override
    public boolean deleteMep(MdId mdName, MaIdShort maName, MepId mepId,
                             Optional<MaintenanceDomain> oldMd) throws CfmConfigException {
        MepKeyId key = new MepKeyId(mdName, maName, mepId);

        //Will throw IllegalArgumentException if ma does not exist
        cfmMdService.getMaintenanceAssociation(mdName, maName);

        //Get the device ID from the MEP
        Optional<Mep> deletedMep = mepStore.getMep(key);
        if (!deletedMep.isPresent()) {
            log.warn("MEP {} not found when deleting Mep", key);
            return false;
        }

        DeviceId mepDeviceId = deletedMep.get().deviceId();
        boolean deleted = mepStore.deleteMep(key);

        Device mepDevice = deviceService.getDevice(mepDeviceId);
        if (mepDevice == null || !mepDevice.is(CfmMepProgrammable.class)) {
            throw new CfmConfigException("Unexpeced fault on device driver for "
                    + mepDeviceId);
        }
        try {
            deleted = mepDevice.as(CfmMepProgrammable.class)
                    .deleteMep(mdName, maName, mepId, oldMd);
        } catch (CfmConfigException e) {
            log.warn("MEP could not be deleted on device - perhaps it "
                    + "does not exist. Continuing");
        }

        //Iterate through all other devices and remove as a Remote Mep
        int mepsOnMdCount = 0;
        int mepsOnMaCount = 0;
        List<DeviceId> alreadyHandledDevices = new ArrayList<>();
        for (Mep mep : mepStore.getAllMeps()) {
            if (mep.deviceId().equals(mepDeviceId) && mdName.equals(mep.mdId())) {
                mepsOnMdCount++;
                if (maName.equals(mep.maId())) {
                    mepsOnMaCount++;
                }
            }
            if (mep.deviceId().equals(mepDeviceId) || !mep.mdId().equals(mdName) ||
                    !mep.maId().equals(maName) ||
                    alreadyHandledDevices.contains(mep.deviceId())) {
                continue;
            }
            deviceService.getDevice(mep.deviceId())
                    .as(CfmMepProgrammable.class)
                    .deleteMaRemoteMepOnDevice(mdName, maName, mepId);
            alreadyHandledDevices.add(mep.deviceId());
            log.info("Deleted RMep entry on {} on device {}",
                    mdName.mdName() + "/" + maName.maName(), mep.deviceId());
        }

        //Also if this is the last MEP in this MA then delete this MA from device
        //If this is the last MA in this MD on device, then delete the MD from the device
        if (mepsOnMdCount == 0) {
            boolean deletedMd = deviceService.getDevice(mepDeviceId)
                    .as(CfmMepProgrammable.class).deleteMdOnDevice(mdName, oldMd);
            log.info("Deleted MD {} from Device {}", mdName.mdName(), mepDeviceId);
        } else if (mepsOnMaCount == 0) {
            boolean deletedMa = deviceService.getDevice(mepDeviceId)
                    .as(CfmMepProgrammable.class).deleteMaOnDevice(mdName, maName, oldMd);
            log.info("Deleted MA {} from Device {}",
                    mdName.mdName() + "/" + maName.maName(), mepDeviceId);
        }

        return deleted;
    }
@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();
        expect(deviceService.getDevice(DEVICE_ID2)).andReturn(device2).anyTimes();
        replay(deviceService);

        expect(driverService.getDriver(DEVICE_ID1)).andReturn(testDriver).anyTimes();
        expect(driverService.getDriver(DEVICE_ID2)).andReturn(testDriver).anyTimes();
        replay(driverService);

        assertTrue(mepManager.deleteMep(MDNAME1, MANAME1, MEPID1, Optional.empty()));
    }