Bug 2231 - Secure transport for PCEP
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / tls / SslKeyStore.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, 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.protocol.pcep.impl.tls;
9
10 import java.io.File;
11 import java.io.FileInputStream;
12 import java.io.FileNotFoundException;
13 import java.io.InputStream;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.impl.rev130627.PathType;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 public final class SslKeyStore {
19
20     private static final Logger LOG = LoggerFactory.getLogger(SslKeyStore.class);
21
22     private SslKeyStore() {
23         throw new UnsupportedOperationException("Utility class shouldn't be instantiated");
24     }
25
26     /**
27      * InputStream instance of key - key location is on classpath or specific path
28      *
29      * @param filename
30      *          keystore location
31      * @param pathType
32      *          keystore location type - "classpath" or "path"
33      *
34      * @return key as InputStream
35      */
36     public static InputStream asInputStream(final String filename, final PathType pathType) {
37         InputStream in;
38         switch (pathType) {
39         case CLASSPATH:
40             in = SslKeyStore.class.getResourceAsStream(filename);
41             if (in == null) {
42                 throw new IllegalStateException("KeyStore file not found: "
43                         + filename);
44             }
45             break;
46         case PATH:
47             LOG.debug("Current dir using System: {}", System.getProperty("user.dir"));
48             final File keystorefile = new File(filename);
49             try {
50                 in = new FileInputStream(keystorefile);
51             } catch (FileNotFoundException e) {
52                 throw new IllegalStateException("KeyStore file not found: "
53                         + filename,e);
54             }
55             break;
56         default:
57             throw new IllegalArgumentException("Unknown path type: " + pathType);
58         }
59         return in;
60     }
61 }
62