refactoring of listmappingservice into Northbound (future REST) and Southbound (LISP...
[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.interfaces.dao.ILispDAO;
15 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO.MappingEntry;
16 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServer;
17 import org.opendaylight.lispflowmapping.type.lisp.EidToLocatorRecord;
18 import org.opendaylight.lispflowmapping.type.lisp.LocatorRecord;
19 import org.opendaylight.lispflowmapping.type.lisp.MapNotify;
20 import org.opendaylight.lispflowmapping.type.lisp.MapRegister;
21 import org.opendaylight.lispflowmapping.type.lisp.address.LispAddress;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class MapServer implements IMapServer {
26     private ILispDAO dao;
27     protected static final Logger logger = LoggerFactory.getLogger(MapServer.class);
28
29     public MapServer(ILispDAO dao) {
30         this.dao = dao;
31     }
32
33     public MapNotify handleMapRegister(MapRegister mapRegister) {
34         if (dao == null) {
35             logger.warn("handleMapRegister called while dao is uninitialized");
36             return null;
37         }
38         EidToLocatorRecord eidRecord = mapRegister.getEidToLocatorRecords().get(0);
39         List<MappingEntry<?>> rlocs = new ArrayList<ILispDAO.MappingEntry<?>>();
40         rlocs.add(new MappingEntry<Integer>("NumRLOCs", eidRecord.getLocators().size()));
41         int i = 0;
42         for (LocatorRecord locatorRecord : eidRecord.getLocators()) {
43             rlocs.add(new MappingEntry<LispAddress>("RLOC" + (i++), locatorRecord.getLocator()));
44         }
45         dao.put(eidRecord.getPrefix(), rlocs.toArray(new MappingEntry[rlocs.size()]));
46         MapNotify mapNotify = null;
47         if (mapRegister.isWantMapNotify()) {
48             mapNotify = new MapNotify();
49             mapNotify.setFromMapRegister(mapRegister);
50         }
51         return mapNotify;
52     }
53 }