changed the header license to new license
[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-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 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 package org.onap.aai.sparky.security.portal.config;
22
23 import java.io.IOException;
24 import java.lang.reflect.Type;
25 import java.net.URISyntaxException;
26 import java.nio.file.Files;
27 import java.nio.file.Paths;
28 import java.util.Collections;
29 import java.util.List;
30
31 import org.onap.aai.sparky.viewandinspect.config.SparkyConstants;
32 import org.openecomp.portalsdk.core.restful.domain.EcompRole;
33
34 import com.google.gson.Gson;
35 import com.google.gson.JsonSyntaxException;
36 import com.google.gson.reflect.TypeToken;
37
38 /**
39  * Provides roles configuration.
40  */
41 public class RolesConfig {
42
43   private List<EcompRole> roles;
44
45   private static final Gson GSON = new Gson();
46   private static final String ROLES_CONFIG_FILE = SparkyConstants.ROLES_FILE_LOCATION;
47
48   private RolesConfig() {
49     // Prevent instantiation
50   }
51
52   private static class RolesConfigHelper {
53     private static final RolesConfig INSTANCE = new RolesConfig();
54
55     private RolesConfigHelper() {
56       // Deliberately empty
57     }
58   }
59
60   /**
61    * Get a singleton instance of the configuration.
62    *
63    * @return
64    */
65   public static RolesConfig getInstance() {
66     try {
67       RolesConfigHelper.INSTANCE.load();
68     } catch (Exception e) {
69       throw new ExceptionInInitializerError(e);
70     }
71
72     return RolesConfigHelper.INSTANCE;
73   }
74
75   public List<EcompRole> getRoles() {
76     return roles;
77   }
78
79   private void load() throws JsonSyntaxException, IOException, URISyntaxException {
80     Type collectionType = new TypeToken<List<EcompRole>>() {
81     }.getType();
82
83     roles = Collections.unmodifiableList(GSON
84         .fromJson(new String(Files.readAllBytes(Paths.get(ROLES_CONFIG_FILE))), collectionType));
85   }
86 }