2fdb0a822be03a4c99794d9b1d391de858f928d2
[cli.git] / framework / src / main / java / org / onap / cli / fw / conf / OnapCommandConfg.java
1 /*
2  * Copyright 2017 Huawei Technologies Co., Ltd.
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  */
16
17 package org.onap.cli.fw.conf;
18
19 import java.io.IOException;
20 import java.util.Arrays;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Properties;
25 import java.util.Set;
26 import java.util.UUID;
27 import java.util.stream.Collectors;
28
29 /**
30  * Onap command constants.
31  *
32  */
33 public final class OnapCommandConfg {
34
35     private static Properties prps = new Properties();
36
37     /**
38      * Private constructor.
39      */
40     private OnapCommandConfg() {
41
42     }
43
44     static {
45         try {
46             prps.load(OnapCommandConfg.class.getClassLoader().getResourceAsStream(Constants.CONF));
47         } catch (IOException e) {
48             throw new RuntimeException(e); // NOSONAR
49         }
50     }
51
52     /**
53      * is auth service ignored.
54      *
55      * @return boolean
56      */
57     public static boolean isAuthIgnored() {
58         if ("true".equals(prps.getProperty(Constants.ONAP_IGNORE_AUTH))) {
59             return true;
60         }
61
62         return false;
63     }
64
65     public static String getVersion() {
66         return prps.getProperty(Constants.ONAP_CLI_VERSION);
67     }
68
69     /**
70      * checks if cookies based auth.
71      *
72      * @return boolean
73      */
74     public static boolean isCookiesBasedAuth() {
75         if ("true".equals(prps.getProperty(Constants.HTTP_API_KEY_USE_COOKIES))) {
76             return true;
77         }
78
79         return false;
80     }
81
82     public static String getXAuthTokenName() {
83         return prps.getProperty(Constants.SERVICE_AUTH_BASIC_HTTP_HEADERS + "." + Constants.X_AUTH_TOKEN);
84     }
85
86     public static String getInternalCmd() {
87         return prps.getProperty(Constants.SERVICE_NAME);
88     }
89
90     public static String getApiGateway() {
91         return prps.getProperty(Constants.API_GATEWAY);
92     }
93
94     public static String getAuthService() {
95         return prps.getProperty(Constants.AUTH_SERVICE);
96     }
97
98     public static String getAuthType() {
99         return prps.getProperty(Constants.SERVICE_AUTH, Constants.AUTH_BASIC);
100     }
101
102     public static Map<String, String> getBasicCommonHeaders() {
103         Map<String, String> mapHeaders = new HashMap<String, String> ();
104
105         Arrays.stream(prps.getProperty(Constants.SERVICE_AUTH_BASIC_HTTP_HEADERS)  // NOSONAR
106                 .split(",")).map(String::trim).forEach(header -> {
107                     String headerName = prps.getProperty(Constants.SERVICE_AUTH_BASIC_HTTP_HEADERS + "." + header);
108                     String headerValue = prps.getProperty(Constants.SERVICE_AUTH_BASIC_HTTP_HEADERS + "." + header + ".value", null);
109                     if (headerValue != null) {
110                         headerValue = headerValue.replaceAll("uuid", UUID.randomUUID().toString());
111                     }
112                     mapHeaders.put(headerName, headerValue);
113                 });
114
115         return mapHeaders;
116     }
117
118     public static Set<String> getExcludeParamsForInternalCmd() {
119         return Arrays.stream(prps.getProperty(Constants.EXCLUDE_PARAMS_INTERNAL_CMD)  // NOSONAR
120                 .split(",")).map(String::trim).collect(Collectors.toSet());
121     }
122
123     public static Set<String> getIncludeParamsForNoAuthDisableExternalCmd() {
124         return Arrays.stream(prps.getProperty(Constants.NO_AUTH_DISABLE_INCLUDE_PARAMS_EXTERNAL_CMD)  // NOSONAR
125                 .split(",")).map(String::trim).collect(Collectors.toSet());
126     }
127
128     public static Set<String> getExcludeParamsForNoAuthEnableExternalCmd() {
129         return Arrays.stream(prps.getProperty(Constants.NO_AUTH_ENABLE_EXCLUDE_PARAMS_EXTERNAL_CMD)  // NOSONAR
130                 .split(",")).map(String::trim).collect(Collectors.toSet());
131     }
132
133     public static Set<String> getIncludeParamsForNoAuthEnableExternalCmd() {
134         return Arrays.stream(prps.getProperty(Constants.NO_AUTH_ENABLE_INCLUDE_PARAMS_EXTERNAL_CMD)  // NOSONAR
135                 .split(",")).map(String::trim).collect(Collectors.toSet());
136     }
137
138     public static List<String> getSchemaAttrInfo(String key) {
139         return Arrays.stream(prps.getProperty(key).split(",")).map(String::trim).collect(Collectors.toList());  // NOSONAR
140     }
141
142 }