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   
9   package org.opendaylight.lispflowmapping.lisp.util;
10  
11  import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
12  import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey;
13  import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
14  import org.slf4j.Logger;
15  import org.slf4j.LoggerFactory;
16  
17  /**
18   * @author Lorand Jakab
19   *
20   */
21  
22  public final class SourceDestKeyHelper {
23      // Utility class, should not be instantiated
24      private SourceDestKeyHelper() {
25      }
26  
27      private final static Logger LOG = LoggerFactory.getLogger(SourceDestKeyHelper.class);
28      public static Eid getSrc(Eid eid) {
29          Address addr = eid.getAddress();
30          if (addr instanceof SourceDestKey) {
31              return LispAddressUtil.asEid(((SourceDestKey) addr).getSourceDestKey().getSource(),
32                      eid.getVirtualNetworkId());
33          } else {
34              return eid;
35          }
36      }
37  
38      public static Eid getDst(Eid eid) {
39          Address addr = eid.getAddress();
40          if (addr instanceof SourceDestKey) {
41              return LispAddressUtil.asEid(((SourceDestKey) addr).getSourceDestKey().getDest(),
42                      eid.getVirtualNetworkId());
43          } else {
44              return eid;
45          }
46      }
47  
48      public static short getSrcMask(Eid eid) {
49          Address addr = eid.getAddress();
50          if (!isSrcDst(addr)) {
51              return 0;
52          }
53          return MaskUtil.getMaskForAddress(((SourceDestKey)addr).getSourceDestKey().getSource());
54      }
55  
56      public static short getDstMask(Eid eid) {
57          Address addr = eid.getAddress();
58          if (!isSrcDst(addr)) {
59              return 0;
60          }
61          return MaskUtil.getMaskForAddress(((SourceDestKey)addr).getSourceDestKey().getDest());
62      }
63  
64      private static boolean isSrcDst(Address addr) {
65          if (!(addr instanceof SourceDestKey)) {
66              LOG.warn("Address {} is not a valid SourceDest LCAF", addr);
67              return false;
68          }
69          return true;
70      }
71  }