Migrate users of Optional.get()
[netconf.git] / netconf / mdsal-netconf-connector / src / main / java / org / opendaylight / netconf / mdsal / connector / ops / CopyConfig.java
index c3e3cb423d84812517fc17767da4ee6da418daa1..55d28668be3175b9f777c668ae0540d6b2fc8278 100644 (file)
@@ -5,12 +5,10 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.netconf.mdsal.connector.ops;
 
 import static org.opendaylight.netconf.api.xml.XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0;
 
-import com.google.common.base.Optional;
 import java.io.IOException;
 import java.net.URI;
 import java.net.URISyntaxException;
@@ -19,22 +17,23 @@ import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.ExecutionException;
 import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
 import javax.xml.transform.dom.DOMResult;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
 import org.opendaylight.netconf.api.DocumentedException;
-import org.opendaylight.netconf.api.DocumentedException.ErrorSeverity;
-import org.opendaylight.netconf.api.DocumentedException.ErrorTag;
-import org.opendaylight.netconf.api.DocumentedException.ErrorType;
 import org.opendaylight.netconf.api.xml.XmlElement;
 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
 import org.opendaylight.netconf.api.xml.XmlUtil;
 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
 import org.opendaylight.netconf.mdsal.connector.TransactionProvider;
+import org.opendaylight.yangtools.yang.common.ErrorSeverity;
+import org.opendaylight.yangtools.yang.common.ErrorTag;
+import org.opendaylight.yangtools.yang.common.ErrorType;
 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;
@@ -45,9 +44,7 @@ import org.opendaylight.yangtools.yang.data.codec.xml.XMLStreamNormalizedNodeStr
 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
@@ -81,20 +78,16 @@ public final class CopyConfig extends AbstractEdit {
         final String target = targetElement.getName();
         if (Datastore.running.toString().equals(target)) {
             throw new DocumentedException("edit-config on running datastore is not supported",
-                ErrorType.PROTOCOL,
-                ErrorTag.OPERATION_NOT_SUPPORTED,
-                ErrorSeverity.ERROR);
+                ErrorType.PROTOCOL, ErrorTag.OPERATION_NOT_SUPPORTED, ErrorSeverity.ERROR);
         } else if (Datastore.candidate.toString().equals(target)) {
             copyToCandidate(operationElement);
         } else if (URL_KEY.equals(target)) {
             copyToUrl(targetElement, operationElement);
         } else {
             throw new DocumentedException("Unsupported target: " + target,
-                ErrorType.PROTOCOL,
-                ErrorTag.BAD_ELEMENT,
-                ErrorSeverity.ERROR);
+                ErrorType.PROTOCOL, ErrorTag.BAD_ELEMENT, ErrorSeverity.ERROR);
         }
-        return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.absent());
+        return document.createElement(XmlNetconfConstants.OK);
     }
 
     private void copyToCandidate(final XmlElement operationElement)
@@ -104,16 +97,15 @@ public final class CopyConfig extends AbstractEdit {
 
         // <copy-config>, unlike <edit-config>, always replaces entire configuration,
         // so remove old configuration first:
-        final DOMDataReadWriteTransaction rwTx = transactionProvider.getOrCreateTransaction();
-        rwTx.put(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY, EMPTY_ROOT_NODE);
+        final DOMDataTreeReadWriteTransaction rwTx = transactionProvider.getOrCreateTransaction();
+        rwTx.put(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty(), EMPTY_ROOT_NODE);
 
         // Then create nodes present in the <config> element:
         for (final XmlElement element : configElements) {
-            final String ns = element.getNamespace();
-            final DataSchemaNode schemaNode = getSchemaNodeFromNamespace(ns, element);
             final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
-            parseIntoNormalizedNode(schemaNode, element, ImmutableNormalizedNodeStreamWriter.from(resultHolder));
-            final NormalizedNode<?, ?> data = resultHolder.getResult();
+            parseIntoNormalizedNode(getSchemaNodeFromNamespace(element.getNamespace(), element), element,
+                ImmutableNormalizedNodeStreamWriter.from(resultHolder));
+            final NormalizedNode data = resultHolder.getResult();
             final YangInstanceIdentifier path = YangInstanceIdentifier.create(data.getIdentifier());
             // Doing merge instead of put to support top-level list:
             rwTx.merge(LogicalDatastoreType.CONFIGURATION, path, data);
@@ -121,24 +113,16 @@ public final class CopyConfig extends AbstractEdit {
     }
 
     private static XmlElement getSourceElement(final XmlElement parent) throws DocumentedException {
-        final Optional<XmlElement> sourceElement = parent.getOnlyChildElementOptionally(SOURCE_KEY);
-        if (!sourceElement.isPresent()) {
-            throw new DocumentedException("<source> element is missing",
-                DocumentedException.ErrorType.PROTOCOL,
-                DocumentedException.ErrorTag.MISSING_ELEMENT,
-                DocumentedException.ErrorSeverity.ERROR);
-        }
-
-        return sourceElement.get();
+        return parent.getOnlyChildElementOptionally(SOURCE_KEY)
+            .orElseThrow(() ->  new DocumentedException("<source> element is missing",
+                ErrorType.PROTOCOL, ErrorTag.MISSING_ELEMENT, ErrorSeverity.ERROR));
     }
 
     private void copyToUrl(final XmlElement urlElement, final XmlElement operationElement) throws DocumentedException {
         final String url = urlElement.getTextContent();
         if (!url.startsWith("file:")) {
             throw new DocumentedException("Unsupported <url> protocol: " + url,
-                ErrorType.PROTOCOL,
-                ErrorTag.OPERATION_NOT_SUPPORTED,
-                ErrorSeverity.ERROR);
+                ErrorType.PROTOCOL, ErrorTag.OPERATION_NOT_SUPPORTED, ErrorSeverity.ERROR);
         }
 
         // Read data from datastore:
@@ -156,29 +140,25 @@ public final class CopyConfig extends AbstractEdit {
             Files.write(file, xml.getBytes(StandardCharsets.UTF_8));
         } catch (URISyntaxException | IllegalArgumentException e) {
             throw new DocumentedException("Invalid URI: " + url, e,
-                ErrorType.RPC,
-                ErrorTag.INVALID_VALUE,
-                ErrorSeverity.ERROR);
+                ErrorType.RPC, ErrorTag.INVALID_VALUE, ErrorSeverity.ERROR);
         } catch (IOException e) {
             throw new DocumentedException("Failed to write : " + url, e,
-                ErrorType.APPLICATION,
-                ErrorTag.OPERATION_FAILED,
-                ErrorSeverity.ERROR);
+                ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, ErrorSeverity.ERROR);
         }
     }
 
     private ContainerNode readData(final XmlElement source) throws DocumentedException {
         final Datastore sourceDatastore = getDatastore(source);
-        final DOMDataReadWriteTransaction rwTx = getTransaction(sourceDatastore);
-        final YangInstanceIdentifier dataRoot = YangInstanceIdentifier.EMPTY;
+        final DOMDataTreeReadWriteTransaction rwTx = getTransaction(sourceDatastore);
+        final YangInstanceIdentifier dataRoot = YangInstanceIdentifier.empty();
         try {
-            final Optional<NormalizedNode<?, ?>> normalizedNodeOptional = rwTx.read(
-                LogicalDatastoreType.CONFIGURATION, dataRoot).checkedGet();
+            final Optional<NormalizedNode> normalizedNodeOptional = rwTx.read(
+                LogicalDatastoreType.CONFIGURATION, dataRoot).get();
             if (sourceDatastore == Datastore.running) {
                 transactionProvider.abortRunningTransaction(rwTx);
             }
-            return (ContainerNode) normalizedNodeOptional.get();
-        } catch (ReadFailedException e) {
+            return (ContainerNode) normalizedNodeOptional.orElseThrow();
+        } catch (InterruptedException | ExecutionException e) {
             throw new IllegalStateException("Unable to read data " + dataRoot, e);
         }
     }
@@ -188,13 +168,11 @@ public final class CopyConfig extends AbstractEdit {
             return Datastore.valueOf(source.getName());
         } catch (IllegalArgumentException e) {
             throw new DocumentedException("Unsupported source for <url> target", e,
-                ErrorType.PROTOCOL,
-                ErrorTag.OPERATION_NOT_SUPPORTED,
-                ErrorSeverity.ERROR);
+                ErrorType.PROTOCOL, ErrorTag.OPERATION_NOT_SUPPORTED, ErrorSeverity.ERROR);
         }
     }
 
-    private DOMDataReadWriteTransaction getTransaction(final Datastore datastore) throws DocumentedException {
+    private DOMDataTreeReadWriteTransaction getTransaction(final Datastore datastore) throws DocumentedException {
         if (datastore == Datastore.candidate) {
             return transactionProvider.getOrCreateTransaction();
         } else if (datastore == Datastore.running) {
@@ -210,16 +188,17 @@ public final class CopyConfig extends AbstractEdit {
         try {
             final XMLStreamWriter xmlWriter = XML_OUTPUT_FACTORY.createXMLStreamWriter(result);
             final NormalizedNodeStreamWriter nnStreamWriter = XMLStreamNormalizedNodeStreamWriter.create(xmlWriter,
-                schemaContext.getCurrentContext(), SchemaPath.ROOT);
+                schemaContext.getCurrentContext());
 
             final NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(nnStreamWriter, true);
-            for (DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> child : data.getValue()) {
+            for (DataContainerChild child : data.body()) {
                 nnWriter.write(child);
             }
             nnWriter.flush();
             xmlWriter.flush();
         } catch (XMLStreamException | IOException e) {
-            throw new RuntimeException(e);
+            // FIXME: throw DocumentedException
+            throw new IllegalStateException(e);
         }
         return result.getNode();
     }