diff --git a/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/impl/LispExpireMapDatabase.java b/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/impl/LispExpireMapDatabase.java new file mode 100644 index 0000000000..e3649b9d81 --- /dev/null +++ b/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/impl/LispExpireMapDatabase.java @@ -0,0 +1,160 @@ +/* + * Copyright 2016-present Open Networking Laboratory + * + * 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.lisp.ctl.impl; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; +import org.onosproject.lisp.ctl.impl.map.ExpireMap; +import org.onosproject.lisp.ctl.impl.map.ExpireHashMap; +import org.onosproject.lisp.msg.protocols.DefaultLispProxyMapRecord.DefaultMapWithProxyBuilder; +import org.onosproject.lisp.msg.protocols.LispEidRecord; +import org.onosproject.lisp.msg.protocols.LispMapRecord; +import org.onosproject.lisp.msg.protocols.LispProxyMapRecord; +import org.onosproject.lisp.msg.types.LispAfiAddress; +import org.slf4j.Logger; + +import java.util.List; +import java.util.Optional; + +import static org.slf4j.LoggerFactory.getLogger; +import static org.onosproject.lisp.ctl.impl.util.LispMapUtil.isInRange; + +/** + * An expire map based LISP mapping database. + * A singleton class that stores EID-RLOC mapping information. + */ +public final class LispExpireMapDatabase implements LispMappingDatabase { + + private static final long MINUTE_TO_MS_UNIT = 60 * 1000; + + private static final Logger log = getLogger(LispExpireMapDatabase.class); + + private final ExpireMap map = new ExpireHashMap<>(); + + /** + * Prevents object instantiation from external. + */ + private LispExpireMapDatabase() { + } + + /** + * Obtains a singleton instance. + * + * @return singleton instance + */ + public static LispExpireMapDatabase getInstance() { + return SingletonHelper.INSTANCE; + } + + @Override + public void putMapRecord(LispEidRecord eid, LispMapRecord rloc, + boolean proxyMapReply) { + LispProxyMapRecord mapWithProxy = new DefaultMapWithProxyBuilder() + .withMapRecord(rloc) + .withIsProxyMapReply(proxyMapReply) + .build(); + map.put(eid, mapWithProxy, rloc.getRecordTtl() * MINUTE_TO_MS_UNIT); + } + + /** + * Returns the results whether a given EidRecord is contained in the map. + * + * @param eid endpoint identifier + * @return the results whether a given EidRecord is contained in the map + */ + public boolean hasEidRecord(LispEidRecord eid) { + return map.containsKey(eid); + } + + @Override + public void removeMapRecordByEid(LispEidRecord eid) { + map.remove(eid); + } + + @Override + public void removeAllMapRecords() { + map.clear(); + } + + /** + * Obtains all of the EID-RLOC mapping records. + * + * @return all of the EID-RLOC mapping records + */ + public List getAllMapRecords() { + + List mapRecords = Lists.newArrayList(); + + map.values().forEach(value -> mapRecords.add(value.getMapRecord())); + + return mapRecords; + } + + @Override + public LispMapRecord getMapRecordByEidRecord(LispEidRecord eid, + boolean proxyMapReply) { + Optional filteredEidRecord = map.keySet().parallelStream() + .filter(k -> isInRange(k, eid)).findAny(); + if (filteredEidRecord.isPresent()) { + LispProxyMapRecord record = map.get(filteredEidRecord.get()); + if (record != null && record.isProxyMapReply() == proxyMapReply) { + return record.getMapRecord(); + } + } + + return null; + } + + @Override + public List getMapRecordByEidRecords(List eids, + boolean proxyMapReply) { + List mapRecords = Lists.newArrayList(); + eids.forEach(eidRecord -> { + LispMapRecord mapRecord = + getMapRecordByEidRecord(eidRecord, proxyMapReply); + if (mapRecord != null) { + mapRecords.add(mapRecord); + } + }); + return ImmutableList.copyOf(mapRecords); + } + + /** + * Obtains an EID-RLOC mapping record with given EID address. + * + * @param address endpoint identifier address + * @return an EID-RLOC mapping record + */ + public LispMapRecord getMapRecordByEidAddress(LispAfiAddress address) { + Optional eidRecord = + map.keySet().stream().filter(k -> + k.getPrefix().equals(address)).findFirst(); + return eidRecord.map(lispEidRecord -> + map.get(lispEidRecord).getMapRecord()).orElse(null); + } + + /** + * Prevents object instantiation from external. + */ + private static final class SingletonHelper { + private static final String ILLEGAL_ACCESS_MSG = "Should not instantiate this class."; + private static final LispExpireMapDatabase INSTANCE = new LispExpireMapDatabase(); + + private SingletonHelper() { + throw new IllegalAccessError(ILLEGAL_ACCESS_MSG); + } + } +} diff --git a/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/impl/LispMapResolver.java b/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/impl/LispMapResolver.java index 1db6559e0c..fa264d1157 100644 --- a/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/impl/LispMapResolver.java +++ b/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/impl/LispMapResolver.java @@ -58,7 +58,7 @@ public final class LispMapResolver { "No ETR RLOC is found, cannot relay to ETR."; private static final String NO_MAP_INFO_MSG = "Map information is not found."; - private LispMappingDatabase mapDb = LispMappingDatabase.getInstance(); + private LispMappingDatabase mapDb = LispExpireMapDatabase.getInstance(); // non-instantiable (except for our Singleton) private LispMapResolver() { diff --git a/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/impl/LispMapServer.java b/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/impl/LispMapServer.java index ae766ba164..3be23c9978 100644 --- a/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/impl/LispMapServer.java +++ b/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/impl/LispMapServer.java @@ -75,7 +75,7 @@ public final class LispMapServer { private boolean enableSmr = false; - private LispMappingDatabase mapDb = LispMappingDatabase.getInstance(); + private LispMappingDatabase mapDb = LispExpireMapDatabase.getInstance(); private LispAuthenticationConfig authConfig = LispAuthenticationConfig.getInstance(); // non-instantiable (except for our Singleton) diff --git a/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/impl/LispMappingDatabase.java b/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/impl/LispMappingDatabase.java index aff24a2d48..58f128e253 100644 --- a/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/impl/LispMappingDatabase.java +++ b/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/impl/LispMappingDatabase.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-present Open Networking Laboratory + * Copyright 2017-present Open Networking Laboratory * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,48 +15,15 @@ */ package org.onosproject.lisp.ctl.impl; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Lists; -import org.onosproject.lisp.ctl.impl.map.ExpireMap; -import org.onosproject.lisp.ctl.impl.map.ExpireHashMap; -import org.onosproject.lisp.msg.protocols.DefaultLispProxyMapRecord.DefaultMapWithProxyBuilder; import org.onosproject.lisp.msg.protocols.LispEidRecord; import org.onosproject.lisp.msg.protocols.LispMapRecord; -import org.onosproject.lisp.msg.protocols.LispProxyMapRecord; -import org.onosproject.lisp.msg.types.LispAfiAddress; -import org.slf4j.Logger; import java.util.List; -import java.util.Optional; - -import static org.slf4j.LoggerFactory.getLogger; -import static org.onosproject.lisp.ctl.impl.util.LispMapUtil.isInRange; /** - * A singleton class that stores EID-RLOC mapping information. + * A LISP database that stores EID-RLOC mapping information. */ -public final class LispMappingDatabase { - - private static final long MINUTE_TO_MS_UNIT = 60 * 1000; - - private static final Logger log = getLogger(LispMappingDatabase.class); - - private final ExpireMap map = new ExpireHashMap<>(); - - /** - * Prevents object instantiation from external. - */ - private LispMappingDatabase() { - } - - /** - * Obtains a singleton instance. - * - * @return singleton instance - */ - public static LispMappingDatabase getInstance() { - return SingletonHelper.INSTANCE; - } +public interface LispMappingDatabase { /** * Inserts a new EID-RLOC mapping record. @@ -65,47 +32,19 @@ public final class LispMappingDatabase { * @param rloc route locator record * @param proxyMapReply proxy map reply flag */ - public void putMapRecord(LispEidRecord eid, LispMapRecord rloc, - boolean proxyMapReply) { - LispProxyMapRecord mapWithProxy = new DefaultMapWithProxyBuilder() - .withMapRecord(rloc) - .withIsProxyMapReply(proxyMapReply) - .build(); - map.put(eid, mapWithProxy, rloc.getRecordTtl() * MINUTE_TO_MS_UNIT); - } - - /** - * Returns the results whether a given EidRecord is contained in the map. - * - * @param eid endpoint identifier - * @return the results whether a given EidRecord is contained in the map - */ - public boolean hasEidRecord(LispEidRecord eid) { - return map.containsKey(eid); - } + void putMapRecord(LispEidRecord eid, LispMapRecord rloc, boolean proxyMapReply); /** * Removes an EID-RLOC mapping record with given endpoint identifier. * * @param eid endpoint identifier */ - public void removeMapRecordByEid(LispEidRecord eid) { - map.remove(eid); - } + void removeMapRecordByEid(LispEidRecord eid); /** - * Obtains all of the EID-RLOC mapping records. - * - * @return all of the EID-RLOC mapping records + * Removes all EID-RLOC mapping records. */ - public List getAllMapRecords() { - - List mapRecords = Lists.newArrayList(); - - map.values().forEach(value -> mapRecords.add(value.getMapRecord())); - - return mapRecords; - } + void removeAllMapRecords(); /** * Obtains an EID-RLOC mapping record in accordance with the proxy map reply @@ -115,19 +54,7 @@ public final class LispMappingDatabase { * @param proxyMapReply proxy map reply flag * @return an EID-RLOC mapping record */ - public LispMapRecord getMapRecordByEidRecord(LispEidRecord eid, - boolean proxyMapReply) { - Optional filteredEidRecord = map.keySet().parallelStream() - .filter(k -> isInRange(k, eid)).findAny(); - if (filteredEidRecord.isPresent()) { - LispProxyMapRecord record = map.get(filteredEidRecord.get()); - if (record != null && record.isProxyMapReply() == proxyMapReply) { - return record.getMapRecord(); - } - } - - return null; - } + LispMapRecord getMapRecordByEidRecord(LispEidRecord eid, boolean proxyMapReply); /** * Obtains a collection of EID-RLOC mapping record in accordance with the @@ -137,42 +64,9 @@ public final class LispMappingDatabase { * @param proxyMapReply proxy map reply flag * @return a collection of EID-RLOC mapping records */ - public List getMapRecordByEidRecords(List eids, - boolean proxyMapReply) { - List mapRecords = Lists.newArrayList(); - eids.forEach(eidRecord -> { - LispMapRecord mapRecord = - getMapRecordByEidRecord(eidRecord, proxyMapReply); - if (mapRecord != null) { - mapRecords.add(mapRecord); - } - }); - return ImmutableList.copyOf(mapRecords); - } + List getMapRecordByEidRecords(List eids, + boolean proxyMapReply); - /** - * Obtains an EID-RLOC mapping record with given EID address. - * - * @param address endpoint identifier address - * @return an EID-RLOC mapping record - */ - public LispMapRecord getMapRecordByEidAddress(LispAfiAddress address) { - Optional eidRecord = - map.keySet().stream().filter(k -> - k.getPrefix().equals(address)).findFirst(); - return eidRecord.map(lispEidRecord -> - map.get(lispEidRecord).getMapRecord()).orElse(null); - } - /** - * Prevents object instantiation from external. - */ - private static final class SingletonHelper { - private static final String ILLEGAL_ACCESS_MSG = "Should not instantiate this class."; - private static final LispMappingDatabase INSTANCE = new LispMappingDatabase(); - private SingletonHelper() { - throw new IllegalAccessError(ILLEGAL_ACCESS_MSG); - } - } } diff --git a/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/impl/LispRadixTreeDatabase.java b/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/impl/LispRadixTreeDatabase.java new file mode 100644 index 0000000000..0cb9d9e746 --- /dev/null +++ b/protocols/lisp/ctl/src/main/java/org/onosproject/lisp/ctl/impl/LispRadixTreeDatabase.java @@ -0,0 +1,57 @@ +/* + * Copyright 2017-present Open Networking Laboratory + * + * 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.lisp.ctl.impl; + +import org.onosproject.lisp.msg.protocols.LispEidRecord; +import org.onosproject.lisp.msg.protocols.LispMapRecord; + +import java.util.List; + +/** + * A radix tree based LISP mapping database. + */ +public class LispRadixTreeDatabase implements LispMappingDatabase { + + @Override + public void putMapRecord(LispEidRecord eid, LispMapRecord rloc, + boolean proxyMapReply) { + //TODO: requires implementation + } + + @Override + public void removeMapRecordByEid(LispEidRecord eid) { + //TODO: requires implementation + } + + @Override + public void removeAllMapRecords() { + //TODO: requires implementation + } + + @Override + public LispMapRecord getMapRecordByEidRecord(LispEidRecord eid, + boolean proxyMapReply) { + //TODO: requires implementation + return null; + } + + @Override + public List getMapRecordByEidRecords(List eids, + boolean proxyMapReply) { + //TODO: requires implementation + return null; + } +} diff --git a/protocols/lisp/ctl/src/test/java/org/onosproject/lisp/ctl/impl/LispMappingDatabaseTest.java b/protocols/lisp/ctl/src/test/java/org/onosproject/lisp/ctl/impl/LispMappingDatabaseTest.java index b929e25ae3..f8d795db27 100644 --- a/protocols/lisp/ctl/src/test/java/org/onosproject/lisp/ctl/impl/LispMappingDatabaseTest.java +++ b/protocols/lisp/ctl/src/test/java/org/onosproject/lisp/ctl/impl/LispMappingDatabaseTest.java @@ -57,7 +57,7 @@ public class LispMappingDatabaseTest { private static final String EID_IP_PREFIX_2_32 = "10.1.2.1"; private static final String EID_IP_PREFIX_2_24 = "10.1.2.0"; - final LispMappingDatabase mapDb = LispMappingDatabase.getInstance(); + final LispMappingDatabase mapDb = LispExpireMapDatabase.getInstance(); @Before public void setup() {