Migrate lisp-proto implementation to IETF YANG model
[lispflowmapping.git] / mappingservice / lisp-proto / src / main / java / org / opendaylight / lispflowmapping / lisp / util / AddressTypeMap.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc.  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 package org.opendaylight.lispflowmapping.lisp.util;
9
10 import java.util.HashMap;
11 import java.util.Map;
12
13 import org.opendaylight.lispflowmapping.lisp.type.LispCanonicalAddressFormatEnum;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana.afn.safi.rev130704.AddressFamily;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.AfiListLcaf;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ApplicationDataLcaf;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.AsNumberAfi;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.DistinguishedNameAfi;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ExplicitLocatorPathLcaf;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.InstanceIdLcaf;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4Afi;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4PrefixAfi;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv6Afi;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv6PrefixAfi;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.KeyValueAddressLcaf;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Lcaf;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.LispAddressFamily;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.MacAfi;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.NoAddressAfi;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SourceDestKeyLcaf;
32
33 /**
34  * This class contains static HashMaps of AFIs and LCAF types to LispAddressFamily identities
35  *
36  * @author Lorand Jakab
37  *
38  */
39 public final class AddressTypeMap {
40     private static Map<Short, Class<? extends LispAddressFamily>> afiToAddressTypeMap;
41     private static Map<Short, Class<? extends LispAddressFamily>> lcafToAddressTypeMap;
42
43     private static void initializeAfiMap() {
44         afiToAddressTypeMap = new HashMap<Short, Class<? extends LispAddressFamily>>();
45         afiToAddressTypeMap.put((short) 0, NoAddressAfi.class);
46         afiToAddressTypeMap.put((short) AddressFamily.IpV4.getIntValue(), Ipv4Afi.class);
47         afiToAddressTypeMap.put((short) (AddressFamily.IpV4.getIntValue() * -1), Ipv4PrefixAfi.class);
48         afiToAddressTypeMap.put((short) AddressFamily.IpV6.getIntValue(), Ipv6Afi.class);
49         afiToAddressTypeMap.put((short) (AddressFamily.IpV6.getIntValue() * -1), Ipv6PrefixAfi.class);
50         afiToAddressTypeMap.put((short) AddressFamily.DistinguishedName.getIntValue(), DistinguishedNameAfi.class);
51         afiToAddressTypeMap.put((short) AddressFamily.AsNumber.getIntValue(), AsNumberAfi.class);
52         afiToAddressTypeMap.put((short) AddressFamily.LispCanonicalAddressFormat.getIntValue(), Lcaf.class);
53         afiToAddressTypeMap.put((short) AddressFamily._48BitMac.getIntValue(), MacAfi.class);
54     }
55
56     private static void initializeLcafMap() {
57         lcafToAddressTypeMap = new HashMap<Short, Class<? extends LispAddressFamily>>();
58         lcafToAddressTypeMap.put((short) LispCanonicalAddressFormatEnum.LIST.getLispCode(), AfiListLcaf.class);
59         lcafToAddressTypeMap.put((short) LispCanonicalAddressFormatEnum.SEGMENT.getLispCode(), InstanceIdLcaf.class);
60         lcafToAddressTypeMap.put((short) LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode(),
61                 ApplicationDataLcaf.class);
62         lcafToAddressTypeMap.put((short) LispCanonicalAddressFormatEnum.TRAFFIC_ENGINEERING.getLispCode(),
63                 ExplicitLocatorPathLcaf.class);
64         lcafToAddressTypeMap.put((short) LispCanonicalAddressFormatEnum.SOURCE_DEST.getLispCode(),
65                 SourceDestKeyLcaf.class);
66         lcafToAddressTypeMap.put((short) LispCanonicalAddressFormatEnum.KEY_VALUE.getLispCode(),
67                 KeyValueAddressLcaf.class);
68         // TODO
69     }
70
71     public static Class<? extends LispAddressFamily> getAddressType(short afi) {
72         if (afiToAddressTypeMap == null) {
73             initializeAfiMap();
74         }
75         return afiToAddressTypeMap.get(afi);
76     }
77
78     public static Class<? extends LispAddressFamily> getLcafType(short lcafType) {
79         if (lcafToAddressTypeMap == null) {
80             initializeLcafMap();
81         }
82         return lcafToAddressTypeMap.get(lcafType);
83     }
84
85     public static Class <? extends LispAddressFamily> getSimpleAddressInnerType(SimpleAddress address) {
86         if (address == null) {
87             return null;
88         } else if (address.getIpAddress() != null) {
89             if (address.getIpAddress().getIpv4Address() != null) {
90                 return Ipv4Afi.class;
91             } else if (address.getIpAddress().getIpv6Address() != null) {
92                 return Ipv6Afi.class;
93             }
94         } else if (address.getIpPrefix() != null) {
95             if (address.getIpPrefix().getIpv4Prefix() != null) {
96                 return Ipv4PrefixAfi.class;
97             } else if (address.getIpPrefix().getIpv6Prefix() != null) {
98                 return Ipv6PrefixAfi.class;
99             }
100         } else if (address.getMacAddress() != null) {
101             return MacAfi.class;
102         } else if (address.getAsNumber() != null) {
103             return AsNumberAfi.class;
104         } else if (address.getDistinguishedNameType() != null) {
105             return DistinguishedNameAfi.class;
106         }
107         return null;
108     }
109 }