aboutsummaryrefslogtreecommitdiffstats
path: root/odl-aaa-moon/aaa/aaa-shiro/src/main/java/org/opendaylight/aaa/shiro/ServiceProxy.java
blob: e4485d733c4d662211f3d6313593469e425a1d0e (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
/*
 * Copyright (c) 2016 Brocade Communications Systems, Inc. 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.shiro;

import org.opendaylight.aaa.shiro.filters.AAAFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Responsible for enabling and disabling the AAA service. By default, the
 * service is disabled; the AAAFilter will not require AuthN or AuthZ. The
 * service is enabled through calling
 * <code>ServiceProxy.getInstance().setEnabled(true)</code>. AuthN and AuthZ are
 * disabled by default in order to support workflows such as the feature
 * <code>odl-restconf-noauth</code>.
 *
 * The AAA service is enabled through installing the <code>odl-aaa-shiro</code>
 * feature. The <code>org.opendaylight.aaa.shiroact.Activator()</code>
 * constructor calls enables AAA through the ServiceProxy, which in turn enables
 * the AAAFilter.
 *
 * ServiceProxy is a singleton; access to the ServiceProxy is granted through
 * the <code>getInstance()</code> function.
 *
 * @author Ryan Goulding (ryandgoulding@gmail.com)
 * @see <a
 *      href="https://github.com/opendaylight/netconf/blob/master/opendaylight/restconf/sal-rest-connector/src/main/resources/WEB-INF/web.xml">resconf
 *      web,xml</a>
 * @see <code>org.opendaylight.aaa.shiro.Activator</code>
 * @see <code>org.opendaylight.aaa.shiro.filters.AAAFilter</code>
 */
public class ServiceProxy {
    private static final Logger LOG = LoggerFactory.getLogger(ServiceProxy.class);

    /**
     * AuthN and AuthZ are disabled by default to support workflows included in
     * features such as <code>odl-restconf-noauth</code>
     */
    public static final boolean DEFAULT_AA_ENABLE_STATUS = false;

    private static ServiceProxy instance = new ServiceProxy();
    private volatile boolean enabled = false;
    private AAAFilter filter;

    /**
     * private for singleton pattern
     */
    private ServiceProxy() {
        final String INFO_MESSAGE = "Creating the ServiceProxy";
        LOG.info(INFO_MESSAGE);
    }

    /**
     * @return ServiceProxy, a feature level singleton
     */
    public static ServiceProxy getInstance() {
        return instance;
    }

    /**
     * Enables/disables the feature, cascading the state information to the
     * AAAFilter.
     *
     * @param enabled A flag indicating whether to enable the Service.
     */
    public synchronized void setEnabled(final boolean enabled) {
        this.enabled = enabled;
        final String SERVICE_ENABLED_INFO_MESSAGE = "Setting ServiceProxy enabled to " + enabled;
        LOG.info(SERVICE_ENABLED_INFO_MESSAGE);
        // check for null because of non-determinism in bundle load
        if (filter != null) {
            filter.setEnabled(enabled);
        }
    }

    /**
     * Extract whether the service is enabled.
     *
     * @param filter
     *            register an optional Filter for callback if enable state
     *            changes
     * @return Whether the service is enabled
     */
    public synchronized boolean getEnabled(final AAAFilter filter) {
        this.filter = filter;
        return enabled;
    }
}