Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / PCEPSessionImpl.java
index a2366a69adae6a3e5b8f5f4b9773a6b99ba13002..2480d62f64486fa80b9d1bc61d7cd90a542c96e3 100644 (file)
@@ -24,19 +24,20 @@ import java.util.concurrent.TimeUnit;
 import org.opendaylight.protocol.framework.AbstractProtocolSession;
 import org.opendaylight.protocol.pcep.PCEPCloseTermination;
 import org.opendaylight.protocol.pcep.PCEPErrors;
-import org.opendaylight.protocol.pcep.PCEPMessage;
 import org.opendaylight.protocol.pcep.PCEPSession;
 import org.opendaylight.protocol.pcep.PCEPSessionListener;
 import org.opendaylight.protocol.pcep.PCEPTlv;
 import org.opendaylight.protocol.pcep.message.PCEPCloseMessage;
 import org.opendaylight.protocol.pcep.message.PCEPErrorMessage;
-import org.opendaylight.protocol.pcep.message.PCEPKeepAliveMessage;
 import org.opendaylight.protocol.pcep.message.PCEPOpenMessage;
 import org.opendaylight.protocol.pcep.object.PCEPCloseObject;
 import org.opendaylight.protocol.pcep.object.PCEPCloseObject.Reason;
 import org.opendaylight.protocol.pcep.object.PCEPErrorObject;
 import org.opendaylight.protocol.pcep.object.PCEPOpenObject;
 import org.opendaylight.protocol.pcep.tlv.NodeIdentifierTlv;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.KeepaliveMessage;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.keepalive.message.KeepaliveMessageBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -48,11 +49,12 @@ import com.google.common.base.Preconditions;
 /**
  * Implementation of PCEPSession. (Not final for testing.)
  */
-class PCEPSessionImpl extends AbstractProtocolSession<PCEPMessage> implements PCEPSession, PCEPSessionRuntimeMXBean {
+@VisibleForTesting
+public class PCEPSessionImpl extends AbstractProtocolSession<Message> implements PCEPSession, PCEPSessionRuntimeMXBean {
        /**
         * System.nanoTime value about when was sent the last message Protected to be updated also in tests.
         */
-       protected long lastMessageSentAt;
+       protected volatile long lastMessageSentAt;
 
        /**
         * System.nanoTime value about when was received the last message
@@ -94,8 +96,10 @@ class PCEPSessionImpl extends AbstractProtocolSession<PCEPMessage> implements PC
 
        private final Channel channel;
 
-       PCEPSessionImpl(final Timer timer, final PCEPSessionListener listener, final int maxUnknownMessages,
-                       final Channel channel, final PCEPOpenObject localOpen, final PCEPOpenObject remoteOpen) {
+       private final KeepaliveMessage kaMessage = (KeepaliveMessage) new KeepaliveMessageBuilder().build();
+
+       PCEPSessionImpl(final Timer timer, final PCEPSessionListener listener, final int maxUnknownMessages, final Channel channel,
+                       final PCEPOpenObject localOpen, final PCEPOpenObject remoteOpen) {
                this.listener = Preconditions.checkNotNull(listener);
                this.stateTimer = Preconditions.checkNotNull(timer);
                this.channel = Preconditions.checkNotNull(channel);
@@ -108,7 +112,7 @@ class PCEPSessionImpl extends AbstractProtocolSession<PCEPMessage> implements PC
                }
 
                if (getDeadTimerValue() != 0) {
-                       stateTimer.newTimeout(new TimerTask() {
+                       this.stateTimer.newTimeout(new TimerTask() {
                                @Override
                                public void run(final Timeout timeout) throws Exception {
                                        handleDeadTimer();
@@ -117,7 +121,7 @@ class PCEPSessionImpl extends AbstractProtocolSession<PCEPMessage> implements PC
                }
 
                if (getKeepAliveTimerValue() != 0) {
-                       stateTimer.newTimeout(new TimerTask() {
+                       this.stateTimer.newTimeout(new TimerTask() {
                                @Override
                                public void run(final Timeout timeout) throws Exception {
                                        handleKeepaliveTimer();
@@ -137,14 +141,14 @@ class PCEPSessionImpl extends AbstractProtocolSession<PCEPMessage> implements PC
        private synchronized void handleDeadTimer() {
                final long ct = System.nanoTime();
 
-               final long nextDead = (long) (this.lastMessageReceivedAt + getDeadTimerValue() * 1E9);
+               final long nextDead = this.lastMessageReceivedAt + TimeUnit.SECONDS.toNanos(getDeadTimerValue());
 
                if (this.channel.isActive()) {
                        if (ct >= nextDead) {
                                logger.debug("DeadTimer expired. " + new Date());
                                this.terminate(Reason.EXP_DEADTIMER);
                        } else {
-                               stateTimer.newTimeout(new TimerTask() {
+                               this.stateTimer.newTimeout(new TimerTask() {
                                        @Override
                                        public void run(final Timeout timeout) throws Exception {
                                                handleDeadTimer();
@@ -163,20 +167,20 @@ class PCEPSessionImpl extends AbstractProtocolSession<PCEPMessage> implements PC
        private synchronized void handleKeepaliveTimer() {
                final long ct = System.nanoTime();
 
-               long nextKeepalive = (long) (this.lastMessageSentAt + getKeepAliveTimerValue() * 1E9);
+               long nextKeepalive = this.lastMessageSentAt + TimeUnit.SECONDS.toNanos(getKeepAliveTimerValue());
 
-               if (channel.isActive()) {
+               if (this.channel.isActive()) {
                        if (ct >= nextKeepalive) {
-                               this.sendMessage(new PCEPKeepAliveMessage());
-                               nextKeepalive = (long) (this.lastMessageSentAt + getKeepAliveTimerValue() * 1E9);
-                       } else {
-                               this.stateTimer.newTimeout(new TimerTask() {
-                                       @Override
-                                       public void run(final Timeout timeout) throws Exception {
-                                               handleKeepaliveTimer();
-                                       }
-                               }, nextKeepalive - ct, TimeUnit.NANOSECONDS);
+                               this.sendMessage(this.kaMessage);
+                               nextKeepalive = this.lastMessageSentAt + TimeUnit.SECONDS.toNanos(getKeepAliveTimerValue());
                        }
+
+                       this.stateTimer.newTimeout(new TimerTask() {
+                               @Override
+                               public void run(final Timeout timeout) throws Exception {
+                                       handleKeepaliveTimer();
+                               }
+                       }, nextKeepalive - ct, TimeUnit.NANOSECONDS);
                }
        }
 
@@ -186,11 +190,11 @@ class PCEPSessionImpl extends AbstractProtocolSession<PCEPMessage> implements PC
         * @param msg to be sent
         */
        @Override
-       public void sendMessage(final PCEPMessage msg) {
+       public void sendMessage(final Message msg) {
                try {
                        this.channel.writeAndFlush(msg);
                        this.lastMessageSentAt = System.nanoTime();
-                       if (!(msg instanceof PCEPKeepAliveMessage)) {
+                       if (!(msg instanceof KeepaliveMessage)) {
                                logger.debug("Sent message: " + msg);
                        }
                        this.sentMsgCount++;
@@ -217,14 +221,16 @@ class PCEPSessionImpl extends AbstractProtocolSession<PCEPMessage> implements PC
        public synchronized void close(final PCEPCloseObject.Reason reason) {
                logger.debug("Closing session: {}", this);
                this.closed = true;
-               this.sendMessage(new PCEPCloseMessage(new PCEPCloseObject(reason)));
+               // FIXME: just to get rid of compilation errors
+               this.sendMessage((Message) new PCEPCloseMessage(new PCEPCloseObject(reason)));
                this.channel.close();
        }
 
        private synchronized void terminate(final PCEPCloseObject.Reason reason) {
                this.listener.onSessionTerminated(this, new PCEPCloseTermination(reason));
                this.closed = true;
-               this.sendMessage(new PCEPCloseMessage(new PCEPCloseObject(reason)));
+               // FIXME: just to get rid of compilation errors
+               this.sendMessage((Message) new PCEPCloseMessage(new PCEPCloseObject(reason)));
                this.close();
        }
 
@@ -250,7 +256,8 @@ class PCEPSessionImpl extends AbstractProtocolSession<PCEPMessage> implements PC
                final PCEPErrorObject error = new PCEPErrorObject(value);
                final List<PCEPErrorObject> errors = new ArrayList<PCEPErrorObject>();
                errors.add(error);
-               this.sendMessage(new PCEPErrorMessage(open, errors, null));
+               // FIXME: just to get rid of compilation errors
+               this.sendMessage((Message) new PCEPErrorMessage(open, errors, null));
        }
 
        /**
@@ -283,13 +290,13 @@ class PCEPSessionImpl extends AbstractProtocolSession<PCEPMessage> implements PC
         * @param msg incoming message
         */
        @Override
-       public void handleMessage(final PCEPMessage msg) {
+       public void handleMessage(final Message msg) {
                // Update last reception time
                this.lastMessageReceivedAt = System.nanoTime();
                this.receivedMsgCount++;
 
                // Internal message handling. The user does not see these messages
-               if (msg instanceof PCEPKeepAliveMessage) {
+               if (msg instanceof KeepaliveMessage) {
                        // Do nothing, the timer has been already reset
                } else if (msg instanceof PCEPOpenMessage) {
                        this.sendErrorMessage(PCEPErrors.ATTEMPT_2ND_SESSION);
@@ -326,17 +333,17 @@ class PCEPSessionImpl extends AbstractProtocolSession<PCEPMessage> implements PC
 
        @Override
        public Integer getDeadTimerValue() {
-               return remoteOpen.getDeadTimerValue();
+               return this.remoteOpen.getDeadTimerValue();
        }
 
        @Override
        public Integer getKeepAliveTimerValue() {
-               return localOpen.getKeepAliveTimerValue();
+               return this.localOpen.getKeepAliveTimerValue();
        }
 
        @Override
        public String getPeerAddress() {
-               InetSocketAddress a = (InetSocketAddress) channel.remoteAddress();
+               final InetSocketAddress a = (InetSocketAddress) this.channel.remoteAddress();
                return a.getHostName();
        }
 
@@ -347,7 +354,7 @@ class PCEPSessionImpl extends AbstractProtocolSession<PCEPMessage> implements PC
 
        @Override
        public String getNodeIdentifier() {
-               for (PCEPTlv tlv : this.remoteOpen.getTlvs()) {
+               for (final PCEPTlv tlv : this.remoteOpen.getTlvs()) {
                        if (tlv instanceof NodeIdentifierTlv) {
                                return tlv.toString();
                        }
@@ -361,13 +368,13 @@ class PCEPSessionImpl extends AbstractProtocolSession<PCEPMessage> implements PC
        }
 
        protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
-               toStringHelper.add("localOpen", localOpen);
-               toStringHelper.add("remoteOpen", remoteOpen);
+               toStringHelper.add("localOpen", this.localOpen);
+               toStringHelper.add("remoteOpen", this.remoteOpen);
                return toStringHelper;
        }
 
        @Override
        protected void sessionUp() {
-               listener.onSessionUp(this);
+               this.listener.onSessionUp(this);
        }
 }