Move NetconfMessage into netconf.api.messages
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / rpc / SimulatedCreateSubscription.java
index 9ec5795de21f0b87d6d7d727ed4d03a4a56916f1..764cd061bcdb3042143f4252dfc7f940f05fcf0e 100644 (file)
@@ -5,19 +5,17 @@
  * 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.test.tool.rpc;
 
-import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Maps;
 import java.io.File;
 import java.io.IOException;
 import java.text.SimpleDateFormat;
-import java.util.Collections;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
@@ -25,51 +23,49 @@ import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Unmarshaller;
 import javax.xml.bind.annotation.XmlRootElement;
-import org.opendaylight.controller.config.util.xml.DocumentedException;
-import org.opendaylight.controller.config.util.xml.XmlElement;
-import org.opendaylight.controller.config.util.xml.XmlUtil;
-import org.opendaylight.netconf.api.NetconfMessage;
+import org.opendaylight.netconf.api.messages.NetconfMessage;
+import org.opendaylight.netconf.api.xml.XmlElement;
 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
-import org.opendaylight.netconf.impl.NetconfServerSession;
-import org.opendaylight.netconf.impl.mapping.operations.DefaultNetconfOperation;
-import org.opendaylight.netconf.util.mapping.AbstractLastNetconfOperation;
+import org.opendaylight.netconf.api.xml.XmlUtil;
+import org.opendaylight.netconf.server.NetconfServerSession;
+import org.opendaylight.netconf.server.api.operations.AbstractLastNetconfOperation;
+import org.opendaylight.netconf.server.mapping.operations.DefaultNetconfOperation;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.xml.sax.SAXException;
 
 public class SimulatedCreateSubscription extends AbstractLastNetconfOperation implements DefaultNetconfOperation {
-
     private final Map<Notification, NetconfMessage> notifications;
+
     private NetconfServerSession session;
     private ScheduledExecutorService scheduledExecutorService;
 
-    public SimulatedCreateSubscription(final String id, final Optional<File> notificationsFile) {
-        super(id);
-
-        Optional<Notifications> notifications;
+    public SimulatedCreateSubscription(final SessionIdType sessionId, final Optional<File> notificationsFile) {
+        super(sessionId);
 
+        final Optional<Notifications> notifs;
         if (notificationsFile.isPresent()) {
-            notifications = Optional.of(loadNotifications(notificationsFile.get()));
+            notifs = Optional.of(loadNotifications(notificationsFile.orElseThrow()));
             scheduledExecutorService = Executors.newScheduledThreadPool(1);
         } else {
-            notifications = Optional.absent();
+            notifs = Optional.empty();
         }
 
-        if (notifications.isPresent()) {
-            Map<Notification, NetconfMessage> preparedMessages = Maps.newHashMapWithExpectedSize(
-                notifications.get().getNotificationList().size());
-            for (final Notification notification : notifications.get().getNotificationList()) {
+        if (notifs.isPresent()) {
+            final List<Notification> toCopy = notifs.orElseThrow().getNotificationList();
+            final Map<Notification, NetconfMessage> preparedMessages = Maps.newHashMapWithExpectedSize(toCopy.size());
+            for (final Notification notification : toCopy) {
                 final NetconfMessage parsedNotification = parseNetconfNotification(notification.getContent());
                 preparedMessages.put(notification, parsedNotification);
             }
-            this.notifications = preparedMessages;
+            notifications = preparedMessages;
         } else {
-            this.notifications = Collections.emptyMap();
+            notifications = Map.of();
         }
-
     }
 
-    private Notifications loadNotifications(final File file) {
+    private static Notifications loadNotifications(final File file) {
         try {
             final JAXBContext jaxbContext = JAXBContext.newInstance(Notifications.class);
             final Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
@@ -90,8 +86,7 @@ public class SimulatedCreateSubscription extends AbstractLastNetconfOperation im
     }
 
     @Override
-    protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement)
-            throws DocumentedException {
+    protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) {
         long delayAggregator = 0;
 
         for (final Map.Entry<Notification, NetconfMessage> notification : notifications.entrySet()) {
@@ -99,16 +94,13 @@ public class SimulatedCreateSubscription extends AbstractLastNetconfOperation im
 
                 delayAggregator += notification.getKey().getDelayInSeconds();
 
-                scheduledExecutorService.schedule(new Runnable() {
-                    @Override
-                    public void run() {
-                        Preconditions.checkState(session != null, "Session is not set, cannot process notifications");
-                        session.sendMessage(notification.getValue());
-                    }
+                scheduledExecutorService.schedule(() -> {
+                    Preconditions.checkState(session != null, "Session is not set, cannot process notifications");
+                    session.sendMessage(notification.getValue());
                 }, delayAggregator, TimeUnit.SECONDS);
             }
         }
-        return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
+        return document.createElement(XmlNetconfConstants.OK);
     }
 
     private static NetconfMessage parseNetconfNotification(String content) {
@@ -127,8 +119,8 @@ public class SimulatedCreateSubscription extends AbstractLastNetconfOperation im
     }
 
     @Override
-    public void setNetconfSession(final NetconfServerSession session) {
-        this.session = session;
+    public void setNetconfSession(final NetconfServerSession newSession) {
+        session = newSession;
     }
 
     @XmlRootElement(name = "notifications")
@@ -143,7 +135,7 @@ public class SimulatedCreateSubscription extends AbstractLastNetconfOperation im
 
         @Override
         public String toString() {
-            final StringBuffer sb = new StringBuffer("Notifications{");
+            final StringBuilder sb = new StringBuilder("Notifications{");
             sb.append("notificationList=").append(notificationList);
             sb.append('}');
             return sb.toString();
@@ -175,7 +167,7 @@ public class SimulatedCreateSubscription extends AbstractLastNetconfOperation im
 
         @Override
         public String toString() {
-            final StringBuffer sb = new StringBuffer("Notification{");
+            final StringBuilder sb = new StringBuilder("Notification{");
             sb.append("delayInSeconds=").append(delayInSeconds);
             sb.append(", times=").append(times);
             sb.append(", content='").append(content).append('\'');