Merge "Increase timeout for waiting for broker service in sal-binding-it."
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / NetconfDeviceListener.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.controller.sal.connect.netconf;
9
10 import com.google.common.base.Objects;
11
12 import io.netty.util.concurrent.EventExecutor;
13 import io.netty.util.concurrent.Promise;
14
15 import java.util.List;
16 import java.util.concurrent.ConcurrentMap;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.locks.ReentrantLock;
19
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;
33
34 @SuppressWarnings("all")
35 class NetconfDeviceListener extends NetconfClientSessionListener {
36     private final NetconfDevice device;
37     private final EventExecutor eventExecutor;
38
39     public NetconfDeviceListener(final NetconfDevice device, final EventExecutor eventExecutor) {
40         this.device = device;
41         this.eventExecutor = eventExecutor;
42     }
43
44     private Promise<NetconfMessage> messagePromise;
45     private ConcurrentMap<String, Promise<NetconfMessage>> promisedMessages;
46
47     private final ReentrantLock promiseLock = new ReentrantLock();
48
49     public void onMessage(final NetconfClientSession session, final NetconfMessage message) {
50         if (isNotification(message)) {
51             this.onNotification(session, message);
52         } else {
53             try {
54                 this.promiseLock.lock();
55                 boolean _notEquals = (!Objects.equal(this.messagePromise, null));
56                 if (_notEquals) {
57                     this.device.logger.debug("Setting promised reply {} with message {}", this.messagePromise, message);
58                     this.messagePromise.setSuccess(message);
59                     this.messagePromise = null;
60                 }
61             } finally {
62                 this.promiseLock.unlock();
63             }
64         }
65     }
66
67     /**
68      * Method intended to customize notification processing.
69      * 
70      * @param session
71      *            {@see
72      *            NetconfClientSessionListener#onMessage(NetconfClientSession,
73      *            NetconfMessage)}
74      * @param message
75      *            {@see
76      *            NetconfClientSessionListener#onMessage(NetconfClientSession,
77      *            NetconfMessage)}
78      */
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());
84         }
85         if (domNotification != null) {
86             MountProvisionInstance _mountInstance = null;
87             if (this.device != null) {
88                 _mountInstance = this.device.getMountInstance();
89             }
90             if (_mountInstance != null) {
91                 _mountInstance.publish(domNotification);
92             }
93         }
94     }
95
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);
101             }
102         }
103         return null;
104     }
105
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) {
112             try {
113                 try {
114                     return promise.get();
115                 } catch (Throwable _e) {
116                     throw Exceptions.sneakyThrow(_e);
117                 }
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;
123                 } else {
124                     throw Exceptions.sneakyThrow(_t);
125                 }
126             }
127         }
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;
132     }
133
134     public synchronized Promise<NetconfMessage> promiseReply() {
135         this.device.logger.debug("Promising reply.");
136         this.promiseLock.lock();
137         try {
138             boolean _equals = Objects.equal(this.messagePromise, null);
139             if (_equals) {
140                 Promise<NetconfMessage> _newPromise = this.eventExecutor.<NetconfMessage> newPromise();
141                 this.messagePromise = _newPromise;
142                 return this.messagePromise;
143             }
144             return this.messagePromise;
145         } finally {
146             this.promiseLock.unlock();
147         }
148     }
149
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);
155     }
156 }