2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.lispflowmapping.northbound;
11 import java.util.ArrayList;
12 import java.util.List;
14 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
15 import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
16 import org.opendaylight.lispflowmapping.type.lisp.EidToLocatorRecord;
17 import org.opendaylight.lispflowmapping.type.lisp.LocatorRecord;
18 import org.opendaylight.lispflowmapping.type.lisp.MapRegister;
19 import org.opendaylight.lispflowmapping.type.lisp.address.LispASAddress;
20 import org.opendaylight.lispflowmapping.type.lisp.address.LispAddress;
21 import org.opendaylight.lispflowmapping.type.lisp.address.LispAddressGeneric;
22 import org.opendaylight.lispflowmapping.type.lisp.address.LispApplicationDataLCAFAddress;
23 import org.opendaylight.lispflowmapping.type.lisp.address.LispDistinguishedNameAddress;
24 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv4Address;
25 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv6Address;
26 import org.opendaylight.lispflowmapping.type.lisp.address.LispListLCAFAddress;
27 import org.opendaylight.lispflowmapping.type.lisp.address.LispMACAddress;
28 import org.opendaylight.lispflowmapping.type.lisp.address.LispSegmentLCAFAddress;
29 import org.opendaylight.lispflowmapping.type.lisp.address.LispSourceDestLCAFAddress;
30 import org.opendaylight.lispflowmapping.type.lisp.address.LispTrafficEngineeringLCAFAddress;
31 import org.opendaylight.lispflowmapping.type.lisp.address.ReencapHop;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class LispAddressConvertorNB {
37 protected static final Logger logger = LoggerFactory.getLogger(LispAddressConvertorNB.class);
40 public static LispAddress convertToLispAddress(LispAddressGeneric generic){
43 logger.warn("Couldn't convert address, generic address is null");
47 LispAddress lispAddress = null;
49 AddressFamilyNumberEnum afiEnum = AddressFamilyNumberEnum.valueOf((short) generic.getAfi());
54 lispAddress = new LispIpv4Address(generic.getIpAddress());
55 if (((LispIpv4Address)lispAddress).getAddress() == null){
56 throw new IllegalArgumentException("Convertor was unable to convert address");
60 lispAddress = new LispASAddress(generic.getAsNum());
63 lispAddress = new LispIpv6Address(generic.getIpAddress());
64 if (((LispIpv6Address)lispAddress).getAddress() == null){
65 throw new IllegalArgumentException("Convertor was unable to convert address");
69 lispAddress = new LispMACAddress(generic.getMac());
71 case DISTINGUISHED_NAME:
72 lispAddress = new LispDistinguishedNameAddress(generic.getDistinguishedName());
75 LispCanonicalAddressFormatEnum lcafEnum =
76 LispCanonicalAddressFormatEnum.valueOf(generic.getLcafType());
78 case APPLICATION_DATA:
79 lispAddress = convertToApplicationData(generic);
82 lispAddress = convertToList(generic);
85 lispAddress = convertToSegment(generic);
88 lispAddress = convertToSrcDst(generic);
90 case TRAFFIC_ENGINEERING:
91 lispAddress = convertToTE(generic);
94 throw new IllegalArgumentException("LCAF type " + lcafEnum +
95 " not supported by this convertor: convertToLispAddress(LispAddressGeneric generic)");
99 throw new IllegalArgumentException("AFI " + afiEnum +
100 " not supported by this convertor: convertToLispAddress(LispAddressGeneric generic)");
108 private static LispApplicationDataLCAFAddress convertToApplicationData(LispAddressGeneric generic){
109 return new LispApplicationDataLCAFAddress(
110 (byte) 0, generic.getProtocol(), generic.getIpTos(), generic.getLocalPort(),
111 generic.getRemotePort(), convertToLispAddress(generic.getAddress()));
114 private static LispListLCAFAddress convertToList(LispAddressGeneric generic){
115 List<LispAddress> list = new ArrayList<LispAddress>();
117 for (int i=0;i<generic.getAddresses().size();i++){
118 list.add(convertToLispAddress(generic.getAddresses().get(i)));
121 return new LispListLCAFAddress((byte) 0, list);
124 private static LispSegmentLCAFAddress convertToSegment(LispAddressGeneric generic){
125 byte idMaskLen = 0; //Not used here
126 return new LispSegmentLCAFAddress(idMaskLen, generic.getInstanceId(),
127 convertToLispAddress(generic.getAddress()));
130 private static LispSourceDestLCAFAddress convertToSrcDst(LispAddressGeneric generic){
131 return new LispSourceDestLCAFAddress((byte)0, (short)0, generic.getSrcMaskLength(),
132 generic.getDstMaskLength(), convertToLispAddress(generic.getSrcAddress()),
133 convertToLispAddress(generic.getDstAddress()));
138 private static LispTrafficEngineeringLCAFAddress convertToTE(LispAddressGeneric generic){
139 List<ReencapHop> listHops = new ArrayList<ReencapHop>();
142 for (int i=0;i<generic.getHops().size();i++){
143 hop = new ReencapHop(
144 convertToLispAddress(generic.getHops().get(i).getAddress()), (short) 0,
145 generic.isLookup(), generic.isRLOCProbe(), generic.isStrict());
148 return new LispTrafficEngineeringLCAFAddress((byte)0, listHops);
155 public static void convertGenericToLispAddresses(MapRegister mapRegister){
157 LispAddressGeneric EIDGeneric;
159 List<LocatorRecord> locRecordList;
161 LispAddressGeneric RLOCGeneric;
163 if (mapRegister == null){
164 logger.warn("Couldn't convert addresses, mapRegister is null");
168 int EIDtoLocatorRecordCount = mapRegister.getEidToLocatorRecords().size();
170 for (int i=0;i<EIDtoLocatorRecordCount;i++){
171 EIDGeneric = mapRegister.getEidToLocatorRecords().get(i).getPrefixGeneric();
173 EID = convertToLispAddress(EIDGeneric);
175 mapRegister.getEidToLocatorRecords().get(i).setPrefix(EID);
176 locRecordList = mapRegister.getEidToLocatorRecords().get(i).getLocators();
178 for(int j=0;j<locRecordList.size();j++){
180 RLOCGeneric = locRecordList.get(j).getLocatorGeneric();
182 RLOC = convertToLispAddress(RLOCGeneric);
183 locRecordList.get(j).setLocator(RLOC);
190 public static void convertRecordToGenericAddress(EidToLocatorRecord record){
193 List<LocatorRecord> locRecordList;
197 logger.warn("Couldn't convert addresses, record is null");
201 EID = record.getPrefix();
202 record.setPrefixGeneric(new LispAddressGeneric(EID));
204 locRecordList = record.getLocators();
205 for(int j=0;j<locRecordList.size();j++){
207 RLOC = locRecordList.get(j).getLocator();
208 locRecordList.get(j).setLocatorGeneric(new LispAddressGeneric(RLOC));