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