updated test script and folders
[domino.git] / DominoClient.py
index 9cdcc39..e91f6cd 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 
-#Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
+#Copyright 2016 Open Platform for NFV Project, Inc. and its contributors
 #   Licensed under the Apache License, Version 2.0 (the "License");
 #   you may not use this file except in compliance with the License.
 #   You may obtain a copy of the License at
@@ -12,9 +12,9 @@
 #   limitations under the License.
 
 
-import sys, glob, threading
+import sys, os, glob, threading
 import getopt, socket
-import logging
+import logging, errno
 
 #sys.path.append('gen-py')
 #sys.path.insert(0, glob.glob('./lib/py/build/lib.*')[0])
@@ -46,18 +46,9 @@ class CommunicationHandler:
   def __init__(self, dominoclient):
     self.log = {}
     self.dominoClient = dominoclient
-    try:
-      # Make socket
-      transport = TSocket.TSocket(DOMINO_SERVER_IP, DOMINO_SERVER_PORT)
-      transport.setTimeout(THRIFT_RPC_TIMEOUT_MS)
-      # Add buffering to compensate for slow raw sockets
-      self.transport = TTransport.TBufferedTransport(transport)
-      # Wrap in a protocol
-      self.protocol = TBinaryProtocol.TBinaryProtocol(self.transport)
-      # Create a client to use the protocol encoder
-      self.sender = Communication.Client(self.protocol)
-    except Thrift.TException, tx: 
-      logging.error('%s' , tx.message)
+    self.transport = None
+    self.protocol = None
+    self.sender = None
 
   # Template Push from Domino Server is received
   # Actions:
@@ -66,10 +57,25 @@ class CommunicationHandler:
   def d_push(self, push_msg):
     logging.info('%d Received Template File', self.dominoClient.UDID)
     # Retrieve the template file
-
-    ## End of retrieval
-    # Any inspection code goes here
+    try:
+      os.makedirs(TOSCA_RX_DIR+str(self.dominoClient.UDID))
+    except OSError as exception:
+      if exception.errno == errno.EEXIST:
+        logging.debug('IGNORING error: ERRNO %d; %s exists.', exception.errno, TOSCA_RX_DIR+str(self.dominoClient.UDID))
+      else:
+        logging.error('IGNORING error in creating %s. Err no: %d', exception.errno)
+
+    try:  
+      miscutil.write_templatefile(TOSCA_RX_DIR+str(self.dominoClient.UDID)+'/'+str(push_msg.seq_no)+'.yaml' , push_msg.template)
+    except:    
+      logging.error('FAILED to write the pushed file: %s', sys.exc_info()[0])
+      push_r = PushResponseMessage()
+      # Fill response message fields
+      push_r.domino_udid = self.dominoClient.UDID
+      push_r.seq_no = self.dominoClient.seqno
+      push_r.responseCode = FAILED
+      self.dominoClient.seqno = self.dominoClient.seqno + 1
+      return push_r# Any inspection code goes here
 
     ## End of inspection
 
@@ -95,7 +101,19 @@ class CommunicationHandler:
 
   
   def openconnection(self):
-    self.transport.open()
+    try:
+      # Make socket
+      transport = TSocket.TSocket(self.dominoClient.dominoserver_IP, DOMINO_SERVER_PORT)
+      transport.setTimeout(THRIFT_RPC_TIMEOUT_MS)
+      # Add buffering to compensate for slow raw sockets
+      self.transport = TTransport.TBufferedTransport(transport)
+      # Wrap in a protocol
+      self.protocol = TBinaryProtocol.TBinaryProtocol(self.transport)
+      # Create a client to use the protocol encoder
+      self.sender = Communication.Client(self.protocol)
+      self.transport.open()
+    except Thrift.TException, tx:
+      logging.error('%s' , tx.message)
 
   def closeconnection():
     self.transport.close()
@@ -118,11 +136,6 @@ class CLIHandler:
     CLIrespmsg.CLI_response = "Testing..."
     return CLIrespmsg
  
-def read_templatefile(temp_filename): 
-  f = open(temp_filename, 'r')
-  lines = f.read().splitlines()
-
-  return lines
 
 class DominoClientCLIService(threading.Thread):
   def __init__(self, dominoclient, communicationhandler, interactive):
@@ -132,6 +145,10 @@ class DominoClientCLIService(threading.Thread):
     self.interactive = interactive
 
   def process_input(self, args):
+    if len(args) == 0:
+      print 'Empty API body'
+      return
+
     try:
       if args[0] == 'heartbeat':
         self.dominoclient.heartbeat()
@@ -214,6 +231,8 @@ class DominoClientCLIService(threading.Thread):
 
 class DominoClient:
   def __init__(self):
+
+
     self.communicationHandler = CommunicationHandler(self)
     self.processor = None
     self.transport = None
@@ -224,8 +243,7 @@ class DominoClient:
     self.CLIservice = None
 
     self.serviceport = 9091
-    self.dominoserver_IP = 'localhost'
-
+    self.dominoserver_IP = DOMINO_SERVER_IP
     self.CLIport = DOMINO_CLI_PORT 
 
     #Start from UNREGISTERED STATE
@@ -278,7 +296,7 @@ class DominoClient:
       except (Thrift.TException, TSocket.TTransportException) as tx:
         logging.error('%s' , tx.message)
       except (socket.timeout) as tx:
-        self.dominoclient.handle_RPC_timeout(pub_msg)
+        self.handle_RPC_timeout(reg_msg)
       except (socket.error) as tx:
         logging.error('%s' , tx.message)
       self.seqno = self.seqno + 1
@@ -315,13 +333,13 @@ class DominoClient:
     pub_msg.template_type = 'tosca-nfv-v1.0'
 
     try:
-      pub_msg.template = read_templatefile(toscafile)
+      pub_msg.template = miscutil.read_templatefile(toscafile)
     except IOError as e:
       logging.error('I/O error(%d): %s' , e.errno, e.strerror)
       return
     try:
       pub_msg_r = self.sender().d_publish(pub_msg)
-      logging.info('Publish Response is received from: %d ,sequence number: %d', pub_msg_r.domino_udid, pub_msg_r.seq_no)
+      logging.info('Publish Response is received from: %d ,sequence number: %d Op. Status: %d', pub_msg_r.domino_udid, pub_msg_r.seq_no, pub_msg_r.responseCode)
     except (Thrift.TException, TSocket.TTransportException) as tx:
       print '%s' % (tx.message)
     except (socket.timeout) as tx:
@@ -398,11 +416,11 @@ def main(argv):
   try:
       opts, args = getopt.getopt(argv,"hc:p:i:l:",["conf=","port=","ipaddr=","log=","iac=","cliport="])
   except getopt.GetoptError:
-      print 'DominoClient.py -c/--conf <configfile> -p/--port <socketport> -i/--ipaddr <IPaddr> -l/--log <loglevel> --iac=true/false'
+      print 'DominoClient.py -c/--conf <configfile> -p/--port <socketport> -i/--ipaddr <IPaddr> -l/--log <loglevel> --iac=true/false --cliport <cliport>'
       sys.exit(2)
   for opt, arg in opts:
       if opt == '-h':
-         print 'DominoClient.py -c/--conf <configfile> -p/--port <socketport> -i/--ipaddr <IPaddr> -l/--log <loglevel> --iac=true/false'
+         print 'DominoClient.py -c/--conf <configfile> -p/--port <socketport> -i/--ipaddr <IPaddr> -l/--log <loglevel> --iac=true/false --cliport <cliport>'
          sys.exit()
       elif opt in ("-c", "--conf"):
          configfile = arg