All Classes Functions
hypervisor_factory.py
1 from docker_driver import DockerDriver
2 from libvirt_driver import Libvirt
3 
4 
5 ##
6 #
7 # A singletone class for creating hypervisor driver objects. For an
8 # instantiation of nf.io there can be exactly one object of only one type of
9 # hyperviosr. HyervisorFactory takes care of the creation logic.
10 #
11 class HypervisorFactory(object):
12 
13  __hyp_instance = None
14  __hyp_instance_type = None
15 
16  ##
17  #
18  # Instantiates a HypervisorFactory object.
19  #
20  # Args:
21  # hypervisor_type: The type of hypervisor object to instantiate. Valid
22  # hypervisor types are 'DockerDriver' and 'Libvirt' for the time being.
23  #
24  # Returns:
25  # Nothing. Initializaes the factory object.
26  #
27  # Note:
28  # If this factory class is instantiated multiple times with different
29  # types of hypervisor_type argument then it raises a ValueError.
30  #
31  # If this factory class is instantiated with a hypervisor type other
32  # than Docker or Libvirt it raises a TypeError.
33  #
34  def __init__(self, hypervisor_type="DockerDriver"):
35  if not HypervisorFactory.__hyp_instance:
36  if hypervisor_type == "DockerDriver":
37  HypervisorFactory.__hyp_instance_type = hypervisor_type
38  HypervisorFactory.__hyp_instance = DockerDriver()
39  elif hypervisor_type == "Libvirt":
40  HypervisorFactory.__hyp_instance_type = hypervisor_type
41  HypervisorFactory.__hyp_instance = Libvirt()
42  else:
43  raise TypeError(
44  "Invalid hypervisor type. Valid types are: Docker, Libvirt")
45  elif HypervisorFactory.__hyp_instance_type != hypervisor_type:
46  raise ValueError(
47  "An instantiation of type " +
48  HypervisorFactory.__hyp_instance_type +
49  " already exists.")
50 
51  @staticmethod
52  ##
53  #
54  # Returns the hypervisor driver nstance. If the instance is not initialized then
55  # a RuntimeError is raised.
56  #
58  if HypervisorFactory.__hyp_instance is not None:
59  return HypervisorFactory.__hyp_instance
60  else:
61  raise RuntimeError("Hypervisor not initialized.")
def __init__
Instantiates a HypervisorFactory object.
def get_hypervisor_instance
Returns the hypervisor driver nstance.
A singletone class for creating hypervisor driver objects.