* 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;
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();
}
@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()) {
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) {
}
@Override
- public void setNetconfSession(final NetconfServerSession session) {
- this.session = session;
+ public void setNetconfSession(final NetconfServerSession newSession) {
+ session = newSession;
}
@XmlRootElement(name = "notifications")
@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();
@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('\'');