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