blob: 11492d609d5a0bf28f4571caf38a3628aabb7633 [file] [log] [blame]
Jian Li55648ed2017-05-01 02:20:55 +09001/*
2 * Copyright 2017-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.provider.lisp.mapping.util;
17
18import org.onlab.packet.IpAddress;
19import org.onlab.packet.IpPrefix;
20import org.onlab.packet.MacAddress;
21import org.onosproject.lisp.ctl.ExtensionMappingAddressInterpreter;
22import org.onosproject.lisp.msg.types.LispAfiAddress;
23import org.onosproject.lisp.msg.types.LispAsAddress;
24import org.onosproject.lisp.msg.types.LispDistinguishedNameAddress;
25import org.onosproject.lisp.msg.types.LispIpv4Address;
26import org.onosproject.lisp.msg.types.LispIpv6Address;
27import org.onosproject.lisp.msg.types.LispMacAddress;
28import org.onosproject.lisp.msg.types.lcaf.LispLcafAddress;
29import org.onosproject.mapping.addresses.ExtensionMappingAddress;
30import org.onosproject.mapping.addresses.MappingAddress;
31import org.onosproject.mapping.addresses.MappingAddresses;
32import org.onosproject.net.Device;
33import org.onosproject.net.DeviceId;
34import org.onosproject.net.device.DeviceService;
35import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
38import static org.onosproject.mapping.addresses.ExtensionMappingAddressType.ExtensionMappingAddressTypes.*;
39
40/**
41 * Mapping address builder class.
42 */
43public final class MappingAddressBuilder {
44
45 private static final Logger log =
46 LoggerFactory.getLogger(MappingAddressBuilder.class);
47
48 private static final int IPV4_PREFIX_LENGTH = 32;
49 private static final int IPV6_PREFIX_LENGTH = 128;
50
51 // prevent from instantiation
52 private MappingAddressBuilder() {
53 }
54
55 /**
56 * Converts LispAfiAddress into abstracted mapping address.
57 *
58 * @param deviceService device service
59 * @param deviceId device identifier
60 * @param address LispAfiAddress
61 * @return abstracted mapping address
62 */
63 protected static MappingAddress getAddress(DeviceService deviceService,
64 DeviceId deviceId,
65 LispAfiAddress address) {
66
67 if (address == null) {
68 log.warn("Address is not specified.");
69 return null;
70 }
71
72 switch (address.getAfi()) {
73 case IP4:
74 return afi2mapping(address);
75 case IP6:
76 return afi2mapping(address);
77 case AS:
78 int asNum = ((LispAsAddress) address).getASNum();
79 return MappingAddresses.asMappingAddress(String.valueOf(asNum));
80 case DISTINGUISHED_NAME:
81 String dn = ((LispDistinguishedNameAddress)
82 address).getDistinguishedName();
83 return MappingAddresses.dnMappingAddress(dn);
84 case MAC:
85 MacAddress macAddress = ((LispMacAddress) address).getAddress();
86 return MappingAddresses.ethMappingAddress(macAddress);
87 case LCAF:
88 return deviceService == null ? null :
89 lcaf2extension(deviceService, deviceId, (LispLcafAddress) address);
90 default:
91 log.warn("Unsupported address type {}", address.getAfi());
92 break;
93 }
94
95 return null;
96 }
97
98 /**
99 * Converts AFI address to generalized mapping address.
100 *
101 * @param afiAddress IP typed AFI address
102 * @return generalized mapping address
103 */
104 private static MappingAddress afi2mapping(LispAfiAddress afiAddress) {
105 switch (afiAddress.getAfi()) {
106 case IP4:
107 IpAddress ipv4Address = ((LispIpv4Address) afiAddress).getAddress();
108 IpPrefix ipv4Prefix = IpPrefix.valueOf(ipv4Address, IPV4_PREFIX_LENGTH);
109 return MappingAddresses.ipv4MappingAddress(ipv4Prefix);
110 case IP6:
111 IpAddress ipv6Address = ((LispIpv6Address) afiAddress).getAddress();
112 IpPrefix ipv6Prefix = IpPrefix.valueOf(ipv6Address, IPV6_PREFIX_LENGTH);
113 return MappingAddresses.ipv6MappingAddress(ipv6Prefix);
114 default:
115 log.warn("Only support to convert IP address type");
116 break;
117 }
118 return null;
119 }
120
121 /**
122 * Converts LCAF address to extension mapping address.
123 *
124 * @param deviceService device service
125 * @param deviceId device identifier
126 * @param lcaf LCAF address
127 * @return extension mapping address
128 */
129 private static MappingAddress lcaf2extension(DeviceService deviceService,
130 DeviceId deviceId,
131 LispLcafAddress lcaf) {
132
133 Device device = deviceService.getDevice(deviceId);
134
135 ExtensionMappingAddressInterpreter addressInterpreter;
136 ExtensionMappingAddress mappingAddress = null;
137 if (device.is(ExtensionMappingAddressInterpreter.class)) {
138 addressInterpreter = device.as(ExtensionMappingAddressInterpreter.class);
139 } else {
140 addressInterpreter = null;
141 }
142
143 switch (lcaf.getType()) {
144 case LIST:
145 if (addressInterpreter != null &&
146 addressInterpreter.supported(LIST_ADDRESS.type())) {
147 mappingAddress = addressInterpreter.mapLcafAddress(lcaf);
148 }
149 break;
150 case SEGMENT:
151 if (addressInterpreter != null &&
152 addressInterpreter.supported(SEGMENT_ADDRESS.type())) {
153 mappingAddress = addressInterpreter.mapLcafAddress(lcaf);
154 }
155 break;
156 case AS:
157 if (addressInterpreter != null &&
158 addressInterpreter.supported(AS_ADDRESS.type())) {
159 mappingAddress = addressInterpreter.mapLcafAddress(lcaf);
160 }
161 break;
162 case APPLICATION_DATA:
163 if (addressInterpreter != null &&
164 addressInterpreter.supported(APPLICATION_DATA_ADDRESS.type())) {
165 mappingAddress = addressInterpreter.mapLcafAddress(lcaf);
166 }
167 break;
168 case GEO_COORDINATE:
169 if (addressInterpreter != null &&
170 addressInterpreter.supported(GEO_COORDINATE_ADDRESS.type())) {
171 mappingAddress = addressInterpreter.mapLcafAddress(lcaf);
172 }
173 break;
174 case NAT:
175 if (addressInterpreter != null &&
176 addressInterpreter.supported(NAT_ADDRESS.type())) {
177 mappingAddress = addressInterpreter.mapLcafAddress(lcaf);
178 }
179 break;
180 case NONCE:
181 if (addressInterpreter != null &&
182 addressInterpreter.supported(NONCE_ADDRESS.type())) {
183 mappingAddress = addressInterpreter.mapLcafAddress(lcaf);
184 }
185 break;
186 case MULTICAST:
187 if (addressInterpreter != null &&
188 addressInterpreter.supported(MULTICAST_ADDRESS.type())) {
189 mappingAddress = addressInterpreter.mapLcafAddress(lcaf);
190 }
191 break;
192 case TRAFFIC_ENGINEERING:
193 if (addressInterpreter != null &&
194 addressInterpreter.supported(TRAFFIC_ENGINEERING_ADDRESS.type())) {
195 mappingAddress = addressInterpreter.mapLcafAddress(lcaf);
196 }
197 break;
198 case SOURCE_DEST:
199 if (addressInterpreter != null &&
200 addressInterpreter.supported(SOURCE_DEST_ADDRESS.type())) {
201 mappingAddress = addressInterpreter.mapLcafAddress(lcaf);
202 }
203 break;
204 default:
205 log.warn("Unsupported extension mapping address type {}", lcaf.getType());
206 break;
207 }
208
209 return mappingAddress != null ?
210 MappingAddresses.extensionMappingAddressWrapper(mappingAddress, deviceId) : null;
211 }
212}