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 | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 18 | import org.onosproject.lisp.msg.protocols.DefaultLispMapReply.DefaultReplyBuilder; |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 19 | import org.onosproject.lisp.msg.protocols.LispEncapsulatedControl; |
| 20 | import org.onosproject.lisp.msg.protocols.LispMapRecord; |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame] | 21 | import org.onosproject.lisp.msg.protocols.LispMapReply; |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 22 | import org.onosproject.lisp.msg.protocols.LispMapReply.ReplyBuilder; |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 23 | import org.onosproject.lisp.msg.protocols.LispMessage; |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 24 | import org.onosproject.lisp.msg.types.LispIpAddress; |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 25 | import org.onosproject.lisp.msg.protocols.LispMapRequest; |
| 26 | |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import java.net.InetSocketAddress; |
| 31 | import java.util.List; |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * LISP map resolver class. |
| 35 | * Handles map-request message and acknowledges with map-reply message. |
| 36 | */ |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 37 | public final class LispMapResolver { |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 38 | |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 39 | private static final Logger log = LoggerFactory.getLogger(LispMapResolver.class); |
| 40 | |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 41 | private LispMappingDatabase mapDb = LispMappingDatabase.getInstance(); |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 42 | |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 43 | // non-instantiable (except for our Singleton) |
| 44 | private LispMapResolver() { |
| 45 | } |
| 46 | |
| 47 | public static LispMapResolver getInstance() { |
| 48 | return SingletonHelper.INSTANCE; |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Handles encapsulated control message and replies with map-reply message. |
| 53 | * |
| 54 | * @param message encapsulated control message |
| 55 | * @return map-reply message |
| 56 | */ |
| 57 | public LispMessage processMapRequest(LispMessage message) { |
| 58 | |
| 59 | LispEncapsulatedControl ecm = (LispEncapsulatedControl) message; |
| 60 | LispMapRequest request = (LispMapRequest) ecm.getControlMessage(); |
| 61 | |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 62 | // build map-reply message |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 63 | ReplyBuilder replyBuilder = new DefaultReplyBuilder(); |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 64 | replyBuilder.withNonce(request.getNonce()); |
| 65 | replyBuilder.withIsEtr(false); |
| 66 | replyBuilder.withIsSecurity(false); |
| 67 | replyBuilder.withIsProbe(request.isProbe()); |
| 68 | |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 69 | List<LispMapRecord> mapRecords = mapDb.getMapRecordByEidRecords(request.getEids()); |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 70 | |
| 71 | if (mapRecords.size() == 0) { |
| 72 | log.warn("Map information is not found."); |
| 73 | } else { |
| 74 | replyBuilder.withMapRecords(mapRecords); |
| 75 | } |
| 76 | |
| 77 | LispMapReply reply = replyBuilder.build(); |
| 78 | |
| 79 | if (request.getItrRlocs() != null && request.getItrRlocs().size() > 0) { |
| 80 | LispIpAddress itr = (LispIpAddress) request.getItrRlocs().get(0); |
| 81 | InetSocketAddress address = new InetSocketAddress(itr.getAddress().toInetAddress(), |
| 82 | ecm.innerUdp().getSourcePort()); |
| 83 | reply.configSender(address); |
| 84 | } else { |
| 85 | log.warn("No ITR RLOC is found, cannot respond back to ITR."); |
| 86 | } |
| 87 | |
| 88 | return reply; |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 89 | } |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 90 | |
| 91 | /** |
| 92 | * Prevents object instantiation from external. |
| 93 | */ |
| 94 | private static final class SingletonHelper { |
| 95 | private static final String ILLEGAL_ACCESS_MSG = "Should not instantiate this class."; |
| 96 | private static final LispMapResolver INSTANCE = new LispMapResolver(); |
| 97 | |
| 98 | private SingletonHelper() { |
| 99 | throw new IllegalAccessError(ILLEGAL_ACCESS_MSG); |
| 100 | } |
| 101 | } |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 102 | } |