Adding interfaces in documentation
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / util / Encryptor.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.onap.aai.sparky.util;
26
27 import org.apache.commons.cli.BasicParser;
28 import org.apache.commons.cli.CommandLine;
29 import org.apache.commons.cli.CommandLineParser;
30 import org.apache.commons.cli.Options;
31 import org.apache.commons.cli.ParseException;
32 import org.eclipse.jetty.util.security.Password;
33 import org.onap.aai.cl.api.Logger;
34 import org.onap.aai.cl.eelf.LoggerFactory;
35 import org.onap.aai.sparky.logging.AaiUiMsgs; 
36
37 /**
38  * The Class Encryptor.
39  */
40 public class Encryptor {
41
42   private static final Logger LOG = LoggerFactory.getInstance().getLogger(Encryptor.class);
43   /**
44    * Instantiates a new encryptor.
45    */
46   public Encryptor() {  
47   }
48   
49   /**
50    * Encrypt value.
51    *
52    * @param value to encrypt
53    * @return the encrypted string
54    */
55   public String encryptValue(String value) {
56     String encyptedValue = "";
57     try {
58       encyptedValue = Password.obfuscate(value);
59     } catch (Exception exc) {
60       LOG.error(AaiUiMsgs.ENCRYPTION_ERROR, value, exc.toString());
61     }
62     return encyptedValue;
63   }
64   
65   /**
66    * Decrypt value.
67    *
68    * @param value the value
69    * @return the string
70    */
71   public String decryptValue(String value) {
72     String decyptedValue = "";
73     try {
74       decyptedValue = Password.deobfuscate(value);
75     } catch (Exception exc) {
76       LOG.error(AaiUiMsgs.DECRYPTION_ERROR, value, exc.toString());
77     }
78
79     return decyptedValue;
80   }
81
82   /**
83    * Usage.
84    */
85   public static void usage() {
86     usage(null);
87   }
88
89   /**
90    * Usage.
91    *
92    * @param msg the msg
93    */
94   public static void usage(String msg) {
95     if (msg != null) {
96       System.err.println(msg);
97     }
98     System.err.println("Usage: java Encryptor -e value");
99     System.err.println("\tEncrypt the given value");
100     System.err.println("Usage: java Encryptor -d value");
101     System.err.println("\tDecrypt the given value");
102     System.exit(1);
103   }
104
105   /**
106    * The main method.
107    *
108    * @param args the arguments
109    */
110   public static void main(String[] args) {
111
112     Options options = new Options();
113     options.addOption("d", true, "value to decrypt");
114     options.addOption("h", false, "show help");
115     options.addOption("?", false, "show help");
116
117     String value = null;
118     boolean encrypt = false;
119     boolean decrypt = false;
120
121     CommandLineParser parser = new BasicParser();
122     CommandLine cmd = null;
123
124     try {
125       cmd = parser.parse(options, args);
126
127       if (cmd.hasOption("d")) {
128         value = cmd.getOptionValue("d");
129         decrypt = true;
130       }
131
132       if (cmd.hasOption("?") || cmd.hasOption("h")) {
133         usage();
134         System.exit(0);
135       }
136
137       if ((encrypt && decrypt) || (!encrypt && !decrypt)) {
138         usage("Must specify one (and only one) of the -e or -d options");
139       }
140
141       Encryptor encryptor = new Encryptor();
142
143       if (decrypt) {
144         String out = encryptor.decryptValue(value);
145         System.out.println(out);
146       }
147     } catch (ParseException exc) {
148       System.out.println("Failed to parse command line properties: " + exc.toString());
149     } catch (Exception exc) {
150       System.out.println("Failure: " + exc.toString());
151     }
152
153     System.exit(0);
154   }
155 }