aboutsummaryrefslogtreecommitdiffstats
path: root/cadi/core/src/main/java/org/onap/ccsdk/apps/cadi/config/RegistrationPropHolder.java
blob: 17d0279ed240bc7a7eb1c9d809e3b8b8d8b8f770 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
 * ============LICENSE_START====================================================
 * org.onap.ccsdk
 * ===========================================================================
 * Copyright (c) 2023 AT&T 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.apps.cadi.config;

import java.net.Inet4Address;
import java.net.UnknownHostException;

import org.onap.ccsdk.apps.cadi.Access;
import org.onap.ccsdk.apps.cadi.Access.Level;
import org.onap.ccsdk.apps.cadi.CadiException;
import org.onap.ccsdk.apps.cadi.util.Split;

public class RegistrationPropHolder {
    private final String PUBLIC_NAME="%NS.%N";
    private final String REGI="RegistrationProperty: %s='%s'";
    private final Access access;
    public String hostname;
    private int port;
    public String public_fqdn;
    private Integer public_port;
    public Float latitude;
    public Float longitude;
    public final String default_fqdn;
    public final String default_container_ns;
    public final String default_name;
    public final String lentries;
    public final String lcontainer;
    public final String default_container;
    private static boolean firstlog = true;

    public RegistrationPropHolder(final Access access, final int port) throws UnknownHostException, CadiException {
        this.access = access;
        StringBuilder errs = new StringBuilder();
        String str;
        this.port = port;

        lentries=access.getProperty(Config.AAF_LOCATOR_ENTRIES,"");
        default_container = access.getProperty(Config.AAF_LOCATOR_CONTAINER, "");
        if(firstlog) {
            access.printf(Level.INIT, REGI,"default_container",default_container);
        }
        if(!default_container.isEmpty()) {
            lcontainer=',' + default_container; // "" makes a blank default Public Entry
            str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT+'.'+default_container, null);
            if(str==null) {
                str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT, null);
            }
        } else {
            lcontainer=default_container;
            str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT, null);
        }
        if(str!=null) {
            public_port=Integer.decode(str);
        }
        if(firstlog) {
            access.printf(Level.INIT, "RegistrationProperty: public_port='%d'",public_port);
        }

        hostname = access.getProperty(Config.HOSTNAME, null);
        if (hostname==null) {
            hostname = Inet4Address.getLocalHost().getHostName();
        }
        if (hostname==null) {
            mustBeDefined(errs,Config.HOSTNAME);
        }
        if(firstlog) {
            access.printf(Level.INIT, REGI,"hostname",hostname);
        }

        public_fqdn = access.getProperty(Config.AAF_LOCATOR_PUBLIC_FQDN, hostname);
        if(firstlog) {
            access.printf(Level.INIT, REGI,"public_fqdn",public_fqdn);
        }

        // Allow Container to reset the standard name for public
        String container_public_name = access.getProperty(Config.AAF_LOCATOR_PUBLIC_NAME+'.'+default_container, null);
        if(container_public_name==null) {
            container_public_name = access.getProperty(Config.AAF_LOCATOR_PUBLIC_NAME, null);
            if(container_public_name==null) {
                container_public_name = access.getProperty(Config.AAF_LOCATOR_NAME, PUBLIC_NAME);
            }
        }
        default_name = container_public_name;

        if(firstlog) {
            access.printf(Level.INIT, REGI,"default_name",default_name);
        }

        latitude=null;
        String slatitude = access.getProperty(Config.CADI_LATITUDE, null);
        if(slatitude == null) {
            mustBeDefined(errs,Config.CADI_LATITUDE);
        } else {
            latitude = Float.parseFloat(slatitude);
        }
        if(firstlog) {
            access.printf(Level.INIT, REGI,"latitude",slatitude);
        }

        longitude=null;
        String slongitude = access.getProperty(Config.CADI_LONGITUDE, null);
        if(slongitude == null) {
            mustBeDefined(errs,Config.CADI_LONGITUDE);
        } else {
            longitude = Float.parseFloat(slongitude);
        }
        if(firstlog) {
            access.printf(Level.INIT, REGI,"longitude",slongitude);
        }

        String dot_le;
        // Note: only one of the ports can be public...  Therefore, only the last
        for(String le : Split.splitTrim(',', lcontainer)) {
            dot_le = le.isEmpty()?le :"."+le;
            str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_FQDN+dot_le,null);
            if( str != null && !str.isEmpty()) {
                public_fqdn=str;
                if(firstlog) {
                    access.printf(Level.INIT, "RegistrationProperty: public_hostname(overloaded by %s)='%s'",dot_le,public_fqdn);
                }
            }
        }

        default_fqdn = access.getProperty(Config.AAF_LOCATOR_FQDN, hostname);
        if(firstlog) {
            access.printf(Level.INIT, REGI,"default_fqdn",default_fqdn);
        }
        default_container_ns = access.getProperty(Config.AAF_LOCATOR_CONTAINER_NS,"");
        if(firstlog) {
            access.printf(Level.INIT, REGI,"default_container_ns",default_container_ns);
        }
        if(errs.length()>0) {
            throw new CadiException(errs.toString());
        }
        firstlog = false;
    }

    private void mustBeDefined(StringBuilder errs, String propname) {
        errs.append('\n');
        errs.append(propname);
        errs.append(" must be defined.");

    }

    public String getEntryFQDN(final String entry, final String dot_le) {
        String str;
        if(public_fqdn!=null && dot_le.isEmpty()) {
            str = public_fqdn;
        } else {
            str = access.getProperty(Config.AAF_LOCATOR_FQDN+dot_le, default_fqdn);
        }
        return replacements("RegistrationPropHolder.getEntryFQDN",str,entry,dot_le);
    }

    public String getEntryName(final String entry, final String dot_le) {
        String str;
        if(dot_le.isEmpty()) {
            str = default_name;
        } else {
            str = access.getProperty(Config.AAF_LOCATOR_NAME+dot_le, default_name);
        }
        return replacements("RegistrationPropHolder.getEntryName",str,entry,dot_le);
    }

    public String getPublicEntryName(final String entry, final String dot_le) {
        String str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_NAME+dot_le, null);
        if(str==null) {
            str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_NAME,null);
        }
        if(str==null) {
            str = default_name;
        }
        return replacements("RegistrationPropHolder.getEntryName",str,entry,dot_le);
    }


    private String getNS(String dot_le) {
        String ns;
        ns = access.getProperty(Config.AAF_LOCATOR_APP_NS+dot_le,null);
        if(ns==null) {
            ns = access.getProperty(Config.AAF_LOCATOR_APP_NS, "AAF_NS");
        }
        return ns;
    }


    public String replacements(final String fromCode, final String source, final String name, final String _dot_le) {
        if(source == null) {
            return "";
        } else if(source.isEmpty()) {
            return source;
        }
        String value = source;
        String dot_le;
        if(_dot_le==null) {
            dot_le = default_container.isEmpty()?"":'.'+default_container;
        } else {
            dot_le = _dot_le;
        }

        String aaf_locator_host = access.getProperty(Config.AAF_LOCATE_URL+dot_le,null);
        if(aaf_locator_host==null) {
            aaf_locator_host = access.getProperty(Config.AAF_LOCATE_URL,null);
        }

        String str;
        if(aaf_locator_host!=null) {
            if("https://AAF_LOCATE_URL".equals(value)) {
                value = aaf_locator_host;
            } else {
                str = aaf_locator_host;
                if(value.indexOf(Config.AAF_LOCATE_URL_TAG)>=0) {
                    if(!str.endsWith("/")) {
                        str+='/';
                    }
                    if(!str.endsWith("/locate/")) {
                        str+="locate/";
                    }
                    if(value.startsWith("http:")) {
                        value = value.replace("http://AAF_LOCATE_URL/", str);
                    } else {
                        value = value.replace("https://AAF_LOCATE_URL/", str);

                    }
                }
            }
        }

        int atC = value.indexOf("%C");
        if(atC>=0) {
            // aaf_locator_container_ns
            str = access.getProperty(Config.AAF_LOCATOR_CONTAINER_NS+dot_le, default_container_ns);
            if(str.isEmpty()) {
                value = value.replace("%CNS"+'.', str);
            }
            value = value.replace("%CNS", str);

            str = access.getProperty(Config.AAF_LOCATOR_CONTAINER+dot_le,default_container);
            if(str.isEmpty()) {
                value = value.replace("%C"+'.', str);
            }
            value = value.replace("%C", str);
        }

        if(value.indexOf("%NS")>=0) {
            str = getNS(dot_le);
            if(str==null || str.isEmpty()) {
                value = value.replace("%NS"+'.', "");
            } else {
                value = value.replace("%NS", str);
            }
        }

        // aaf_root_ns
        if(value.indexOf("AAF_NS")>=0) {
            str = access.getProperty(Config.AAF_ROOT_NS, Config.AAF_ROOT_NS_DEF) + '.';
            String temp = value.replace("%AAF_NS.", str);
            if(temp.equals(value)) { // intended
                value = value.replace("AAF_NS.", str); // Backward Compatibility
            } else {
                value = temp;
            }
        }


        if(value.indexOf('%')>=0) {
            // These shouldn't be expected to have dot elements
            if(name!=null) {
              value = value.replace("%N", name);
            }
            if(default_fqdn!=null) {
              value = value.replace("%DF", default_fqdn);
            }
            if(public_fqdn!=null) {
              value = value.replace("%PH", public_fqdn);
            }
        }
        access.printf(Level.DEBUG,
                "RegistrationReplacement from %s, source: %s, dot_le: %s, value: %s",
                fromCode,source,dot_le,value);

        return value;
    }

    public int getEntryPort(final String dot_le) {
        return public_port!=null && dot_le.isEmpty()?
                public_port:
                port;
    }

    public Access access() {
        return access;
    }
}