<!--
 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.
-->

{% extends 'base.html' %}

{% block content %}
    <h1>Policy</h1>

    <div class="row">
        <div class="col">
            {{ policies | length }} policies found in context <i>{{ session['context_uuid'] }}</i>
        </div>
    </div>

    <table class="table table-striped table-hover">
        <thead>
          <tr>
            <th scope="col">UUID</th>
            <th scope="col">Kind</th>
            <th scope="col">Priority</th>
            <th scope="col">Condition</th>
            <th scope="col">Operator</th>
            <th scope="col">Action</th>
            <th scope="col">Service</th>
            <th scope="col">Devices</th>
            <th scope="col">State</th>
            <th scope="col">Message</th>
            <th scope="col">Extra</th>
            <th scope="col"></th>
          </tr>
        </thead>
        <tbody>
            {% if policies %}
                {% for policy in policies %}
                    {% if policy.WhichOneof('policy_rule') == 'device' %}
                        <tr>
                            <td>{{ policy.device.policyRuleBasic.policyRuleId.uuid }}</td>
                            <td>{{ policy.WhichOneof('policy_rule') }}</td>
                            <td>{{ policy.device.policyRuleBasic.priority }}</td>
                            <td>{{ policy.device.policyRuleBasic.conditionList }}</td>
                            <td>{{ policy.device.policyRuleBasic.booleanOperator }}</td>
                            <td>{{ policy.device.policyRuleBasic.actionList }}</td>
                            <td>-</td>
                            <td>{{ policy.device.deviceList }}</td>
                            <td>{{ prse.Name(policy.device.policyRuleBasic.policyRuleState.policyRuleState).replace('POLICY_', '') }}</td>
                            <td>{{ policy.device.policyRuleBasic.policyRuleState.policyRuleStateMessage }}</td>
                        </tr>
                    {% elif policy.WhichOneof('policy_rule') == 'service' %}
                        <tr>
                            <td>{{ policy.service.policyRuleBasic.policyRuleId.uuid }}</td>
                            <td>{{ policy.WhichOneof('policy_rule') }}</td>
                            <td>{{ policy.service.policyRuleBasic.priority }}</td>
                            <td>{{ policy.service.policyRuleBasic.conditionList }}</td>
                            <td>{{ policy.service.policyRuleBasic.booleanOperator }}</td>
                            <td>{{ policy.service.policyRuleBasic.actionList }}</td>
                            <td>{{ policy.service.serviceId }}</td>
                            <td>{{ policy.service.deviceList }}</td>
                            <td>{{ prse.Name(policy.service.policyRuleBasic.policyRuleState.policyRuleState).replace('POLICY_', '') }}</td>
                            <td>{{ policy.service.policyRuleBasic.policyRuleState.policyRuleStateMessage }}</td>
                        </tr>
                    {% else %}
                        <tr><td colspan="11">Unsupported policy type {{ policy.WhichOneof('policy_rule') }}</td></tr>
                    {% endif %}
                {% endfor %}
            {% else %}
                <tr><td colspan="11">No policies found</td></tr>
            {% endif %}
        </tbody>
    </table>

{% endblock %}
