/**
 * *  Copyright (C) 2011 Citrix Systems, Inc.  All rights reserved
*
 *
 * This software is licensed under the GNU General Public License v3 or later.
 *
 * It is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 *
 */
package com.cloud.agent;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ScheduledFuture;
import org.apache.log4j.Logger;
public class MockVmMetrics implements Runnable {
	private static final Logger s_logger = Logger.getLogger(MockVmMetrics.class);
	
	private String vmName;
	
	//the maximum number of network interfaces to a VM (should be 1)
	public final int MAX_INTERFACES=1;
	
	//the maximum number of disks to a VM 
	public final int MAX_DISKS=8;
	
	//the last calculated traffic speed (transmit) per interface
	private Map netTxKBps = new HashMap();
	
	//the last calculated traffic speed (receive) per interface
	private Map netRxKBps = new HashMap();
	
	//the last calculated disk write speed per disk (Bytes Per Second)
	private Map diskWriteKBytesPerSec = new HashMap();
	
	//the last calculated disk read speed per disk (Bytes Per Second)
	private Map diskReadKBytesPerSec = new HashMap();
	
	//Total Bytes Transmitted on network interfaces
	private Map netTxTotalBytes = new HashMap();
	
	//Total Bytes Received on network interfaces
	private Map netRxTotalBytes = new HashMap();
	
	//Total Bytes read per disk
	private Map diskReadTotalBytes = new HashMap();
	//Total Bytes written per disk
	private Map diskWriteTotalBytes = new HashMap();
	
	//CPU time in seconds
	private Double cpuSeconds = new Double(0.0);
	
	//CPU percentage
	private Float cpuPercent = new Float(0.0);
	
	private Map diskMap = new HashMap();
	private Map vifMap = new HashMap();
	
	private Map diskStatTimestamp = new HashMap();
	private Map netStatTimestamp = new HashMap();
	
	private long cpuStatTimestamp = 0L;
	
	private ScheduledFuture> future;
	private boolean stopped = false;
	private Random randSeed = new Random();
	public MockVmMetrics(String vmName) {
		this.vmName = vmName;
		vifMap.put("eth0", "eth0");
		vifMap.put("eth1", "eth1");
		vifMap.put("eth2", "eth2");
		
		Long networkStart = 0L;
		netTxTotalBytes.put("eth0", networkStart);
		netRxTotalBytes.put("eth0", networkStart);
		
		netTxTotalBytes.put("eth1", networkStart);
		netRxTotalBytes.put("eth1", networkStart);
		
		netTxTotalBytes.put("eth2", networkStart);
		netRxTotalBytes.put("eth2", networkStart);		
	}
	
	private int getIncrementor() {
		return randSeed.nextInt(100);
	}
	
	@Override
	public void run() {
		if(s_logger.isDebugEnabled()) {
			s_logger.debug("Generating MockVM metrics");
		}
		for (Map.Entry entry : netRxTotalBytes.entrySet()) {
			entry.setValue(entry.getValue() + getIncrementor());		
		}
		
		for (Map.Entry entry : netTxTotalBytes.entrySet()) {
			entry.setValue(entry.getValue() + getIncrementor());
		}
	}
	
	public String getVmName() {
		return vmName;
	}
	
	public Map getNetTxKBps() {
		return netTxKBps;
	}
	
	public Map getNetRxKBps() {
		return netRxKBps;
	}
	public Map getDiskWriteBytesPerSec() {
		return diskWriteKBytesPerSec;
	}
	
	public Map getDiskReadBytesPerSec() {
		return diskReadKBytesPerSec;
	}
	
	public  Map getNetTxTotalBytes() {
		return netTxTotalBytes;
	}
	public Map getNetRxTotalBytes() {
		return netRxTotalBytes;
	}
	
	public Map getDiskReadTotalBytes() {
		return diskReadTotalBytes;
	}
	public Map getDiskWriteTotalBytes() {
		return diskWriteTotalBytes;
	}
	
	public Double getNetTxKBps(String intf) {
		return netTxKBps.get(intf);
	}
	public Double getNetRxKBps(String intf) {
		return netRxKBps.get(intf);
	}
	
	public Double getDiskWriteKBytesPerSec(String disk) {
		return diskWriteKBytesPerSec.get(disk);
	}
	public Double getDiskReadKBytesPerSec(String disk) {
		return diskReadKBytesPerSec.get(disk);
	}
	
	public Long getNetTxTotalBytes(String intf) {
		return netTxTotalBytes.get(intf);
	}
	public Long getNetRxTotalBytes(String intf) {
		return netRxTotalBytes.get(intf);
	}
	
	public Long getDiskReadTotalBytes(String disk) {
		return diskReadTotalBytes.get(disk);
	}
	public Long getDiskWriteTotalBytes(String disk) {
		return diskWriteTotalBytes.get(disk);
	}
	
	public Double getCpuSeconds() {
		return cpuSeconds;
	}
	public Map getDiskMap() {
		return diskMap;
	}
	public Float getCpuPercent() {
		return cpuPercent;
	}
	
	public void setFuture(ScheduledFuture> sf) {
		this.future = sf;
	}
	public ScheduledFuture> getFuture() {
		return future;
	}
	
	public void stop() {
		this.stopped = true;
	}
}