Skip to content
Snippets Groups Projects
JavaRunner.py 5.35 KiB
Newer Older
  • Learn to ignore specific revisions
  • # Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
    #
    # 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 json, logging,threading, queue,time,signal
    from datetime import datetime, timedelta
    from typing import Any, Iterator, List, Optional, Tuple, Union
    import logging
    import grpc
    
    from concurrent import futures
    from lxml import etree
    import os
    import subprocess
    from multiprocessing import Pool
    
    
    SERVER_ADDRESS = 'localhost:2021'
    SERVER_ID = 1
    _ONE_DAY_IN_SECONDS = 60 * 60 * 24
    
    #XML_FILE="/var/teraflow/bgpls_speaker/service/resources/BGP4Parameters_2.xml"
    XML_CONFIG_FILE="/var/teraflow/pcep/resources/PCEServerConfiguration.xml"
    
    LOGGER = logging.getLogger(__name__)
    
    
    class JavaRunner:
    
        #def __init__(self,localPort : int, address : str ="10.95.86.214", mngPort : int = 1112):
        def __init__(self,address): 
            #self.__peerPort=11179
            #self.__localPort=localPort 
            #self.__managementPort=mngPort
            # To support multiple speakers at same time 
            # Add 1 to port and then pass port to subproccess call
            #self.__configFile=XML_CONFIG_FILE
            #self.__process=0
            self.__lock = threading.Lock()
            self.__address = address
            #self.__portConf=6666
            #self.__pceAddress="10.95.43.16"
            #self.__asNumber=1
    
        def getCurrentLocalPort(self):
            with self.__lock:
                return self.__localPort
    
        def getCurrentMngPort(self):
            with self.__lock:
                return self.__managementPort
        def getPid(self):
            return self.__process.pid
    
        def execAndKill(self):
    
            LOGGER.debug("Before exec and kill")
            os.chdir("/var/teraflow/pcep/service/resources/")
            cwd = os.getcwd()
            LOGGER.info("Current working directory: %s", cwd)
            # Security shell=False
            self.__process=subprocess.Popen(['java -jar Ejecutable.jar '+ XML_CONFIG_FILE],
                    shell=False,start_new_session=True,stdout=subprocess.PIPE)
            LOGGER.debug("Time to sleep")
            java_pid = self.__process.pid
            print("Java PID:", java_pid)    
            time.sleep(15)
            self.__process.terminate()
    
    
        def execPcep(self) -> bool:
                """
                Executes java pcep in non-blocking process
                """
                # CHECKEAR muchas cosas
                LOGGER.debug("Before exec")
                os.chdir("/var/teraflow/pcep/service/resources/")
                # Security reasons shell=False
                self.__process=subprocess.Popen(['java' , '-jar' , 'Ejecutable.jar' , XML_CONFIG_FILE],
                    shell=False,start_new_session=True)
                return self.__process
    
        def setPort(self,port):
             self.__peerPort=port
             return True
        def setAsNumber(self,asNumber):
             self.__asNumber=asNumber
             return True
    
        def setPeer(self) -> bool:
                """
                Sets XML existing config file with peer address and port. TODO: as_number
                """
    
                #XMLParser = etree.XMLParser(remove_blank_text=False)
                #tree = etree.parse(XML_FILE, parser=XMLParser)
                #root = tree.getroot()
                ##peerAddress = root.find(".//peer")
                ##peerAddress.text=self.__address
                #peerPort = root.find(".//peerPort")
                #peerPort.text=str(self.__peerPort)
                #localPort = root.find(".//BGP4Port")
                #localPort.text=str(self.__localPort)
                #myAutonomousSystem = root.find(".//myAutonomousSystem")
                #myAutonomousSystem.text=str(self.__asNumber)
                #managePort = root.find(".//BGP4ManagementPort")
                #managePort.text=str(self.__managementPort)
                #tree.write(XML_FILE) #with ... as ..
    
                XMLParser = etree.XMLParser(remove_blank_text=False)
                tree = etree.parse(XML_CONFIG_FILE, parser=XMLParser)
                root = tree.getroot()
                #portConf = root.find(".//PCEManagementPort")
                #portConf.text=str(self.__portConf)
                pceAddress = root.find(".//LocalPCEAddress")
    
                LOGGER.debug("Valor anterior de LocalPCEAddress: %s", pceAddress.text)
                pceAddress.text=str(self.__address)
                LOGGER.debug("Valor posterior de LocalPCEAddress: %s", pceAddress.text)          
                tree.write(XML_CONFIG_FILE)
                return True
    
        def endBGPSpeaker(self) -> bool:
                """
                Kills java program connected to BGPLS Speaker with SIGKILL signal
                """
                LOGGER.debug("sending kill signal to process %s",self.__process.pid)
                # time.sleep(15)
                LOGGER.debug("PID: %d",self.__process.pid)
                # LOGGER.debug("Group PID: %d",os.getpgid(self.__process.pid))
                # os.killpg(os.getpgid(self.__process.pid), signal.SIGKILL)
                self.__process.kill()
                # .terminate() for SIGTERM
                return True
        
        def getRunnerInfo(self):
             return self.__address,self.__asNumber,self.__peerPort