eeccf601f5e24ed1dc8b6be3f5dffe0c26febc8a
[ccsdk/features.git] /
1 /*******************************************************************************
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.config.impl;
19
20 import com.typesafe.config.Config;
21 import com.typesafe.config.ConfigFactory;
22 import java.io.File;
23 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.util.ClusterConfig;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class AkkaConfig {
28
29     @SuppressWarnings("unused")
30     private static final Logger LOG = LoggerFactory.getLogger(AkkaConfig.class);
31
32     private static final String DEFAULT_FILENAME = "configuration/initial/akka.conf";
33     private final String filename;
34     private ClusterConfig cluserConfig;
35
36     public ClusterConfig getClusterConfig() {
37         return this.cluserConfig;
38     }
39
40     private AkkaConfig(String filename) {
41         this.filename = filename;
42     }
43
44     public AkkaConfig() {
45         this(null);
46     }
47
48     @Override
49     public String toString() {
50         return "AkkaConfig [filename=" + filename + ", cluserConfig=" + cluserConfig + "]";
51     }
52
53     private void loadFromFile() throws Exception {
54         Config cfg = ConfigFactory.parseFile(new File(this.filename));
55         this.cluserConfig = new ClusterConfig(cfg.getConfig("odl-cluster-data").getConfig("akka").getConfig("cluster"));
56     }
57
58     public boolean isCluster() {
59         return this.cluserConfig != null ? this.cluserConfig.isCluster() : false;
60     }
61
62     public boolean isClusterAndFirstNode() {
63         return isSingleNode() || isCluster() && getClusterConfig().getRoleMemberIndex() == 1;
64     }
65
66     public static AkkaConfig load() throws Exception {
67         return load(DEFAULT_FILENAME);
68     }
69
70     public static AkkaConfig load(String filename) throws Exception {
71         AkkaConfig cfg = new AkkaConfig(filename);
72         cfg.loadFromFile();
73         return cfg;
74     }
75
76     public boolean isSingleNode() {
77         return !this.isCluster();
78     }
79      public static AkkaConfig parse(String content) throws Exception {
80         Config cfg = ConfigFactory.parseString(content);
81         AkkaConfig c = new AkkaConfig();
82         c.cluserConfig=new ClusterConfig(cfg.getConfig("odl-cluster-data").getConfig("akka").getConfig("cluster"));
83         return c;
84     }
85 }