/* * Copyright (c) 2014 Contextream, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.lispflowmapping.implementation.util; import java.util.AbstractMap; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKeyUtil; import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO; import org.opendaylight.lispflowmapping.interfaces.dao.IMappingServiceKey; import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceRLOCGroup; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispAFIAddress; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.eidrecords.EidRecord; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.eidtolocatorrecords.EidToLocatorRecord; public class DAOMappingUtil { public static List getLocatorsByEidRecord(EidRecord eid, ILispDAO dao, boolean iterateMask) { IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eid.getLispAddressContainer(), eid.getMask()); return getLocators(key, dao, iterateMask); } public static Object getLocatorsSpecificByEidRecord(EidRecord eid, ILispDAO dao, String subkey, boolean iterateMask) { IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eid.getLispAddressContainer(), eid.getMask()); return getLocatorsSpecific(key, dao, subkey, iterateMask); } public static List getLocatorsByEidToLocatorRecord(EidToLocatorRecord eid, ILispDAO dao, boolean iterateMask) { IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eid.getLispAddressContainer(), eid.getMaskLength()); return getLocators(key, dao, iterateMask); } public static Object getLocatorsSpecificByEidtoLocatorRecord(EidToLocatorRecord eid, ILispDAO dao, String subkey, boolean iterateMask) { IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eid.getLispAddressContainer(), eid.getMaskLength()); return getLocatorsSpecific(key, dao, subkey, iterateMask); } public static List getLocators(IMappingServiceKey key, ILispDAO dao, boolean iterateMask) { Map locators = dao.get(key); List result = aggregateLocators(locators); LispAFIAddress eid = LispAFIConvertor.toAFI(key.getEID()); if (iterateMask && result.isEmpty() && MaskUtil.isMaskable(eid)) { result = findMaskLocators(dao, key); } return result; } private static List aggregateLocators(Map locators) { List result = new ArrayList(); if (locators != null) { for (Object value : locators.values()) { if (value != null && value instanceof MappingServiceRLOCGroup) { if (!((MappingServiceRLOCGroup) value).getRecords().isEmpty()) { result.add((MappingServiceRLOCGroup) value); } } } } return result; } public static Object getLocatorsSpecific(IMappingServiceKey key, ILispDAO dao, String subkey, boolean iterateMask) { Object locators = dao.getSpecific(key, subkey); LispAFIAddress eid = LispAFIConvertor.toAFI(key.getEID()); if (iterateMask && locators == null && MaskUtil.isMaskable(eid)) { locators = findMaskLocatorsSpecific(key, dao, subkey); } return locators; } private static Object findMaskLocatorsSpecific(IMappingServiceKey key, ILispDAO dao, String subkey) { int mask = key.getMask(); while (mask > 0) { LispAFIAddress eid = LispAFIConvertor.toAFI(key.getEID()); key = MappingServiceKeyUtil.generateMappingServiceKey( LispAFIConvertor.toContainer(MaskUtil.normalize(eid, mask)), mask); mask--; Object locators = dao.getSpecific(key, subkey); if (locators != null) { return locators; } } return null; } private static List findMaskLocators(ILispDAO dao, IMappingServiceKey key) { int mask = key.getMask(); while (mask > 0) { LispAFIAddress eid = LispAFIConvertor.toAFI(key.getEID()); key = MappingServiceKeyUtil.generateMappingServiceKey( LispAFIConvertor.toContainer(MaskUtil.normalize(eid, mask)), mask); mask--; Map locators = dao.get(key); if (locators != null) { List result = aggregateLocators(locators); if (result != null && !result.isEmpty()) { return result; } } } return null; } public static Entry> getMappingForEidRecord(EidRecord eid, ILispDAO dao) { IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eid.getLispAddressContainer(), eid.getMask()); LispAFIAddress eidAddress = LispAFIConvertor.toAFI(key.getEID()); if (MaskUtil.isMaskable(eidAddress)) { int mask = eid.getMask(); while (mask > 0) { key = MappingServiceKeyUtil.generateMappingServiceKey( LispAFIConvertor.toContainer(MaskUtil.normalize(eidAddress, mask)), mask); mask--; Map locators = dao.get(key); if (locators != null) { List locatorsList = aggregateLocators(locators); if (locatorsList != null && !locatorsList.isEmpty()) { Entry> result = new AbstractMap.SimpleImmutableEntry<>(key, locatorsList); return result; } } } // empty mapping key = MappingServiceKeyUtil.generateMappingServiceKey(eid.getLispAddressContainer(), eid.getMask()); return new AbstractMap.SimpleImmutableEntry<>(key, null); } else { Map locators = dao.get(key); List aggLocators = aggregateLocators(locators); return new AbstractMap.SimpleImmutableEntry<>(key, aggLocators); } } }