Commit 7e2ac894 authored by Elisabetta Giani's avatar Elisabetta Giani
Browse files

AT5-262: Use new verion of the lmcbaseclasses package (version>= 0.2.0).

Replace usage of dev_logging() with logger().
Removed the path change.
Moved the AdminModeCheck decorator before the is_XXX_allowed methods.
Updated Dockerfile to use last version of lmc base classes.
parent f999dab9
Loading
Loading
Loading
Loading
+80 −70
Original line number Diff line number Diff line
@@ -13,12 +13,10 @@ CSP.LMC Common Class for the CSPMaster TANGO Device.
"""
# PROTECTED REGION ID (CspMaster.standardlibray_import) ENABLED START #
# Python standard library
from __future__ import absolute_import
import sys
import os
from future.utils import with_metaclass
from collections import defaultdict
from enum import IntEnum, unique
import threading
import time
# PROTECTED REGION END# //CspMaster.standardlibray_import
@@ -42,14 +40,10 @@ from skabase.auxiliary import utils

# PROTECTED REGION ID (CspMaster.add_path) ENABLED START #
# add the path to import global_enum package.
file_path = os.path.dirname(os.path.abspath(__file__))
utils_path = os.path.abspath(os.path.join(file_path, os.pardir)) + "/utils"
sys.path.insert(0, utils_path)
import decorators
from decorators import AdminModeCheck, CmdInputArgsCheck
import cspcommons
from cspcommons import HealthState, AdminMode, ObsState, CmdExecState
import release

from .utils.decorators import AdminModeCheck, CmdInputArgsCheck
from .utils.cspcommons import HealthState, AdminMode, ObsState, CmdExecState
from . import release
# PROTECTED REGION END# //CspMaster.add_path


@@ -147,25 +141,25 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
                    else:
                        log_msg = ("Attribute {} not still "
                                   "handled".format(evt.attr_name))
                        self.dev_logging(log_msg, tango.LogLevel.LOG_WARN)
                        self.logger.warn(log_msg)
                else:
                    log_msg = ("Unexpected change event for"
                               " attribute: {}".format(str(evt.attr_name)))
                    self.dev_logging(log_msg, tango.LogLevel.LOG_WARN)
                    self.logger.warn(log_msg)
                    return

                log_msg = "New value for {} is {}".format(str(evt.attr_name),
                                                          str(evt.attr_value.value))
                self.dev_logging(log_msg, tango.LogLevel.LOG_INFO)
                self.logger.info(log_msg)
                # update CSP global state
                # healthState and State are updated accordingly to the updated values of
                # sub-elements healthState, State and adminMode
                if evt.attr_value.name.lower() in ["state", "healthstate", "adminmode"]:
                    self._update_csp_state()
            except tango.DevFailed as df:
                self.dev_logging(str(df.args[0].desc), tango.LogLevel.LOG_ERROR)
                self.logger.error(str(df.args[0].desc))
            except Exception as except_occurred:
                self.dev_logging(str(except_occurred), tango.LogLevel.LOG_ERROR)
                self.logger.error(str(except_occurred))
        else:
            for item in evt.errors:
                # API_EventTimeout: if sub-element device not reachable it transitions
@@ -185,7 +179,7 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
                    # update the State and healthState of the CSP Element
                    self._update_csp_state()
                log_msg = item.reason + ": on attribute " + str(evt.attr_name)
                self.dev_logging(log_msg, tango.LogLevel.LOG_WARN)
                self.logger.warn(log_msg)
    
    def _attributes_change_evt_cb(self, evt):
        """
@@ -217,20 +211,20 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
                else:
                    log_msg = ("Unexpected change event for"
                               " attribute: {}".format(str(evt.attr_name)))
                    self.dev_logging(log_msg, tango.LogLevel.LOG_WARN)
                    self.logger.warn(log_msg)
                    return

                log_msg = "New value for {} is {}".format(str(evt.attr_name),
                                                          str(evt.attr_value.value))
                self.dev_logging(log_msg, tango.LogLevel.LOG_INFO)
                self.logger.info(log_msg)
            except tango.DevFailed as df:
                self.dev_logging(str(df.args[0].desc), tango.LogLevel.LOG_ERROR)
                self.logger.error(str(df.args[0].desc))
            except Exception as except_occurred:
                self.dev_logging(str(except_occurred), tango.LogLevel.LOG_ERROR)
                self.logger.error(str(except_occurred))
        else:
            for item in evt.errors:
                log_msg = item.reason + ": on attribute " + str(evt.attr_name)
                self.dev_logging(log_msg, tango.LogLevel.LOG_WARN)
                self.logger.warn(log_msg)
              
    def _cmd_ended_cb(self, evt):
        """
@@ -262,28 +256,25 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
                if not evt.err:
                    msg = "Device {} is processing command {}".format(evt.device,
                                                                      evt.cmd_name)
                    print(msg)
                    self.dev_logging(msg, tango.LogLevel.LOG_INFO)
                    self.logger.info(msg)
                else:
                    msg = "Error!!Command {} ended on device {}.\n".format(evt.cmd_name,
                                                                           evt.device.dev_name())
                    msg += " Desc: {}".format(evt.errors[0].desc)
                    self.dev_logging(msg, tango.LogLevel.LOG_ERROR)
                    print(msg)
                    self.logger.error(msg)
                    self._se_cmd_execution_state[evt.device.dev_name()][evt.cmd_name.lower()] = CmdExecState.FAILED
                    self._alarm_message[evt.cmd_name] = msg
                    # obsState and obsMode values take on the CbfSubarray's values via
                    # the subscribe/publish mechanism
            else:
                self.dev_logging("cmd_ended callback: evt is empty!!",
                                 tango.LogLevel.LOG_ERRO)
                self.logger.error("cmd_ended callback: evt is empty!!")
        except tango.DevFailed as df:
            msg = ("CommandCallback cmd_ended failure - desc: {}"
                   " reason: {}".format(df.args[0].desc, df.args[0].reason))
            self.dev_logging(msg, tango.LogLevel.LOG_ERROR)
            self.logger.error(msg)
        except Exception as ex:
            msg = "CommandCallBack cmd_ended general exception: {}".format(str(ex))
            self.dev_logging(msg, tango.LogLevel.LOG_ERROR)
            self.logger.error(msg)
    
    # ---------------
    # Class methods
@@ -304,7 +295,7 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
        self._update_csp_health_state()
        self.set_state(self._se_state[self.CspCbf])
        if self._admin_mode in [AdminMode.OFFLINE, AdminMode.NOTFITTED, AdminMode.RESERVED]:
            self.set_state[tango.DevState.DISABLE]
            self.set_state(tango.DevState.DISABLE)

    def _update_csp_health_state(self):
        """
@@ -357,8 +348,7 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
            try:
                # DeviceProxy to sub-elements
                log_msg = "Trying connection to" + str(fqdn) + " device"
                print(log_msg)
                self.dev_logging(log_msg, int(tango.LogLevel.LOG_INFO))
                self.logger.info(log_msg)
                device_proxy = DeviceProxy(fqdn)
                #device_proxy.ping()
                # Note: ping() method is not called. The DeviceProxy is initialized even if the
@@ -392,12 +382,12 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
                self._se_event_id[fqdn]['healthState'] = ev_id
            except KeyError as key_err:
                log_msg = ("No key {} found".format(str(key_err)))
                self.dev_logging(log_msg, tango.LogLevel.LOG_WARN)
                self.logger.warn(log_msg)
            except tango.DevFailed as df:
                #for item in df.args:
                log_msg = ("Failure in connection to {}"
                           " device: {}".format(str(fqdn), str(df.args[0].desc)))
                self.dev_logging(log_msg, tango.LogLevel.LOG_ERROR)
                self.logger.error(log_msg)
                
    def _is_device_running(self, device_fqdn, proxy_list=None):
        """
@@ -424,7 +414,7 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
            # of existing keys.
            # no proxy registered for the subelement device
            msg = "Can't retrieve the information of key {}".format(key_err)
            self.dev_logging(msg, tango.LogLevel.LOG_ERROR)
            self.logger.error(msg)
            return False
        except tango.DevFailed:
            return False
@@ -474,7 +464,7 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
        self._num_dev_completed_task[cmd_name] = 0
        self._list_dev_completed_task[cmd_name] = []
        self._cmd_progress[cmd_name] = 0
        self._cmd_duration_measuredcmd_name] = 0
        self._cmd_duration_measured[cmd_name] = 0
        # sub-element command execution measured time
        se_cmd_duration_measured = defaultdict(lambda:defaultdict(lambda:0))
        # loop on the devices and power-on them sequentially
@@ -506,10 +496,9 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
                command_progress = self._cmd_progress[cmd_name]
                while True:
                    if self._se_state[device] == dev_successful_state:
                        print("Command {} ended with success on device {}.".format(cmd_name, 
                        self.logger.info("Command {} ended with success on device {}.".format(cmd_name,
                                                                                              device))
                        self.dev_logging("Command {} executed on device {}.".format(cmd_name,device),
                                         tango.LogLevel.LOG_INFO)
                        self.logger.info("Command {} executed on device {}.".format(cmd_name,device))
                        # update the list and number of device that completed the task
                        self._num_dev_completed_task[cmd_name]  += 1
                        self._list_dev_completed_task[cmd_name].append(device)
@@ -518,11 +507,7 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
                        self._se_cmd_execution_state[device][cmd_name] = CmdExecState.IDLE
                        se_cmd_duration_measured[device][cmd_name] = (time.time() 
                                                                      - self._se_cmd_starting_time[device])
                        print("measured duration:", se_cmd_duration_measured[device][cmd_name])
                        # The event_id dictionary uses the attribute name in lower case letters as
                        # dictionary key
                        print("evt {}: {}".format(cmd_progress_attr,
                                               self._se_event_id[device][cmd_progress_attr.lower()]))
                        self.logger.info("measured duration:", se_cmd_duration_measured[device][cmd_name])
                        self._se_cmd_progress[device][cmd_name] = 100
                        # command success: exit from the wait loop and issue the command
                        # on the next device of the list
@@ -531,8 +516,7 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
                    if self._se_state[device] in [tango.DevState.FAULT, tango.DevState.ALARM]:
                        self._se_cmd_execution_state[device][cmd_name] = CmdExecState.FAILED
                        self._alarm_message[device] = ("Device {} is {}".format(device, self.get_status()))
                        print(self._alarm_message)
                        self.dev_logging(self._alarm_message[device], tango.LogLevel.LOG_WARN) 
                        self.logger.warn(self._alarm_message[device]) 
                        num_of_failed_device += 1
                        break
                    # check if sub-element command ended throwing an exception: in this case the
@@ -563,14 +547,14 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
                        self._se_cmd_execution_state[device][cmd_name] == CmdExecState.TIMEOUT):
                        msg = ("Timeout executing {} command  on device {}".format(cmd_name, device))
                        print(msg)
                        self.dev_logging(msg, tango.LogLevel.LOG_WARN)
                        self.logger.warn(msg)
                        self._se_cmd_execution_state[device][cmd_name] = CmdExecState.TIMEOUT       
                        num_of_failed_device += 1
                        # if the CBF command timeout expires, the CSP power-on is stopped
                        # TODO: verify if this behavior conflicts with ICD
                        print("elapsed_time:{} device {}".format(elapsed_time, device))
                        if device == self.CspCbf:
                            self.dev_logging("CBF Timeout during power-on!!! Exit", tango.LogLevel.LOG_ERROR)
                            self.logger.error("CBF Timeout during power-on!!! Exit")
                            self._timeout_expired[cmd_name] = True
                            self._se_cmd_execution_state[device][cmd_name] = CmdExecState.IDLE
                            self._timeout_expired[cmd_name] = True
@@ -588,17 +572,17 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
                # update the progress counter at the end of the loop 
                self._cmd_progress[cmd_name] = command_progress + (self._se_cmd_progress[device][cmd_name]/len(device_list))
                if len(device_list) ==  self._num_dev_completed_task[cmd_name] + num_of_failed_device:
                    print("All devices have been handled!")
                    self.logger.info("All devices have been handled!")
                    # end of the command: the command has been issued on all the sub-element devices
                    # reset the execution flag for the CSP
                    break   
            except KeyError as key_err:
                msg = "No key {} found".format(str(key_err))
                self.dev_logging(msg, tango.LogLevel.LOG_WARN)
                self.logger.warn(msg)
            except tango.DevFailed as df:
                # It should not happen! Verify
                msg = "Failure reason: {} Desc: {}".format(str(df.args[0].reason), str(df.args[0].desc))
                self.dev_logging(msg, tango.LogLevel.LOG_ERROR)
                self.logger.warn(msg)
        # out of the for loop
        # reset the CSP command execution flag
        self._cmd_execution_state[cmd_name] = CmdExecState.IDLE
@@ -644,7 +628,7 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
                # TODO: add checks for timeout/errors
            except KeyError as key_err:
                msg = "Can't retrieve the information of key {}".format(key_err)
                self.dev_logging(msg, tango.LogLevel.LOG_ERROR)
                self.logger.error(msg)
                tango.Except.throw_exception("DevFailed excpetion", msg,
                                             "write adminMode", tango.ErrSeverity.ERR)
    def _connect_capabilities_monitor(self):
@@ -1214,7 +1198,7 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
            self._admin_mode = memorized_attr_dict['adminMode']
            self._storage_logging_level = memorized_attr_dict['storageLoggingLevel']
        except KeyError as key_err:
            self.dev_logging("Key {} not found".format(key_err), tango.LogLevel.LOG_INFO)
            self.logger.info("Key {} not found".format(key_err))
        
        # initialize list with CSP sub-element FQDNs
        self._se_fqdn = []
@@ -1235,14 +1219,14 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
        for fqdn in  self._se_fqdn:
            attribute_properties = csp_tango_db.get_device_attribute_property(fqdn,
                                                                              {'adminMode': ['__value']})
            print("fqdn: {} attribute_properties: {}".format(fqdn, attribute_properties))
            self.logger.debug("fqdn: {} attribute_properties: {}".format(fqdn, attribute_properties))
            try:
                admin_mode_memorized = attribute_properties['adminMode']['__value']
                self._se_admin_mode[fqdn] = int(admin_mode_memorized[0])
            except KeyError as key_err:
                msg = ("No key {} found for sub-element {}"
                        " adminMode attribute".format(key_err, fqdn))
                self.dev_logging(msg, tango.LogLevel.LOG_INFO)
                self.logger.info(msg)
                                                                                      
        # _se_proxies: the sub-element proxies
        # implementes s a dictionary:
@@ -1399,7 +1383,7 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
                               "on device {}. Reason: {}".format(event_id,
                                                                 fqdn,
                                                                 key_err))
                        self.dev_logging(msg, tango.LogLevel.LOG_ERROR)
                        self.logger.error(msg)
                # remove the attribute entry from the fqdn dictionary
                for attr_name in event_to_remove[fqdn]:
                    del self._se_event_id[fqdn][attr_name]
@@ -1411,10 +1395,10 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
                else:
                    # What to do in this case??. Only log (for the moment)
                    msg = "Still subscribed events: {}".format(self._se_event_id)
                    self.dev_logging(msg, tango.LogLevel.LOG_WARN)
                    self.logger.warn(msg)
            except KeyError as key_err:
                msg = " Can't retrieve the information of key {}".format(key_err)
                self.dev_logging(msg, tango.LogLevel.LOG_ERROR)
                self.logger.error(msg)
                continue
        # clear any list and dict
        self._se_fqdn.clear()
@@ -1444,7 +1428,6 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
        :return: None
        """
        # PROTECTED REGION ID(CspMaster.adminMode_write) ENABLED START #
        print("set admin mode: fqdn {}".format(self._se_fqdn))
        for fqdn in self._se_fqdn:
            try:
                self._se_write_adminMode(value, fqdn)
@@ -1453,7 +1436,7 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
                log_msg = (("Failure in setting adminMode "
                            "for device {}: {}".format(str(fqdn), 
                            str(df.args[0].reason))))
                self.dev_logging(log_msg, int(tango.LogLevel.LOG_ERROR))                
                self.logger.error(log_msg)
        #TODO: what happens if one sub-element fails?
        # add check on timeout command execution
        self._admin_mode = value
@@ -1717,12 +1700,12 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
    # --------
    # Commands
    # --------
    
    @AdminModeCheck('On')
    def is_On_allowed(self):
        """
        *TANGO is_allowed method*

        Command *Off* is allowed when the device *State* is STANDBY.
        Command *On* is allowed when the device *State* is STANDBY.

        Returns:
            True if the method is allowed, otherwise False.
@@ -1740,8 +1723,6 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
                "the CSP SubElement to switch ON."),
    )
    @DebugIt()  
    @AdminModeCheck('On')
    #@CmdInputArgsCheck(cmd_name = "on", attr_name = "onCommnadProgress")
    @CmdInputArgsCheck("onCommandProgress", "onCmdTimeoutExpired", cmd_name = "on")
    def On(self, argin):
        # PROTECTED REGION ID(CspMaster.On) ENABLED START #
@@ -1795,6 +1776,22 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
        time.sleep(0.2)
        # PROTECTED REGION END #    //  CspMaster.On

    @AdminModeCheck('Off')
    def is_Off_allowed(self):
        """
        *TANGO is_allowed method*

        Command *Off* is allowed when the device *State* is STANDBY.

        Returns:
            True if the method is allowed, otherwise False.
        """
        # PROTECTED REGION ID(CspMaster.is_On_allowed) ENABLED START #
        # Note: as per SKA Guidelines, the command is allowed when 
        if self.get_state() not in [tango.DevState.STANDBY, tango.DevState.OFF]:
            return False
        return True

    @command(
        dtype_in='DevVarStringArray',
        doc_in=("If the array length is 0, the command applies to the whole CSP Element."
@@ -1802,8 +1799,6 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
                " CSP SubElement to switch OFF."),
    )
    @DebugIt()
    @AdminModeCheck('Off')
    #@CmdInputArgsCheck(cmd_name = "off", attr_name = "offcommandprogress")
    @CmdInputArgsCheck("offCommandProgress", "offCmdTimeoutExpired", cmd_name = "off")
    def Off(self, argin):
        # PROTECTED REGION ID(CspMaster.Off) ENABLED START #
@@ -1858,6 +1853,23 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
        
        # PROTECTED REGION END #    //  CspMaster.Off

    @AdminModeCheck('Standby')
    def is_Standby_allowed(self):
        """
        *TANGO is_allowed method*

        Command *Standby* is allowed when the device *State* is ON.

        Returns:
            True if the method is allowed, otherwise False.
        """
        # PROTECTED REGION ID(CspMaster.is_On_allowed) ENABLED START #
        # Note: as per SKA Guidelines, the command is allowed when 
        if self.get_state() not in [tango.DevState.STANDBY, tango.DevState.ON]:
            return False
        return True


    @command(
        dtype_in='DevVarStringArray',
        doc_in=("If the array length is 0, the command applies to the whole CSP Element. "
@@ -1865,8 +1877,6 @@ class CspMaster(with_metaclass(DeviceMeta, SKAMaster)):
                "CSP SubElement to put in STANDBY mode."),
    )
    @DebugIt()
    @AdminModeCheck('Standby')
    #@CmdInputArgsCheck(cmd_name = "standby", attr_name = "standbycommandprogress")
    @CmdInputArgsCheck("standbyCommandProgress", 
                  "standbyCmdTimeoutExpired", cmd_name = "standby")
    def Standby(self, argin):
+0 −0

Empty file added.

+18 −29

File changed.

Preview size limit exceeded, changes collapsed.

+2 −2
Original line number Diff line number Diff line
@@ -9,8 +9,8 @@

"""Release information for Python Package"""

name = """csplmc"""
version = "0.1.0"
name = """csp-lmc-common"""
version = "0.3.0"
version_info = version.split(".")
description = """SKA CSP.LMC Common Software"""
author = "INAF-OAA"
+1 −1
Original line number Diff line number Diff line
@@ -47,7 +47,7 @@ DEFAULT_TAG=$(IMAGE):$(BASE_VERSION)

SHELL=/bin/bash

DOCKER_BUILD_CONTEXT=../csp-lmc-common
DOCKER_BUILD_CONTEXT=..
DOCKER_FILE_PATH=Dockerfile

.PHONY: pre-build docker-build post-build build release patch-release minor-release major-release tag check-status check-release showver \
Loading