add prefix tests TELSDN-384: #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         while (mask > 0) {
72             key = MappingServiceKeyUtil.generateMappingServiceKey(key.getEID(), (byte) mask);
73             ((IMaskable) key.getEID()).normalize(mask);
74             mask--;
75             Map<String, ?> locators = dao.get(key);
76             if (locators != null) {
77                 return locators;
78             }
79         }
80         return null;
81     }
82
83     private void addLocators(EidToLocatorRecord eidToLocators, Map<String, ?> locators) {
84         try {
85             MappingServiceValue value = (MappingServiceValue) locators.get("value");
86             for (MappingServiceRLOC rloc : value.getRlocs()) {
87                 addLocator(eidToLocators, rloc);
88
89             }
90         } catch (ClassCastException cce) {
91         }
92     }
93
94     private void addLocator(EidToLocatorRecord eidToLocators, MappingServiceRLOC locatorObject) {
95         if (locatorObject == null) {
96             return;
97         }
98         try {
99             eidToLocators.addLocator(locatorObject.getRecord().clone().setRouted(true));
100             eidToLocators.setAction(locatorObject.getAction());
101             eidToLocators.setAuthoritative(locatorObject.isAuthoritative());
102             eidToLocators.setRecordTtl(locatorObject.getTtl());
103         } catch (ClassCastException cce) {
104         }
105     }
106
107     public boolean shouldIterateMask() {
108         return shouldIterateMask;
109     }
110
111     public boolean shouldAuthenticate() {
112         return shouldAuthenticate;
113     }
114
115     public void setShouldIterateMask(boolean shouldIterateMask) {
116         this.shouldIterateMask = shouldIterateMask;
117     }
118
119     public void setShouldAuthenticate(boolean shouldAuthenticate) {
120         this.shouldAuthenticate = shouldAuthenticate;
121     }
122
123 }