Checkstyle: fix issues and enforce on lisp-proto
[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  * Helper methods for Source/Dest Key type LCAF addresses.
19  *
20  * @author Lorand Jakab
21  *
22  */
23
24 public final class SourceDestKeyHelper {
25     // Utility class, should not be instantiated
26     private SourceDestKeyHelper() {
27     }
28
29     private static final Logger LOG = LoggerFactory.getLogger(SourceDestKeyHelper.class);
30
31     public static Eid getSrc(Eid eid) {
32         Address addr = eid.getAddress();
33         if (addr instanceof SourceDestKey) {
34             return LispAddressUtil.asEid(((SourceDestKey) addr).getSourceDestKey().getSource(),
35                     eid.getVirtualNetworkId());
36         } else {
37             return eid;
38         }
39     }
40
41     public static Eid getDst(Eid eid) {
42         Address addr = eid.getAddress();
43         if (addr instanceof SourceDestKey) {
44             return LispAddressUtil.asEid(((SourceDestKey) addr).getSourceDestKey().getDest(),
45                     eid.getVirtualNetworkId());
46         } else {
47             return eid;
48         }
49     }
50
51     public static Eid getSrcBinary(Eid eid) {
52         if (eid.getAddress() instanceof SourceDestKey) {
53             return LispAddressUtil.asBinaryEid(((SourceDestKey) eid.getAddress()).getSourceDestKey().getSource(),
54                     eid.getVirtualNetworkId());
55         }
56         return eid;
57     }
58
59     public static Eid getDstBinary(Eid eid) {
60         if (eid.getAddress() instanceof SourceDestKey) {
61             return LispAddressUtil.asBinaryEid(((SourceDestKey) eid.getAddress()).getSourceDestKey().getDest(),
62                     eid.getVirtualNetworkId());
63         }
64         return eid;
65     }
66
67     public static short getSrcMask(Eid eid) {
68         Address addr = eid.getAddress();
69         if (!isSrcDst(addr)) {
70             return 0;
71         }
72         return MaskUtil.getMaskForAddress(((SourceDestKey)addr).getSourceDestKey().getSource());
73     }
74
75     public static short getDstMask(Eid eid) {
76         Address addr = eid.getAddress();
77         if (!isSrcDst(addr)) {
78             return 0;
79         }
80         return MaskUtil.getMaskForAddress(((SourceDestKey)addr).getSourceDestKey().getDest());
81     }
82
83     private static boolean isSrcDst(Address addr) {
84         if (!(addr instanceof SourceDestKey)) {
85             LOG.warn("Address {} is not a valid SourceDest LCAF", addr);
86             return false;
87         }
88         return true;
89     }
90 }