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.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
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;
36 public class MapServer extends AbstractLispComponent implements IMapServerAsync {
38 protected static final Logger logger = LoggerFactory.getLogger(MapServer.class);
39 private boolean overwrite;
41 public MapServer(ILispDAO dao) {
45 public MapServer(ILispDAO dao, boolean overwrite) {
46 this(dao, overwrite, true);
49 public MapServer(ILispDAO dao, boolean overwrite, boolean authenticate) {
50 this(dao, overwrite, authenticate, true);
53 public MapServer(ILispDAO dao, boolean overwrite, boolean authenticate, boolean iterateAuthenticationMask) {
54 super(dao, authenticate, iterateAuthenticationMask);
55 this.overwrite = overwrite;
58 public void handleMapRegister(MapRegister mapRegister, IMapNotifyHandler callback) {
60 logger.warn("handleMapRegister called while dao is uninitialized");
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");
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));
84 callback.handleMapNotify(builder.build());
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()));
99 rlocGroups.get(subkey).addRecord(locatorRecord);
102 List<MappingEntry<MappingServiceRLOCGroup>> entries = new ArrayList<>();
103 for (String subkey : rlocGroups.keySet()) {
104 entries.add(new MappingEntry<>(subkey, rlocGroups.get(subkey)));
106 dao.put(key, entries.toArray(new MappingEntry[entries.size()]));
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();
116 if (shouldOverwrite()) {
117 return ADDRESS_SUBKEY;
119 return String.valueOf(locatorRecord.getLispAddressContainer().getAddress().hashCode());
123 public String getAuthenticationKey(LispAddressContainer address, int maskLen) {
124 return getPassword(address, maskLen);
127 public boolean removeAuthenticationKey(LispAddressContainer address, int maskLen) {
128 IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(address, maskLen);
129 dao.removeSpecific(key, PASSWORD_SUBKEY);
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));
139 public boolean shouldOverwrite() {
143 public void setOverwrite(boolean overwrite) {
144 this.overwrite = overwrite;