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