added integration test TELSDN-220
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / lisp / MapServer.java
1 /*
2  * Copyright (c) 2013 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.List;
13
14 import org.opendaylight.lispflowmapping.implementation.authentication.LispAuthenticationUtil;
15 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
16 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO.MappingEntry;
17 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceKey;
18 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceNoMaskKey;
19 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceValue;
20 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServer;
21 import org.opendaylight.lispflowmapping.type.lisp.EidToLocatorRecord;
22 import org.opendaylight.lispflowmapping.type.lisp.LocatorRecord;
23 import org.opendaylight.lispflowmapping.type.lisp.MapNotify;
24 import org.opendaylight.lispflowmapping.type.lisp.MapRegister;
25 import org.opendaylight.lispflowmapping.type.lisp.address.IMaskable;
26 import org.opendaylight.lispflowmapping.type.lisp.address.LispAddress;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class MapServer implements IMapServer {
31     private ILispDAO dao;
32     protected static final Logger logger = LoggerFactory.getLogger(MapServer.class);
33
34     public MapServer(ILispDAO dao) {
35         this.dao = dao;
36     }
37
38     public MapNotify handleMapRegister(MapRegister mapRegister) {
39         if (dao == null) {
40             logger.warn("handleMapRegister called while dao is uninitialized");
41             return null;
42         }
43         if (!LispAuthenticationUtil.validate(mapRegister)) {
44                 logger.debug("Authentication failed");
45             return null;
46         }
47         EidToLocatorRecord eidRecord = mapRegister.getEidToLocatorRecords().get(0);
48         List<MappingEntry<?>> rlocs = new ArrayList<ILispDAO.MappingEntry<?>>();
49         rlocs.add(new MappingEntry<Integer>("NumRLOCs", eidRecord.getLocators().size()));
50         int i = 0;
51         for (LocatorRecord locatorRecord : eidRecord.getLocators()) {
52             rlocs.add(new MappingEntry<MappingServiceValue>("RLOC" + (i++), new MappingServiceValue(locatorRecord, eidRecord.getRecordTtl())));
53         }
54         if (eidRecord.getPrefix() instanceof IMaskable && eidRecord.getMaskLength() > 0 && eidRecord.getMaskLength() < ((IMaskable)eidRecord.getPrefix()).getMaxMask()) {
55             ((IMaskable)eidRecord.getPrefix()).normalize(eidRecord.getMaskLength());
56             dao.put(new MappingServiceKey(eidRecord.getPrefix(), (byte)eidRecord.getMaskLength()), rlocs.toArray(new MappingEntry[rlocs.size()]));
57         } else {
58             dao.put(new MappingServiceNoMaskKey(eidRecord.getPrefix()), rlocs.toArray(new MappingEntry[rlocs.size()]));
59             
60         }
61         MapNotify mapNotify = null;
62         if (mapRegister.isWantMapNotify()) {
63                 logger.trace("MapRegister wants MapNotify");
64             mapNotify = new MapNotify();
65             mapNotify.setFromMapRegister(mapRegister);
66             mapNotify.setAuthenticationData(LispAuthenticationUtil.getAuthenticationData(mapNotify));
67         }
68         return mapNotify;
69     }
70
71     public String getAuthenticationKey(LispAddress address, int maskLen) {
72         return null;
73     }
74
75     public boolean removeAuthenticationKey(LispAddress address, int maskLen) {
76         return false;
77     }
78
79     public boolean addAuthenticationKey(LispAddress address, int maskLen, String key) {
80         return false;
81     }
82
83 }