3ec866e29cdbcf19e56b0a22f76adf7ddd169911
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / security / portal / config / RolesConfig.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.security.portal.config;
24
25 import java.io.IOException;
26 import java.lang.reflect.Type;
27 import java.net.URISyntaxException;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import java.util.Collections;
31 import java.util.List;
32
33 import org.onap.aai.sparky.viewandinspect.config.TierSupportUiConstants;
34 import org.openecomp.portalsdk.core.restful.domain.EcompRole;
35
36 import com.google.gson.Gson;
37 import com.google.gson.JsonSyntaxException;
38 import com.google.gson.reflect.TypeToken;
39
40 /**
41  * Provides roles configuration.
42  */
43 public class RolesConfig {
44
45   private List<EcompRole> roles;
46
47   private static final Gson GSON = new Gson();
48   private static final String ROLES_CONFIG_FILE = TierSupportUiConstants.ROLES_FILE_LOCATION;
49
50   private RolesConfig() {
51     // Prevent instantiation
52   }
53
54   private static class RolesConfigHelper {
55     private static final RolesConfig INSTANCE = new RolesConfig();
56
57     private RolesConfigHelper() {
58       // Deliberately empty
59     }
60   }
61
62   /**
63    * Get a singleton instance of the configuration.
64    *
65    * @return
66    */
67   public static RolesConfig getInstance() {
68     try {
69       RolesConfigHelper.INSTANCE.load();
70     } catch (Exception e) {
71       throw new ExceptionInInitializerError(e);
72     }
73
74     return RolesConfigHelper.INSTANCE;
75   }
76
77   public List<EcompRole> getRoles() {
78     return roles;
79   }
80
81   private void load() throws JsonSyntaxException, IOException, URISyntaxException {
82     Type collectionType = new TypeToken<List<EcompRole>>() {}.getType();
83
84     roles = Collections.unmodifiableList(GSON
85         .fromJson(new String(Files.readAllBytes(Paths.get(ROLES_CONFIG_FILE))), collectionType));
86   }
87 }