cdaa00bc131ff1bc12b06c9c3aee4438221a82d7
[netconf.git] / netconf / messagebus-netconf / src / main / java / org / opendaylight / netconf / messagebus / eventsources / netconf / NetconfEventSource.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.messagebus.eventsources.netconf;
10
11 import static com.google.common.util.concurrent.Futures.immediateFuture;
12
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15 import com.google.common.collect.ArrayListMultimap;
16 import com.google.common.collect.Maps;
17 import com.google.common.collect.Multimap;
18 import com.google.common.collect.Multimaps;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Date;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import java.util.Set;
28 import java.util.concurrent.Future;
29 import java.util.regex.Pattern;
30 import javax.xml.stream.XMLStreamException;
31 import javax.xml.transform.dom.DOMResult;
32 import javax.xml.transform.dom.DOMSource;
33 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
34 import org.opendaylight.controller.md.sal.dom.api.DOMEvent;
35 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
36 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
37 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
38 import org.opendaylight.controller.messagebus.app.util.TopicDOMNotification;
39 import org.opendaylight.controller.messagebus.app.util.Util;
40 import org.opendaylight.controller.messagebus.spi.EventSource;
41 import org.opendaylight.netconf.api.xml.XmlUtil;
42 import org.opendaylight.netconf.util.NetconfUtil;
43 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.NotificationPattern;
44 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicId;
45 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicNotification;
46 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.DisJoinTopicInput;
47 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.JoinTopicInput;
48 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.JoinTopicOutput;
49 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.JoinTopicOutputBuilder;
50 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.JoinTopicStatus;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
53 import org.opendaylight.yangtools.yang.common.QName;
54 import org.opendaylight.yangtools.yang.common.RpcResult;
55 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
56 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
57 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
58 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
59 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
60 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
61 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
62 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
63 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
66 import org.w3c.dom.Document;
67 import org.w3c.dom.Element;
68
69 /**
70  * NetconfEventSource serves as proxy between nodes and messagebus. Subscribers can join topic stream from this source.
71  * Then they will receive notifications from device that matches pattern specified by topic.
72  */
73 public class NetconfEventSource implements EventSource, DOMNotificationListener {
74
75     private static final Logger LOG = LoggerFactory.getLogger(NetconfEventSource.class);
76
77     private static final NodeIdentifier TOPIC_NOTIFICATION_ARG = NodeIdentifier.create(TopicNotification.QNAME);
78     private static final NodeIdentifier EVENT_SOURCE_ARG = NodeIdentifier.create(
79             QName.create(TopicNotification.QNAME, "node-id"));
80     private static final NodeIdentifier TOPIC_ID_ARG = NodeIdentifier.create(
81             QName.create(TopicNotification.QNAME, "topic-id"));
82     private static final NodeIdentifier PAYLOAD_ARG = NodeIdentifier.create(
83             QName.create(TopicNotification.QNAME, "payload"));
84     private static final String CONNECTION_NOTIFICATION_SOURCE_NAME = "ConnectionNotificationSource";
85
86     private final DOMNotificationPublishService domPublish;
87
88     private final Map<String, String> urnPrefixToStreamMap; // key = urnPrefix, value = StreamName
89
90     /**
91      * Map notification uri -> registrations.
92      */
93     private final Multimap<String, NotificationTopicRegistration>
94             notificationTopicRegistrations = Multimaps.synchronizedListMultimap(ArrayListMultimap.create());
95     private final NetconfEventSourceMount mount;
96
97     /**
98      * Creates new NetconfEventSource for node. Topic notifications will be published via provided
99      * {@link DOMNotificationPublishService}
100      *
101      * @param streamMap      netconf streams from device
102      * @param publishService publish service
103      */
104     public NetconfEventSource(final Map<String, String> streamMap,
105                               final NetconfEventSourceMount mount,
106                               final DOMNotificationPublishService publishService) {
107         this.mount = mount;
108         this.urnPrefixToStreamMap = Preconditions.checkNotNull(streamMap);
109         this.domPublish = Preconditions.checkNotNull(publishService);
110         this.initializeNotificationTopicRegistrationList();
111
112         LOG.info("NetconfEventSource [{}] created.", mount.getNodeId());
113     }
114
115     /**
116      * Creates {@link ConnectionNotificationTopicRegistration} for connection. Also creates
117      * {@link StreamNotificationTopicRegistration} for every prefix and available stream as defined in config file.
118      */
119     private void initializeNotificationTopicRegistrationList() {
120         final ConnectionNotificationTopicRegistration cntr =
121                 new ConnectionNotificationTopicRegistration(CONNECTION_NOTIFICATION_SOURCE_NAME, this);
122         notificationTopicRegistrations
123                 .put(cntr.getNotificationUrnPrefix(), cntr);
124         Map<String, Stream> availableStreams = getAvailableStreams();
125         LOG.debug("Stream configuration compare...");
126         for (Entry<String, String> entry : this.urnPrefixToStreamMap.entrySet()) {
127             String urnPrefix = entry.getKey();
128             final String streamName = entry.getValue();
129             LOG.debug("urnPrefix: {} streamName: {}", urnPrefix, streamName);
130             if (availableStreams.containsKey(streamName)) {
131                 LOG.debug("Stream containig on device");
132                 notificationTopicRegistrations
133                         .put(urnPrefix, new StreamNotificationTopicRegistration(availableStreams.get(streamName),
134                                 urnPrefix, this));
135             }
136         }
137     }
138
139     private Map<String, Stream> getAvailableStreams() {
140         Map<String, Stream> streamMap = new HashMap<>();
141         final List<Stream> availableStreams;
142         try {
143             availableStreams = mount.getAvailableStreams();
144             streamMap = Maps.uniqueIndex(availableStreams, input -> input.getName().getValue());
145         } catch (ReadFailedException e) {
146             LOG.warn("Can not read streams for node {}", mount.getNodeId());
147         }
148         return streamMap;
149     }
150
151     @Override
152     public Future<RpcResult<JoinTopicOutput>> joinTopic(final JoinTopicInput input) {
153         LOG.debug("Join topic {} on {}", input.getTopicId().getValue(), mount.getNodeId());
154         final NotificationPattern notificationPattern = input.getNotificationPattern();
155         final List<SchemaPath> matchingNotifications = getMatchingNotifications(notificationPattern);
156         return registerTopic(input.getTopicId(), matchingNotifications);
157
158     }
159
160     @Override
161     public Future<RpcResult<Void>> disJoinTopic(final DisJoinTopicInput input) {
162         for (NotificationTopicRegistration reg : notificationTopicRegistrations.values()) {
163             reg.unRegisterNotificationTopic(input.getTopicId());
164         }
165         return Util.resultRpcSuccessFor((Void) null);
166     }
167
168     private synchronized Future<RpcResult<JoinTopicOutput>> registerTopic(
169             final TopicId topicId,
170             final List<SchemaPath> notificationsToSubscribe) {
171         Preconditions.checkNotNull(notificationsToSubscribe);
172         LOG.debug("Join topic {} - register", topicId);
173         JoinTopicStatus joinTopicStatus = JoinTopicStatus.Down;
174
175         LOG.debug("Notifications to subscribe has found - count {}", notificationsToSubscribe.size());
176         int registeredNotificationCount = 0;
177         for (SchemaPath schemaPath : notificationsToSubscribe) {
178             final Collection<NotificationTopicRegistration> topicRegistrations =
179                     notificationTopicRegistrations.get(schemaPath.getLastComponent().getNamespace().toString());
180             for (NotificationTopicRegistration reg : topicRegistrations) {
181                 LOG.info("Source of notification {} is activating, TopicId {}", reg.getSourceName(),
182                         topicId.getValue());
183                 boolean regSuccess = reg.registerNotificationTopic(schemaPath, topicId);
184                 if (regSuccess) {
185                     registeredNotificationCount = registeredNotificationCount + 1;
186                 }
187             }
188         }
189         if (registeredNotificationCount > 0) {
190             joinTopicStatus = JoinTopicStatus.Up;
191         }
192         final JoinTopicOutput output = new JoinTopicOutputBuilder().setStatus(joinTopicStatus).build();
193         return immediateFuture(RpcResultBuilder.success(output).build());
194
195     }
196
197     public void reActivateStreams() {
198         for (NotificationTopicRegistration reg : notificationTopicRegistrations.values()) {
199             LOG.info("Source of notification {} is reactivating on node {}", reg.getSourceName(), mount.getNodeId());
200             reg.reActivateNotificationSource();
201         }
202     }
203
204     public void deActivateStreams() {
205         for (NotificationTopicRegistration reg : notificationTopicRegistrations.values()) {
206             LOG.info("Source of notification {} is deactivating on node {}", reg.getSourceName(), mount.getNodeId());
207             reg.deActivateNotificationSource();
208         }
209     }
210
211     @Override
212     public void onNotification(final DOMNotification notification) {
213         SchemaPath notificationPath = notification.getType();
214         Date notificationEventTime = null;
215         if (notification instanceof DOMEvent) {
216             notificationEventTime = ((DOMEvent) notification).getEventTime();
217         }
218         final String namespace = notification.getType().getLastComponent().getNamespace().toString();
219         for (NotificationTopicRegistration notifReg : notificationTopicRegistrations.get(namespace)) {
220             notifReg.setLastEventTime(notificationEventTime);
221             Set<TopicId> topicIdsForNotification = notifReg.getTopicsForNotification(notificationPath);
222             for (TopicId topicId : topicIdsForNotification) {
223                 publishNotification(notification, topicId);
224                 LOG.debug("Notification {} has been published for TopicId {}", notification.getType(),
225                         topicId.getValue());
226             }
227         }
228     }
229
230     private void publishNotification(final DOMNotification notification, final TopicId topicId) {
231         final ContainerNode topicNotification = Builders.containerBuilder().withNodeIdentifier(TOPIC_NOTIFICATION_ARG)
232                 .withChild(ImmutableNodes.leafNode(TOPIC_ID_ARG, topicId))
233                 .withChild(ImmutableNodes.leafNode(EVENT_SOURCE_ARG, mount.getNodeId()))
234                 .withChild(encapsulate(notification))
235                 .build();
236         try {
237             domPublish.putNotification(new TopicDOMNotification(topicNotification));
238         } catch (final InterruptedException e) {
239             throw new RuntimeException(e);
240         }
241     }
242
243     private AnyXmlNode encapsulate(final DOMNotification body) {
244         // FIXME: Introduce something like YangModeledAnyXmlNode in Yangtools
245         final Document doc = XmlUtil.newDocument();
246         final Optional<String> namespace = Optional.of(PAYLOAD_ARG.getNodeType().getNamespace().toString());
247         final Element element = XmlUtil.createElement(doc, "payload", namespace);
248
249         final DOMResult result = new DOMResult(element);
250
251         final SchemaContext context = mount.getSchemaContext();
252         final SchemaPath schemaPath = body.getType();
253         try {
254             NetconfUtil.writeNormalizedNode(body.getBody(), result, schemaPath, context);
255             return Builders.anyXmlBuilder().withNodeIdentifier(PAYLOAD_ARG).withValue(new DOMSource(element)).build();
256         } catch (IOException | XMLStreamException e) {
257             LOG.error("Unable to encapsulate notification.", e);
258             throw new RuntimeException(e);
259         }
260     }
261
262     /**
263      * Returns all available notification paths that matches given pattern.
264      *
265      * @param notificationPattern pattern
266      * @return notification paths
267      */
268     private List<SchemaPath> getMatchingNotifications(final NotificationPattern notificationPattern) {
269         final String regex = notificationPattern.getValue();
270
271         final Pattern pattern = Pattern.compile(regex);
272         List<SchemaPath> availableNotifications = getAvailableNotifications();
273         return Util.expandQname(availableNotifications, pattern);
274     }
275
276     @Override
277     public void close() throws Exception {
278         for (NotificationTopicRegistration streamReg : notificationTopicRegistrations.values()) {
279             streamReg.close();
280         }
281     }
282
283     @Override
284     public NodeKey getSourceNodeKey() {
285         return mount.getNode().getKey();
286     }
287
288     @Override
289     public List<SchemaPath> getAvailableNotifications() {
290
291         final List<SchemaPath> availNotifList = new ArrayList<>();
292         // add Event Source Connection status notification
293         availNotifList.add(ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH);
294
295         final Set<NotificationDefinition> availableNotifications = mount.getSchemaContext()
296                 .getNotifications();
297         // add all known notifications from netconf device
298         for (final NotificationDefinition nd : availableNotifications) {
299             availNotifList.add(nd.getPath());
300         }
301         return availNotifList;
302     }
303
304     NetconfEventSourceMount getMount() {
305         return mount;
306     }
307
308 }