390751fe70b52dbacb841f7684b48dd45d7e529d
[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 import java.util.Map;
14
15 import org.apache.commons.lang3.BooleanUtils;
16 import org.opendaylight.lispflowmapping.implementation.authentication.LispAuthenticationUtil;
17 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKeyUtil;
18 import org.opendaylight.lispflowmapping.implementation.util.MapNotifyBuilderHelper;
19 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
20 import org.opendaylight.lispflowmapping.interfaces.dao.IMappingServiceKey;
21 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
22 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceRLOC;
23 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceValue;
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.MapNotify;
27 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRegister;
28 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecord;
29 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.LispAddressContainer;
30 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.locatorrecords.LocatorRecord;
31 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.mapnotifymessage.MapNotifyBuilder;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class MapServer implements IMapServerAsync {
36     private ILispDAO dao;
37     private volatile boolean shouldAuthenticate;
38     private volatile boolean shouldIterateMask;
39     protected static final Logger logger = LoggerFactory.getLogger(MapServer.class);
40
41     public MapServer(ILispDAO dao) {
42         this(dao, true);
43     }
44
45     public MapServer(ILispDAO dao, boolean authenticate) {
46         this(dao, authenticate, true);
47     }
48
49     public MapServer(ILispDAO dao, boolean authenticate, boolean iterateAuthenticationMask) {
50         this.dao = dao;
51         this.shouldAuthenticate = authenticate;
52         this.shouldIterateMask = iterateAuthenticationMask;
53     }
54
55     public void handleMapRegister(MapRegister mapRegister, IMapNotifyHandler callback) {
56         if (dao == null) {
57             logger.warn("handleMapRegister called while dao is uninitialized");
58         } else {
59             boolean failed = false;
60             String password = null;
61             for (EidToLocatorRecord eidRecord : mapRegister.getEidToLocatorRecord()) {
62                 if (shouldAuthenticate) {
63                     password = getPassword(eidRecord.getLispAddressContainer(), eidRecord.getMaskLength());
64                     if (!LispAuthenticationUtil.validate(mapRegister, password)) {
65                         logger.debug("Authentication failed");
66                         failed = true;
67                         break;
68                     }
69                 }
70                 MappingServiceValue value = new MappingServiceValue();
71                 MappingEntry<MappingServiceValue> entry = new MappingEntry<MappingServiceValue>("value", value);
72                 List<MappingServiceRLOC> rlocs = new ArrayList<MappingServiceRLOC>();
73                 if (eidRecord.getLocatorRecord() != null) {
74                     for (LocatorRecord locatorRecord : eidRecord.getLocatorRecord()) {
75                         rlocs.add(new MappingServiceRLOC(locatorRecord, eidRecord.getRecordTtl(), eidRecord.getAction(), eidRecord.isAuthoritative()));
76                     }
77                 }
78                 value.setRlocs(rlocs);
79                 IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eidRecord.getLispAddressContainer(),
80                         eidRecord.getMaskLength());
81                 dao.put(key, entry);
82
83             }
84             if (!failed) {
85                 MapNotifyBuilder builder = new MapNotifyBuilder();
86                 if (BooleanUtils.isTrue(mapRegister.isWantMapNotify())) {
87                     logger.trace("MapRegister wants MapNotify");
88                     MapNotifyBuilderHelper.setFromMapRegister(builder, mapRegister);
89                     if (shouldAuthenticate) {
90                         builder.setAuthenticationData(LispAuthenticationUtil.createAuthenticationData(builder.build(), password));
91                     }
92                     callback.handleMapNotify(builder.build());
93                 }
94             }
95         }
96     }
97
98     private String getPassword(LispAddressContainer prefix, int maskLength) {
99         while (maskLength >= 0) {
100             IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(prefix, maskLength);
101             Map<String, ?> daoMap = dao.get(key);
102             if (daoMap != null) {
103                 MappingServiceValue value = (MappingServiceValue) daoMap.get("value");
104                 if (value != null && value.getKey() != null) {
105                     return value.getKey();
106                 } else if (shouldIterateMask()) {
107                     maskLength -= 1;
108                 } else {
109                     return null;
110                 }
111             } else {
112                 maskLength -= 1;
113
114             }
115         }
116         return null;
117     }
118
119     public String getAuthenticationKey(LispAddressContainer address, int maskLen) {
120         return getPassword(address, maskLen);
121     }
122
123     public boolean removeAuthenticationKey(LispAddressContainer address, int maskLen) {
124         IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(address, maskLen);
125         Map<String, ?> daoMap = dao.get(key);
126         if (daoMap != null) {
127             MappingServiceValue value = (MappingServiceValue) daoMap.get("value");
128             if (value != null) {
129                 value.setKey(null);
130                 if (value.isEmpty()) {
131                     dao.remove(key);
132                 } else {
133                     dao.put(key, new MappingEntry<MappingServiceValue>("value", value));
134                 }
135                 return true;
136             } else {
137                 return false;
138             }
139         } else {
140             return false;
141         }
142     }
143
144     public boolean addAuthenticationKey(LispAddressContainer address, int maskLen, String key) {
145         IMappingServiceKey mappingServiceKey = MappingServiceKeyUtil.generateMappingServiceKey(address, maskLen);
146         Map<String, ?> daoMap = dao.get(mappingServiceKey);
147         MappingServiceValue value = null;
148         if (daoMap != null) {
149             value = (MappingServiceValue) daoMap.get("value");
150             if (value == null) {
151                 value = new MappingServiceValue();
152             }
153         } else {
154             value = new MappingServiceValue();
155         }
156         value.setKey(key);
157         MappingEntry<MappingServiceValue> entry = new MappingEntry<MappingServiceValue>("value", value);
158         dao.put(mappingServiceKey, entry);
159         return true;
160     }
161
162     public boolean shouldAuthenticate() {
163         return shouldAuthenticate;
164     }
165
166     public boolean shouldIterateMask() {
167         return shouldIterateMask;
168     }
169
170     public void setShouldIterateMask(boolean shouldIterateMask) {
171         this.shouldIterateMask = shouldIterateMask;
172     }
173
174     public void setShouldAuthenticate(boolean shouldAuthenticate) {
175         this.shouldAuthenticate = shouldAuthenticate;
176     }
177
178 }