2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.rest;
10 import com.google.common.base.Preconditions;
11 import java.util.Collection;
12 import java.util.Collections;
13 import org.opendaylight.controller.sal.core.api.Broker.ProviderSession;
14 import org.opendaylight.controller.sal.core.api.Provider;
15 import org.opendaylight.controller.sal.core.api.model.SchemaService;
16 import org.opendaylight.netconf.sal.rest.api.RestConnector;
17 import org.opendaylight.yangtools.concepts.ListenerRegistration;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
19 import org.osgi.framework.BundleContext;
20 import org.osgi.framework.FrameworkUtil;
21 import org.osgi.framework.ServiceReference;
24 * Provider for restconf draft11.
27 public class RestConnectorProvider implements Provider, RestConnector, AutoCloseable {
29 private ListenerRegistration<SchemaContextListener> listenerRegistration;
32 public void onSessionInitiated(final ProviderSession session) {
33 final SchemaService schemaService = Preconditions.checkNotNull(session.getService(SchemaService.class));
34 final RestconfApplication restApp = getObjectFromBundleContext(RestconfApplication.class,
35 RestconfApplicationService.class.getName());
36 Preconditions.checkNotNull(restApp, "RestconfApplication service doesn't exist.");
37 this.listenerRegistration = schemaService.registerSchemaContextListener(restApp.getSchemaContextHandler());
40 private <T> T getObjectFromBundleContext(final Class<T> type, final String serviceRefName) {
41 final BundleContext bundleContext = FrameworkUtil.getBundle(getClass()).getBundleContext();
42 final ServiceReference<?> serviceReference = bundleContext.getServiceReference(serviceRefName);
43 return (T) bundleContext.getService(serviceReference);
47 public Collection<ProviderFunctionality> getProviderFunctionality() {
48 return Collections.emptySet();
52 public void close() throws Exception {
53 if (this.listenerRegistration != null) {
54 this.listenerRegistration.close();