*/
package org.opendaylight.yangtools.restconf.utils;
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.BiMap;
+import com.google.common.collect.HashBiMap;
+import com.google.common.collect.Iterables;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
-
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
-
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.RpcService;
import org.opendaylight.yangtools.yang.common.QName;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.BiMap;
-import com.google.common.collect.HashBiMap;
-import com.google.common.collect.Iterables;
-
public class RestconfUtils {
private static final Logger logger = LoggerFactory.getLogger(RestconfUtils.class);
- private static final BiMap<URI,String> uriToModuleName = HashBiMap.<URI, String>create();
+ private static final BiMap<URI, String> uriToModuleName = HashBiMap.<URI, String> create();
- public static Entry<String,DataSchemaNode> toRestconfIdentifier(org.opendaylight.yangtools.yang.binding.InstanceIdentifier<?> bindingIdentifier, BindingIndependentMappingService mappingService, SchemaContext schemaContext) {
+ public static Entry<String, DataSchemaNode> toRestconfIdentifier(
+ final org.opendaylight.yangtools.yang.binding.InstanceIdentifier<?> bindingIdentifier,
+ final BindingIndependentMappingService mappingService, final SchemaContext schemaContext) {
InstanceIdentifier domIdentifier = mappingService.toDataDom(bindingIdentifier);
return toRestconfIdentifier(domIdentifier, schemaContext);
-
}
- public static Entry<String,DataSchemaNode> toRestconfIdentifier(
- InstanceIdentifier xmlInstanceIdentifier,
- SchemaContext schemaContext) {
+ public static Entry<String, DataSchemaNode> toRestconfIdentifier(final InstanceIdentifier xmlInstanceIdentifier,
+ final SchemaContext schemaContext) {
- final List<InstanceIdentifier.PathArgument> elements = xmlInstanceIdentifier.getPath();
+ final Iterable<InstanceIdentifier.PathArgument> elements = xmlInstanceIdentifier.getPathArguments();
final StringBuilder ret = new StringBuilder();
final QName startQName = elements.iterator().next().getNodeType();
- URI _namespace = startQName.getNamespace();
- Date _revision = startQName.getRevision();
- final Module initialModule = schemaContext.findModuleByNamespaceAndRevision(_namespace, _revision);
+ URI namespace = startQName.getNamespace();
+ Date revision = startQName.getRevision();
+ final Module initialModule = schemaContext.findModuleByNamespaceAndRevision(namespace, revision);
DataNodeContainer node = (initialModule);
DataSchemaNode schemaNode = null;
for (final InstanceIdentifier.PathArgument element : elements) {
- {
- final DataSchemaNode potentialNode = node.getDataChildByName(element.getNodeType());
- if (!isListOrContainer(potentialNode)) {
- return null;
- }
- node = ((DataNodeContainer) potentialNode);
- schemaNode = potentialNode;
- ret.append(convertToRestconfIdentifier(element, node,schemaContext));
+ final DataSchemaNode potentialNode = node.getDataChildByName(element.getNodeType());
+ if (!isListOrContainer(potentialNode)) {
+ return null;
}
+ node = ((DataNodeContainer) potentialNode);
+ schemaNode = potentialNode;
+ ret.append(convertToRestconfIdentifier(element, node, schemaContext));
}
- return new SimpleEntry<>(ret.toString(),schemaNode);
+ return new SimpleEntry<>(ret.toString(), schemaNode);
}
- private static CharSequence convertContainerToRestconfIdentifier(final InstanceIdentifier.NodeIdentifier argument, SchemaContext schemaContext) {
+ private static CharSequence convertContainerToRestconfIdentifier(final InstanceIdentifier.NodeIdentifier argument,
+ final SchemaContext schemaContext) {
return "/" + toRestconfIdentifier(argument.getNodeType(), schemaContext);
}
- private static CharSequence convertListToRestconfIdentifier(final InstanceIdentifier.NodeIdentifierWithPredicates argument, final ListSchemaNode node,SchemaContext schemaContext) {
+ private static CharSequence convertListToRestconfIdentifier(
+ final InstanceIdentifier.NodeIdentifierWithPredicates argument, final ListSchemaNode node,
+ final SchemaContext schemaContext) {
QName _nodeType = argument.getNodeType();
- final CharSequence nodeIdentifier = toRestconfIdentifier(_nodeType,schemaContext);
- final Map<QName,Object> keyValues = argument.getKeyValues();
+ final CharSequence nodeIdentifier = toRestconfIdentifier(_nodeType, schemaContext);
+ final Map<QName, Object> keyValues = argument.getKeyValues();
- StringBuilder sb = new StringBuilder("/");
+ StringBuilder sb = new StringBuilder('/');
sb.append(nodeIdentifier);
sb.append('/');
- {
- List<QName> _keyDefinition = node.getKeyDefinition();
- boolean _hasElements = false;
- for(final QName key : _keyDefinition) {
- if (!_hasElements) {
- _hasElements = true;
- } else {
- sb.append('/');
- }
- Object _get = keyValues.get(key);
- sb.append(toUriString(_get));
+
+ List<QName> keyDefinition = node.getKeyDefinition();
+ boolean _hasElements = false;
+ for (final QName key : keyDefinition) {
+ if (_hasElements) {
+ sb.append('/');
+ } else {
+ _hasElements = true;
}
+ Object _get = keyValues.get(key);
+ sb.append(toUriString(_get));
}
+
return sb.toString();
}
+
private static String toUriString(final Object object) {
- boolean _tripleEquals = (object == null);
- if (_tripleEquals) {
+ if (object == null) {
return "";
}
String _string = object.toString();
return URLEncoder.encode(_string);
}
- public static CharSequence toRestconfIdentifier(final QName qname,SchemaContext schemaContext) {
- URI _namespace = qname.getNamespace();
- String module = uriToModuleName.get(_namespace);
- boolean _tripleEquals = (module == null);
- if (_tripleEquals) {
- URI _namespace_1 = qname.getNamespace();
- Date _revision = qname.getRevision();
- final Module moduleSchema = schemaContext.findModuleByNamespaceAndRevision(_namespace_1, _revision);
- boolean _tripleEquals_1 = (moduleSchema == null);
- if (_tripleEquals_1) {
+ public static CharSequence toRestconfIdentifier(final QName qname, final SchemaContext schemaContext) {
+ URI namespace = qname.getNamespace();
+ String module = uriToModuleName.get(namespace);
+ if (module == null) {
+ Date revision = qname.getRevision();
+ final Module moduleSchema = schemaContext.findModuleByNamespaceAndRevision(namespace, revision);
+ if (moduleSchema == null) {
return null;
}
- URI _namespace_2 = qname.getNamespace();
- String _name = moduleSchema.getName();
- uriToModuleName.put(_namespace_2, _name);
- String _name_1 = moduleSchema.getName();
- module = _name_1;
+ String name = moduleSchema.getName();
+ uriToModuleName.put(namespace, name);
+ module = name;
}
-
return module + ':' + qname.getLocalName();
}
- private static CharSequence convertToRestconfIdentifier(final InstanceIdentifier.PathArgument argument, final DataNodeContainer node, SchemaContext schemaContext) {
+
+ private static CharSequence convertToRestconfIdentifier(final InstanceIdentifier.PathArgument argument,
+ final DataNodeContainer node, final SchemaContext schemaContext) {
if (argument instanceof InstanceIdentifier.NodeIdentifier) {
- return convertContainerToRestconfIdentifier((NodeIdentifier)argument, schemaContext);
+ return convertContainerToRestconfIdentifier((NodeIdentifier) argument, schemaContext);
} else if (argument instanceof InstanceIdentifier.NodeIdentifierWithPredicates
&& node instanceof ListSchemaNode) {
- return convertListToRestconfIdentifier((NodeIdentifierWithPredicates) argument,(ListSchemaNode) node,schemaContext);
- } else {
- throw new IllegalArgumentException("Unhandled parameter types: " +
- Arrays.<Object>asList(argument, node).toString());
+ return convertListToRestconfIdentifier((NodeIdentifierWithPredicates) argument, (ListSchemaNode) node,
+ schemaContext);
+ } else {
+ throw new IllegalArgumentException("Unhandled parameter types: " + Arrays.asList(argument, node).toString());
}
}
+
private static boolean isListOrContainer(final DataSchemaNode node) {
- boolean _or = false;
- if ((node instanceof ListSchemaNode)) {
- _or = true;
- } else {
- _or = ((node instanceof ListSchemaNode) || (node instanceof ContainerSchemaNode));
+ if (node instanceof ListSchemaNode || node instanceof ContainerSchemaNode) {
+ return true;
}
- return _or;
+ return false;
}
- public static Module findModuleByNamespace(final URI namespace,SchemaContext schemaContext) {
- boolean _tripleNotEquals = (namespace != null);
- Preconditions.checkArgument(_tripleNotEquals);
+ public static Module findModuleByNamespace(final URI namespace, final SchemaContext schemaContext) {
+ Preconditions.checkArgument(namespace != null);
final Set<Module> moduleSchemas = schemaContext.findModuleByNamespace(namespace);
- Module _filterLatestModule = null;
- if (moduleSchemas!=null) {
- _filterLatestModule=filterLatestModule(moduleSchemas);
+ Module filterLatestModule = null;
+ if (moduleSchemas != null) {
+ filterLatestModule = filterLatestModule(moduleSchemas);
}
- return _filterLatestModule;
+ return filterLatestModule;
}
private static Module filterLatestModule(final Iterable<Module> modules) {
Module latestModule = Iterables.getFirst(modules, null);
for (final Module module : modules) {
- Date _revision = module.getRevision();
- Date _revision_1 = latestModule.getRevision();
- boolean _after = _revision.after(_revision_1);
- if (_after) {
+ Date revision = module.getRevision();
+ Date latestModuleRevision = latestModule.getRevision();
+ if (revision.after(latestModuleRevision)) {
latestModule = module;
}
}
return latestModule;
}
- public String findModuleNameByNamespace(final URI namespace,SchemaContext schemaContext) {
+ public String findModuleNameByNamespace(final URI namespace, final SchemaContext schemaContext) {
String moduleName = uriToModuleName.get(namespace);
- boolean _tripleEquals = (moduleName == null);
- if (_tripleEquals) {
+ if (moduleName == null) {
final Module module = findModuleByNamespace(namespace, schemaContext);
- boolean _tripleEquals_1 = (module == null);
- if (_tripleEquals_1) {
+ if (module == null) {
return null;
}
- String _name = module.getName();
- moduleName = _name;
+ moduleName = module.getName();
uriToModuleName.put(namespace, moduleName);
}
return moduleName;
* parsed yang data context
* @return Set of classes representing rpc services parsed from input stream
*/
- public static Set<Class<? extends RpcService>> rpcServicesFromInputStream(InputStream inputStream, BindingIndependentMappingService mappingService,SchemaContext schemaContext){
+ public static Set<Class<? extends RpcService>> rpcServicesFromInputStream(final InputStream inputStream,
+ final BindingIndependentMappingService mappingService, final SchemaContext schemaContext) {
try {
DocumentBuilderFactory documentBuilder = DocumentBuilderFactory.newInstance();
documentBuilder.setNamespaceAware(true);
Document doc = builder.parse(inputStream);
Element rootElement = doc.getDocumentElement();
- List<Node<?>> domNodes = XmlDocumentUtils.toDomNodes(rootElement, Optional.of(schemaContext.getChildNodes()));
+ List<Node<?>> domNodes = XmlDocumentUtils.toDomNodes(rootElement,
+ Optional.of(schemaContext.getChildNodes()));
Set<Class<? extends RpcService>> rpcServices = new HashSet<Class<? extends RpcService>>();
- for (Node<?> node:domNodes){
- if (node instanceof ImmutableCompositeNode){
- ImmutableCompositeNode icNode = (ImmutableCompositeNode)node;
+ for (Node<?> node : domNodes) {
+ if (node instanceof ImmutableCompositeNode) {
+ ImmutableCompositeNode icNode = (ImmutableCompositeNode) node;
QName namespace = null;
QName revision = null;
QName name = null;
- for (QName q:icNode.keySet()){
- if (q.getLocalName().equals("namespace")){
+ for (QName q : icNode.keySet()) {
+ if (q.getLocalName().equals("namespace")) {
namespace = q;
}
- if (q.getLocalName().equals("revision")){
+ if (q.getLocalName().equals("revision")) {
revision = q;
}
- if (q.getLocalName().equals("name")){
+ if (q.getLocalName().equals("name")) {
name = q;
}
}
- //FIXME: Method getRpcServiceClassFor has been modified and fixed to follow API contract. This call MUST be updated to follow
- // contract i.e. pass correct parameters: "NAMESPACE" and "REVISION"
- Optional<Class<? extends RpcService>> rpcService = mappingService.getRpcServiceClassFor(icNode.get(name).get(0).getValue().toString(),icNode.get(revision).get(0).getValue().toString());
- if (rpcService.isPresent()){
+ // FIXME: Method getRpcServiceClassFor has been modified and
+ // fixed to follow API contract. This call MUST be updated
+ // to follow contract i.e. pass correct parameters:
+ // "NAMESPACE" and "REVISION"
+ Optional<Class<? extends RpcService>> rpcService = mappingService.getRpcServiceClassFor(
+ icNode.get(name).get(0).getValue().toString(), icNode.get(revision).get(0).getValue()
+ .toString());
+ if (rpcService.isPresent()) {
rpcServices.add(rpcService.get());
}
}
return rpcServices;
} catch (ParserConfigurationException e) {
- logger.trace("Parse configuration exception {}",e);
+ logger.trace("Parse configuration exception {}", e);
} catch (SAXException e) {
- logger.trace("SAX exception {}",e);
+ logger.trace("SAX exception {}", e);
} catch (IOException e) {
- logger.trace("IOException {}",e);
+ logger.trace("IOException {}", e);
}
return null;
}
* yang data schema node representation of resulting data object
* @return DataObject instance parsed from input stream
*/
- public static DataObject dataObjectFromInputStream(org.opendaylight.yangtools.yang.binding.InstanceIdentifier<?> path, InputStream inputStream, SchemaContext schemaContext, BindingIndependentMappingService mappingService, DataSchemaNode dataSchema) {
+ public static DataObject dataObjectFromInputStream(
+ final org.opendaylight.yangtools.yang.binding.InstanceIdentifier<?> path, final InputStream inputStream,
+ final SchemaContext schemaContext, final BindingIndependentMappingService mappingService,
+ final DataSchemaNode dataSchema) {
// Parse stream into w3c Document
try {
DocumentBuilderFactory documentBuilder = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = documentBuilder.newDocumentBuilder();
Document doc = builder.parse(inputStream);
Element rootElement = doc.getDocumentElement();
- Node<?> domNode = XmlDocumentUtils.toDomNode(rootElement, Optional.of(dataSchema), Optional.<XmlCodecProvider>absent());
- DataObject dataObject = mappingService.dataObjectFromDataDom(path, (CompositeNode) domNode); //getDataFromResponse
+ Node<?> domNode = XmlDocumentUtils.toDomNode(rootElement, Optional.of(dataSchema),
+ Optional.<XmlCodecProvider> absent());
+ DataObject dataObject = mappingService.dataObjectFromDataDom(path, (CompositeNode) domNode); // getDataFromResponse
return dataObject;
} catch (DeserializationException e) {
- logger.trace("Deserialization exception {}",e);
+ logger.trace("Deserialization exception {}", e);
} catch (ParserConfigurationException e) {
- logger.trace("Parse configuration exception {}",e);
+ logger.trace("Parse configuration exception {}", e);
} catch (SAXException e) {
logger.trace("SAX exception {}", e);
} catch (IOException e) {