X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=netconf%2Fsal-netconf-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Fsal%2Fconnect%2Fnetconf%2FLibraryModulesSchemas.java;h=261ba28d2978f9169c36cfdbf63253783be6d1b8;hb=1ff7b91a129bd42ac4583ac632ab095c2d7d58ad;hp=8a4ea7efd7284c1c66cecb14fa3edc8e2a193eaf;hpb=d3961ec78d49d5f2df14380e46cbb528ad78d3b9;p=netconf.git diff --git a/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/LibraryModulesSchemas.java b/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/LibraryModulesSchemas.java index 8a4ea7efd7..261ba28d29 100644 --- a/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/LibraryModulesSchemas.java +++ b/netconf/sal-netconf-connector/src/main/java/org/opendaylight/netconf/sal/connect/netconf/LibraryModulesSchemas.java @@ -13,7 +13,6 @@ import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTr import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toId; import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath; -import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.google.common.base.Strings; import com.google.common.collect.ImmutableMap; @@ -22,30 +21,37 @@ import com.google.gson.stream.JsonReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.StringReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.AbstractMap; import java.util.Collections; +import java.util.Locale; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.regex.Pattern; import javax.xml.parsers.DocumentBuilder; -import org.opendaylight.controller.config.util.xml.XmlUtil; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.stream.XMLStreamException; +import javax.xml.transform.dom.DOMSource; import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult; import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext; import org.opendaylight.netconf.sal.connect.api.NetconfDeviceSchemas; import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceRpc; import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil; import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.ModulesState; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.module.list.Module; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.ModulesState; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160621.module.list.Module; import org.opendaylight.yangtools.util.xml.UntrustedXML; import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.common.Revision; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild; @@ -67,12 +73,13 @@ import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; +import org.xml.sax.SAXException; /** * Holds URLs with YANG schema resources for all yang modules reported in * ietf-netconf-yang-library/modules-state/modules node. */ -public class LibraryModulesSchemas implements NetconfDeviceSchemas { +public final class LibraryModulesSchemas implements NetconfDeviceSchemas { private static final Logger LOG = LoggerFactory.getLogger(LibraryModulesSchemas.class); private static final Pattern DATE_PATTERN = Pattern.compile("(\\d{4}-\\d{2}-\\d{2})"); @@ -81,7 +88,7 @@ public class LibraryModulesSchemas implements NetconfDeviceSchemas { static { final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create(); moduleInfoBackedContext.registerModuleInfo(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang - .library.rev160409.$YangModuleInfoImpl.getInstance()); + .library.rev160621.$YangModuleInfoImpl.getInstance()); LIBRARY_CONTEXT = moduleInfoBackedContext.tryToCreateSchemaContext().get(); } @@ -107,7 +114,7 @@ public class LibraryModulesSchemas implements NetconfDeviceSchemas { final Map result = Maps.newHashMap(); for (final Map.Entry entry : availableModels.entrySet()) { final SourceIdentifier sId = RevisionSourceIdentifier - .create(entry.getKey().getLocalName(), Optional.fromNullable(entry.getKey().getFormattedRevision())); + .create(entry.getKey().getLocalName(), entry.getKey().getRevision()); result.put(sId, entry.getValue()); } @@ -130,7 +137,7 @@ public class LibraryModulesSchemas implements NetconfDeviceSchemas { if (connection instanceof HttpURLConnection) { connection.setRequestProperty("Accept", "application/xml"); final String userpass = username + ":" + password; - final String basicAuth = "Basic " + printBase64Binary(userpass.getBytes()); + final String basicAuth = "Basic " + printBase64Binary(userpass.getBytes(StandardCharsets.UTF_8)); connection.setRequestProperty("Authorization", basicAuth); } @@ -139,7 +146,7 @@ public class LibraryModulesSchemas implements NetconfDeviceSchemas { } catch (final IOException e) { LOG.warn("Unable to download yang library from {}", url, e); - return new LibraryModulesSchemas(Collections.emptyMap()); + return new LibraryModulesSchemas(Collections.emptyMap()); } } @@ -156,13 +163,13 @@ public class LibraryModulesSchemas implements NetconfDeviceSchemas { } catch (final ExecutionException e) { LOG.warn("{}: Unable to detect available schemas, get to {} failed", deviceId, MODULES_STATE_MODULE_LIST, e); - return new LibraryModulesSchemas(Collections.emptyMap()); + return new LibraryModulesSchemas(Collections.emptyMap()); } if (moduleListNodeResult.getErrors().isEmpty() == false) { LOG.warn("{}: Unable to detect available schemas, get to {} failed, {}", deviceId, MODULES_STATE_MODULE_LIST, moduleListNodeResult.getErrors()); - return new LibraryModulesSchemas(Collections.emptyMap()); + return new LibraryModulesSchemas(Collections.emptyMap()); } @@ -171,11 +178,11 @@ public class LibraryModulesSchemas implements NetconfDeviceSchemas { if (modulesStateNode.isPresent()) { Preconditions.checkState(modulesStateNode.get() instanceof ContainerNode, "Expecting container containing schemas, but was %s", modulesStateNode.get()); - return create(((ContainerNode) modulesStateNode.get())); - } else { - LOG.warn("{}: Unable to detect available schemas, get to {} was empty", deviceId, toId(ModulesState.QNAME)); - return new LibraryModulesSchemas(Collections.emptyMap()); + return create((ContainerNode) modulesStateNode.get()); } + + LOG.warn("{}: Unable to detect available schemas, get to {} was empty", deviceId, toId(ModulesState.QNAME)); + return new LibraryModulesSchemas(Collections.emptyMap()); } private static LibraryModulesSchemas create(final ContainerNode modulesStateNode) { @@ -219,18 +226,18 @@ public class LibraryModulesSchemas implements NetconfDeviceSchemas { } catch (final IOException e) { LOG.warn("Unable to download yang library from {}", url, e); - return new LibraryModulesSchemas(Collections.emptyMap()); + return new LibraryModulesSchemas(Collections.emptyMap()); } } private static Optional> findModulesStateNode(final NormalizedNode result) { if (result == null) { - return Optional.absent(); + return Optional.empty(); } final Optional> dataNode = ((DataContainerNode) result).getChild(toId(NETCONF_DATA_QNAME)); if (dataNode.isPresent() == false) { - return Optional.absent(); + return Optional.empty(); } return ((DataContainerNode) dataNode.get()).getChild(toId(ModulesState.QNAME)); @@ -253,7 +260,7 @@ public class LibraryModulesSchemas implements NetconfDeviceSchemas { contentType.equals("application/json") ? readJson(in) : readXml(in); if (!optionalModulesStateNode.isPresent()) { - return new LibraryModulesSchemas(Collections.emptyMap()); + return new LibraryModulesSchemas(Collections.emptyMap()); } final NormalizedNode modulesStateNode = optionalModulesStateNode.get(); @@ -283,7 +290,7 @@ public class LibraryModulesSchemas implements NetconfDeviceSchemas { return new LibraryModulesSchemas(schemasMapping.build()); } catch (final IOException e) { LOG.warn("Unable to download yang library from {}", connection.getURL(), e); - return new LibraryModulesSchemas(Collections.emptyMap()); + return new LibraryModulesSchemas(Collections.emptyMap()); } } @@ -291,7 +298,7 @@ public class LibraryModulesSchemas implements NetconfDeviceSchemas { String extension = ""; final int i = fileName.lastIndexOf(46); if (i != -1) { - extension = fileName.substring(i).toLowerCase(); + extension = fileName.substring(i).toLowerCase(Locale.ROOT); } return extension.equals(".json"); @@ -302,15 +309,13 @@ public class LibraryModulesSchemas implements NetconfDeviceSchemas { final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder); final JsonParserStream jsonParser = JsonParserStream.create(writer, LIBRARY_CONTEXT); - final JsonReader reader = new JsonReader(new InputStreamReader(in)); + final JsonReader reader = new JsonReader(new InputStreamReader(in, Charset.defaultCharset())); jsonParser.parse(reader); - return resultHolder.isFinished() - ? Optional.of(resultHolder.getResult()) : Optional.>absent(); + return resultHolder.isFinished() ? Optional.of(resultHolder.getResult()) : Optional.empty(); } - @SuppressWarnings("checkstyle:IllegalCatch") private static Optional> readXml(final InputStream in) { try { final DocumentBuilder docBuilder = UntrustedXML.newDocumentBuilder(); @@ -336,16 +341,15 @@ public class LibraryModulesSchemas implements NetconfDeviceSchemas { final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder); final XmlParserStream xmlParser = XmlParserStream.create(writer, LIBRARY_CONTEXT, LIBRARY_CONTEXT.getDataChildByName(ModulesState.QNAME)); - - xmlParser.parse(UntrustedXML.createXMLStreamReader(new StringReader(XmlUtil.toString( - doc.getDocumentElement(), false)))); + xmlParser.traverse(new DOMSource(doc.getDocumentElement())); final NormalizedNode parsed = resultHolder.getResult(); return Optional.of(parsed); - } catch (final Exception e) { + } catch (XMLStreamException | URISyntaxException | IOException | ParserConfigurationException + | SAXException e) { LOG.warn("Unable to parse yang library xml content", e); } - return Optional.>absent(); + return Optional.empty(); } private static Optional> createFromEntry(final MapEntryNode moduleNode) { @@ -359,9 +363,9 @@ public class LibraryModulesSchemas implements NetconfDeviceSchemas { childNodeId = new YangInstanceIdentifier.NodeIdentifier(QName.create(Module.QNAME, "revision")); final Optional revision = getSingleChildNodeValue(moduleNode, childNodeId); if (revision.isPresent()) { - if (!SourceIdentifier.REVISION_PATTERN.matcher(revision.get()).matches()) { + if (!Revision.STRING_FORMAT_PATTERN.matcher(revision.get()).matches()) { LOG.warn("Skipping library schema for {}. Revision {} is in wrong format.", moduleNode, revision.get()); - return Optional.>absent(); + return Optional.empty(); } } @@ -375,15 +379,15 @@ public class LibraryModulesSchemas implements NetconfDeviceSchemas { final QName moduleQName = revision.isPresent() ? QName.create(moduleNameSpace, revision.get(), moduleName) - : QName.create(URI.create(moduleNameSpace), null, moduleName); + : QName.create(URI.create(moduleNameSpace), moduleName); try { - return Optional.>of(new AbstractMap.SimpleImmutableEntry<>( + return Optional.of(new AbstractMap.SimpleImmutableEntry<>( moduleQName, new URL(schemaUriAsString.get()))); } catch (final MalformedURLException e) { LOG.warn("Skipping library schema for {}. URL {} representing yang schema resource is not valid", moduleNode, schemaUriAsString.get()); - return Optional.>absent(); + return Optional.empty(); } } @@ -397,9 +401,8 @@ public class LibraryModulesSchemas implements NetconfDeviceSchemas { private static Optional getValueOfSimpleNode( final NormalizedNode node) { - final Object value = node.getValue(); - return value == null || Strings.isNullOrEmpty(value.toString()) - ? Optional.absent() : Optional.of(value.toString().trim()); + final String valueStr = node.getValue().toString(); + return Strings.isNullOrEmpty(valueStr) ? Optional.empty() : Optional.of(valueStr.trim()); } @Override