2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.lispflowmapping.mapcache;
11 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
12 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
13 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
14 import org.opendaylight.lispflowmapping.interfaces.mapcache.IAuthKeyDb;
15 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
16 import org.opendaylight.lispflowmapping.mapcache.lisp.LispMapCacheStringifier;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * Simple in-memory database for authentication keys, that works with 'simple' addresses (see lisp-proto.yang). It can
25 * do longest prefix matching for IP addresses.
27 * @author Lorand Jakab
30 public class AuthKeyDb implements IAuthKeyDb {
31 private static final Logger LOG = LoggerFactory.getLogger(AuthKeyDb.class);
34 public AuthKeyDb(ILispDAO dao) {
38 private long getVni(Eid eid) {
39 if (eid.getVirtualNetworkId() == null) {
42 return eid.getVirtualNetworkId().getValue().toJava();
46 private ILispDAO getVniTable(Eid eid) {
47 return (ILispDAO) dao.getSpecific(getVni(eid), SubKeys.VNI);
50 private void removeVniTable(Eid eid) {
51 dao.removeSpecific(getVni(eid), SubKeys.VNI);
54 private ILispDAO getOrInstantiateVniTable(Eid eid) {
55 long vni = getVni(eid);
56 ILispDAO table = (ILispDAO) dao.getSpecific(vni, SubKeys.VNI);
58 table = dao.putNestedTable(vni, SubKeys.VNI);
64 public void addAuthenticationKey(Eid eid, MappingAuthkey authKey) {
65 Eid key = MaskUtil.normalize(eid);
66 ILispDAO table = getOrInstantiateVniTable(key);
67 table.put(key, new MappingEntry<>(SubKeys.AUTH_KEY, authKey));
70 private MappingAuthkey getAuthKeyLpm(Eid prefix, ILispDAO db) {
71 short maskLength = MaskUtil.getMaskForAddress(prefix.getAddress());
72 while (maskLength >= 0) {
73 Eid key = MaskUtil.normalize(prefix, maskLength);
74 Object password = db.getSpecific(key, SubKeys.AUTH_KEY);
75 if (password != null && password instanceof MappingAuthkey) {
76 return (MappingAuthkey) password;
84 * Retrieves authentication key from the database. As opposed to the mapping cache, Source/Dest keys are treated as
85 * exact match keys here, and a two level longest prefix match is NOT performed.
88 public MappingAuthkey getAuthenticationKey(Eid eid) {
89 ILispDAO table = getVniTable(eid);
93 if (MaskUtil.isMaskable(eid.getAddress()) && !(eid.getAddress() instanceof SourceDestKey)) {
94 return getAuthKeyLpm(eid, table);
96 Eid key = MaskUtil.normalize(eid);
97 Object password = table.getSpecific(key, SubKeys.AUTH_KEY);
98 if (password != null && password instanceof MappingAuthkey) {
99 return (MappingAuthkey) password;
101 LOG.warn("Failed to find password!");
108 public void removeAuthenticationKey(Eid eid) {
109 Eid key = MaskUtil.normalize(eid);
110 ILispDAO table = getVniTable(key);
114 table.removeSpecific(key, SubKeys.AUTH_KEY);
115 if (table.isEmpty()) {
121 public String printKeys() {
122 return LispMapCacheStringifier.printKeys(dao);
126 public String prettyPrintKeys() {
127 return LispMapCacheStringifier.prettyPrintKeys(dao);