#!/usr/bin/python

from vm import VM
from vmhost import VMhost

class VMPoolPath:
    """
    This class represents an ordered sequence of VM shutdowns,
    migrations, and provisions, between two VM pool states.  The VM
    shutdowns always happen first, followed by the migrations, then
    finally the provisions last.
    """

    def __init__(self, initial_state, final_state):
        self.vms_to_shutdown = [ ]
        self.vms_to_provision = [ ]
        self.path = [ ]
        self.cost = 0

    def set_post_shutdown_state(self, state):
        self.post_shutdown_state = state

    def set_pre_provision_state(self, state):
        self.pre_provision_state = state

    def set_migration_sequence(self, seq):
        self.migration_sequence = seq

    def set_cost(self, cost):
        self.cost = cost

    def report(self):
        print "Short path found with cost %d:" % self.cost
        print "First shut down VMs: %s" % ", ".join(self.vms_to_shutdown)

        print "Start: ", self.post_shutdown_state
        # for state in self.path[1:]:
        #     prev = self.previous[state]
        #     from_state = self.cache[prev]
        #     (vm, to_host, cost) = self.route[state]
        #     from_host = from_state.vm2vmhost[vm]
        #     print "%s: %s -> %s  cost %d" % (vm, from_host, to_host, cost)
        for migration in self.migration_sequence:
            print "%s: %s -> %s  cost %d" % \
                (migration.vm, migration.from_host,
                 migration.to_host, migration.cost())
        print "End:   ", self.pre_provision_state

        provisions = [ "%s on %s" % (vm, vmhost) \
                           for vm, vmhost in self.vms_to_provision ]
        print "Finally provision VMs: %s" % ", ".join(provisions)

@@ -889,16 +889,53 @@
 lf.cost%0A
+        if self.vms_to_shutdown:%0A    
         
@@ -941,16 +941,18 @@
  print %22
+- 
 First sh
@@ -1018,16 +1018,18 @@
  print %22
+  
 Start: %22
@@ -1422,32 +1422,34 @@
          print %22
+! 
 %25s: %25s -%3E %25s  co
@@ -1580,16 +1580,18 @@
  print %22
+  
 End:   %22
@@ -1614,24 +1614,62 @@
 sion_state%0A%0A
+        if self.vms_to_provision:%0A    
         prov
@@ -1707,16 +1707,20 @@
 host) %5C%0A
+    
         
@@ -1788,23 +1788,29 @@
         
+    
 print %22
++ 
 Finally 

improve VMPoolPath.report()