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;
11 import java.util.ArrayList;
12 import java.util.List;
15 import org.apache.commons.lang3.BooleanUtils;
16 import org.opendaylight.lispflowmapping.implementation.authentication.LispAuthenticationUtil;
17 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKeyUtil;
18 import org.opendaylight.lispflowmapping.implementation.util.MapNotifyBuilderHelper;
19 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
20 import org.opendaylight.lispflowmapping.interfaces.dao.IMappingServiceKey;
21 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
22 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceRLOC;
23 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceValue;
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.MapNotify;
27 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRegister;
28 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecord;
29 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.LispAddressContainer;
30 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.locatorrecords.LocatorRecord;
31 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.mapnotifymessage.MapNotifyBuilder;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class MapServer implements IMapServerAsync {
37 private volatile boolean shouldAuthenticate;
38 private volatile boolean shouldIterateMask;
39 protected static final Logger logger = LoggerFactory.getLogger(MapServer.class);
41 public MapServer(ILispDAO dao) {
45 public MapServer(ILispDAO dao, boolean authenticate) {
46 this(dao, authenticate, true);
49 public MapServer(ILispDAO dao, boolean authenticate, boolean iterateAuthenticationMask) {
51 this.shouldAuthenticate = authenticate;
52 this.shouldIterateMask = iterateAuthenticationMask;
55 public void handleMapRegister(MapRegister mapRegister, IMapNotifyHandler callback) {
57 logger.warn("handleMapRegister called while dao is uninitialized");
59 boolean failed = false;
60 String password = null;
61 for (EidToLocatorRecord eidRecord : mapRegister.getEidToLocatorRecord()) {
62 if (shouldAuthenticate) {
63 password = getPassword(eidRecord.getLispAddressContainer(), eidRecord.getMaskLength());
64 if (!LispAuthenticationUtil.validate(mapRegister, password)) {
65 logger.debug("Authentication failed");
70 MappingServiceValue value = new MappingServiceValue();
71 MappingEntry<MappingServiceValue> entry = new MappingEntry<MappingServiceValue>("value", value);
72 List<MappingServiceRLOC> rlocs = new ArrayList<MappingServiceRLOC>();
73 if (eidRecord.getLocatorRecord() != null) {
74 for (LocatorRecord locatorRecord : eidRecord.getLocatorRecord()) {
75 rlocs.add(new MappingServiceRLOC(locatorRecord, eidRecord.getRecordTtl(), eidRecord.getAction(), eidRecord.isAuthoritative()));
78 value.setRlocs(rlocs);
79 IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eidRecord.getLispAddressContainer(),
80 eidRecord.getMaskLength());
85 MapNotifyBuilder builder = new MapNotifyBuilder();
86 if (BooleanUtils.isTrue(mapRegister.isWantMapNotify())) {
87 logger.trace("MapRegister wants MapNotify");
88 MapNotifyBuilderHelper.setFromMapRegister(builder, mapRegister);
89 if (shouldAuthenticate) {
90 builder.setAuthenticationData(LispAuthenticationUtil.createAuthenticationData(builder.build(), password));
92 callback.handleMapNotify(builder.build());
98 private String getPassword(LispAddressContainer prefix, int maskLength) {
99 while (maskLength >= 0) {
100 IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(prefix, maskLength);
101 Map<String, ?> daoMap = dao.get(key);
102 if (daoMap != null) {
103 MappingServiceValue value = (MappingServiceValue) daoMap.get("value");
104 if (value != null && value.getKey() != null) {
105 return value.getKey();
106 } else if (shouldIterateMask()) {
119 public String getAuthenticationKey(LispAddressContainer address, int maskLen) {
120 return getPassword(address, maskLen);
123 public boolean removeAuthenticationKey(LispAddressContainer address, int maskLen) {
124 IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(address, maskLen);
125 Map<String, ?> daoMap = dao.get(key);
126 if (daoMap != null) {
127 MappingServiceValue value = (MappingServiceValue) daoMap.get("value");
130 if (value.isEmpty()) {
133 dao.put(key, new MappingEntry<MappingServiceValue>("value", value));
144 public boolean addAuthenticationKey(LispAddressContainer address, int maskLen, String key) {
145 IMappingServiceKey mappingServiceKey = MappingServiceKeyUtil.generateMappingServiceKey(address, maskLen);
146 Map<String, ?> daoMap = dao.get(mappingServiceKey);
147 MappingServiceValue value = null;
148 if (daoMap != null) {
149 value = (MappingServiceValue) daoMap.get("value");
151 value = new MappingServiceValue();
154 value = new MappingServiceValue();
157 MappingEntry<MappingServiceValue> entry = new MappingEntry<MappingServiceValue>("value", value);
158 dao.put(mappingServiceKey, entry);
162 public boolean shouldAuthenticate() {
163 return shouldAuthenticate;
166 public boolean shouldIterateMask() {
167 return shouldIterateMask;
170 public void setShouldIterateMask(boolean shouldIterateMask) {
171 this.shouldIterateMask = shouldIterateMask;
174 public void setShouldAuthenticate(boolean shouldAuthenticate) {
175 this.shouldAuthenticate = shouldAuthenticate;