Bug 6278: Use odlparent's karaf-parent
[lispflowmapping.git] / mappingservice / lisp-proto / src / main / java / org / opendaylight / lispflowmapping / lisp / util / SourceDestKeyHelper.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
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 Eid getSrcBinary(Eid eid) {
49         if (eid.getAddress() instanceof SourceDestKey) {
50             return LispAddressUtil.asBinaryEid(((SourceDestKey) eid.getAddress()).getSourceDestKey().getSource(),
51                     eid.getVirtualNetworkId());
52         }
53         return eid;
54     }
55
56     public static Eid getDstBinary(Eid eid) {
57         if (eid.getAddress() instanceof SourceDestKey) {
58             return LispAddressUtil.asBinaryEid(((SourceDestKey) eid.getAddress()).getSourceDestKey().getDest(),
59                     eid.getVirtualNetworkId());
60         }
61         return eid;
62     }
63
64     public static short getSrcMask(Eid eid) {
65         Address addr = eid.getAddress();
66         if (!isSrcDst(addr)) {
67             return 0;
68         }
69         return MaskUtil.getMaskForAddress(((SourceDestKey)addr).getSourceDestKey().getSource());
70     }
71
72     public static short getDstMask(Eid eid) {
73         Address addr = eid.getAddress();
74         if (!isSrcDst(addr)) {
75             return 0;
76         }
77         return MaskUtil.getMaskForAddress(((SourceDestKey)addr).getSourceDestKey().getDest());
78     }
79
80     private static boolean isSrcDst(Address addr) {
81         if (!(addr instanceof SourceDestKey)) {
82             LOG.warn("Address {} is not a valid SourceDest LCAF", addr);
83             return false;
84         }
85         return true;
86     }
87 }