import random
import string
import time
import datetime
import sys
import collections
import json
import tzlocal
import os
import fcntl
import logging
from locust import HttpLocust, TaskSet, task
from decimal import Decimal


class UserBehavior(TaskSet):
    base = "/ecomp/mso/infra/e2eServiceInstances/v3"
    headers = {"Accept":"application/json","Content-Type":"application/json","Authorization":"Basic SW5mcmFQb3J0YWxDbGllbnQ6cGFzc3dvcmQxJA=="}
    service_creation_body = "{\"service\": {\"name\": \"E2E_volte_%s\", \"description\": \"E2E_volte_ONAP_deploy\", \"serviceDefId\": \"a16eb184-4a81-4c8c-89df-c287d390315a\", \"templateId\": \"012c3446-51db-4a2a-9e64-a936f10a5e3c\", \"parameters\": { \"globalSubscriberId\": \"Demonstration\", \"subscriberName\": \"Demonstration\", \"serviceType\": \"vIMS\", \"templateName\": \"VoLTE e2e Service:null\", \"resources\": [ { \"resourceName\": \"VL OVERLAYTUNNEL\", \"resourceDefId\": \"671d4757-b018-47ab-9df3-351c3bda0a98\", \"resourceId\": \"e859b0fd-d928-4cc8-969e-0fee7795d623\", \"nsParameters\": { \"locationConstraints\": [], \"additionalParamForNs\": { \"site2_vni\": \"5010\", \"site1_localNetworkAll\": \"false\", \"site1_vni\": \"5010\", \"site1_exportRT1\": \"11:1\", \"description\": \"overlay\", \"site2_localNetworkAll\": \"false\", \"site1_routerId\": \"9.9.9.9\", \"site1_fireWallEnable\": \"false\", \"site1_networkName\": \"network1\", \"site2_description\": \"overlay\", \"site1_importRT1\": \"11:1\", \"site1_description\": \"overlay\", \"site2_networkName\": \"network3\", \"name\": \"overlay\", \"site2_fireWallEnable\": \"false\", \"site2_id\": \"ZTE-DCI-Controller\", \"site2_routerId\": \"9.9.9.9\", \"site2_importRT1\": \"11:1\", \"site2_exportRT1\": \"11:1\", \"site2_fireWallId\": \"false\", \"site1_id\": \"DCI-Controller-1\", \"tunnelType\": \"L3-DCI\" } } },{\"resourceName\": \"VL UNDERLAYVPN\", \"resourceDefId\": \"4f5d692b-4022-43ab-b878-a93deb5b2061\", \"resourceId\": \"b977ec47-45b2-41f6-aa03-bf6554dc9620\", \"nsParameters\": { \"locationConstraints\": [], \"additionalParamForNs\": { \"topology\": \"full-mesh\", \"site2_name\": \"site2\", \"sna2_name\": \"site2_sna\", \"description\": \"underlay\", \"sna1_name\": \"site1_sna\", \"ac1_route\": \"3.3.3.12/30:dc84ce88-99f7\", \"ac2_peer_ip\": \"3.3.3.20/30\", \"technology\": \"mpls\", \"ac2_route\": \"3.3.3.20/30:98928302-3287\", \"ac2_id\": \"84d937a4-b227-375f-a744-2b778f36e04e\", \"ac1_protocol\": \"STATIC\", \"ac2_svlan\": \"4004\", \"serviceType\": \"l3vpn-ipwan\", \"ac2_ip\": \"3.3.3.21/30\", \"pe2_id\": \"4412d3f0-c296-314d-9284-b72fc5d485e8\", \"ac1_id\": \"b4f01ac0-c1e1-3e58-a8be-325e4372c960\", \"af_type\": \"ipv4\", \"ac1_svlan\": \"4002\", \"ac1_peer_ip\": \"3.3.3.12/30\", \"ac1_ip\": \"3.3.3.13/30\", \"version\": \"1.0\", \"name\": \"testunderlay\", \"id\": \"123124141\", \"pe1_id\": \"2ef788f0-407c-3070-b756-3a5cd71fde18\", \"ac2_protocol\": \"STATIC\", \"site1_name\": \"stie1\" } } } ] } } }"
    # following class variables to make them unique across all users
    transaction_file= open("transaction.log", "w+")
    operation_file = open("operation.log", "w+")
    
    def on_start(self):
        """ on_start is called when a Locust start before any task is scheduled """
        self.init()

    def init(self):
        pass

    def myconverter(self, o):
        if isinstance(o, datetime.datetime):
            return o.__str__()

    @task(1)
    def create_service(self):
	# Post a E2E service instantiation request to SO
        method = "POST"
        url = self.base
	service_instance_name = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10))
	data = self.service_creation_body % service_instance_name

	t1 = datetime.datetime.now(tzlocal.get_localzone())
        response = self.client.request(method, url, headers=self.headers, data=data)
	t2 = datetime.datetime.now(tzlocal.get_localzone())
	delta = t2 - t1
        data = collections.OrderedDict()
        data['datetime'] = datetime.datetime.now(tzlocal.get_localzone()).strftime("%Y-%m-%dT%H:%M:%S%Z")
        data['method'] = method
        data['url'] = url
        data['status_code'] = response.status_code
        data['transaction_time'] = (delta.seconds*10^6 + delta.microseconds)/1000
        fcntl.flock(self.transaction_file, fcntl.LOCK_EX)
        self.transaction_file.write(json.dumps(data, default = self.myconverter) + "\n") 
        self.transaction_file.flush()
        os.fsync(self.transaction_file)
        fcntl.flock(self.transaction_file, fcntl.LOCK_UN)
	serviceId = response.json()['service']['serviceId']
	operationId = response.json()['service']['operationId']

	# Get the request status
	method = "GET"
	url = self.base + "/" + serviceId + "/operations/" + operationId
	url1 = "/ecomp/mso/infra/e2eServiceInstances/v3/{serviceId}/operations/{operationId}"
	count = 1
	while count < 50:
	    tt1 = datetime.datetime.now()
	    response = self.client.request(method, url, name=url1, headers=self.headers)
	    tt2 = datetime.datetime.now()
            delta = tt2 - tt1
	    result = response.json()['operationStatus']['result']	    
	    progress = response.json()['operationStatus']['progress']
            data = collections.OrderedDict()
            data['datetime'] = datetime.datetime.now(tzlocal.get_localzone()).strftime("%Y-%m-%dT%H:%M:%S%Z")
            data['method'] = method
            data['url'] = url1
            data['status_code'] = response.status_code
            data['count'] = count
            data['result'] = result
            data['progress'] = progress
            data['transaction_time'] = (delta.seconds*10^6 + delta.microseconds)/1000
            fcntl.flock(self.transaction_file, fcntl.LOCK_EX)
            self.transaction_file.write(json.dumps(data, default = self.myconverter) + "\n") 
            self.transaction_file.flush()
            os.fsync(self.transaction_file)
            fcntl.flock(self.transaction_file, fcntl.LOCK_UN)
	    if result == "finished" or result == "error":
                break
	    else:
		time.sleep(1)
		count = count + 1
	
        if result == "finished":
            result = "success"
        else:
            result = "failure"
	t3 = datetime.datetime.now(tzlocal.get_localzone())
	delta = t3 - t1
        data = collections.OrderedDict()
        data['datetime'] = t1.strftime("%Y-%m-%dT%H:%M:%S%Z")
        data['operation'] = "volte_create"
        data['result'] = result
        data['duration'] = round(delta.seconds + Decimal(delta.microseconds/1000000.0), 3)
        fcntl.flock(self.operation_file, fcntl.LOCK_EX)
        self.operation_file.write(json.dumps(data, default = self.myconverter) + "\n")
        self.operation_file.flush()
        os.fsync(self.operation_file)
        fcntl.flock(self.operation_file, fcntl.LOCK_UN)

	self.delete_service(serviceId)

    def delete_service(self, serviceId):
	method = "DELETE"
 	url = self.base + "/" + serviceId
	data = "{\"globalSubscriberId\":\"Demonstration\", \"serviceType\":\"vIMS\"}" 	
	t1 = datetime.datetime.now(tzlocal.get_localzone())
        response = self.client.request(method, url, name=self.base, headers=self.headers, data=data)
	t2 = datetime.datetime.now(tzlocal.get_localzone())
	delta = t2 - t1
        data = collections.OrderedDict()
        data['datetime'] = datetime.datetime.now(tzlocal.get_localzone()).strftime("%Y-%m-%dT%H:%M:%S%Z")
        data['method'] = method
        data['url'] = self.base
        data['status_code'] = response.status_code
        data['transaction_time'] = (delta.seconds*10^6 + delta.microseconds)/1000
        fcntl.flock(self.transaction_file, fcntl.LOCK_EX)
        self.transaction_file.write(json.dumps(data, default = self.myconverter) + "\n")
        self.transaction_file.flush()
        os.fsync(self.transaction_file)
        fcntl.flock(self.transaction_file, fcntl.LOCK_UN)
	operationId = response.json()['operationId']

	# Get the request status
	method = "GET"
	url = self.base + "/" + serviceId + "/operations/" + operationId
	url1 = "/ecomp/mso/infra/e2eServiceInstances/v3/{serviceId}/operations/{operationId}"
	count = 1
	while count < 50:
	    tt1 = datetime.datetime.now(tzlocal.get_localzone())
	    response = self.client.request(method, url, name=url1, headers=self.headers)
	    tt2 = datetime.datetime.now(tzlocal.get_localzone())
            delta = tt2 - tt1
	    result = response.json()['operationStatus']['result']	    
	    progress = response.json()['operationStatus']['progress']
            data = collections.OrderedDict()
            data['datetime'] = datetime.datetime.now(tzlocal.get_localzone()).strftime("%Y-%m-%dT%H:%M:%S%Z")
            data['method'] = method
            data['url'] = url1
            data['status_code'] = response.status_code
            data['count'] = count
            data['result'] = result
            data['progress'] = progress
            data['transaction_time'] = (delta.seconds*10^6 + delta.microseconds)/1000
            fcntl.flock(self.transaction_file, fcntl.LOCK_EX)
            self.transaction_file.write(json.dumps(data, default = self.myconverter) + "\n") 
            self.transaction_file.flush()
            os.fsync(self.transaction_file)
            fcntl.flock(self.transaction_file, fcntl.LOCK_UN)
	    if result == "finished" or result == "error":
		break
	    else:
		time.sleep(1)
		count = count + 1
	
        if result == "finished":
            result = "success"
        else:
            result = "failure"
	t3 = datetime.datetime.now(tzlocal.get_localzone())
	delta = t3 - t1
        data = collections.OrderedDict()
        data['datetime'] = t1.strftime("%Y-%m-%dT%H:%M:%S%Z")
        data['operation'] = "volte_delete"
        data['result'] = result
        data['duration'] = round(delta.seconds + Decimal(delta.microseconds/1000000.0), 3)
        fcntl.flock(self.operation_file, fcntl.LOCK_EX)
        self.operation_file.write(json.dumps(data, default = self.myconverter) + "\n")
        self.operation_file.flush()
        os.fsync(self.operation_file)
        fcntl.flock(self.operation_file, fcntl.LOCK_UN)

        
class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    min_wait = 1000
    max_wait = 3000