1139ba96d38385264a353bef3e2cc19877a808ec
[lispflowmapping.git] / mappingservice / northbound / src / main / java / org / opendaylight / lispflowmapping / northbound / LispAddressConvertorNB.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.lispflowmapping.northbound;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
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;
34
35 public class LispAddressConvertorNB {
36         
37         protected static final Logger logger = LoggerFactory.getLogger(LispAddressConvertorNB.class);
38
39         
40 public static LispAddress convertToLispAddress(LispAddressGeneric generic){
41         
42         if (generic == null){
43                 logger.warn("Couldn't convert address, generic address is null");
44                 return null;
45         }
46         
47         LispAddress lispAddress = null;
48         
49         AddressFamilyNumberEnum afiEnum  = AddressFamilyNumberEnum.valueOf((short) generic.getAfi());
50         
51         switch (afiEnum) {
52         
53         case IP: 
54                 lispAddress = new LispIpv4Address(generic.getIpAddress());
55                 if (((LispIpv4Address)lispAddress).getAddress() == null){
56                         throw new IllegalArgumentException("Convertor was unable to convert address");
57                 }
58                 break;
59                 case AS:
60                         lispAddress = new LispASAddress(generic.getAsNum());
61                         break;
62                 case IP6:
63                         lispAddress = new LispIpv6Address(generic.getIpAddress());
64                 if (((LispIpv6Address)lispAddress).getAddress() == null){
65                         throw new IllegalArgumentException("Convertor was unable to convert address");
66                 }
67                         break;
68                 case MAC:
69                         lispAddress = new LispMACAddress(generic.getMac());
70                         break;
71                 case DISTINGUISHED_NAME:
72                         lispAddress = new LispDistinguishedNameAddress(generic.getDistinguishedName());
73                         break;
74                 case LCAF:
75                         LispCanonicalAddressFormatEnum lcafEnum = 
76                                 LispCanonicalAddressFormatEnum.valueOf(generic.getLcafType());
77                         switch (lcafEnum){
78                         case APPLICATION_DATA:
79                                 lispAddress = convertToApplicationData(generic);
80                                 break;
81                         case LIST:
82                                 lispAddress = convertToList(generic);
83                                 break;
84                         case SEGMENT:
85                                 lispAddress = convertToSegment(generic);
86                                 break;
87                         case SOURCE_DEST:
88                                 lispAddress = convertToSrcDst(generic);
89                                 break;
90                         case TRAFFIC_ENGINEERING:
91                                 lispAddress = convertToTE(generic);
92                                 break;
93                         default:
94                                 throw new IllegalArgumentException("LCAF type " + lcafEnum + 
95                                                 " not supported by this convertor: convertToLispAddress(LispAddressGeneric generic)");
96                         }
97                         break;
98                 default:
99                         throw new IllegalArgumentException("AFI " + afiEnum + 
100                                         " not supported by this convertor: convertToLispAddress(LispAddressGeneric generic)");
101         }
102         
103
104         
105         return lispAddress;
106     }
107
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()));
112 }
113
114 private static LispListLCAFAddress convertToList(LispAddressGeneric generic){
115         List<LispAddress> list = new ArrayList<LispAddress>();
116         
117         for (int i=0;i<generic.getAddresses().size();i++){
118                 list.add(convertToLispAddress(generic.getAddresses().get(i)));
119         }
120         
121         return new LispListLCAFAddress((byte) 0, list);
122 }
123
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()));
128 }
129
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()));
134 }
135
136
137
138 private static LispTrafficEngineeringLCAFAddress convertToTE(LispAddressGeneric generic){
139         List<ReencapHop> listHops = new ArrayList<ReencapHop>();
140         ReencapHop hop;
141
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());
146                 listHops.add(hop);
147         }
148         return new LispTrafficEngineeringLCAFAddress((byte)0, listHops);
149 }
150
151
152
153
154
155 public static void convertGenericToLispAddresses(MapRegister mapRegister){
156         LispAddress EID;
157         LispAddressGeneric EIDGeneric;
158         
159         List<LocatorRecord> locRecordList;
160         LispAddress RLOC;
161         LispAddressGeneric RLOCGeneric;
162         
163         if (mapRegister == null){
164                 logger.warn("Couldn't convert addresses, mapRegister is null");
165                 return;
166         }
167         
168         int EIDtoLocatorRecordCount = mapRegister.getEidToLocatorRecords().size();
169         
170         for (int i=0;i<EIDtoLocatorRecordCount;i++){
171         EIDGeneric = mapRegister.getEidToLocatorRecords().get(i).getPrefixGeneric();
172         
173         EID = convertToLispAddress(EIDGeneric);
174         
175         mapRegister.getEidToLocatorRecords().get(i).setPrefix(EID);
176         locRecordList = mapRegister.getEidToLocatorRecords().get(i).getLocators();
177         
178         for(int j=0;j<locRecordList.size();j++){
179                 
180                 RLOCGeneric = locRecordList.get(j).getLocatorGeneric();
181                 
182                 RLOC = convertToLispAddress(RLOCGeneric);
183                 locRecordList.get(j).setLocator(RLOC);
184         }
185         
186         }
187 }
188
189
190 public static void convertRecordToGenericAddress(EidToLocatorRecord record){
191
192         LispAddress EID;        
193         List<LocatorRecord> locRecordList;
194         LispAddress RLOC;
195         
196         if (record == null){
197                 logger.warn("Couldn't convert addresses, record is null");
198                 return;
199         }
200         
201         EID = record.getPrefix();
202     record.setPrefixGeneric(new LispAddressGeneric(EID));
203         
204     locRecordList = record.getLocators();
205     for(int j=0;j<locRecordList.size();j++){
206         
207         RLOC = locRecordList.get(j).getLocator();
208         locRecordList.get(j).setLocatorGeneric(new LispAddressGeneric(RLOC));
209     }
210 }       
211
212
213         
214 }