JUnit Test - LispSouthboundStatsTest
[lispflowmapping.git] / mappingservice / southbound / src / main / java / org / opendaylight / lispflowmapping / southbound / authentication / LispAuthenticationUtil.java
1 /*
2  * Copyright (c) 2014 Contextream, Inc. and others.  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.southbound.authentication;
9
10 import java.nio.ByteBuffer;
11 import org.opendaylight.lispflowmapping.interfaces.lisp.ILispAuthentication;
12 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapNotify;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container.MappingAuthkey;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public final class LispAuthenticationUtil {
21     protected static final Logger LOG = LoggerFactory.getLogger(LispAuthenticationUtil.class);
22
23     // Utility class, should not be instantiated
24     private LispAuthenticationUtil() {
25     }
26
27     private static ILispAuthentication resolveAuthentication(final MapRegister mapRegister, final Eid eid, final
28             MappingAuthkey key) {
29         if (key == null) {
30             LOG.warn("Authentication failed: mapping authentication key is null");
31             return null;
32         }
33         short keyId = 0;
34         if (mapRegister.getKeyId() != null) {
35             keyId = mapRegister.getKeyId();
36         }
37         if (keyId != key.getKeyType().shortValue()) {
38             LOG.warn("Authentication failed: key-ID in Map-Register is different from the one on file for {}",
39                     LispAddressStringifier.getString(eid));
40             return null;
41         }
42         return LispAuthenticationFactory.getAuthentication(LispKeyIDEnum.valueOf(keyId));
43     }
44
45
46     public static boolean validate(MapRegister mapRegister, ByteBuffer byteBuffer, Eid eid, MappingAuthkey key) {
47         final ILispAuthentication authentication = resolveAuthentication(mapRegister, eid, key);
48         return authentication == null ? false : authentication.validate(byteBuffer, mapRegister.getAuthenticationData(),
49                 key.getKeyString());
50     }
51
52     public static byte[] createAuthenticationData(MapNotify mapNotify, String key) {
53         // We assume that the key-ID is correctly set in the Map-Notify message
54         ILispAuthentication authentication = LispAuthenticationFactory.getAuthentication(
55                 LispKeyIDEnum.valueOf(mapNotify.getKeyId()));
56         return authentication.getAuthenticationData(mapNotify, key);
57     }
58 }