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