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 _notificationBody = null;
82 CompositeNode _compositeNode = null;
83 if (message != null) {
84 _compositeNode = NetconfMapping.toCompositeNode(message,device.getSchemaContext());
86 if (_compositeNode != null) {
87 _notificationBody = NetconfDeviceListener.getNotificationBody(_compositeNode);
89 final CompositeNode domNotification = _notificationBody;
90 boolean _notEquals = (!Objects.equal(domNotification, null));
92 MountProvisionInstance _mountInstance = null;
93 if (this.device != null) {
94 _mountInstance = this.device.getMountInstance();
96 if (_mountInstance != null) {
97 _mountInstance.publish(domNotification);
102 private static CompositeNode getNotificationBody(final CompositeNode node) {
103 List<Node<? extends Object>> _children = node.getChildren();
104 for (final Node<? extends Object> child : _children) {
105 if ((child instanceof CompositeNode)) {
106 return ((CompositeNode) child);
112 public NetconfMessage getLastMessage(final int attempts, final int attemptMsDelay) throws InterruptedException {
113 final Promise<NetconfMessage> promise = this.promiseReply();
114 this.device.logger.debug("Waiting for reply {}", promise);
115 int _plus = (attempts * attemptMsDelay);
116 final boolean messageAvailable = promise.await(_plus);
117 if (messageAvailable) {
120 return promise.get();
121 } catch (Throwable _e) {
122 throw Exceptions.sneakyThrow(_e);
124 } catch (final Throwable _t) {
125 if (_t instanceof ExecutionException) {
126 final ExecutionException e = (ExecutionException) _t;
127 IllegalStateException _illegalStateException = new IllegalStateException(e);
128 throw _illegalStateException;
130 throw Exceptions.sneakyThrow(_t);
134 String _plus_1 = ("Unsuccessful after " + Integer.valueOf(attempts));
135 String _plus_2 = (_plus_1 + " attempts.");
136 IllegalStateException _illegalStateException_1 = new IllegalStateException(_plus_2);
137 throw _illegalStateException_1;
140 public synchronized Promise<NetconfMessage> promiseReply() {
141 this.device.logger.debug("Promising reply.");
142 this.promiseLock.lock();
144 boolean _equals = Objects.equal(this.messagePromise, null);
146 Promise<NetconfMessage> _newPromise = this.eventExecutor.<NetconfMessage> newPromise();
147 this.messagePromise = _newPromise;
148 return this.messagePromise;
150 return this.messagePromise;
152 this.promiseLock.unlock();
156 public boolean isNotification(final NetconfMessage message) {
157 Document _document = message.getDocument();
158 final XmlElement xmle = XmlElement.fromDomDocument(_document);
159 String _name = xmle.getName();
160 return XmlNetconfConstants.NOTIFICATION_ELEMENT_NAME.equals(_name);