8873d217ed603681f4b2a9c04b218b618e07a401
[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 class SourceDestKeyHelper {
23     private final static Logger LOG = LoggerFactory.getLogger(SourceDestKeyHelper.class);
24     public static Eid getSrc(Eid eid) {
25         Address addr = eid.getAddress();
26         if (addr instanceof SourceDestKey) {
27             return LispAddressUtil.toEid(eid, ((SourceDestKey)addr).getSourceDestKey().getSource());
28         } else {
29             return eid;
30         }
31     }
32
33     public static Eid getDst(Eid eid) {
34         Address addr = eid.getAddress();
35         if (addr instanceof SourceDestKey) {
36             return LispAddressUtil.toEid(eid, ((SourceDestKey)addr).getSourceDestKey().getDest());
37         } else {
38             return eid;
39         }
40     }
41
42     public static short getSrcMask(Eid eid) {
43         Address addr = eid.getAddress();
44         if (!isSrcDst(addr)) {
45             return 0;
46         }
47         return MaskUtil.getMaskForIpPrefix(((SourceDestKey)addr).getSourceDestKey().getSource());
48     }
49
50     public static short getDstMask(Eid eid) {
51         Address addr = eid.getAddress();
52         if (!isSrcDst(addr)) {
53             return 0;
54         }
55         return MaskUtil.getMaskForIpPrefix(((SourceDestKey)addr).getSourceDestKey().getDest());
56     }
57
58     private static boolean isSrcDst(Address addr) {
59         if (!(addr instanceof SourceDestKey)) {
60             LOG.warn("Address {} is not a valid SourceDest LCAF", addr);
61             return false;
62         }
63         return true;
64     }
65 }