import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
+import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
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.xml.XmlElement;
import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
+import org.opendaylight.netconf.api.xml.XmlUtil;
import org.opendaylight.netconf.impl.NetconfServerSession;
import org.opendaylight.netconf.impl.mapping.operations.DefaultNetconfOperation;
import org.opendaylight.netconf.util.mapping.AbstractLastNetconfOperation;
public SimulatedCreateSubscription(final String id, final Optional<File> notificationsFile) {
super(id);
- Optional<Notifications> notifications;
+ final Optional<Notifications> notifs;
- if(notificationsFile.isPresent()) {
- notifications = Optional.of(loadNotifications(notificationsFile.get()));
+ if (notificationsFile.isPresent()) {
+ notifs = Optional.of(loadNotifications(notificationsFile.get()));
scheduledExecutorService = Executors.newScheduledThreadPool(1);
} else {
- notifications = Optional.absent();
+ notifs = Optional.absent();
}
- if(notifications.isPresent()) {
- Map<Notification, NetconfMessage> preparedMessages = Maps.newHashMapWithExpectedSize(notifications.get().getNotificationList().size());
- for (final Notification notification : notifications.get().getNotificationList()) {
+ if (notifs.isPresent()) {
+ final Collection<Notification> toCopy = notifs.get().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);
}
} else {
this.notifications = Collections.emptyMap();
}
-
}
- 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 XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.absent());
}
private static NetconfMessage parseNetconfNotification(String content) {
final int startEventTime = content.indexOf("<eventTime>") + "<eventTime>".length();
final int endEventTime = content.indexOf("</eventTime>");
final String eventTime = content.substring(startEventTime, endEventTime);
- if(eventTime.equals("XXXX")) {
+ if (eventTime.equals("XXXX")) {
content = content.replace(eventTime, new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX").format(new Date()));
}
}
@Override
- public void setNetconfSession(final NetconfServerSession s) {
- this.session = s;
+ public void setNetconfSession(final NetconfServerSession newSession) {
+ this.session = newSession;
}
@XmlRootElement(name = "notifications")