Decouple config and netconf subsystems.
[controller.git] / opendaylight / md-sal / messagebus-impl / src / main / java / org / opendaylight / controller / 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.controller.messagebus.eventsources.netconf;
10 //
11 //import static com.google.common.util.concurrent.Futures.immediateFuture;
12 //
13 //import java.io.IOException;
14 //import java.util.ArrayList;
15 //import java.util.Date;
16 //import java.util.HashMap;
17 //import java.util.List;
18 //import java.util.Map;
19 //import java.util.Set;
20 //import java.util.concurrent.Future;
21 //import java.util.regex.Pattern;
22 //
23 //import javax.xml.stream.XMLStreamException;
24 //import javax.xml.transform.dom.DOMResult;
25 //import javax.xml.transform.dom.DOMSource;
26 //
27 //import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 //import org.opendaylight.controller.md.sal.binding.api.MountPoint;
29 //import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
30 //import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 //import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
32 //import org.opendaylight.controller.md.sal.dom.api.DOMEvent;
33 //import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
34 //import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
35 //import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
36 //import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
37 //import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
38 //import org.opendaylight.controller.messagebus.app.impl.TopicDOMNotification;
39 //import org.opendaylight.controller.messagebus.app.impl.Util;
40 //import org.opendaylight.controller.messagebus.spi.EventSource;
41 //import org.opendaylight.controller.config.util.xml.XmlUtil;
42 //import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
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.JoinTopicInput;
47 //import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.JoinTopicOutput;
48 //import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.JoinTopicOutputBuilder;
49 //import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.JoinTopicStatus;
50 //import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.DisJoinTopicInput;
51 //import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
52 //import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
53 //import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
54 //import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
55 //import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
56 //import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
57 //import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
58 //import org.opendaylight.yangtools.yang.common.QName;
59 //import org.opendaylight.yangtools.yang.common.RpcResult;
60 //import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
61 //import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
62 //import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
63 //import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
64 //import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
65 //import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
66 //import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
67 //import org.opendaylight.yangtools.yang.model.api.SchemaContext;
68 //import org.opendaylight.yangtools.yang.model.api.SchemaPath;
69 //import org.slf4j.Logger;
70 //import org.slf4j.LoggerFactory;
71 //import org.w3c.dom.Document;
72 //import org.w3c.dom.Element;
73 //
74 //import com.google.common.base.Optional;
75 //import com.google.common.base.Preconditions;
76 //import com.google.common.base.Throwables;
77 //import com.google.common.util.concurrent.CheckedFuture;
78 //
79 //public class NetconfEventSource implements EventSource, DOMNotificationListener {
80 //
81 //    private static final Logger LOG = LoggerFactory.getLogger(NetconfEventSource.class);
82 //
83 //    private static final NodeIdentifier TOPIC_NOTIFICATION_ARG = new NodeIdentifier(TopicNotification.QNAME);
84 //    private static final NodeIdentifier EVENT_SOURCE_ARG = new NodeIdentifier(QName.create(TopicNotification.QNAME, "node-id"));
85 //    private static final NodeIdentifier TOPIC_ID_ARG = new NodeIdentifier(QName.create(TopicNotification.QNAME, "topic-id"));
86 //    private static final NodeIdentifier PAYLOAD_ARG = new NodeIdentifier(QName.create(TopicNotification.QNAME, "payload"));
87 //    private static final String ConnectionNotificationSourceName = "ConnectionNotificationSource";
88 //
89 //    private final String nodeId;
90 //    private final Node node;
91 //
92 //    private final DOMMountPoint netconfMount;
93 //    private final MountPoint mountPoint;
94 //    private final DOMNotificationPublishService domPublish;
95 //
96 //    private final Map<String, String> urnPrefixToStreamMap; // key = urnPrefix, value = StreamName
97 //    private final List<NotificationTopicRegistration> notificationTopicRegistrationList = new ArrayList<>();
98 //
99 //    public NetconfEventSource(final Node node, final Map<String, String> streamMap, final DOMMountPoint netconfMount, final MountPoint mountPoint, final DOMNotificationPublishService publishService) {
100 //        this.netconfMount = Preconditions.checkNotNull(netconfMount);
101 //        this.mountPoint = Preconditions.checkNotNull(mountPoint);
102 //        this.node = Preconditions.checkNotNull(node);
103 //        this.urnPrefixToStreamMap = Preconditions.checkNotNull(streamMap);
104 //        this.domPublish = Preconditions.checkNotNull(publishService);
105 //        this.nodeId = node.getNodeId().getValue();
106 //        this.initializeNotificationTopicRegistrationList();
107 //
108 //        LOG.info("NetconfEventSource [{}] created.", this.nodeId);
109 //    }
110 //
111 //    private void initializeNotificationTopicRegistrationList() {
112 //        notificationTopicRegistrationList.add(new ConnectionNotificationTopicRegistration(ConnectionNotificationSourceName, this));
113 //        Optional<Map<String, Stream>> streamMap = getAvailableStreams();
114 //        if(streamMap.isPresent()){
115 //            LOG.debug("Stream configuration compare...");
116 //            for (String urnPrefix : this.urnPrefixToStreamMap.keySet()) {
117 //                final String streamName = this.urnPrefixToStreamMap.get(urnPrefix);
118 //                LOG.debug("urnPrefix: {} streamName: {}", urnPrefix, streamName);
119 //                if(streamMap.get().containsKey(streamName)){
120 //                    LOG.debug("Stream containig on device");
121 //                    notificationTopicRegistrationList.add(new StreamNotificationTopicRegistration(streamMap.get().get(streamName),urnPrefix, this));
122 //                }
123 //            }
124 //        }
125 //    }
126 //
127 //    private Optional<Map<String, Stream>> getAvailableStreams(){
128 //
129 //        Map<String,Stream> streamMap = null;
130 //        InstanceIdentifier<Streams> pathStream = InstanceIdentifier.builder(Netconf.class).child(Streams.class).build();
131 //        Optional<DataBroker> dataBroker = this.mountPoint.getService(DataBroker.class);
132 //
133 //        if(dataBroker.isPresent()){
134 //            LOG.debug("GET Available streams ...");
135 //            ReadOnlyTransaction tx = dataBroker.get().newReadOnlyTransaction();
136 //            CheckedFuture<Optional<Streams>, ReadFailedException> checkFeature = tx.read(LogicalDatastoreType.OPERATIONAL,pathStream);
137 //
138 //            try {
139 //                Optional<Streams> streams = checkFeature.checkedGet();
140 //                if(streams.isPresent()){
141 //                    streamMap = new HashMap<>();
142 //                    for(Stream stream : streams.get().getStream()){
143 //                        LOG.debug("*** find stream {}", stream.getName().getValue());
144 //                        streamMap.put(stream.getName().getValue(), stream);
145 //                    }
146 //                }
147 //            } catch (ReadFailedException e) {
148 //                LOG.warn("Can not read streams for node {}",this.nodeId);
149 //            }
150 //
151 //        } else {
152 //            LOG.warn("No databroker on node {}", this.nodeId);
153 //        }
154 //
155 //        return Optional.fromNullable(streamMap);
156 //    }
157 //
158 //    @Override
159 //    public Future<RpcResult<JoinTopicOutput>> joinTopic(final JoinTopicInput input) {
160 //        LOG.debug("Join topic {} on {}", input.getTopicId().getValue(), this.nodeId);
161 //        final NotificationPattern notificationPattern = input.getNotificationPattern();
162 //        final List<SchemaPath> matchingNotifications = getMatchingNotifications(notificationPattern);
163 //        return registerTopic(input.getTopicId(),matchingNotifications);
164 //
165 //    }
166 //
167 //    @Override
168 //    public Future<RpcResult<Void>> disJoinTopic(DisJoinTopicInput input) {
169 //         for(NotificationTopicRegistration reg : notificationTopicRegistrationList){
170 //             reg.unRegisterNotificationTopic(input.getTopicId());
171 //         }
172 //        return Util.resultRpcSuccessFor((Void) null) ;
173 //    }
174 //
175 //    private synchronized Future<RpcResult<JoinTopicOutput>> registerTopic(final TopicId topicId, final List<SchemaPath> notificationsToSubscribe){
176 //        LOG.debug("Join topic {} - register");
177 //        JoinTopicStatus joinTopicStatus = JoinTopicStatus.Down;
178 //        if(notificationsToSubscribe != null && notificationsToSubscribe.isEmpty() == false){
179 //            LOG.debug("Notifications to subscribe has found - count {}",notificationsToSubscribe.size() );
180 //            final Optional<DOMNotificationService> notifyService = getDOMMountPoint().getService(DOMNotificationService.class);
181 //            if(notifyService.isPresent()){
182 //                int registeredNotificationCount = 0;
183 //                for(SchemaPath schemaNotification : notificationsToSubscribe){
184 //                   for(NotificationTopicRegistration reg : notificationTopicRegistrationList){
185 //                       LOG.debug("Try notification registratio {} on SchemaPathNotification {}", reg.getSourceName(), schemaNotification.getLastComponent().getLocalName());
186 //                       if(reg.checkNotificationPath(schemaNotification)){
187 //                           LOG.info("Source of notification {} is activating, TopicId {}", reg.getSourceName(), topicId.getValue() );
188 //                           boolean regSuccess = reg.registerNotificationTopic(schemaNotification, topicId);
189 //                           if(regSuccess){
190 //                              registeredNotificationCount = registeredNotificationCount +1;
191 //                           }
192 //                       }
193 //                   }
194 //                }
195 //                if(registeredNotificationCount > 0){
196 //                    joinTopicStatus = JoinTopicStatus.Up;
197 //                }
198 //            } else {
199 //                LOG.warn("NO DOMNotification service on node {}", this.nodeId);
200 //            }
201 //        } else {
202 //            LOG.debug("Notifications to subscribe has NOT found");
203 //        }
204 //
205 //        final JoinTopicOutput output = new JoinTopicOutputBuilder().setStatus(joinTopicStatus).build();
206 //        return immediateFuture(RpcResultBuilder.success(output).build());
207 //
208 //    }
209 //
210 //    public void reActivateStreams(){
211 //        for (NotificationTopicRegistration reg : notificationTopicRegistrationList) {
212 //           LOG.info("Source of notification {} is reactivating on node {}", reg.getSourceName(), this.nodeId);
213 //            reg.reActivateNotificationSource();
214 //        }
215 //    }
216 //
217 //    public void deActivateStreams(){
218 //        for (NotificationTopicRegistration reg : notificationTopicRegistrationList) {
219 //           LOG.info("Source of notification {} is deactivating on node {}", reg.getSourceName(), this.nodeId);
220 //           reg.deActivateNotificationSource();
221 //        }
222 //    }
223 //
224 //    @Override
225 //    public void onNotification(final DOMNotification notification) {
226 //        SchemaPath notificationPath = notification.getType();
227 //        Date notificationEventTime = null;
228 //        if(notification instanceof DOMEvent){
229 //            notificationEventTime = ((DOMEvent) notification).getEventTime();
230 //        }
231 //        for(NotificationTopicRegistration notifReg : notificationTopicRegistrationList){
232 //            ArrayList<TopicId> topicIdsForNotification = notifReg.getNotificationTopicIds(notificationPath);
233 //            if(topicIdsForNotification != null && topicIdsForNotification.isEmpty() == false){
234 //
235 //                if(notifReg instanceof StreamNotificationTopicRegistration){
236 //                    StreamNotificationTopicRegistration streamReg = (StreamNotificationTopicRegistration)notifReg;
237 //                    streamReg.setLastEventTime(notificationEventTime);
238 //                }
239 //
240 //                for(TopicId topicId : topicIdsForNotification){
241 //                    publishNotification(notification, topicId);
242 //                    LOG.debug("Notification {} has been published for TopicId {}",notification.getType(), topicId.getValue());
243 //                }
244 //
245 //            }
246 //        }
247 //    }
248 //
249 //    private void publishNotification(final DOMNotification notification, TopicId topicId){
250 //         final ContainerNode topicNotification = Builders.containerBuilder()
251 //                 .withNodeIdentifier(TOPIC_NOTIFICATION_ARG)
252 //                 .withChild(ImmutableNodes.leafNode(TOPIC_ID_ARG, topicId))
253 //                 .withChild(ImmutableNodes.leafNode(EVENT_SOURCE_ARG, this.nodeId))
254 //                 .withChild(encapsulate(notification))
255 //                 .build();
256 //         try {
257 //             domPublish.putNotification(new TopicDOMNotification(topicNotification));
258 //         } catch (final InterruptedException e) {
259 //             throw Throwables.propagate(e);
260 //         }
261 //    }
262 //
263 //    private AnyXmlNode encapsulate(final DOMNotification body) {
264 //        // FIXME: Introduce something like AnyXmlWithNormalizedNodeData in Yangtools
265 //        final Document doc = XmlUtil.newDocument();
266 //        final Optional<String> namespace = Optional.of(PAYLOAD_ARG.getNodeType().getNamespace().toString());
267 //        final Element element = XmlUtil.createElement(doc , "payload", namespace);
268 //
269 //        final DOMResult result = new DOMResult(element);
270 //
271 //        final SchemaContext context = getDOMMountPoint().getSchemaContext();
272 //        final SchemaPath schemaPath = body.getType();
273 //        try {
274 //            NetconfMessageTransformUtil.writeNormalizedNode(body.getBody(), result, schemaPath, context);
275 //            return Builders.anyXmlBuilder().withNodeIdentifier(PAYLOAD_ARG)
276 //                    .withValue(new DOMSource(element))
277 //                    .build();
278 //        } catch (IOException | XMLStreamException e) {
279 //            LOG.error("Unable to encapsulate notification.",e);
280 //            throw Throwables.propagate(e);
281 //        }
282 //    }
283 //
284 //    private List<SchemaPath> getMatchingNotifications(NotificationPattern notificationPattern){
285 //        // FIXME: default language should already be regex
286 //        final String regex = Util.wildcardToRegex(notificationPattern.getValue());
287 //
288 //        final Pattern pattern = Pattern.compile(regex);
289 //        List<SchemaPath> availableNotifications = getAvailableNotifications();
290 //        if(availableNotifications == null || availableNotifications.isEmpty()){
291 //            return null;
292 //        }
293 //        return Util.expandQname(availableNotifications, pattern);
294 //    }
295 //
296 //    @Override
297 //    public void close() throws Exception {
298 //        for(NotificationTopicRegistration streamReg : notificationTopicRegistrationList){
299 //            streamReg.close();
300 //        }
301 //    }
302 //
303 //    @Override
304 //    public NodeKey getSourceNodeKey(){
305 //        return getNode().getKey();
306 //    }
307 //
308 //    @Override
309 //    public List<SchemaPath> getAvailableNotifications() {
310 //
311 //        final List<SchemaPath> availNotifList = new ArrayList<>();
312 //        // add Event Source Connection status notification
313 //        availNotifList.add(ConnectionNotificationTopicRegistration.EVENT_SOURCE_STATUS_PATH);
314 //
315 //        // FIXME: use SchemaContextListener to get changes asynchronously
316 //        final Set<NotificationDefinition> availableNotifications = getDOMMountPoint().getSchemaContext().getNotifications();
317 //        // add all known notifications from netconf device
318 //        for (final NotificationDefinition nd : availableNotifications) {
319 //            availNotifList.add(nd.getPath());
320 //        }
321 //        return availNotifList;
322 //    }
323 //
324 //    public Node getNode() {
325 //        return node;
326 //    }
327 //
328 //    DOMMountPoint getDOMMountPoint() {
329 //        return netconfMount;
330 //    }
331 //
332 //    MountPoint getMountPoint() {
333 //        return mountPoint;
334 //    }
335 //
336 //    NetconfNode getNetconfNode(){
337 //        return node.getAugmentation(NetconfNode.class);
338 //    }
339 //
340 //}