aboutsummaryrefslogtreecommitdiffstats
path: root/upstream/odl-aaa-moon/aaa/aaa-authn-federation/src/main/java/org/opendaylight/aaa/federation/FederationConfiguration.java
blob: a68dc15ce6ecdc41f298c46b304958170ac6d0b7 (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
/*
 * Copyright (c) 2014, 2015 Hewlett-Packard Development Company, L.P. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

package org.opendaylight.aaa.federation;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedService;

/**
 * AAA federation configurations in OSGi.
 *
 * @author liemmn
 *
 */
public class FederationConfiguration implements ManagedService {
    private static final String FEDERATION_CONFIG_ERR = "Error saving federation configuration";

    static final String HTTP_HEADERS = "httpHeaders";
    static final String HTTP_ATTRIBUTES = "httpAttributes";
    static final String SECURE_PROXY_PORTS = "secureProxyPorts";

    static FederationConfiguration instance = new FederationConfiguration();

    static final Hashtable<String, String> defaults = new Hashtable<>();
    static {
        defaults.put(HTTP_HEADERS, "");
        defaults.put(HTTP_ATTRIBUTES, "");
    }
    private static Map<String, String> configs = new ConcurrentHashMap<>();

    // singleton
    private FederationConfiguration() {
    }

    public static FederationConfiguration instance() {
        return instance;
    }

    @Override
    public void updated(Dictionary<String, ?> props) throws ConfigurationException {
        if (props == null) {
            configs.clear();
            configs.putAll(defaults);
        } else {
            try {
                Enumeration<String> keys = props.keys();
                while (keys.hasMoreElements()) {
                    String key = keys.nextElement();
                    configs.put(key, (String) props.get(key));
                }
            } catch (Throwable t) {
                throw new ConfigurationException(null, FEDERATION_CONFIG_ERR, t);
            }
        }
    }

    public List<String> httpHeaders() {
        String headers = configs.get(HTTP_HEADERS);
        return (headers == null) ? new ArrayList<String>() : Arrays.asList(headers.split(" "));
    }

    public List<String> httpAttributes() {
        String attributes = configs.get(HTTP_ATTRIBUTES);
        return (attributes == null) ? new ArrayList<String>() : Arrays
                .asList(attributes.split(" "));
    }

    public Set<Integer> secureProxyPorts() {
        String ports = configs.get(SECURE_PROXY_PORTS);
        Set<Integer> secureProxyPorts = new TreeSet<Integer>();

        if (ports != null && !ports.isEmpty()) {
            for (String port : ports.split(" ")) {
                secureProxyPorts.add(Integer.parseInt(port));
            }
        }
        return secureProxyPorts;
    }

}