aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/websocketmanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager/utils/AkkaConfig.java
blob: 7bbbfea6d596bae6bb2c3073cc7978cb8c7c5702 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * ============LICENSE_START========================================================================
 * ONAP : ccsdk feature sdnr wt
 * =================================================================================================
 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
 * =================================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 * ============LICENSE_END==========================================================================
 */
package org.onap.ccsdk.features.sdnr.wt.websocketmanager.utils;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.onap.ccsdk.features.sdnr.wt.websocketmanager.WebSocketManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AkkaConfig {

    private static final Logger LOG = LoggerFactory.getLogger(WebSocketManager.class.getName());

    public static class ClusterNodeInfo {
        @Override
        public String toString() {
            return "ClusterNodeInfo [protocol=" + protocol + ", clusterName=" + clusterName + ", remoteAdr=" + remoteAdr
                    + ", port=" + port + "]";
        }

        private final String protocol;
        private final String clusterName;
        private final String remoteAdr;
        private final int port;

        public String getRemoteAddress() {
            return this.remoteAdr;
        }

        public ClusterNodeInfo(String s) throws Exception {
            final String regex = "([a-z.]{0,10}):\\/\\/([a-zA-Z0-9-]{0,1024})@([a-zA-Z0-9.-]{0,1024}):([0-9]{0,10})";
            final Pattern pattern = Pattern.compile(regex);
            final Matcher matcher = pattern.matcher(s);
            if (!matcher.find()) {
                throw new Exception("invalid seedNode format");
            }
            this.protocol = matcher.group(1);
            this.clusterName = matcher.group(2);
            this.remoteAdr = matcher.group(3);
            this.port = Integer.parseInt(matcher.group(4));
        }
    }
    public static class ClusterRoleInfo {
        @Override
        public String toString() {
            return "ClusterRoleInfo [Role=" + Role + ", Index=" + Index + "]";
        }

        private final String Role;
        private final int Index;

        public ClusterRoleInfo(String s) throws Exception {
            // role with minimum 1 character
            // index with minimum 1 character or Integer.parseInt raise an exception
            // index with maximum 10 because it's an integer
            final String regex = "([a-z]{1,1024})-([0-9]{1,10})";
            final Pattern pattern = Pattern.compile(regex);
            final Matcher matcher = pattern.matcher(s);
            if (!matcher.find()) {
                throw new Exception("invalid role format");
            }
            this.Role = matcher.group(1);
            this.Index = Integer.parseInt(matcher.group(2));
        }

    }
    public static class ClusterConfig {
        @Override
        public String toString() {
            return "ClusterConfig [seedNodes=" + seedNodes + ", roles=" + roles + "]";
        }

        private final List<ClusterNodeInfo> seedNodes;
        private final List<ClusterRoleInfo> roles;
        private final ClusterNodeInfo ismeInfo;

        public ClusterConfig(Config o) throws Exception {
            {
                this.seedNodes = new ArrayList<>();
                List<String> a = o.getStringList("seed-nodes");
                for (int i = 0; i < a.size(); i++) {
                    ClusterNodeInfo info = new ClusterNodeInfo(a.get(i));
                    this.seedNodes.add(info);
                }
                this.roles = new ArrayList<>();
                a = o.getStringList("roles");
                for (int i = 0; i < a.size(); i++) {
                    ClusterRoleInfo s = new ClusterRoleInfo(a.get(i));
                    this.roles.add(s);
                }
                int idx = this.roles.get(0).Index - 1;
                if (idx >= 0 && idx < this.seedNodes.size()) {
                    this.ismeInfo = this.seedNodes.get(idx);
                } else {
                    this.ismeInfo = null;
                }
            }

        }

        public boolean isCluster() {
            return this.seedNodes != null ? this.seedNodes.size() > 1 : false;
        }

        public boolean isMe(ClusterNodeInfo i) {
            return this.ismeInfo != null ? this.ismeInfo.equals(i) : false;
        }

        public List<ClusterNodeInfo> getSeedNodes() {
            return this.seedNodes;
        }
    }

    private static final String DEFAULT_FILENAME = "configuration/initial/akka.conf";
    private final File file;
    private final String resourceFilename;
    private final String fileContent;
    private ClusterConfig cluserConfig;

    public ClusterConfig getClusterConfig() {
        return this.cluserConfig;
    }

    private AkkaConfig(File file, boolean isResource) {
        this.file = isResource ? null : file;
        this.fileContent = null;
        this.resourceFilename = isResource ? file.getName() : null;
    }

    private AkkaConfig(String fileContent) {
        this.file = null;
        this.fileContent = fileContent;
        this.resourceFilename = null;
    }


    @Override
    public String toString() {
        return "AkkaConfig [filename=" + file + ", cluserConfig=" + cluserConfig + "]";
    }

    private void loadFromFile() throws Exception {
        Config cfg = null;
        if (this.file != null) {
            cfg = ConfigFactory.parseFile(this.file);
        } else if (this.fileContent != null) {
            cfg = ConfigFactory.parseString(this.fileContent);
        } else if (this.resourceFilename != null) {
            cfg = ConfigFactory.parseResources(this.getClass(), this.resourceFilename);
        }

        if (cfg != null) {
            this.cluserConfig =
                    new ClusterConfig(cfg.getConfig("odl-cluster-data").getConfig("akka").getConfig("cluster"));
        } else {
            LOG.warn("unable to parse config file");
            this.cluserConfig = null;
        }
    }

    public boolean isCluster() {
        return this.cluserConfig != null && this.cluserConfig.isCluster();
    }

    public static AkkaConfig load() throws Exception {
        return load(DEFAULT_FILENAME);
    }

    public static AkkaConfig load(String filename) throws Exception {
        return load(filename, false);
    }

    public static AkkaConfig load(String filename, boolean isResource) throws Exception {
        AkkaConfig cfg = new AkkaConfig(new File(filename), isResource);
        cfg.loadFromFile();

        return cfg;
    }

    public static AkkaConfig loadContent(String content) throws Exception {
        AkkaConfig cfg = new AkkaConfig(content);
        cfg.loadFromFile();

        return cfg;
    }



}