2 * Copyright (c) 2014 Contextream, 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.implementation.lisp;
11 import java.net.InetAddress;
12 import java.net.NetworkInterface;
13 import java.net.SocketException;
14 import java.util.ArrayList;
15 import java.util.Enumeration;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.List;
20 import java.util.Random;
21 import java.util.Map.Entry;
23 import org.apache.commons.lang3.BooleanUtils;
24 import org.opendaylight.lispflowmapping.implementation.authentication.LispAuthenticationUtil;
25 import org.opendaylight.lispflowmapping.implementation.config.ConfigIni;
26 import org.opendaylight.lispflowmapping.implementation.dao.HashMapDb;
27 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKeyUtil;
28 import org.opendaylight.lispflowmapping.implementation.util.DAOMappingUtil;
29 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
30 import org.opendaylight.lispflowmapping.implementation.util.MapNotifyBuilderHelper;
31 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
32 import org.opendaylight.lispflowmapping.interfaces.dao.IMappingServiceKey;
33 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
34 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceRLOCGroup;
35 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceSubscriberRLOC;
36 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
37 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispAFIAddress;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.MapRegister;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.MapRequest;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.eidrecords.EidRecordBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.eidtolocatorrecords.EidToLocatorRecord;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.eidtolocatorrecords.EidToLocatorRecordBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.LispAddressContainer;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.Address;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.LcafKeyValue;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.LcafSourceDest;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispsimpleaddress.primitiveaddress.DistinguishedName;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.locatorrecords.LocatorRecord;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.mapnotifymessage.MapNotifyBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.maprequest.ItrRloc;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.maprequest.ItrRlocBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.maprequest.SourceEidBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.maprequestnotification.MapRequestBuilder;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
58 public class MapServer extends AbstractLispComponent implements IMapServerAsync {
60 protected static final Logger LOG = LoggerFactory.getLogger(MapServer.class);
62 private static final ConfigIni configIni = new ConfigIni();
63 private static final boolean overwriteConfig = configIni.mappingOverwriteIsSet();
64 private boolean overwrite;
66 public MapServer(ILispDAO dao) {
67 this(dao, overwriteConfig);
70 public MapServer(ILispDAO dao, boolean overwrite) {
71 this(dao, overwrite, true);
74 public MapServer(ILispDAO dao, boolean overwrite, boolean authenticate) {
75 this(dao, overwrite, authenticate, true);
78 public MapServer(ILispDAO dao, boolean overwrite, boolean authenticate, boolean iterateAuthenticationMask) {
79 super(dao, authenticate, iterateAuthenticationMask);
80 this.overwrite = overwrite;
83 private static InetAddress getLocalAddress() {
85 Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
86 while (interfaces.hasMoreElements()) {
87 NetworkInterface current = interfaces.nextElement();
88 LOG.debug("Interface " + current.toString());
89 if (!current.isUp() || current.isLoopback() || current.isVirtual())
91 Enumeration<InetAddress> addresses = current.getInetAddresses();
92 while (addresses.hasMoreElements()) {
93 InetAddress current_addr = addresses.nextElement();
94 // Skip loopback and link local addresses
95 if (current_addr.isLoopbackAddress() || current_addr.isLinkLocalAddress())
97 LOG.debug(current_addr.getHostAddress());
101 } catch (SocketException se) {
106 private static MapRequest buildSMR(EidToLocatorRecord eidRecord) {
107 MapRequestBuilder builder = new MapRequestBuilder();
108 builder.setAuthoritative(false);
109 builder.setMapDataPresent(false);
110 builder.setPitr(false);
111 builder.setProbe(false);
112 builder.setSmr(true);
113 builder.setSmrInvoked(false);
115 builder.setEidRecord(new ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.eidrecords.EidRecord>());
116 LispAddressContainer container = eidRecord.getLispAddressContainer();
117 builder.getEidRecord().add(new EidRecordBuilder().setMask((short) eidRecord.getMaskLength()).setLispAddressContainer(container).build());
119 builder.setItrRloc(new ArrayList<ItrRloc>());
120 builder.getItrRloc().add(new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(getLocalAddress())).build());
122 builder.setMapReply(null);
123 builder.setNonce(new Random().nextLong());
125 // XXX For now we set source EID to queried EID...
126 builder.setSourceEid(new SourceEidBuilder().setLispAddressContainer(container).build());
127 return builder.build();
130 public void handleMapRegister(MapRegister mapRegister, boolean smr, IMapNotifyHandler callback) {
132 LOG.warn("handleMapRegister called while dao is uninitialized");
134 boolean failed = false;
135 String password = null;
136 for (EidToLocatorRecord eidRecord : mapRegister.getEidToLocatorRecord()) {
137 if (shouldAuthenticate()) {
138 password = getPassword(eidRecord.getLispAddressContainer(), eidRecord.getMaskLength());
139 if (!LispAuthenticationUtil.validate(mapRegister, password)) {
140 LOG.warn("Authentication failed");
145 boolean mappingChanged = saveRlocs(eidRecord, smr);
146 if (smr && mappingChanged) {
147 HashSet<MappingServiceSubscriberRLOC> subscribers = getSubscribers(eidRecord.getLispAddressContainer(),
148 eidRecord.getMaskLength());
149 handleSmr(eidRecord, subscribers, callback);
153 MapNotifyBuilder builder = new MapNotifyBuilder();
154 if (BooleanUtils.isTrue(mapRegister.isWantMapNotify())) {
155 LOG.trace("MapRegister wants MapNotify");
156 MapNotifyBuilderHelper.setFromMapRegister(builder, mapRegister);
157 if (shouldAuthenticate()) {
158 builder.setAuthenticationData(LispAuthenticationUtil.createAuthenticationData(builder.build(), password));
160 callback.handleMapNotify(builder.build());
166 public boolean saveRlocs(EidToLocatorRecord eidRecord, boolean checkForChanges) {
167 Map<String, MappingServiceRLOCGroup> rlocGroups = new HashMap<String, MappingServiceRLOCGroup>();
168 if (eidRecord.getLocatorRecord() != null) {
169 for (LocatorRecord locatorRecord : eidRecord.getLocatorRecord()) {
170 String subkey = getAddressKey(locatorRecord.getLispAddressContainer().getAddress());
171 if (!rlocGroups.containsKey(subkey)) {
172 rlocGroups.put(subkey, new MappingServiceRLOCGroup(eidRecord.getRecordTtl(), eidRecord.getAction(), eidRecord.isAuthoritative()));
174 rlocGroups.get(subkey).addRecord(locatorRecord);
177 List<MappingEntry<MappingServiceRLOCGroup>> entries = new ArrayList<>();
178 for (String subkey : rlocGroups.keySet()) {
179 entries.add(new MappingEntry<>(subkey, rlocGroups.get(subkey)));
182 if (eidRecord.getLispAddressContainer().getAddress() instanceof LcafSourceDest) {
183 Entry<IMappingServiceKey, List<MappingServiceRLOCGroup>> oldMapping= null, newMapping = null;
184 LispAFIAddress srcAddr = getSrcForLcafSrcDst(eidRecord.getLispAddressContainer());
185 LispAFIAddress dstAddr = getDstForLcafSrcDst(eidRecord.getLispAddressContainer());
186 short srcMask = getSrcMaskForLcafSrcDst(eidRecord.getLispAddressContainer());
187 short dstMask = getDstMaskForLcafSrcDst(eidRecord.getLispAddressContainer());
189 if (checkForChanges) {
190 oldMapping = DAOMappingUtil.getMappingExact(srcAddr, dstAddr, srcMask, dstMask, dao);
192 IMappingServiceKey dstKey = MappingServiceKeyUtil.generateMappingServiceKey(dstAddr, dstMask);
193 ILispDAO srcDstDao = (ILispDAO) dao.getSpecific(dstKey, LCAF_SRCDST_SUBKEY);
194 if (srcDstDao == null) {
195 srcDstDao = new HashMapDb();
196 dao.put(dstKey, new MappingEntry<>(LCAF_SRCDST_SUBKEY, srcDstDao));
198 IMappingServiceKey srcKey = MappingServiceKeyUtil.generateMappingServiceKey(srcAddr, srcMask);
199 srcDstDao.put(srcKey, entries.toArray(new MappingEntry[entries.size()]));
200 if (checkForChanges) {
201 newMapping = DAOMappingUtil.getMappingExact(srcAddr, dstAddr, srcMask, dstMask, dao);
202 if (!newMapping.getValue().equals(oldMapping.getValue())) {
207 List<MappingServiceRLOCGroup> oldLocators = null, newLocators = null;
208 if (checkForChanges) {
209 oldLocators = DAOMappingUtil.getLocatorsByEidToLocatorRecord(eidRecord, dao, shouldIterateMask());
211 IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eidRecord.getLispAddressContainer(),
212 eidRecord.getMaskLength());
213 dao.put(key, entries.toArray(new MappingEntry[entries.size()]));
214 if (checkForChanges) {
215 newLocators = DAOMappingUtil.getLocatorsByEidToLocatorRecord(eidRecord, dao, shouldIterateMask());
216 if (!newLocators.equals(oldLocators)) {
224 private String getAddressKey(Address address) {
225 if (address instanceof LcafKeyValue) {
226 LcafKeyValue keyVal = (LcafKeyValue) address;
227 if (keyVal.getLcafKeyValueAddressAddr().getKey().getPrimitiveAddress() instanceof DistinguishedName) {
228 return ((DistinguishedName) keyVal.getLcafKeyValueAddressAddr().getKey().getPrimitiveAddress()).getDistinguishedNameAddress().getDistinguishedName();
231 if (shouldOverwrite()) {
232 return ADDRESS_SUBKEY;
234 return String.valueOf(address.hashCode());
238 public String getAuthenticationKey(LispAddressContainer address, int maskLen) {
239 return getPassword(address, maskLen);
242 public void removeAuthenticationKey(LispAddressContainer address, int maskLen) {
243 if (address.getAddress() instanceof LcafSourceDest) {
244 ILispDAO srcDstDao = getSrcDstInnerDao(address, maskLen);
245 if (srcDstDao != null) {
246 IMappingServiceKey srcKey = MappingServiceKeyUtil.generateMappingServiceKey(getSrcForLcafSrcDst(address),
247 getSrcMaskForLcafSrcDst(address));
248 srcDstDao.removeSpecific(srcKey, PASSWORD_SUBKEY);
251 IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(address, maskLen);
252 dao.removeSpecific(key, PASSWORD_SUBKEY);
256 public void addAuthenticationKey(LispAddressContainer address, int maskLen, String key) {
257 IMappingServiceKey mappingServiceKey = MappingServiceKeyUtil.generateMappingServiceKey(address, maskLen);
258 if (address.getAddress() instanceof LcafSourceDest) {
259 IMappingServiceKey srcKey = MappingServiceKeyUtil.generateMappingServiceKey(getSrcForLcafSrcDst(address),
260 getSrcMaskForLcafSrcDst(address));
261 ILispDAO srcDstDao = getOrInstantiateSrcDstInnerDao(address, maskLen);
262 srcDstDao.put(srcKey, new MappingEntry<String>(PASSWORD_SUBKEY, key));
264 dao.put(mappingServiceKey, new MappingEntry<String>(PASSWORD_SUBKEY, key));
268 public void removeMapping(LispAddressContainer address, int maskLen, boolean smr, IMapNotifyHandler callback) {
269 Entry<IMappingServiceKey, List<MappingServiceRLOCGroup>> mapping = DAOMappingUtil.
270 getMappingForEid(LispAFIConvertor.toAFI(address), maskLen, dao);
272 if (address.getAddress() instanceof LcafSourceDest) {
273 db = getSrcDstInnerDao(address, maskLen);
278 HashSet<MappingServiceSubscriberRLOC> subscribers = getSubscribers(address, maskLen);
279 // mapping is removed before first SMR is sent to avoid inconsistent replies
280 removeMappingRlocs(mapping, db);
281 handleSmr(new EidToLocatorRecordBuilder().setLispAddressContainer(address).
282 setMaskLength((short) maskLen).build(), subscribers, callback);
283 db.removeSpecific(mapping.getKey(), SUBSCRIBERS_SUBKEY);
285 removeMappingRlocs(mapping, db);
286 db.removeSpecific(mapping.getKey(), SUBSCRIBERS_SUBKEY);
290 private void removeMappingRlocs(Entry<IMappingServiceKey, List<MappingServiceRLOCGroup>> mapping, ILispDAO db) {
291 for (MappingServiceRLOCGroup group : mapping.getValue()) {
292 for (LocatorRecord record : group.getRecords()) {
293 db.removeSpecific(mapping.getKey(), getAddressKey(record.getLispAddressContainer().getAddress()));
298 private void handleSmr(EidToLocatorRecord record, HashSet<MappingServiceSubscriberRLOC> subscribers,
299 IMapNotifyHandler callback) {
300 if (subscribers == null) {
303 MapRequest mapRequest = buildSMR(record);
304 LOG.trace("Built SMR packet: " + mapRequest.toString());
305 for (MappingServiceSubscriberRLOC rloc : subscribers) {
306 if (rloc.timedOut()) {
307 LOG.trace("Lazy removing expired subscriber entry " + rloc.toString());
308 subscribers.remove(rloc);
311 callback.handleSMR(mapRequest, rloc.getSrcRloc());
312 } catch (Exception e) {
313 LOG.error("Errors encountered while handling SMR:" + e.getStackTrace());
317 IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(record.getLispAddressContainer(),
318 record.getMaskLength());
319 dao.put(key, new MappingEntry<HashSet<MappingServiceSubscriberRLOC>>(SUBSCRIBERS_SUBKEY, subscribers));
322 public boolean shouldOverwrite() {
326 public void setOverwrite(boolean overwrite) {
327 this.overwrite = overwrite;