2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.aaa.authz.srv;
11 import java.util.Collection;
13 import org.opendaylight.aaa.api.AuthenticationService;
14 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
15 import org.opendaylight.controller.sal.core.api.Broker;
16 import org.opendaylight.controller.sal.core.api.Consumer;
17 import org.opendaylight.controller.sal.core.api.Provider;
18 import org.osgi.framework.BundleContext;
21 * Created by wdec on 26/08/2014.
23 public class AuthzBrokerImpl implements Broker, AutoCloseable, Provider {
25 private Broker broker;
26 private ProviderSession providerSession;
27 private AuthenticationService authenticationService;
29 public void setBroker(Broker broker) {
34 public void close() throws Exception {
38 // Implements AuthzBroker handling of registering consumers or providers.
40 public ConsumerSession registerConsumer(Consumer consumer) {
42 ConsumerSession realSession = broker.registerConsumer(new ConsumerWrapper(consumer));
43 AuthzConsumerContextImpl authzConsumerContext = new AuthzConsumerContextImpl(realSession,
45 consumer.onSessionInitiated(authzConsumerContext);
46 return authzConsumerContext;
50 public ConsumerSession registerConsumer(Consumer consumer, BundleContext bundleContext) {
52 ConsumerSession realSession = broker.registerConsumer(new ConsumerWrapper(consumer),
54 AuthzConsumerContextImpl authzConsumerContext = new AuthzConsumerContextImpl(realSession,
56 consumer.onSessionInitiated(authzConsumerContext);
57 return authzConsumerContext;
61 public ProviderSession registerProvider(Provider provider) {
63 ProviderSession realSession = broker.registerProvider(new ProviderWrapper(provider));
64 AuthzProviderContextImpl authzProviderContext = new AuthzProviderContextImpl(realSession,
66 provider.onSessionInitiated(authzProviderContext);
67 return authzProviderContext;
71 public ProviderSession registerProvider(Provider provider, BundleContext bundleContext) {
73 // Allow the real broker to do its thing, while providing a wrapped
75 ProviderSession realSession = broker.registerProvider(new ProviderWrapper(provider),
78 // Create Authz ProviderContext
79 AuthzProviderContextImpl authzProviderContext = new AuthzProviderContextImpl(realSession,
82 // Run onsessionInitiated on injected provider with the AuthZ provider
84 provider.onSessionInitiated(authzProviderContext);
85 return authzProviderContext;
89 // Handle the AuthZBroker registration with the real broker
91 public void onSessionInitiated(ProviderSession providerSession) {
93 // Get now the real DOMDataBroker and register it with the
94 // AuthzDOMBroker together with the provider session
95 final DOMDataBroker domDataBroker = providerSession.getService(DOMDataBroker.class);
96 AuthzDomDataBroker.getInstance().setProviderSession(providerSession);
97 AuthzDomDataBroker.getInstance().setDomDataBroker(domDataBroker);
98 AuthzDomDataBroker.getInstance().setAuthService(this.authenticationService);
102 public Collection<ProviderFunctionality> getProviderFunctionality() {
106 public void setAuthenticationService(AuthenticationService authenticationService) {
107 this.authenticationService = authenticationService;
110 // Wrapper for Provider
112 public static class ProviderWrapper implements Provider {
113 private final Provider provider;
115 public ProviderWrapper(Provider provider) {
116 this.provider = provider;
120 public void onSessionInitiated(ProviderSession providerSession) {
121 // Do a Noop when the real broker calls back
125 public Collection<ProviderFunctionality> getProviderFunctionality() {
126 // Allow the RestconfImpl to respond to this
127 return provider.getProviderFunctionality();
131 // Wrapper for Consumer
132 public static class ConsumerWrapper implements Consumer {
134 private final Consumer consumer;
136 public ConsumerWrapper(Consumer consumer) {
137 this.consumer = consumer;
141 public void onSessionInitiated(ConsumerSession consumerSession) {
142 // Do a Noop when the real broker calls back
146 public Collection<ConsumerFunctionality> getConsumerFunctionality() {
147 return consumer.getConsumerFunctionality();