2 * Copyright (c) 2014 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
8 package org.opendaylight.controller.sal.connect.netconf;
10 import com.google.common.base.Objects;
12 import io.netty.util.concurrent.EventExecutor;
13 import io.netty.util.concurrent.Promise;
15 import java.util.List;
16 import java.util.concurrent.ConcurrentMap;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.locks.ReentrantLock;
20 import org.eclipse.xtext.xbase.lib.Exceptions;
21 import org.eclipse.xtext.xbase.lib.Functions.Function0;
22 import org.opendaylight.controller.netconf.api.NetconfMessage;
23 import org.opendaylight.controller.netconf.client.NetconfClientSession;
24 import org.opendaylight.controller.netconf.client.NetconfClientSessionListener;
25 import org.opendaylight.controller.netconf.util.xml.XmlElement;
26 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
27 import org.opendaylight.controller.sal.connect.netconf.NetconfDevice;
28 import org.opendaylight.controller.sal.connect.netconf.NetconfMapping;
29 import org.opendaylight.controller.sal.core.api.mount.MountProvisionInstance;
30 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
31 import org.opendaylight.yangtools.yang.data.api.Node;
32 import org.w3c.dom.Document;
34 @SuppressWarnings("all")
35 class NetconfDeviceListener extends NetconfClientSessionListener {
36 private final NetconfDevice device;
37 private final EventExecutor eventExecutor;
39 public NetconfDeviceListener(final NetconfDevice device, final EventExecutor eventExecutor) {
41 this.eventExecutor = eventExecutor;
44 private Promise<NetconfMessage> messagePromise;
45 private ConcurrentMap<String, Promise<NetconfMessage>> promisedMessages;
47 private final ReentrantLock promiseLock = new ReentrantLock();
49 public void onMessage(final NetconfClientSession session, final NetconfMessage message) {
50 if (isNotification(message)) {
51 this.onNotification(session, message);
54 this.promiseLock.lock();
55 boolean _notEquals = (!Objects.equal(this.messagePromise, null));
57 this.device.logger.debug("Setting promised reply {} with message {}", this.messagePromise, message);
58 this.messagePromise.setSuccess(message);
59 this.messagePromise = null;
62 this.promiseLock.unlock();
68 * Method intended to customize notification processing.
72 * NetconfClientSessionListener#onMessage(NetconfClientSession,
76 * NetconfClientSessionListener#onMessage(NetconfClientSession,
79 public void onNotification(final NetconfClientSession session, final NetconfMessage message) {
80 this.device.logger.debug("Received NETCONF notification.", message);
81 CompositeNode domNotification = null;
82 if (message != null) {
83 domNotification = NetconfMapping.toNotificationNode(message, device.getSchemaContext());
85 if (domNotification != null) {
86 MountProvisionInstance _mountInstance = null;
87 if (this.device != null) {
88 _mountInstance = this.device.getMountInstance();
90 if (_mountInstance != null) {
91 _mountInstance.publish(domNotification);
96 private static CompositeNode getNotificationBody(final CompositeNode node) {
97 List<Node<? extends Object>> _children = node.getChildren();
98 for (final Node<? extends Object> child : _children) {
99 if ((child instanceof CompositeNode)) {
100 return ((CompositeNode) child);
106 public NetconfMessage getLastMessage(final int attempts, final int attemptMsDelay) throws InterruptedException {
107 final Promise<NetconfMessage> promise = this.promiseReply();
108 this.device.logger.debug("Waiting for reply {}", promise);
109 int _plus = (attempts * attemptMsDelay);
110 final boolean messageAvailable = promise.await(_plus);
111 if (messageAvailable) {
114 return promise.get();
115 } catch (Throwable _e) {
116 throw Exceptions.sneakyThrow(_e);
118 } catch (final Throwable _t) {
119 if (_t instanceof ExecutionException) {
120 final ExecutionException e = (ExecutionException) _t;
121 IllegalStateException _illegalStateException = new IllegalStateException(e);
122 throw _illegalStateException;
124 throw Exceptions.sneakyThrow(_t);
128 String _plus_1 = ("Unsuccessful after " + Integer.valueOf(attempts));
129 String _plus_2 = (_plus_1 + " attempts.");
130 IllegalStateException _illegalStateException_1 = new IllegalStateException(_plus_2);
131 throw _illegalStateException_1;
134 public synchronized Promise<NetconfMessage> promiseReply() {
135 this.device.logger.debug("Promising reply.");
136 this.promiseLock.lock();
138 boolean _equals = Objects.equal(this.messagePromise, null);
140 Promise<NetconfMessage> _newPromise = this.eventExecutor.<NetconfMessage> newPromise();
141 this.messagePromise = _newPromise;
142 return this.messagePromise;
144 return this.messagePromise;
146 this.promiseLock.unlock();
150 public boolean isNotification(final NetconfMessage message) {
151 Document _document = message.getDocument();
152 final XmlElement xmle = XmlElement.fromDomDocument(_document);
153 String _name = xmle.getName();
154 return XmlNetconfConstants.NOTIFICATION_ELEMENT_NAME.equals(_name);