d4ac79af490756d16e6b61a8b48478b4a29e3218
[moon.git] /
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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.authz.srv;
10
11 import java.util.Collection;
12
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;
19
20 /**
21  * Created by wdec on 26/08/2014.
22  */
23 public class AuthzBrokerImpl implements Broker, AutoCloseable, Provider {
24
25     private Broker broker;
26     private ProviderSession providerSession;
27     private AuthenticationService authenticationService;
28
29     public void setBroker(Broker broker) {
30         this.broker = broker;
31     }
32
33     @Override
34     public void close() throws Exception {
35
36     }
37
38     // Implements AuthzBroker handling of registering consumers or providers.
39     @Override
40     public ConsumerSession registerConsumer(Consumer consumer) {
41
42         ConsumerSession realSession = broker.registerConsumer(new ConsumerWrapper(consumer));
43         AuthzConsumerContextImpl authzConsumerContext = new AuthzConsumerContextImpl(realSession,
44                 this);
45         consumer.onSessionInitiated(authzConsumerContext);
46         return authzConsumerContext;
47     }
48
49     @Override
50     public ConsumerSession registerConsumer(Consumer consumer, BundleContext bundleContext) {
51
52         ConsumerSession realSession = broker.registerConsumer(new ConsumerWrapper(consumer),
53                 bundleContext);
54         AuthzConsumerContextImpl authzConsumerContext = new AuthzConsumerContextImpl(realSession,
55                 this);
56         consumer.onSessionInitiated(authzConsumerContext);
57         return authzConsumerContext;
58     }
59
60     @Override
61     public ProviderSession registerProvider(Provider provider) {
62
63         ProviderSession realSession = broker.registerProvider(new ProviderWrapper(provider));
64         AuthzProviderContextImpl authzProviderContext = new AuthzProviderContextImpl(realSession,
65                 this);
66         provider.onSessionInitiated(authzProviderContext);
67         return authzProviderContext;
68     }
69
70     @Override
71     public ProviderSession registerProvider(Provider provider, BundleContext bundleContext) {
72
73         // Allow the real broker to do its thing, while providing a wrapped
74         // callback
75         ProviderSession realSession = broker.registerProvider(new ProviderWrapper(provider),
76                 bundleContext);
77
78         // Create Authz ProviderContext
79         AuthzProviderContextImpl authzProviderContext = new AuthzProviderContextImpl(realSession,
80                 this);
81
82         // Run onsessionInitiated on injected provider with the AuthZ provider
83         // context.
84         provider.onSessionInitiated(authzProviderContext);
85         return authzProviderContext;
86
87     }
88
89     // Handle the AuthZBroker registration with the real broker
90     @Override
91     public void onSessionInitiated(ProviderSession providerSession) {
92
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);
99     }
100
101     @Override
102     public Collection<ProviderFunctionality> getProviderFunctionality() {
103         return null;
104     }
105
106     public void setAuthenticationService(AuthenticationService authenticationService) {
107         this.authenticationService = authenticationService;
108     }
109
110     // Wrapper for Provider
111
112     public static class ProviderWrapper implements Provider {
113         private final Provider provider;
114
115         public ProviderWrapper(Provider provider) {
116             this.provider = provider;
117         }
118
119         @Override
120         public void onSessionInitiated(ProviderSession providerSession) {
121             // Do a Noop when the real broker calls back
122         }
123
124         @Override
125         public Collection<ProviderFunctionality> getProviderFunctionality() {
126             // Allow the RestconfImpl to respond to this
127             return provider.getProviderFunctionality();
128         }
129     }
130
131     // Wrapper for Consumer
132     public static class ConsumerWrapper implements Consumer {
133
134         private final Consumer consumer;
135
136         public ConsumerWrapper(Consumer consumer) {
137             this.consumer = consumer;
138         }
139
140         @Override
141         public void onSessionInitiated(ConsumerSession consumerSession) {
142             // Do a Noop when the real broker calls back
143         }
144
145         @Override
146         public Collection<ConsumerFunctionality> getConsumerFunctionality() {
147             return consumer.getConsumerFunctionality();
148         }
149     }
150 }