Beispiel #1
0
    def ApplyConnectivityChanges(self, context, request):
        """
        Configures VLANs on multiple ports or port-channels
        :param ResourceCommandContext context: The context object for the command with resource and reservation info
        :param str request: A JSON object with the list of requested connectivity changes
        :return: a json object with the list of connectivity changes which were carried out by the switch
        :rtype: str
        """

        return apply_connectivity_changes(request=request,
                                          add_vlan_action=lambda x: ConnectivitySuccessResponse(x,'Success'),
                                          remove_vlan_action=lambda x: ConnectivitySuccessResponse(x,'Success'))
    def test_it_delegates_all_remove_vlan_calls_to_supplied_callback(self):
        unique_message = 'Unique Result'

        remove_vlan_function = MagicMock(
            side_effect=lambda action: ConnectivitySuccessResponse(
                action, unique_message))

        # Arrange
        remove_vlan_action = self._stub_remove_vlan_action(
            full_address='192.1.3.4/1', full_name='res1/port1', vlan_id='200')

        server_request = DriverRequest()
        server_request.actions = [remove_vlan_action]
        request_json = jsonpickle.encode(
            DriverRequestSimulation(server_request), unpicklable=False)

        # Act
        result = apply_connectivity_changes(
            request=request_json,
            logger=self.logger,
            add_vlan_action={},
            remove_vlan_action=remove_vlan_function)

        # Assert
        remove_vlan_function.assert_called_once()
        response = result.driverResponse
        """:type : DriverResponse """
        action_results = response.actionResults
        """:type : list[ConnectivityActionResult] """

        # We validate that the action was delegated by looking for th eunique value we returned
        self.assertEqual(action_results[0].infoMessage, unique_message)
    def test_init(self):
        """Check that __init__ nethod will set up object with correct params"""
        action = mock.MagicMock()
        result_string = "info message"

        # act
        response = ConnectivitySuccessResponse(action=action, result_string=result_string)

        # verify
        self.assertEqual(response.type, action.type)
        self.assertEqual(response.actionId, action.actionId)
        self.assertIsNone(response.errorMessage)
        self.assertEqual(response.updatedInterface, action.actionTarget.fullName)
        self.assertEqual(response.infoMessage, result_string)
        self.assertTrue(response.success)
    def test_it_merges_the_result_of_all_callbacks_to_one_result_object(self):
        unique_message = 'Unique Result'

        add_vlan_function = MagicMock(
            side_effect=lambda action: ConnectivitySuccessResponse(
                action, unique_message))

        # Arrange
        set_vlan_action_1 = self._stub_set_vlan_action(
            full_address='192.1.3.4/1', full_name='res1/port1', vlan_id='200')
        set_vlan_action_2 = self._stub_set_vlan_action(
            full_address='192.1.3.4/2', full_name='res1/port2', vlan_id='201')
        set_vlan_action_3 = self._stub_set_vlan_action(
            full_address='192.1.3.4/3', full_name='res1/port3', vlan_id='202')
        set_vlan_action_4 = self._stub_set_vlan_action(
            full_address='192.1.3.4/4', full_name='res1/port4', vlan_id='203')

        server_request = DriverRequest()
        server_request.actions = [
            set_vlan_action_1, set_vlan_action_2, set_vlan_action_3,
            set_vlan_action_4
        ]
        request_json = jsonpickle.encode(
            DriverRequestSimulation(server_request), unpicklable=False)

        # Act
        result = apply_connectivity_changes(request=request_json,
                                            logger=self.logger,
                                            add_vlan_action=add_vlan_function,
                                            remove_vlan_action=[])

        # Assert
        add_vlan_function.assert_called()
        # We validate that the action was delegated by looking for th eunique value we returned
        response = result.driverResponse
        """:type : DriverResponse """
        action_results = response.actionResults
        """:type : list[ConnectivityActionResult] """
        self.assertEqual(len(action_results), 4)
        for result in action_results:
            self.assertEqual(result.infoMessage, unique_message)
    def apply_connectivity_changes(self, request):
        """ Handle apply connectivity changes request json, trigger add or remove vlan methods,
        get responce from them and create json response

        :param request: json with all required action to configure or remove vlans from certain port
        :return Serialized DriverResponseRoot to json
        :rtype json
        """

        if request is None or request == "":
            raise Exception(self.__class__.__name__,
                            "request is None or empty")

        holder = JsonRequestDeserializer(jsonpickle.decode(request))

        if not holder or not hasattr(holder, "driverRequest"):
            raise Exception(self.__class__.__name__,
                            "Deserialized request is None or empty")

        driver_response = DriverResponse()
        add_vlan_thread_list = []
        remove_vlan_thread_list = []
        driver_response_root = DriverResponseRoot()

        for action in holder.driverRequest.actions:
            self._logger.info("Action: ", action.__dict__)
            self._validate_request_action(action)

            action_id = action.actionId
            full_name = action.actionTarget.fullName
            port_mode = action.connectionParams.mode.lower()

            if action.type == "setVlan":
                qnq = False
                ctag = ""
                for attribute in action.connectionParams.vlanServiceAttributes:
                    if attribute.attributeName.lower(
                    ) == "qnq" and attribute.attributeValue.lower() == "true":
                        qnq = True
                    if attribute.attributeName.lower() == "ctag":
                        ctag = attribute.attributeValue

                for vlan_id in self._get_vlan_list(
                        action.connectionParams.vlanId):
                    add_vlan_thread = Thread(target=self.add_vlan,
                                             name=action_id,
                                             args=(vlan_id, full_name,
                                                   port_mode, qnq, ctag))
                    add_vlan_thread_list.append(add_vlan_thread)
            elif action.type == "removeVlan":
                for vlan_id in self._get_vlan_list(
                        action.connectionParams.vlanId):
                    remove_vlan_thread = Thread(target=self.remove_vlan,
                                                name=action_id,
                                                args=(
                                                    vlan_id,
                                                    full_name,
                                                    port_mode,
                                                ))
                    remove_vlan_thread_list.append(remove_vlan_thread)
            else:
                self._logger.warning(
                    "Undefined action type determined '{}': {}".format(
                        action.type, action.__dict__))
                continue

        # Start all created remove_vlan_threads
        for thread in remove_vlan_thread_list:
            thread.start()

        # Join all remove_vlan_threads. Main thread will wait completion of all remove_vlan_thread
        for thread in remove_vlan_thread_list:
            thread.join()

        # Start all created add_vlan_threads
        for thread in add_vlan_thread_list:
            thread.start()

        # Join all add_vlan_threads. Main thread will wait completion of all add_vlan_thread
        for thread in add_vlan_thread_list:
            thread.join()

        request_result = []
        for action in holder.driverRequest.actions:
            result_statuses, message = zip(*self.result.get(action.actionId))
            if all(result_statuses):
                action_result = ConnectivitySuccessResponse(
                    action,
                    "Add Vlan {vlan} configuration successfully completed".
                    format(vlan=action.connectionParams.vlanId))
            else:
                message_details = "\n\t".join(message)
                action_result = ConnectivityErrorResponse(
                    action, "Add Vlan {vlan} configuration failed."
                    "\nAdd Vlan configuration details:\n{message_details}".
                    format(vlan=action.connectionParams.vlanId,
                           message_details=message_details))
            request_result.append(action_result)

        driver_response.actionResults = request_result
        driver_response_root.driverResponse = driver_response
        return serialize_to_json(
            driver_response_root)  # .replace("[true]", "true")