c0ebce8c44b6b668797e09034a8ce557b9f92112
[aaa.git] / aaa-authn-federation / src / main / java / org / opendaylight / aaa / federation / FederationConfiguration.java
1 /*
2  * Copyright (c) 2014 Hewlett-Packard Development Company, L.P. and others.
3  * All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9 package org.opendaylight.aaa.federation;
10
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.Dictionary;
14 import java.util.Enumeration;
15 import java.util.Hashtable;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.Set;
20 import java.util.TreeSet;
21
22 import org.osgi.service.cm.ConfigurationException;
23 import org.osgi.service.cm.ManagedService;
24
25 /**
26  * AAA federation configurations in OSGi.
27  *
28  * @author liemmn
29  *
30  */
31 public class FederationConfiguration implements ManagedService {
32     private static final String FEDERATION_CONFIG_ERR = "Error saving federation configuration";
33
34     static final String HTTP_HEADERS = "httpHeaders";
35     static final String HTTP_ATTRIBUTES = "httpAttributes";
36     static final String SECURE_PROXY_PORTS = "secureProxyPorts";
37
38     static FederationConfiguration instance = new FederationConfiguration();
39
40     static final Hashtable<String, String> defaults = new Hashtable<>();
41     static {
42         defaults.put(HTTP_HEADERS, "");
43         defaults.put(HTTP_ATTRIBUTES, "");
44     }
45     private static Map<String, String> configs = new ConcurrentHashMap<>();
46
47     // singleton
48     private FederationConfiguration() {
49     }
50
51     public static FederationConfiguration instance() {
52         return instance;
53     }
54
55     @Override
56     public void updated(Dictionary<String, ?> props)
57             throws ConfigurationException {
58         if (props == null) {
59             configs.clear();
60             configs.putAll(defaults);
61         } else {
62             try {
63                 Enumeration<String> keys = props.keys();
64                 while (keys.hasMoreElements()) {
65                     String key = keys.nextElement();
66                     configs.put(key, (String) props.get(key));
67                 }
68             } catch (Throwable t) {
69                 throw new ConfigurationException(null, FEDERATION_CONFIG_ERR);
70             }
71         }
72     }
73
74     public List<String> httpHeaders() {
75         String headers = configs.get(HTTP_HEADERS);
76         return (headers == null) ? new ArrayList<String>() : Arrays
77                 .asList(headers.split(" "));
78     }
79
80     public List<String> httpAttributes() {
81         String attributes = configs.get(HTTP_ATTRIBUTES);
82         return (attributes == null) ? new ArrayList<String>() : Arrays
83                 .asList(attributes.split(" "));
84     }
85
86     public Set<Integer> secureProxyPorts() {
87         String ports = configs.get(SECURE_PROXY_PORTS);
88         Set<Integer> secureProxyPorts = new TreeSet<Integer>();
89
90         if (ports != null && !ports.isEmpty()) {
91             for (String port : ports.split(" ")) {
92                 secureProxyPorts.add(Integer.parseInt(port));
93             }
94         }
95         return secureProxyPorts;
96     }
97
98 }