added key value to yang and changed map server so that it overides old rlocs CXTMAP-102
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / lisp / MapServer.java
1 /*
2  * Copyright (c) 2014 Contextream, 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.lisp;
10
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15
16 import org.apache.commons.lang3.BooleanUtils;
17 import org.opendaylight.lispflowmapping.implementation.authentication.LispAuthenticationUtil;
18 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKeyUtil;
19 import org.opendaylight.lispflowmapping.implementation.util.MapNotifyBuilderHelper;
20 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
21 import org.opendaylight.lispflowmapping.interfaces.dao.IMappingServiceKey;
22 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
23 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceRLOCGroup;
24 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
25 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
26 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRegister;
27 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecord;
28 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.LispAddressContainer;
29 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafKeyValue;
30 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.DistinguishedName;
31 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.locatorrecords.LocatorRecord;
32 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.mapnotifymessage.MapNotifyBuilder;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class MapServer extends AbstractLispComponent implements IMapServerAsync {
37
38     protected static final Logger logger = LoggerFactory.getLogger(MapServer.class);
39     private boolean overwrite;
40
41     public MapServer(ILispDAO dao) {
42         this(dao, true);
43     }
44
45     public MapServer(ILispDAO dao, boolean overwrite) {
46         this(dao, overwrite, true);
47     }
48
49     public MapServer(ILispDAO dao, boolean overwrite, boolean authenticate) {
50         this(dao, overwrite, authenticate, true);
51     }
52
53     public MapServer(ILispDAO dao, boolean overwrite, boolean authenticate, boolean iterateAuthenticationMask) {
54         super(dao, authenticate, iterateAuthenticationMask);
55         this.overwrite = overwrite;
56     }
57
58     public void handleMapRegister(MapRegister mapRegister, IMapNotifyHandler callback) {
59         if (dao == null) {
60             logger.warn("handleMapRegister called while dao is uninitialized");
61         } else {
62             boolean failed = false;
63             String password = null;
64             for (EidToLocatorRecord eidRecord : mapRegister.getEidToLocatorRecord()) {
65                 if (shouldAuthenticate()) {
66                     password = getPassword(eidRecord.getLispAddressContainer(), eidRecord.getMaskLength());
67                     if (!LispAuthenticationUtil.validate(mapRegister, password)) {
68                         logger.warn("Authentication failed");
69                         failed = true;
70                         break;
71                     }
72                 }
73                 saveRlocs(eidRecord);
74
75             }
76             if (!failed) {
77                 MapNotifyBuilder builder = new MapNotifyBuilder();
78                 if (BooleanUtils.isTrue(mapRegister.isWantMapNotify())) {
79                     logger.trace("MapRegister wants MapNotify");
80                     MapNotifyBuilderHelper.setFromMapRegister(builder, mapRegister);
81                     if (shouldAuthenticate()) {
82                         builder.setAuthenticationData(LispAuthenticationUtil.createAuthenticationData(builder.build(), password));
83                     }
84                     callback.handleMapNotify(builder.build());
85                 }
86             }
87         }
88     }
89
90     public void saveRlocs(EidToLocatorRecord eidRecord) {
91         IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eidRecord.getLispAddressContainer(), eidRecord.getMaskLength());
92         Map<String, MappingServiceRLOCGroup> rlocGroups = new HashMap<String, MappingServiceRLOCGroup>();
93         if (eidRecord.getLocatorRecord() != null) {
94             for (LocatorRecord locatorRecord : eidRecord.getLocatorRecord()) {
95                 String subkey = getLocatorKey(locatorRecord);
96                 if (!rlocGroups.containsKey(subkey)) {
97                     rlocGroups.put(subkey, new MappingServiceRLOCGroup(eidRecord.getRecordTtl(), eidRecord.getAction(), eidRecord.isAuthoritative()));
98                 }
99                 rlocGroups.get(subkey).addRecord(locatorRecord);
100             }
101         }
102         List<MappingEntry<MappingServiceRLOCGroup>> entries = new ArrayList<>();
103         for (String subkey : rlocGroups.keySet()) {
104             entries.add(new MappingEntry<>(subkey, rlocGroups.get(subkey)));
105         }
106         dao.put(key, entries.toArray(new MappingEntry[entries.size()]));
107     }
108
109     private String getLocatorKey(LocatorRecord locatorRecord) {
110         if (locatorRecord.getLispAddressContainer().getAddress() instanceof LcafKeyValue) {
111             LcafKeyValue keyVal = (LcafKeyValue) locatorRecord.getLispAddressContainer().getAddress();
112             if (keyVal.getKey().getPrimitiveAddress() instanceof DistinguishedName) {
113                 return ((DistinguishedName) keyVal.getKey().getPrimitiveAddress()).getDistinguishedName();
114             }
115         }
116         if (shouldOverwrite()) {
117             return ADDRESS_SUBKEY;
118         } else {
119             return String.valueOf(locatorRecord.getLispAddressContainer().getAddress().hashCode());
120         }
121     }
122
123     public String getAuthenticationKey(LispAddressContainer address, int maskLen) {
124         return getPassword(address, maskLen);
125     }
126
127     public boolean removeAuthenticationKey(LispAddressContainer address, int maskLen) {
128         IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(address, maskLen);
129         dao.removeSpecific(key, PASSWORD_SUBKEY);
130         return true;
131     }
132
133     public boolean addAuthenticationKey(LispAddressContainer address, int maskLen, String key) {
134         IMappingServiceKey mappingServiceKey = MappingServiceKeyUtil.generateMappingServiceKey(address, maskLen);
135         dao.put(mappingServiceKey, new MappingEntry<String>(PASSWORD_SUBKEY, key));
136         return true;
137     }
138
139     public boolean shouldOverwrite() {
140         return overwrite;
141     }
142
143     public void setOverwrite(boolean overwrite) {
144         this.overwrite = overwrite;
145     }
146
147 }