Beispiel #1
0
    def editOne(self, scenario_json):
        """
        Edits a current scenario with a JSON file
        :param scenario_json: JSON file with the new scenario
        :return: Response object containing the status of the request
        """
        response = Response()
        print(scenario_json)
        scenario_name = scenario_json["scenario_name"]
        if scenario_name in self.scenarios_dict:

            if "machines" in scenario_json:
                for machine in scenario_json["machines"]:

                    if scenario_json["machines"][machine]["uuid"] == "":
                        new_uuid = uuid.uuid4()
                        new_uuid = str(new_uuid).replace('-', '')
                        print("Unique id: ", new_uuid)
                        scenario_json['machines'][machine]['uuid'] = new_uuid

            scenario_json = Scenario(scenario_name).objectFromDictionary(
                scenario_json)

            self.scenarios_dict[scenario_name] = scenario_json
            #self._saveScenarioAsJSON(new_scenario)
            self.db_manager.editScenario(scenario_json.dictionary().copy())
            response.setResponse(True)
            response.setBody(self.scenarios_dict[scenario_name].dictionary())
        else:
            response.setReason('Scenario doesn\'t exist')
            response.setResponse(False)

            response.setBody(dict())
        return response.dictionary()
 def createScenario(self, scenario_name):
     """
     Creates a new scenario which includes the folders and the scenario JSON file
     :param scenario_name: String with the scenario name
     :return: True if the new scenario was successfully created
     """
     #Folder creation moved to FileManager
     self.file_manager.createScenarioFolders(scenario_name)
     scenario = Scenario(scenario_name)
     scenario.generateScenario(scenario_name)
     result = {"result": True}
     return result
Beispiel #3
0
    def newEmpty(self, scenario_name):
        """
        Creates a new scenario which includes the folders and the scenario JSON file
        :param scenario_name: String with the scenario name
        :return: Response object containing the status of the request
        """
        #Folder creation moved to FileManager
        response = Response()
        if scenario_name not in self.scenarios_dict:
            #self.file_manager.createScenarioFolders(scenario_name)
            scenario = Scenario(scenario_name)
            self.scenarios_dict[scenario_name] = scenario
            #self._saveScenarioAsJSON(scenario)
            self.db_manager.insertScenario(scenario.dictionary().copy())
            response.setResponse(True)
            response.setBody(scenario.dictionary())
        else:
            response.setResponse(False)
            response.setReason('Scenario already exist')
            response.setBody(dict())

        return response.dictionary()
Beispiel #4
0
 def addScenariosToDB(self):
     """
     Pre-populates the database with scenarios.
     :return: None
     """
     scenarios_to_add = ['Scenario_1', 'Scenario_3']
     currentScenarios = self.getScenarios()
     scenarios_list = [
         scenario['scenario_name'] for scenario in currentScenarios
     ]
     scenarios_set = set(scenarios_list)
     for scenario_name in scenarios_to_add:
         if scenario_name not in scenarios_set:
             json_name = ''.join([scenario_name, ".json"])
             with open(
                     self.file_manager.getScenarioJSONPath(scenario_name) /
                     json_name) as outfile:
                 scenario_dict = json.load(outfile)
             scenario = Scenario(scenario_name).objectFromDictionary(
                 scenario_dict)
             self.insertScenario(scenario.dictionary().copy())
     return
Beispiel #5
0
 def newEmptyScenario(self, scenario_name):
     """
     Creates a new scenario which includes the folders and the scenario JSON file
     :param scenario_name: String with the scenario name
     :return: True if the new scenario was successfully created
     """
     #Folder creation moved to FileManager
     if scenario_name not in self.scenarios_dict:
         self.file_manager.createScenarioFolders(scenario_name)
         scenario = Scenario(scenario_name)
         self.scenarios_dict[scenario_name] = scenario
         self._saveScenarioAsJSON(scenario)
         return {
             "Response": True,
             "Note": "Operation successful",
             "Body": scenario.dictionary()
         }
     else:
         return {
             "Response": False,
             "Note": "Scenario already exist",
             "Body": dict()
         }
Beispiel #6
0
 def _initializeScenarios(self):
     # Variables
     scenarios_dict = dict()
     scenarios = os.listdir(self.file_manager.getScenariosPath())
     for scenario_name in scenarios:
         json_name = ''.join([scenario_name, ".json"])
         with open(
                 self.file_manager.getJSONPath(scenario_name) /
                 json_name) as outfile:
             scenario_dict = json.load(outfile)
         scenario = Scenario(scenario_name).objectFromDictionary(
             scenario_dict)
         scenarios_dict[scenario_name] = scenario
     return scenarios_dict
Beispiel #7
0
 def _initializeFromDatabase(self):
     """
     Pre-populates the database with scenarios.
     :return: Dictionary containing scenario's data
     """
     # Variables
     scenarios_dict = dict()
     scenarios = self.db_manager.getScenarios()
     for raw_scenario in scenarios:
         del raw_scenario["_id"]
         scenario_name = raw_scenario["scenario_name"]
         scenario = Scenario(scenario_name).objectFromDictionary(
             raw_scenario)
         scenarios_dict[scenario_name] = scenario
     return scenarios_dict
Beispiel #8
0
 def _initializeFromDirectory(self):
     """
     Initializes the scenario's runtime objects using data from the host folders.
     :return: Dictionary containing scenario's data
     """
     # Variables
     scenarios_dict = dict()
     scenarios = os.listdir(self.file_manager.getScenariosPath())
     for scenario_name in scenarios:
         json_name = ''.join([scenario_name, ".json"])
         with open(
                 self.file_manager.getScenarioJSONPath(scenario_name) /
                 json_name) as outfile:
             scenario_dict = json.load(outfile)
         scenario = Scenario(scenario_name).objectFromDictionary(
             scenario_dict)
         scenarios_dict[scenario_name] = scenario
     return scenarios_dict
Beispiel #9
0
 def editScenario(self, new_scenario):
     """
     Edits a current scenario with a JSON file
     :param scenario_name: String with the scenario name
     :param scenario_json: JSON file with the new scenario
     :return: True if the scenario has been successfully edited, otherwise False
     """
     scenario_name = new_scenario["scenario_name"]
     if scenario_name in self.scenarios_dict:
         new_scenario = Scenario(scenario_name).objectFromDictionary(
             new_scenario)
         self.scenarios_dict[scenario_name] = new_scenario
         self._saveScenarioAsJSON(new_scenario)
         return {
             "Response": True,
             "Note": "Operation successful",
             "Body": dict()
         }
     else:
         return {
             "Response": False,
             "Note": "Scenario doesn't exist",
             "Body": dict()
         }