blob: d49c05c0c84de56f5546bcbd442525f801b48600 [file] [log] [blame]
Jian Lif8c2d4a2016-09-15 02:33:12 +09001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.lisp.msg.authentication;
17
18/**
19 * A factory class that returns LISP authentication instance.
20 */
21public final class LispAuthenticationFactory {
22
23 /**
Jian Li5b2b2362016-11-27 17:38:19 +090024 * Prevents object instantiation from external.
25 */
26 private LispAuthenticationFactory() {
27 }
28
29 /**
Jian Lif8c2d4a2016-09-15 02:33:12 +090030 * Obtains a factory singleton instance.
31 *
32 * @return factory singleton instance
33 */
34 public static LispAuthenticationFactory getInstance() {
35 return SingletonHelper.INSTANCE;
36 }
37
38 /**
39 * Generates a new authentication data with given authentication key and
40 * authentication type.
41 *
42 * @param authType authentication key type
43 * @param authKey authentication key string
Jian Liafe2d3f2016-11-01 02:49:07 +090044 * @param data authentication data
Jian Lif8c2d4a2016-09-15 02:33:12 +090045 * @return authentication data
46 */
47 public byte[] createAuthenticationData(LispAuthenticationKeyEnum authType,
Jian Liafe2d3f2016-11-01 02:49:07 +090048 String authKey, byte[] data) {
Jian Lif8c2d4a2016-09-15 02:33:12 +090049 LispMacAuthentication macAuth = new LispMacAuthentication(authType);
Jian Lif8c2d4a2016-09-15 02:33:12 +090050 byte[] authData;
51 switch (authType) {
52 case SHA1:
53 case SHA256:
Jian Liafe2d3f2016-11-01 02:49:07 +090054 authData = macAuth.getAuthenticationData(authKey, data);
Jian Lif8c2d4a2016-09-15 02:33:12 +090055 break;
56 case NONE:
57 case UNKNOWN:
58 default:
59 authData = macAuth.getAuthenticationData();
60 break;
61 }
62 return authData;
63 }
64
65 /**
Jian Li5b2b2362016-11-27 17:38:19 +090066 * A private singleton helper class.
Jian Lif8c2d4a2016-09-15 02:33:12 +090067 */
Jian Li5b2b2362016-11-27 17:38:19 +090068 private static final class SingletonHelper {
Jian Lif8c2d4a2016-09-15 02:33:12 +090069 private static final LispAuthenticationFactory INSTANCE =
Jian Li5b2b2362016-11-27 17:38:19 +090070 new LispAuthenticationFactory();
71 private static final String ILLEGAL_ACCESS_MSG = "Should not instantiate this class.";
72
73 private SingletonHelper() {
74 throw new IllegalAccessError(ILLEGAL_ACCESS_MSG);
75 }
Jian Lif8c2d4a2016-09-15 02:33:12 +090076 }
77}