Bulk-add copyright headers to java files
[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 _notificationBody = null;
82         CompositeNode _compositeNode = null;
83         if (message != null) {
84             _compositeNode = NetconfMapping.toCompositeNode(message,device.getSchemaContext());
85         }
86         if (_compositeNode != null) {
87             _notificationBody = NetconfDeviceListener.getNotificationBody(_compositeNode);
88         }
89         final CompositeNode domNotification = _notificationBody;
90         boolean _notEquals = (!Objects.equal(domNotification, null));
91         if (_notEquals) {
92             MountProvisionInstance _mountInstance = null;
93             if (this.device != null) {
94                 _mountInstance = this.device.getMountInstance();
95             }
96             if (_mountInstance != null) {
97                 _mountInstance.publish(domNotification);
98             }
99         }
100     }
101
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);
107             }
108         }
109         return null;
110     }
111
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) {
118             try {
119                 try {
120                     return promise.get();
121                 } catch (Throwable _e) {
122                     throw Exceptions.sneakyThrow(_e);
123                 }
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;
129                 } else {
130                     throw Exceptions.sneakyThrow(_t);
131                 }
132             }
133         }
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;
138     }
139
140     public synchronized Promise<NetconfMessage> promiseReply() {
141         this.device.logger.debug("Promising reply.");
142         this.promiseLock.lock();
143         try {
144             boolean _equals = Objects.equal(this.messagePromise, null);
145             if (_equals) {
146                 Promise<NetconfMessage> _newPromise = this.eventExecutor.<NetconfMessage> newPromise();
147                 this.messagePromise = _newPromise;
148                 return this.messagePromise;
149             }
150             return this.messagePromise;
151         } finally {
152             this.promiseLock.unlock();
153         }
154     }
155
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);
161     }
162 }