View Javadoc

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 org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier.Destination;
11  import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
12  
13  import com.google.common.base.Preconditions;
14  
15  /**
16   * Utility class with static methods returning string representations of
17   * supported LISP simple address types
18   *
19   * @author Lorand Jakab
20   *
21   */
22  public final class LispSimpleAddressStringifier {
23      // Utility class, should not be instantiated
24      private LispSimpleAddressStringifier() {
25      }
26  
27      public static String getString(SimpleAddress addr) {
28          return getString(Destination.USER, addr);
29      }
30  
31      public static String getString(Destination dst, SimpleAddress addr) {
32          Preconditions.checkNotNull(addr, "address should not be null");
33  
34          if (addr.getIpAddress() != null) {
35              if (addr.getIpAddress().getIpv4Address() != null) {
36                  return addr.getIpAddress().getIpv4Address().getValue();
37              } else if (addr.getIpAddress().getIpv6Address() != null) {
38                  return addr.getIpAddress().getIpv6Address().getValue();
39              }
40          } else if (addr.getIpPrefix() != null) {
41              if (addr.getIpPrefix().getIpv4Prefix() != null) {
42                  return addr.getIpPrefix().getIpv4Prefix().getValue();
43              } else if (addr.getIpPrefix().getIpv6Prefix() != null) {
44                  return addr.getIpPrefix().getIpv6Prefix().getValue();
45              }
46          } else if (addr.getMacAddress() != null) {
47              return addr.getMacAddress().getValue();
48          } else if (addr.getDistinguishedNameType() != null) {
49              return (addr.getDistinguishedNameType().getValue());
50          } else if (addr.getAsNumber() != null) {
51              return "AS" + addr.getAsNumber().getValue();
52          }
53  
54          return null;
55      }
56  
57      protected static String getURLPrefix(SimpleAddress addr) {
58          Preconditions.checkNotNull(addr, "address should not be null");
59  
60          if (addr.getIpAddress() != null) {
61              if (addr.getIpAddress().getIpv4Address() != null) {
62                  return "ipv4";
63              } else if (addr.getIpAddress().getIpv6Address() != null) {
64                  return "ipv6";
65              }
66          } else if (addr.getIpPrefix() != null) {
67              if (addr.getIpPrefix().getIpv4Prefix() != null) {
68                  return "ipv4";
69              } else if (addr.getIpPrefix().getIpv6Prefix() != null) {
70                  return "ipv6";
71              }
72          } else if (addr.getMacAddress() != null) {
73              return "mac";
74          } else if (addr.getDistinguishedNameType() != null) {
75              return "dn";
76          } else if (addr.getAsNumber() != null) {
77              return "as";
78          }
79  
80          return null;
81      }
82  
83  }