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()
Beispiel #2
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 #3
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 #4
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()
         }