Merge "Unify maven-bundle-plugin version at 2.4.0"
[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 java.net.URL;
11
12 import javassist.ClassPool;
13
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
16 import org.opendaylight.controller.sal.restconf.broker.SalRemoteServiceBroker;
17 import org.opendaylight.yangtools.restconf.client.RestconfClientFactory;
18 import org.opendaylight.yangtools.restconf.client.api.RestconfClientContext;
19 import org.opendaylight.yangtools.restconf.client.api.UnsupportedProtocolException;
20 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
21 import org.opendaylight.yangtools.sal.binding.generator.impl.RuntimeGeneratedMappingServiceImpl;
22 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import com.google.common.base.Preconditions;
27
28 class SalRemoteClientImpl implements SalRemoteClient {
29
30     private static final Logger logger = LoggerFactory.getLogger(SalRemoteClientImpl.class);
31
32     private final RestconfClientContext restconfClientContext;
33     private final SalRemoteServiceBroker salRemoteBroker;
34     private final RuntimeGeneratedMappingServiceImpl mappingService;
35
36     public SalRemoteClientImpl(final URL url) {
37         Preconditions.checkNotNull(url);
38
39         this.mappingService = new RuntimeGeneratedMappingServiceImpl(ClassPool.getDefault());
40
41         final ModuleInfoBackedContext moduleInfo = ModuleInfoBackedContext.create();
42         moduleInfo.addModuleInfos(BindingReflections.loadModuleInfos());
43         this.mappingService.onGlobalContextUpdated(moduleInfo.tryToCreateSchemaContext().get());
44
45         try {
46             this.restconfClientContext = new RestconfClientFactory().getRestconfClientContext(url, this.mappingService,
47                     this.mappingService);
48
49             this.salRemoteBroker = new SalRemoteServiceBroker("remote-broker", restconfClientContext);
50             this.salRemoteBroker.start();
51         } catch (UnsupportedProtocolException e) {
52             logger.error("Unsupported protocol {}.", url.getProtocol(), e);
53             throw new IllegalArgumentException("Unsupported protocol.", e);
54         }
55     }
56
57     @Override
58     public ConsumerContext registerConsumer() {
59         return this.salRemoteBroker.registerConsumer(new BindingAwareConsumer() {
60
61             @Override
62             public void onSessionInitialized(ConsumerContext session) {
63             }
64         }, null);
65     }
66
67     @Override
68     public void close() throws Exception {
69         this.restconfClientContext.close();
70         this.salRemoteBroker.close();
71     }
72
73 }