from flask import Flask, render_template, request
import requests
import warnings
import time

app = Flask(__name__)

# Suppress warnings about unverified HTTPS requests
warnings.filterwarnings("ignore", message="Unverified HTTPS request")

# Define initial authentication URL and credentials
auth_url = 'https://3.143.25.10/nokia-altiplano-ac/rest/auth/login'
username = "adminuser"
password = "SBAc0nnect!"

@app.route('/')
def index():
    return render_template('ontFlask.html')

@app.route('/trigger', methods=['POST'])
def trigger():
    serial_number = request.form['serial_number']
    fiber_name = request.form['fiber_name']
    target = request.form['target']

    # Define the payload for the POST requests
    payload1 = {
        "ibn:intent": {
            "intent-specific-data": {
                "ont:ont": {
                    "ont-type": "G040PQ_v2",
                    "auto": [None],
                    "from-device-mapping": [None],
                    "onu-service-profile": "default",
                    "pon-type": "gpon",
                    "expected-serial-number": serial_number,
                    "uni-service-configuration": [
                        {
                            "service-profile": "default",
                            "uni-id": "LAN1"
                        },
                        {
                            "service-profile": "default",
                            "uni-id": "LAN4"
                        }
                    ],
                    "fiber-name": fiber_name
                }
            },
            "intent-type": "ont",
            "target": target,
            "required-network-state": "active",
            "intent-type-version": 6
        }
    }
    
    payload2 = {
        "ibn:intent": {
            "intent-specific-data": {
                "l2-user:l2-user": {
                    "l2-infra": "ap-mgmt-vlan15",
                    "service-profile": "HSI-1000M-1000M",
                    "user-device-name": target,
                    "q-vlan-id": 15,
                    "uni-id": "LAN1"
                }
            },
            "intent-type": "l2-user",
            "target": f"HSI#{target}-ap-mgmt-vlan15",
            "required-network-state": "active",
            "intent-type-version": 9
        }
    }
    
    payload3 = {
        "ibn:intent": {
            "intent-specific-data": {
                "l2-user:l2-user": {
                    "l2-infra": "IPTV",
                    "service-profile": "IPTV_Single_VLAN",
                    "user-device-name": target,
                    "q-vlan-id": 40,
                    "uni-id": "LAN4"
                }
            },
            "intent-type": "l2-user",
            "target": f"IPTV#{target}",
            "required-network-state": "active",
            "intent-type-version": 9
        }
    }

    try:
        # Perform initial authentication request
        response = requests.post(auth_url, auth=(username, password), verify=False)
        response.raise_for_status()  # Raise an exception for 4xx or 5xx errors

        if response.status_code == 200:
            data = response.json()
            access_token = data.get('accessToken')

            # Define the URLs for subsequent requests
            base_url1 = 'https://3.143.25.10/nokia-altiplano-ac/rest/restconf/data/ibn:ibn?'
            base_url2 = 'https://3.143.25.10/nokia-altiplano-ac/rest/restconf/data/ibn:ibn?'
            base_url3 = 'https://3.143.25.10/nokia-altiplano-ac/rest/restconf/data/ibn:ibn?'

            # Add the access token and content type to the headers for authorization
            headers = {
                'Authorization': f'Bearer {access_token}',
                'Content-Type': 'application/yang-data+json'
            }

            # Make the first trigger request
            response1 = requests.post(base_url1, headers=headers, json=payload1, verify=False)

            # Get response status code and text
            response_status_code1 = response1.status_code
            response_text1 = response1.text

            # Wait for 1 second
            time.sleep(1)

            # Make the second trigger request
            response2 = requests.post(base_url2, headers=headers, json=payload2, verify=False)

            # Get response status code and text
            response_status_code2 = response2.status_code
            response_text2 = response2.text

            # Wait for 1 second
            time.sleep(1)

            # Make the third trigger request
            response3 = requests.post(base_url3, headers=headers, json=payload3, verify=False)

            # Get response status code and text
            response_status_code3 = response3.status_code
            response_text3 = response3.text

            # Return the template with response data
            return render_template('ontResult.html', 
                       status_code1=response_status_code1, response_text1=response_text1,
                       status_code2=response_status_code2, response_text2=response_text2,
                       status_code3=response_status_code3, response_text3=response_text3)

    except requests.exceptions.RequestException as e:
        return render_template('error.html', message="Error: {}".format(e))

if __name__ == '__main__':
    app.run(debug=True, host='10.208.1.243', port=8080)

