Merge "Fix a race PingPongTransactionChain"
[controller.git] / opendaylight / md-sal / sal-restconf-broker / src / main / java / org / opendaylight / controller / sal / restconf / broker / SalRemoteServiceBroker.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.controller.sal.restconf.broker;
9
10
11 import com.google.common.collect.ImmutableClassToInstanceMap;
12 import org.opendaylight.controller.md.sal.binding.util.BindingContextUtils;
13 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareService;
17 import org.opendaylight.controller.sal.binding.api.NotificationService;
18 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
19 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
20 import org.opendaylight.controller.sal.restconf.broker.impl.RemoteServicesFactory;
21 import org.opendaylight.yangtools.restconf.client.api.RestconfClientContext;
22 import org.osgi.framework.BundleContext;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import static com.google.common.base.Preconditions.checkState;
26
27 public class SalRemoteServiceBroker implements BindingAwareBroker,AutoCloseable {
28
29
30     private static final Logger logger = LoggerFactory.getLogger(SalRemoteServiceBroker.class.toString());
31     private ImmutableClassToInstanceMap<BindingAwareService> supportedConsumerServices;
32
33     private final String identifier;
34
35     private RpcConsumerRegistry rpcBroker;
36     private NotificationService notificationBroker;
37     private DataBrokerService dataBroker;
38     private final RemoteServicesFactory servicesFactory;
39
40     public SalRemoteServiceBroker(String instanceName,RestconfClientContext clientContext){
41         this.identifier = instanceName;
42         this.servicesFactory = new RemoteServicesFactory(clientContext);
43     }
44
45     public void start() {
46         logger.info("Starting Binding Aware Broker: {}", identifier);
47
48         supportedConsumerServices = ImmutableClassToInstanceMap.<BindingAwareService> builder()
49                 .put(NotificationService.class, servicesFactory.getNotificationService()) //
50                 .put(DataBrokerService.class,servicesFactory.getDataBrokerService() ) //
51                 .put(RpcConsumerRegistry.class,servicesFactory.getRpcConsumerRegistry() ).build();
52     }
53
54     public ProviderContext registerProvider(BindingAwareProvider provider) {
55         throw new UnsupportedOperationException();
56     }
57     @Override
58     public void close() throws Exception {
59         //TODO decide if serviceFactory should close clientContext or it has to be closed by consumer
60     }
61     @Override
62     public ConsumerContext registerConsumer(BindingAwareConsumer consumer) {
63         checkState(supportedConsumerServices != null, "Broker is not initialized.");
64         return BindingContextUtils.createConsumerContextAndInitialize(consumer, supportedConsumerServices);
65     }
66
67     @Override
68     public ConsumerContext registerConsumer(BindingAwareConsumer consumer,
69             BundleContext ctx) {
70         return registerConsumer(consumer);
71     }
72
73     @Override
74     public ProviderContext registerProvider(BindingAwareProvider provider,
75             BundleContext ctx) {
76         return registerProvider(provider);
77     }
78
79 }