Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 16 | package org.onosproject.lisp.ctl.impl; |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 17 | |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame^] | 18 | import com.google.common.collect.Lists; |
| 19 | import org.onosproject.lisp.msg.protocols.DefaultLispEncapsulatedControl.DefaultEcmBuilder; |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 20 | import org.onosproject.lisp.msg.protocols.DefaultLispMapReply.DefaultReplyBuilder; |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 21 | import org.onosproject.lisp.msg.protocols.LispEncapsulatedControl; |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame^] | 22 | import org.onosproject.lisp.msg.protocols.LispEncapsulatedControl.EcmBuilder; |
| 23 | import org.onosproject.lisp.msg.protocols.LispLocatorRecord; |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 24 | import org.onosproject.lisp.msg.protocols.LispMapRecord; |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 25 | import org.onosproject.lisp.msg.protocols.LispMapReply.ReplyBuilder; |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 26 | import org.onosproject.lisp.msg.protocols.LispMapRequest; |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame^] | 27 | import org.onosproject.lisp.msg.protocols.LispMapReply; |
| 28 | import org.onosproject.lisp.msg.protocols.LispMessage; |
| 29 | import org.onosproject.lisp.msg.types.LispAfiAddress; |
| 30 | import org.onosproject.lisp.msg.types.LispIpAddress; |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 31 | |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | |
| 35 | import java.net.InetSocketAddress; |
| 36 | import java.util.List; |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * LISP map resolver class. |
| 40 | * Handles map-request message and acknowledges with map-reply message. |
| 41 | */ |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 42 | public final class LispMapResolver { |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 43 | |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 44 | private static final Logger log = LoggerFactory.getLogger(LispMapResolver.class); |
| 45 | |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame^] | 46 | private static final int ECM_DST_PORT = 4342; |
| 47 | private static final String NO_ITR_RLOCS_MSG = |
| 48 | "No ITR RLOC is found, cannot respond to ITR."; |
| 49 | private static final String NO_ETR_RLOCS_MSG = |
| 50 | "No ETR RLOC is found, cannot relay to ETR."; |
| 51 | private static final String NO_MAP_INFO_MSG = "Map information is not found."; |
| 52 | |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 53 | private LispMappingDatabase mapDb = LispMappingDatabase.getInstance(); |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 54 | |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 55 | // non-instantiable (except for our Singleton) |
| 56 | private LispMapResolver() { |
| 57 | } |
| 58 | |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame^] | 59 | static LispMapResolver getInstance() { |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 60 | return SingletonHelper.INSTANCE; |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Handles encapsulated control message and replies with map-reply message. |
| 65 | * |
| 66 | * @param message encapsulated control message |
| 67 | * @return map-reply message |
| 68 | */ |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame^] | 69 | List<LispMessage> processMapRequest(LispMessage message) { |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 70 | |
| 71 | LispEncapsulatedControl ecm = (LispEncapsulatedControl) message; |
| 72 | LispMapRequest request = (LispMapRequest) ecm.getControlMessage(); |
| 73 | |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame^] | 74 | List<LispMapRecord> mapReplyRecords = |
| 75 | mapDb.getMapRecordByEidRecords(request.getEids(), true); |
| 76 | |
| 77 | List<LispMapRecord> mapRequestRecords = |
| 78 | mapDb.getMapRecordByEidRecords(request.getEids(), false); |
| 79 | |
| 80 | if (mapReplyRecords.size() + mapRequestRecords.size() == 0) { |
| 81 | |
| 82 | // TODO: need to generate map-reply with configured native-forward action |
| 83 | log.warn(NO_MAP_INFO_MSG); |
| 84 | |
| 85 | } else { |
| 86 | |
| 87 | if (mapReplyRecords.size() > 0) { |
| 88 | |
| 89 | List<LispMessage> mapReplies = Lists.newArrayList(); |
| 90 | |
| 91 | // build map-reply message based on map-request from ITR |
| 92 | ReplyBuilder replyBuilder = initMapReplyBuilder(request); |
| 93 | replyBuilder.withMapRecords(mapReplyRecords); |
| 94 | |
| 95 | List<InetSocketAddress> addresses = |
| 96 | getItrAddresses(request.getItrRlocs(), |
| 97 | ecm.innerUdp().getSourcePort()); |
| 98 | |
| 99 | addresses.forEach(address -> { |
| 100 | if (address != null) { |
| 101 | LispMapReply reply = replyBuilder.build(); |
| 102 | reply.configSender(address); |
| 103 | mapReplies.add(reply); |
| 104 | } else { |
| 105 | log.warn(NO_ITR_RLOCS_MSG); |
| 106 | } |
| 107 | }); |
| 108 | |
| 109 | return mapReplies; |
| 110 | } |
| 111 | |
| 112 | if (mapRequestRecords.size() > 0) { |
| 113 | |
| 114 | List<LispMessage> ecms = Lists.newArrayList(); |
| 115 | |
| 116 | // re-encapsulate encapsulated control message from ITR |
| 117 | List<InetSocketAddress> addresses = |
| 118 | getEtrAddresses(mapRequestRecords, ECM_DST_PORT); |
| 119 | |
| 120 | addresses.forEach(address -> { |
| 121 | if (address != null) { |
| 122 | LispEncapsulatedControl reencapEcm = cloneEcm(ecm); |
| 123 | reencapEcm.configSender(address); |
| 124 | ecms.add(reencapEcm); |
| 125 | } else { |
| 126 | log.warn(NO_ETR_RLOCS_MSG); |
| 127 | } |
| 128 | }); |
| 129 | |
| 130 | return ecms; |
| 131 | } |
| 132 | } |
| 133 | return null; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Initializes MapReply builder without specifying map records. |
| 138 | * |
| 139 | * @param request received map request from ITR |
| 140 | * @return initialized MapReply builder |
| 141 | */ |
| 142 | private ReplyBuilder initMapReplyBuilder(LispMapRequest request) { |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 143 | ReplyBuilder replyBuilder = new DefaultReplyBuilder(); |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 144 | replyBuilder.withNonce(request.getNonce()); |
| 145 | replyBuilder.withIsEtr(false); |
| 146 | replyBuilder.withIsSecurity(false); |
| 147 | replyBuilder.withIsProbe(request.isProbe()); |
| 148 | |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame^] | 149 | return replyBuilder; |
| 150 | } |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 151 | |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame^] | 152 | /** |
| 153 | * Clones ECM from original ECM. |
| 154 | * |
| 155 | * @param ecm original ECM |
| 156 | * @return cloned ECM |
| 157 | */ |
| 158 | private LispEncapsulatedControl cloneEcm(LispEncapsulatedControl ecm) { |
| 159 | EcmBuilder ecmBuilder = new DefaultEcmBuilder(); |
| 160 | ecmBuilder.innerLispMessage(ecm.getControlMessage()); |
| 161 | ecmBuilder.isSecurity(ecm.isSecurity()); |
| 162 | ecmBuilder.innerIpHeader(ecm.innerIpHeader()); |
| 163 | ecmBuilder.innerUdpHeader(ecm.innerUdp()); |
| 164 | |
| 165 | return ecmBuilder.build(); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Obtains a collection of valid ITR addresses with a port number specified. |
| 170 | * These addresses will be used to acknowledge map-reply to ITR. |
| 171 | * |
| 172 | * @param itrRlocs a collection of ITR RLOCs |
| 173 | * @param port port number |
| 174 | * @return a collection of valid ITR addresses with a port number specified |
| 175 | */ |
| 176 | private List<InetSocketAddress> getItrAddresses(List<LispAfiAddress> itrRlocs, |
| 177 | int port) { |
| 178 | List<InetSocketAddress> addresses = Lists.newArrayList(); |
| 179 | for (LispAfiAddress itrRloc : itrRlocs) { |
| 180 | addresses.add(new InetSocketAddress(((LispIpAddress) |
| 181 | itrRloc).getAddress().toInetAddress(), port)); |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 182 | } |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame^] | 183 | return addresses; |
| 184 | } |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 185 | |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame^] | 186 | /** |
| 187 | * Obtains a collection of valid ETR addresses with a port number specified. |
| 188 | * These addresses will be used to relay map-request to ETR. |
| 189 | * |
| 190 | * @param mapRecords a collection of map records |
| 191 | * @param port port number |
| 192 | * @return a collection of valid ETR addresses with a port number specified |
| 193 | */ |
| 194 | private List<InetSocketAddress> getEtrAddresses(List<LispMapRecord> mapRecords, |
| 195 | int port) { |
| 196 | List<InetSocketAddress> addresses = Lists.newArrayList(); |
| 197 | for (LispMapRecord mapRecord : mapRecords) { |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 198 | |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame^] | 199 | // we only select the first locator record in all cases... |
| 200 | LispLocatorRecord locatorRecord = mapRecord.getLocators().get(0); |
| 201 | if (locatorRecord != null) { |
| 202 | addresses.add(new InetSocketAddress(((LispIpAddress) |
| 203 | locatorRecord.getLocatorAfi()).getAddress() |
| 204 | .toInetAddress(), port)); |
| 205 | } |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 206 | } |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame^] | 207 | return addresses; |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 208 | } |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 209 | |
| 210 | /** |
| 211 | * Prevents object instantiation from external. |
| 212 | */ |
| 213 | private static final class SingletonHelper { |
| 214 | private static final String ILLEGAL_ACCESS_MSG = "Should not instantiate this class."; |
| 215 | private static final LispMapResolver INSTANCE = new LispMapResolver(); |
| 216 | |
| 217 | private SingletonHelper() { |
| 218 | throw new IllegalAccessError(ILLEGAL_ACCESS_MSG); |
| 219 | } |
| 220 | } |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 221 | } |