#!/usr/bin/env python #Copyright 2015 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 # http://www.apache.org/licenses/LICENSE-2.0 # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import sys, glob, threading import getopt, socket import logging #sys.path.append('gen-py') #sys.path.insert(0, glob.glob('./lib/py/build/lib.*')[0]) sys.path.insert(0, glob.glob('./lib')[0]) from dominoRPC import Communication from dominoRPC.ttypes import * from dominoRPC.constants import * from dominoCLI import DominoClientCLI from dominoCLI.ttypes import * from dominoCLI.constants import * from thrift import Thrift from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from thrift.server import TServer from util import * #Load configuration parameters from domino_conf import * class CommunicationHandler: def __init__(self): self.log = {} 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) # Template Push from Domino Server is received # Actions: # - Depending on Controller Domain, call API # - Respond Back with Push Response 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 ## End of inspection # Call NB API # If heat client, call heat command # If ONOS client, run as shell script ## End of NB API call # Marshall the response message for the Domino Server Fill push_r = PushResponseMessage() # Fill response message fields push_r.domino_udid = self.dominoClient.UDID push_r.seq_no = self.dominoClient.seqno push_r.responseCode = SUCCESS ## End of filling self.dominoClient.seqno = self.dominoClient.seqno + 1 return push_r def openconnection(self): self.transport.open() def closeconnection(): self.transport.close() class CLIHandler: def __init__(self): self.log = {} def __init__(self, dominoclient, CLIservice): self.log = {} self.dominoClient = dominoclient self.CLIservice = CLIservice def d_CLI(self, msg): logging.info('Received CLI %s', msg.CLI_input) self.CLIservice.process_input(msg.CLI_input) CLIrespmsg = CLIResponse() 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): threading.Thread.__init__(self) self.dominoclient = dominoclient self.communicationhandler = communicationhandler self.interactive = interactive def process_input(self, args): try: if args[0] == 'heartbeat': self.dominoclient.heartbeat() elif args[0] == 'publish': opts, args = getopt.getopt(args[1:],"t:",["tosca-file="]) if len(opts) == 0: print '\nUsage: publish -t ' return for opt, arg in opts: if opt in ('-t', '--tosca-file'): toscafile = arg self.dominoclient.publish(toscafile) elif args[0] == 'subscribe': labels = [] templateTypes = [] opts, args = getopt.getopt(args[1:],"l:t:",["labels=","ttype="]) for opt, arg in opts: if opt in ('-l', '--labels'): labels = labels + arg.split(',') elif opt in ('-t', '--ttype'): templateTypes = templateTypes + arg.split(',') #check if labels or supported templates are nonempty if labels != [] or templateTypes != []: self.dominoclient.subscribe(labels, templateTypes) elif args[0] == 'register': self.dominoclient.start() except getopt.GetoptError: print 'Command is misentered or not supported!' def run(self): global DEFAULT_TOSCA_PUBFILE if self.interactive == "TRUE": flag = True else: flag = False if flag: #interactive CLI, loop in while until killed while True: sys.stdout.write('>>') input_string = raw_input() args = input_string.