Merge changes I1b834def,Iece78be2
[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.Augmentation;
23 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import com.google.common.base.Preconditions;
28
29 class SalRemoteClientImpl implements SalRemoteClient {
30
31     private static final Logger logger = LoggerFactory.getLogger(SalRemoteClientImpl.class);
32
33     private final RestconfClientContext restconfClientContext;
34     private final SalRemoteServiceBroker salRemoteBroker;
35     private final RuntimeGeneratedMappingServiceImpl mappingService;
36
37     public SalRemoteClientImpl(final URL url) {
38         Preconditions.checkNotNull(url);
39
40         this.mappingService = new RuntimeGeneratedMappingServiceImpl();
41         this.mappingService.setPool(ClassPool.getDefault());
42         this.mappingService.init();
43
44         final ModuleInfoBackedContext moduleInfo = ModuleInfoBackedContext.create();
45         moduleInfo.addModuleInfos(BindingReflections.loadModuleInfos());
46         this.mappingService.onGlobalContextUpdated(moduleInfo.tryToCreateSchemaContext().get());
47
48         try {
49             this.restconfClientContext = new RestconfClientFactory().getRestconfClientContext(url, this.mappingService,
50                     this.mappingService);
51
52             this.salRemoteBroker = new SalRemoteServiceBroker("remote-broker", restconfClientContext);
53             this.salRemoteBroker.start();
54         } catch (UnsupportedProtocolException e) {
55             logger.error("Unsupported protocol {}.", url.getProtocol(), e);
56             throw new IllegalArgumentException("Unsupported protocol.", e);
57         }
58     }
59
60     @Override
61     public ConsumerContext registerConsumer() {
62         return this.salRemoteBroker.registerConsumer(new BindingAwareConsumer() {
63
64             @Override
65             public void onSessionInitialized(ConsumerContext session) {
66             }
67         }, null);
68     }
69
70     @Override
71     public void close() throws Exception {
72         this.restconfClientContext.close();
73         this.salRemoteBroker.close();
74     }
75
76 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.