131da871fa047b8e80e8d295e8a5c2c51a858ef3
[lispflowmapping.git] / mappingservice / mapcache / src / main / java / org / opendaylight / lispflowmapping / mapcache / AuthKeyDb.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.lispflowmapping.mapcache;
10
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;
22
23 /**
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.
26  *
27  * @author Lorand Jakab
28  *
29  */
30 public class AuthKeyDb implements IAuthKeyDb {
31     private static final Logger LOG = LoggerFactory.getLogger(AuthKeyDb.class);
32     private ILispDAO dao;
33
34     public AuthKeyDb(ILispDAO dao) {
35         this.dao = dao;
36     }
37
38     private long getVni(Eid eid) {
39         if (eid.getVirtualNetworkId() == null) {
40             return 0;
41         } else {
42             return eid.getVirtualNetworkId().getValue();
43         }
44     }
45
46     private ILispDAO getVniTable(Eid eid) {
47         return (ILispDAO) dao.getSpecific(getVni(eid), SubKeys.VNI);
48     }
49
50     private void removeVniTable(Eid eid) {
51         dao.removeSpecific(getVni(eid), SubKeys.VNI);
52     }
53
54     private ILispDAO getOrInstantiateVniTable(Eid eid) {
55         long vni = getVni(eid);
56         ILispDAO table = (ILispDAO) dao.getSpecific(vni, SubKeys.VNI);
57         if (table == null) {
58             table = dao.putNestedTable(vni, SubKeys.VNI);
59         }
60         return table;
61     }
62
63     @Override
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));
68     }
69
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;
77             }
78             maskLength -= 1;
79         }
80         return null;
81     }
82
83     /*
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.
86      */
87     @Override
88     public MappingAuthkey getAuthenticationKey(Eid eid) {
89         ILispDAO table = getVniTable(eid);
90         if (table == null) {
91             return null;
92         }
93         if (MaskUtil.isMaskable(eid.getAddress()) && !(eid.getAddress() instanceof SourceDestKey)) {
94             return getAuthKeyLpm(eid, table);
95         } else {
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;
100             } else {
101                 LOG.warn("Failed to find password!");
102                 return null;
103             }
104         }
105     }
106
107     @Override
108     public void removeAuthenticationKey(Eid eid) {
109         Eid key = MaskUtil.normalize(eid);
110         ILispDAO table = getVniTable(key);
111         if (table == null) {
112             return;
113         }
114         table.removeSpecific(key, SubKeys.AUTH_KEY);
115         if (table.isEmpty()) {
116             removeVniTable(eid);
117         }
118     }
119
120     @Override
121     public String printKeys() {
122         return LispMapCacheStringifier.printKeys(dao);
123     }
124
125     @Override
126     public String prettyPrintKeys() {
127         return LispMapCacheStringifier.prettyPrintKeys(dao);
128     }
129 }