Merge "Refactor Logger in RestCodec"
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / restconf / rest / RestConnectorProvider.java
1 /*
2  * Copyright (c) 2016 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.restconf.rest;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Collection;
12 import java.util.Collections;
13 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
14 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession;
15 import org.opendaylight.controller.sal.core.api.Provider;
16 import org.opendaylight.controller.sal.core.api.model.SchemaService;
17 import org.opendaylight.netconf.sal.rest.api.RestConnector;
18 import org.opendaylight.restconf.rest.api.schema.context.SchemaContextHandler;
19 import org.opendaylight.restconf.rest.handlers.api.DOMMountPointServiceHandler;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
22 import org.osgi.framework.BundleContext;
23 import org.osgi.framework.FrameworkUtil;
24 import org.osgi.framework.ServiceReference;
25
26 /**
27  * Provider for restconf draft11.
28  *
29  */
30 public class RestConnectorProvider implements Provider, RestConnector, AutoCloseable {
31
32     private ListenerRegistration<SchemaContextListener> listenerRegistration;
33
34     @Override
35     public void onSessionInitiated(final ProviderSession session) {
36         final SchemaService schemaService = Preconditions.checkNotNull(session.getService(SchemaService.class));
37         final RestconfApplication restApp = getObjectFromBundleContext(RestconfApplication.class,
38                 RestconfApplicationService.class.getName());
39         Preconditions.checkNotNull(restApp, "RestconfApplication service doesn't exist.");
40         final SchemaContextHandler schemaContextHandler = restApp.getSchemaContextHandler();
41         final DOMMountPointServiceHandler domMountPointServiceHandler = restApp.getDOMMountPointServiceHandler();
42         domMountPointServiceHandler.setDOMMountPointService(session.getService(DOMMountPointService.class));
43
44         this.listenerRegistration = schemaService.registerSchemaContextListener(schemaContextHandler);
45     }
46
47     private <T> T getObjectFromBundleContext(final Class<T> type, final String serviceRefName) {
48         final BundleContext bundleContext = FrameworkUtil.getBundle(getClass()).getBundleContext();
49         final ServiceReference<?> serviceReference = bundleContext.getServiceReference(serviceRefName);
50         return (T) bundleContext.getService(serviceReference);
51     }
52
53     @Override
54     public Collection<ProviderFunctionality> getProviderFunctionality() {
55         return Collections.emptySet();
56     }
57
58     @Override
59     public void close() throws Exception {
60         if (this.listenerRegistration != null) {
61             this.listenerRegistration.close();
62         }
63     }
64 }