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