2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.netconf.test.tool.rpc;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Maps;
15 import java.io.IOException;
16 import java.text.SimpleDateFormat;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.Date;
20 import java.util.List;
22 import java.util.concurrent.Executors;
23 import java.util.concurrent.ScheduledExecutorService;
24 import java.util.concurrent.TimeUnit;
25 import javax.xml.bind.JAXBContext;
26 import javax.xml.bind.JAXBException;
27 import javax.xml.bind.Unmarshaller;
28 import javax.xml.bind.annotation.XmlRootElement;
29 import org.opendaylight.netconf.api.NetconfMessage;
30 import org.opendaylight.netconf.api.xml.XmlElement;
31 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
32 import org.opendaylight.netconf.api.xml.XmlUtil;
33 import org.opendaylight.netconf.impl.NetconfServerSession;
34 import org.opendaylight.netconf.impl.mapping.operations.DefaultNetconfOperation;
35 import org.opendaylight.netconf.util.mapping.AbstractLastNetconfOperation;
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Element;
38 import org.xml.sax.SAXException;
40 public class SimulatedCreateSubscription extends AbstractLastNetconfOperation implements DefaultNetconfOperation {
42 private final Map<Notification, NetconfMessage> notifications;
43 private NetconfServerSession session;
44 private ScheduledExecutorService scheduledExecutorService;
46 public SimulatedCreateSubscription(final String id, final Optional<File> notificationsFile) {
49 final Optional<Notifications> notifs;
51 if (notificationsFile.isPresent()) {
52 notifs = Optional.of(loadNotifications(notificationsFile.get()));
53 scheduledExecutorService = Executors.newScheduledThreadPool(1);
55 notifs = Optional.absent();
58 if (notifs.isPresent()) {
59 final Collection<Notification> toCopy = notifs.get().getNotificationList();
60 final Map<Notification, NetconfMessage> preparedMessages = Maps.newHashMapWithExpectedSize(toCopy.size());
61 for (final Notification notification : toCopy) {
62 final NetconfMessage parsedNotification = parseNetconfNotification(notification.getContent());
63 preparedMessages.put(notification, parsedNotification);
65 this.notifications = preparedMessages;
67 this.notifications = Collections.emptyMap();
71 private static Notifications loadNotifications(final File file) {
73 final JAXBContext jaxbContext = JAXBContext.newInstance(Notifications.class);
74 final Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
75 return (Notifications) jaxbUnmarshaller.unmarshal(file);
76 } catch (final JAXBException e) {
77 throw new IllegalArgumentException("Canot parse file " + file + " as a notifications file", e);
82 protected String getOperationName() {
83 return "create-subscription";
87 protected String getOperationNamespace() {
88 return "urn:ietf:params:xml:ns:netconf:notification:1.0";
92 protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) {
93 long delayAggregator = 0;
95 for (final Map.Entry<Notification, NetconfMessage> notification : notifications.entrySet()) {
96 for (int i = 0; i <= notification.getKey().getTimes(); i++) {
98 delayAggregator += notification.getKey().getDelayInSeconds();
100 scheduledExecutorService.schedule(() -> {
101 Preconditions.checkState(session != null, "Session is not set, cannot process notifications");
102 session.sendMessage(notification.getValue());
103 }, delayAggregator, TimeUnit.SECONDS);
106 return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.absent());
109 private static NetconfMessage parseNetconfNotification(String content) {
110 final int startEventTime = content.indexOf("<eventTime>") + "<eventTime>".length();
111 final int endEventTime = content.indexOf("</eventTime>");
112 final String eventTime = content.substring(startEventTime, endEventTime);
113 if (eventTime.equals("XXXX")) {
114 content = content.replace(eventTime, new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX").format(new Date()));
118 return new NetconfMessage(XmlUtil.readXmlToDocument(content));
119 } catch (SAXException | IOException e) {
120 throw new IllegalArgumentException("Cannot parse notifications", e);
125 public void setNetconfSession(final NetconfServerSession newSession) {
126 this.session = newSession;
129 @XmlRootElement(name = "notifications")
130 public static final class Notifications {
132 @javax.xml.bind.annotation.XmlElement(nillable = false, name = "notification", required = true)
133 private List<Notification> notificationList;
135 public List<Notification> getNotificationList() {
136 return notificationList;
140 public String toString() {
141 final StringBuilder sb = new StringBuilder("Notifications{");
142 sb.append("notificationList=").append(notificationList);
144 return sb.toString();
148 public static final class Notification {
150 @javax.xml.bind.annotation.XmlElement(nillable = false, name = "delay")
151 private long delayInSeconds;
153 @javax.xml.bind.annotation.XmlElement(nillable = false, name = "times")
156 @javax.xml.bind.annotation.XmlElement(nillable = false, name = "content", required = true)
157 private String content;
159 public long getDelayInSeconds() {
160 return delayInSeconds;
163 public long getTimes() {
167 public String getContent() {
172 public String toString() {
173 final StringBuilder sb = new StringBuilder("Notification{");
174 sb.append("delayInSeconds=").append(delayInSeconds);
175 sb.append(", times=").append(times);
176 sb.append(", content='").append(content).append('\'');
178 return sb.toString();