Merge "BUG 2854 : Do not add empty read write transactions to the replicable journal"
[controller.git] / opendaylight / md-sal / sal-restconf-broker / src / main / java / org / opendaylight / controller / sal / restconf / broker / client / SalRemoteClientImpl.java
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 package org.opendaylight.controller.sal.restconf.broker.client;
9
10 import com.google.common.base.Preconditions;
11 import java.net.URL;
12 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
13 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
14 import org.opendaylight.controller.sal.restconf.broker.SalRemoteServiceBroker;
15 import org.opendaylight.yangtools.restconf.client.RestconfClientFactory;
16 import org.opendaylight.yangtools.restconf.client.api.RestconfClientContext;
17 import org.opendaylight.yangtools.restconf.client.api.UnsupportedProtocolException;
18 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
19 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContextHolder;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 class SalRemoteClientImpl implements SalRemoteClient, SchemaContextHolder {
26
27     private static final Logger logger = LoggerFactory.getLogger(SalRemoteClientImpl.class);
28
29     private final RestconfClientContext restconfClientContext;
30     private final SalRemoteServiceBroker salRemoteBroker;
31
32     private SchemaContext schemaContext;
33
34     public SalRemoteClientImpl(final URL url) {
35         Preconditions.checkNotNull(url);
36
37
38         final ModuleInfoBackedContext moduleInfo = ModuleInfoBackedContext.create();
39         moduleInfo.addModuleInfos(BindingReflections.loadModuleInfos());
40         schemaContext = moduleInfo.tryToCreateSchemaContext().get();
41         try {
42             this.restconfClientContext = new RestconfClientFactory().getRestconfClientContext(url,this);
43
44             this.salRemoteBroker = new SalRemoteServiceBroker("remote-broker", restconfClientContext);
45             this.salRemoteBroker.start();
46         } catch (UnsupportedProtocolException e) {
47             logger.error("Unsupported protocol {}.", url.getProtocol(), e);
48             throw new IllegalArgumentException("Unsupported protocol.", e);
49         }
50     }
51
52     @Override
53     public ConsumerContext registerConsumer() {
54         return this.salRemoteBroker.registerConsumer(new BindingAwareConsumer() {
55
56             @Override
57             public void onSessionInitialized(ConsumerContext session) {
58             }
59         }, null);
60     }
61
62     @Override
63     public SchemaContext getSchemaContext() {
64         return schemaContext;
65     }
66
67     @Override
68     public void close() throws Exception {
69         this.restconfClientContext.close();
70         this.salRemoteBroker.close();
71     }
72
73 }