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.*
-class ControllerContext {
-
+class ControllerContext implements SchemaServiceListener {
+
val static ControllerContext INSTANCE = new ControllerContext
-
+
val static NULL_VALUE = "null"
-
+
@Property
SchemaContext schemas;
private val BiMap<URI, String> uriToModuleName = HashBiMap.create();
private val Map<String, URI> moduleNameToUri = uriToModuleName.inverse();
+ private val Map<QName,RpcDefinition> qnameToRpc = new ConcurrentHashMap();
+
private new() {
if (INSTANCE != null) {
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);
if (targetNode instanceof ListSchemaNode) {
val listNode = targetNode as ListSchemaNode;
val keysSize = listNode.keyDefinition.size
+
// every key has to be filled
if ((strings.length - consumed) < keysSize) {
return null;
var i = 0;
for (key : listNode.keyDefinition) {
val uriKeyValue = uriKeyValues.get(i);
+
// key value cannot be NULL
if (uriKeyValue.equals(NULL_VALUE)) {
return null
consumed = consumed + i;
builder.nodeWithKey(targetNode.QName, keyValues);
} else {
+
// Only one instance of node is allowed
builder.node(targetNode.QName);
}
}
}
- public def QName toRpcQName(String name) {
+ public def QName toQName(String name) {
+ val module = name.toModuleName;
+ val node = name.toNodeName;
+ val namespace = moduleNameToUri.get(module);
+ return new QName(namespace,null,node);
+ }
+
+ override onGlobalContextUpdated(SchemaContext context) {
+ this.schemas = context;
+ for(operation : context.operations) {
+ val qname = new QName(operation.QName.namespace,null,operation.QName.localName);
+ qnameToRpc.put(qname,operation);
+ }
}
+
+ def ContainerSchemaNode getRpcOutputSchema(QName name) {
+ qnameToRpc.get(name)?.output;
+ }
+
+ def ContainerSchemaNode getRpcInputSchema(QName name) {
+ qnameToRpc.get(name)?.input;
+ }
+
}