View Javadoc

1   /*
2    * Copyright (c) 2015 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.implementation.mapcache;
10  
11  import java.util.Date;
12  
13  import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
14  import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor;
15  import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
16  import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
17  import org.opendaylight.lispflowmapping.interfaces.mapcache.IMapCache;
18  import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
19  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
20  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container.MappingAuthkey;
21  
22  /**
23   * Flat key implementation of a map-cache. As the name suggests, no longest prefix matching is done for IP addresses
24   * or their derivatives.
25   *
26   * @author Florin Coras
27   *
28   */
29  
30  public class FlatMapCache implements IMapCache {
31      private ILispDAO dao;
32  
33      public FlatMapCache(ILispDAO dao) {
34          this.dao = dao;
35      }
36  
37      @Override
38      public void addMapping(Eid eid, Object data, boolean shouldOverwrite) {
39          Eid key = MaskUtil.normalize(eid);
40          dao.put(key, new MappingEntry<>(SubKeys.REGDATE, new Date(System.currentTimeMillis())));
41          dao.put(key, new MappingEntry<>(SubKeys.RECORD, data));
42      }
43  
44      @Override
45      public Object getMapping(Eid srcKey, Eid dstKey) {
46          if (dstKey == null) {
47              return null;
48          }
49          Eid key = MaskUtil.normalize(dstKey);
50          return dao.getSpecific(key, SubKeys.RECORD);
51      }
52  
53      @Override
54      public void removeMapping(Eid eid, boolean overwrite) {
55          Eid key = MaskUtil.normalize(eid);
56          dao.removeSpecific(key, SubKeys.RECORD);
57      }
58  
59      @Override
60      public void addAuthenticationKey(Eid eid, MappingAuthkey authKey) {
61          Eid key = MaskUtil.normalize(eid);
62          dao.put(key, new MappingEntry<>(SubKeys.AUTH_KEY, authKey));
63      }
64  
65      @Override
66      public MappingAuthkey getAuthenticationKey(Eid eid) {
67          Eid key = MaskUtil.normalize(eid);
68          Object data = dao.getSpecific(key, SubKeys.AUTH_KEY);
69          if (data instanceof MappingAuthkey) {
70              return (MappingAuthkey) data;
71          } else {
72              return null;
73          }
74      }
75  
76      @Override
77      public void removeAuthenticationKey(Eid eid) {
78          Eid key = MaskUtil.normalize(eid);
79          dao.removeSpecific(key, SubKeys.AUTH_KEY);
80      }
81  
82      @Override
83      public void updateMappingRegistration(Eid eid) {
84          Eid key = MaskUtil.normalize(eid);
85          if (dao.get(key) != null) {
86              dao.put(key, new MappingEntry<>(SubKeys.REGDATE, new Date(System.currentTimeMillis())));
87          }
88      }
89  
90      @Override
91      public void addData(Eid eid, String subKey, Object data) {
92          Eid key = MaskUtil.normalize(eid);
93          dao.put(key, new MappingEntry<>(subKey, data));
94      }
95  
96      @Override
97      public Object getData(Eid eid, String subKey) {
98          Eid key = MaskUtil.normalize(eid);
99          return dao.getSpecific(key, subKey);
100     }
101 
102     @Override
103     public void removeData(Eid eid, String subKey) {
104         Eid key = MaskUtil.normalize(eid);
105         dao.removeSpecific(key, subKey);
106     }
107 
108     @Override
109     public String printMappings() {
110         final StringBuffer sb = new StringBuffer();
111         sb.append("Keys\tValues\n");
112         dao.getAll(new IRowVisitor() {
113             String lastKey = "";
114 
115             public void visitRow(Object keyId, String valueKey, Object value) {
116                 String key = keyId.getClass().getSimpleName() + "#" + keyId;
117                 if (!lastKey.equals(key)) {
118                     sb.append("\n" + key + "\t");
119                 }
120                 sb.append(valueKey + "=" + value + "\t");
121                 lastKey = key;
122             }
123         });
124         sb.append("\n");
125         return sb.toString();
126     }
127 }