blob: 3e4a21d188f77bddf2b2d683c4dcc71051e80bd8 [file] [log] [blame]
Jian Li451cea32016-10-04 15:27:50 +09001/*
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 */
16package org.onosproject.lisp.ctl;
17
Jian Li1118c122016-11-01 21:58:15 +090018import org.onosproject.lisp.msg.protocols.LispEncapsulatedControl;
19import org.onosproject.lisp.msg.protocols.LispMapRecord;
Jian Liafe2d3f2016-11-01 02:49:07 +090020import org.onosproject.lisp.msg.protocols.LispMapReply;
Jian Li1118c122016-11-01 21:58:15 +090021import org.onosproject.lisp.msg.protocols.LispMapRequest;
22import org.onosproject.lisp.msg.protocols.DefaultLispMapReply.DefaultReplyBuilder;
23
Jian Li451cea32016-10-04 15:27:50 +090024import org.onosproject.lisp.msg.protocols.LispMessage;
Jian Li1118c122016-11-01 21:58:15 +090025import org.onosproject.lisp.msg.types.LispIpAddress;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import java.net.InetSocketAddress;
30import java.util.List;
Jian Li451cea32016-10-04 15:27:50 +090031
32/**
33 * LISP map resolver class.
34 * Handles map-request message and acknowledges with map-reply message.
35 */
Jian Li29986d82016-12-01 03:25:12 +090036public final class LispMapResolver {
Jian Li6322a362016-10-31 00:57:19 +090037
Jian Li1118c122016-11-01 21:58:15 +090038 private static final Logger log = LoggerFactory.getLogger(LispMapResolver.class);
39
Jian Li29986d82016-12-01 03:25:12 +090040 private LispMappingDatabase mapDb = LispMappingDatabase.getInstance();
Jian Li1118c122016-11-01 21:58:15 +090041
Jian Li29986d82016-12-01 03:25:12 +090042 // non-instantiable (except for our Singleton)
43 private LispMapResolver() {
44 }
45
46 public static LispMapResolver getInstance() {
47 return SingletonHelper.INSTANCE;
Jian Li1118c122016-11-01 21:58:15 +090048 }
49
50 /**
51 * Handles encapsulated control message and replies with map-reply message.
52 *
53 * @param message encapsulated control message
54 * @return map-reply message
55 */
56 public LispMessage processMapRequest(LispMessage message) {
57
58 LispEncapsulatedControl ecm = (LispEncapsulatedControl) message;
59 LispMapRequest request = (LispMapRequest) ecm.getControlMessage();
60
Jian Li1118c122016-11-01 21:58:15 +090061 // build map-reply message
62 LispMapReply.ReplyBuilder replyBuilder = new DefaultReplyBuilder();
63 replyBuilder.withNonce(request.getNonce());
64 replyBuilder.withIsEtr(false);
65 replyBuilder.withIsSecurity(false);
66 replyBuilder.withIsProbe(request.isProbe());
67
Jian Li29986d82016-12-01 03:25:12 +090068 List<LispMapRecord> mapRecords = mapDb.getMapRecordByEidRecords(request.getEids());
Jian Li1118c122016-11-01 21:58:15 +090069
70 if (mapRecords.size() == 0) {
71 log.warn("Map information is not found.");
72 } else {
73 replyBuilder.withMapRecords(mapRecords);
74 }
75
76 LispMapReply reply = replyBuilder.build();
77
78 if (request.getItrRlocs() != null && request.getItrRlocs().size() > 0) {
79 LispIpAddress itr = (LispIpAddress) request.getItrRlocs().get(0);
80 InetSocketAddress address = new InetSocketAddress(itr.getAddress().toInetAddress(),
81 ecm.innerUdp().getSourcePort());
82 reply.configSender(address);
83 } else {
84 log.warn("No ITR RLOC is found, cannot respond back to ITR.");
85 }
86
87 return reply;
Jian Li451cea32016-10-04 15:27:50 +090088 }
Jian Li29986d82016-12-01 03:25:12 +090089
90 /**
91 * Prevents object instantiation from external.
92 */
93 private static final class SingletonHelper {
94 private static final String ILLEGAL_ACCESS_MSG = "Should not instantiate this class.";
95 private static final LispMapResolver INSTANCE = new LispMapResolver();
96
97 private SingletonHelper() {
98 throw new IllegalAccessError(ILLEGAL_ACCESS_MSG);
99 }
100 }
Jian Li451cea32016-10-04 15:27:50 +0900101}