store map register information and use it in the map reply TELSDN-67: #close
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / lisp / MapResolver.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.Map;
12
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;
26
27 public class MapResolver implements IMapResolver {
28     private ILispDAO dao;
29     private volatile boolean shouldAuthenticate;
30     private volatile boolean shouldIterateMask;
31     protected static final Logger logger = LoggerFactory.getLogger(MapResolver.class);
32
33     public MapResolver(ILispDAO dao) {
34         this(dao, true, true);
35     }
36
37     public MapResolver(ILispDAO dao, boolean authenticate, boolean iterateMask) {
38         this.dao = dao;
39         this.shouldAuthenticate = authenticate;
40         this.shouldIterateMask = iterateMask;
41     }
42
43     public MapReply handleMapRequest(MapRequest request) {
44         if (dao == null) {
45             logger.warn("handleMapRequest called while dao is uninitialized");
46             return null;
47         }
48         MapReply mapReply = new MapReply();
49         mapReply.setNonce(request.getNonce());
50
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);
59             }
60             if (locators != null) {
61                 addLocators(eidToLocators, locators);
62             }
63             mapReply.addEidToLocator(eidToLocators);
64         }
65
66         return mapReply;
67     }
68
69     private Map<String, ?> findMaskLocators(IMappingServiceKey key) {
70         int mask = key.getMask();
71         if (mask == 0) {
72             mask = ((IMaskable) key.getEID()).getMaxMask() - 1;
73             key = MappingServiceKeyUtil.generateMappingServiceKey(key.getEID(), (byte) mask);
74         }
75         while (mask > 0) {
76             ((IMaskable) key.getEID()).normalize(mask);
77             mask--;
78             Map<String, ?> locators = dao.get(key);
79             if (locators != null) {
80                 return locators;
81             }
82         }
83         return null;
84     }
85
86     private void addLocators(EidToLocatorRecord eidToLocators, Map<String, ?> locators) {
87         try {
88             MappingServiceValue value = (MappingServiceValue) locators.get("value");
89             for (MappingServiceRLOC rloc : value.getRlocs()) {
90                 addLocator(eidToLocators, rloc);
91
92             }
93         } catch (ClassCastException cce) {
94         }
95     }
96
97     private void addLocator(EidToLocatorRecord eidToLocators, MappingServiceRLOC locatorObject) {
98         if (locatorObject == null) {
99             return;
100         }
101         try {
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) {
107         }
108     }
109
110     public boolean shouldIterateMask() {
111         return shouldIterateMask;
112     }
113
114     public boolean shouldAuthenticate() {
115         return shouldAuthenticate;
116     }
117
118     public void setShouldIterateMask(boolean shouldIterateMask) {
119         this.shouldIterateMask = shouldIterateMask;
120     }
121
122     public void setShouldAuthenticate(boolean shouldAuthenticate) {
123         this.shouldAuthenticate = shouldAuthenticate;
124     }
125
126 }