2 * Copyright (c) 2013 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;
13 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
14 import org.opendaylight.lispflowmapping.interfaces.dao.IMappingServiceKey;
15 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceKeyUtil;
16 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceRLOC;
17 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceValue;
18 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolver;
19 import org.opendaylight.lispflowmapping.type.lisp.EidRecord;
20 import org.opendaylight.lispflowmapping.type.lisp.EidToLocatorRecord;
21 import org.opendaylight.lispflowmapping.type.lisp.MapReply;
22 import org.opendaylight.lispflowmapping.type.lisp.MapRequest;
23 import org.opendaylight.lispflowmapping.type.lisp.address.IMaskable;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public class MapResolver implements IMapResolver {
29 private volatile boolean shouldAuthenticate;
30 private volatile boolean shouldIterateMask;
31 protected static final Logger logger = LoggerFactory.getLogger(MapResolver.class);
33 public MapResolver(ILispDAO dao) {
34 this(dao, true, true);
37 public MapResolver(ILispDAO dao, boolean authenticate, boolean iterateMask) {
39 this.shouldAuthenticate = authenticate;
40 this.shouldIterateMask = iterateMask;
43 public MapReply handleMapRequest(MapRequest request) {
45 logger.warn("handleMapRequest called while dao is uninitialized");
48 MapReply mapReply = new MapReply();
49 mapReply.setNonce(request.getNonce());
51 for (EidRecord eid : request.getEids()) {
52 EidToLocatorRecord eidToLocators = new EidToLocatorRecord();
53 eidToLocators.setMaskLength(eid.getMaskLength())//
54 .setPrefix(eid.getPrefix());
55 IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eid.getPrefix(), eid.getMaskLength());
56 Map<String, ?> locators = dao.get(key);
57 if (shouldIterateMask() && locators == null && key.getEID() instanceof IMaskable) {
58 locators = findMaskLocators(key);
60 if (locators != null) {
61 addLocators(eidToLocators, locators);
63 mapReply.addEidToLocator(eidToLocators);
69 private Map<String, ?> findMaskLocators(IMappingServiceKey key) {
70 int mask = key.getMask();
72 mask = ((IMaskable) key.getEID()).getMaxMask() - 1;
73 key = MappingServiceKeyUtil.generateMappingServiceKey(key.getEID(), (byte) mask);
76 ((IMaskable) key.getEID()).normalize(mask);
78 Map<String, ?> locators = dao.get(key);
79 if (locators != null) {
86 private void addLocators(EidToLocatorRecord eidToLocators, Map<String, ?> locators) {
88 MappingServiceValue value = (MappingServiceValue) locators.get("value");
89 for (MappingServiceRLOC rloc : value.getRlocs()) {
90 addLocator(eidToLocators, rloc);
93 } catch (ClassCastException cce) {
97 private void addLocator(EidToLocatorRecord eidToLocators, MappingServiceRLOC locatorObject) {
98 if (locatorObject == null) {
102 eidToLocators.addLocator(locatorObject.getRecord().clone().setRouted(true));
103 eidToLocators.setAction(locatorObject.getAction());
104 eidToLocators.setAuthoritative(locatorObject.isAuthoritative());
105 eidToLocators.setRecordTtl(locatorObject.getTtl());
106 } catch (ClassCastException cce) {
110 public boolean shouldIterateMask() {
111 return shouldIterateMask;
114 public boolean shouldAuthenticate() {
115 return shouldAuthenticate;
118 public void setShouldIterateMask(boolean shouldIterateMask) {
119 this.shouldIterateMask = shouldIterateMask;
122 public void setShouldAuthenticate(boolean shouldAuthenticate) {
123 this.shouldAuthenticate = shouldAuthenticate;