blob: 5784a0706b8d7d5b0947e2bbd9c3e7d0bbbeac82 [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 */
Jian Li5e505c62016-12-05 02:44:24 +090016package org.onosproject.lisp.ctl.impl;
Jian Li451cea32016-10-04 15:27:50 +090017
Jian Li5e505c62016-12-05 02:44:24 +090018import org.onosproject.lisp.msg.protocols.DefaultLispMapReply.DefaultReplyBuilder;
Jian Li1118c122016-11-01 21:58:15 +090019import org.onosproject.lisp.msg.protocols.LispEncapsulatedControl;
20import org.onosproject.lisp.msg.protocols.LispMapRecord;
Jian Liafe2d3f2016-11-01 02:49:07 +090021import org.onosproject.lisp.msg.protocols.LispMapReply;
Jian Li5e505c62016-12-05 02:44:24 +090022import org.onosproject.lisp.msg.protocols.LispMapReply.ReplyBuilder;
Jian Li451cea32016-10-04 15:27:50 +090023import org.onosproject.lisp.msg.protocols.LispMessage;
Jian Li1118c122016-11-01 21:58:15 +090024import org.onosproject.lisp.msg.types.LispIpAddress;
Jian Li5e505c62016-12-05 02:44:24 +090025import org.onosproject.lisp.msg.protocols.LispMapRequest;
26
Jian Li1118c122016-11-01 21:58:15 +090027import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import java.net.InetSocketAddress;
31import java.util.List;
Jian Li451cea32016-10-04 15:27:50 +090032
33/**
34 * LISP map resolver class.
35 * Handles map-request message and acknowledges with map-reply message.
36 */
Jian Li29986d82016-12-01 03:25:12 +090037public final class LispMapResolver {
Jian Li6322a362016-10-31 00:57:19 +090038
Jian Li1118c122016-11-01 21:58:15 +090039 private static final Logger log = LoggerFactory.getLogger(LispMapResolver.class);
40
Jian Li29986d82016-12-01 03:25:12 +090041 private LispMappingDatabase mapDb = LispMappingDatabase.getInstance();
Jian Li1118c122016-11-01 21:58:15 +090042
Jian Li29986d82016-12-01 03:25:12 +090043 // non-instantiable (except for our Singleton)
44 private LispMapResolver() {
45 }
46
47 public static LispMapResolver getInstance() {
48 return SingletonHelper.INSTANCE;
Jian Li1118c122016-11-01 21:58:15 +090049 }
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 Li1118c122016-11-01 21:58:15 +090062 // build map-reply message
Jian Li5e505c62016-12-05 02:44:24 +090063 ReplyBuilder replyBuilder = new DefaultReplyBuilder();
Jian Li1118c122016-11-01 21:58:15 +090064 replyBuilder.withNonce(request.getNonce());
65 replyBuilder.withIsEtr(false);
66 replyBuilder.withIsSecurity(false);
67 replyBuilder.withIsProbe(request.isProbe());
68
Jian Li29986d82016-12-01 03:25:12 +090069 List<LispMapRecord> mapRecords = mapDb.getMapRecordByEidRecords(request.getEids());
Jian Li1118c122016-11-01 21:58:15 +090070
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 Li451cea32016-10-04 15:27:50 +090089 }
Jian Li29986d82016-12-01 03:25:12 +090090
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 Li451cea32016-10-04 15:27:50 +0900102}