package org.opendaylight.controller.sal.rest.impl;
-import org.opendaylight.controller.sal.core.api.AbstractProvider;
+import java.util.Collection;
+import java.util.Collections;
+
+import org.opendaylight.controller.sal.core.api.Broker;
import org.opendaylight.controller.sal.core.api.Broker.ProviderSession;
+import org.opendaylight.controller.sal.core.api.Provider;
import org.opendaylight.controller.sal.core.api.data.DataBrokerService;
-import org.opendaylight.controller.sal.core.api.data.DataProviderService;
import org.opendaylight.controller.sal.core.api.model.SchemaService;
import org.opendaylight.controller.sal.core.api.model.SchemaServiceListener;
import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.util.tracker.ServiceTracker;
+import org.osgi.util.tracker.ServiceTrackerCustomizer;
-public class RestconfProvider extends AbstractProvider {
+public class RestconfProvider implements BundleActivator, Provider, ServiceTrackerCustomizer<Broker, Broker> {
+ public final static String NOT_INITALIZED_MSG = "Restcof is not initialized yet. Please try again later";
+
private ListenerRegistration<SchemaServiceListener> listenerRegistration;
+ private ServiceTracker<Broker, Broker> brokerServiceTrancker;
+ private BundleContext bundleContext;
+ private ProviderSession session;
@Override
public void onSessionInitiated(ProviderSession session) {
}
@Override
- protected void stopImpl(BundleContext context) {
- super.stopImpl(context);
+ public void start(BundleContext context) throws Exception {
+ bundleContext = context;
+ brokerServiceTrancker = new ServiceTracker<>(context, Broker.class, this);
+ brokerServiceTrancker.open();
+ }
+
+ @Override
+ public void stop(BundleContext context) {
if (listenerRegistration != null) {
try {
listenerRegistration.close();
e.printStackTrace();
}
}
+ session.close();
+ brokerServiceTrancker.close();
+ }
+
+ @Override
+ public Collection<ProviderFunctionality> getProviderFunctionality() {
+ return Collections.emptySet();
+ }
+
+ @Override
+ public Broker addingService(ServiceReference<Broker> reference) {
+ Broker broker = bundleContext.getService(reference);
+ broker.registerProvider(this, bundleContext);
+ return broker;
+ }
+
+ @Override
+ public void modifiedService(ServiceReference<Broker> reference, Broker service) {
+ // NOOP
+ }
+
+ @Override
+ public void removedService(ServiceReference<Broker> reference, Broker service) {
+ bundleContext.ungetService(reference);
+ BrokerFacade.getInstance().setContext(null);
+ BrokerFacade.getInstance().setDataService(null);
+ ControllerContext.getInstance().setSchemas(null);
}
}
package org.opendaylight.controller.sal.restconf.impl
+import javax.ws.rs.WebApplicationException
+import javax.ws.rs.core.Response
import org.opendaylight.controller.md.sal.common.api.data.DataReader
import org.opendaylight.controller.sal.core.api.Broker.ConsumerSession
import org.opendaylight.controller.sal.core.api.data.DataBrokerService
+import org.opendaylight.controller.sal.rest.impl.RestconfProvider
import org.opendaylight.yangtools.yang.common.QName
import org.opendaylight.yangtools.yang.common.RpcResult
import org.opendaylight.yangtools.yang.data.api.CompositeNode
import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
-import static org.opendaylight.controller.sal.restconf.impl.BrokerFacade.*
class BrokerFacade implements DataReader<InstanceIdentifier, CompositeNode> {
throw new IllegalStateException("Already instantiated");
}
}
-
+
def static BrokerFacade getInstance() {
return INSTANCE
}
+ private def void checkPreconditions() {
+ if (context == null || dataService == null) {
+ throw new WebApplicationException(Response.status(Response.Status.SERVICE_UNAVAILABLE)
+ .entity(RestconfProvider::NOT_INITALIZED_MSG).build())
+ }
+ }
+
override readConfigurationData(InstanceIdentifier path) {
+ checkPreconditions
return dataService.readConfigurationData(path);
}
override readOperationalData(InstanceIdentifier path) {
+ checkPreconditions
return dataService.readOperationalData(path);
}
def RpcResult<CompositeNode> invokeRpc(QName type, CompositeNode payload) {
+ checkPreconditions
val future = context.rpc(type, payload);
return future.get;
}
def commitConfigurationDataPut(InstanceIdentifier path, CompositeNode payload) {
+ checkPreconditions
val transaction = dataService.beginTransaction;
transaction.putConfigurationData(path, payload);
return transaction.commit()
}
def commitOperationalDataPut(InstanceIdentifier path, CompositeNode payload) {
+ checkPreconditions
val transaction = dataService.beginTransaction;
transaction.putOperationalData(path, payload);
return transaction.commit()
import java.util.HashMap
import java.util.List
import java.util.Map
+import java.util.concurrent.ConcurrentHashMap
+import javax.ws.rs.WebApplicationException
+import javax.ws.rs.core.Response
+import org.opendaylight.controller.sal.core.api.model.SchemaServiceListener
+import org.opendaylight.controller.sal.rest.impl.RestconfProvider
import org.opendaylight.yangtools.yang.common.QName
import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.InstanceIdentifierBuilder
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode
import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode
import org.opendaylight.yangtools.yang.model.api.ListSchemaNode
+import org.opendaylight.yangtools.yang.model.api.RpcDefinition
import org.opendaylight.yangtools.yang.model.api.SchemaContext
import static com.google.common.base.Preconditions.*
-import org.opendaylight.controller.sal.core.api.model.SchemaServiceListener
-import org.opendaylight.yangtools.yang.model.api.RpcDefinition
-import java.util.concurrent.ConcurrentHashMap
class ControllerContext implements SchemaServiceListener {
static def getInstance() {
return INSTANCE
}
+
+ private def void checkPreconditions() {
+ if (schemas == null) {
+ throw new WebApplicationException(Response.status(Response.Status.SERVICE_UNAVAILABLE)
+ .entity(RestconfProvider::NOT_INITALIZED_MSG).build())
+ }
+ }
public def InstanceIdWithSchemaNode toInstanceIdentifier(String restconfInstance) {
val ret = InstanceIdentifier.builder();
}
private def findModule(String restconfInstance) {
+ checkPreconditions
checkNotNull(restconfInstance);
val pathArgs = restconfInstance.split("/");
if (pathArgs.empty) {
}
def String toFullRestconfIdentifier(InstanceIdentifier path) {
+ checkPreconditions
val elements = path.path;
val ret = new StringBuilder();
val startQName = elements.get(0).nodeType;
}
def CharSequence toRestconfIdentifier(QName qname) {
+ checkPreconditions
var module = uriToModuleName.get(qname.namespace)
if (module == null) {
val moduleSchema = schemas.findModuleByNamespaceAndRevision(qname.namespace, qname.revision);