Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #include <uve/vm_uve_entry.h> 6 : #include <uve/agent_uve_base.h> 7 : 8 : using namespace std; 9 : 10 26 : VmUveEntryBase::VmUveEntryBase(Agent *agent, const string &vm_name) 11 26 : : agent_(agent), interface_tree_(), uve_info_(), changed_(true), 12 26 : deleted_(false), renewed_(false), mutex_(), add_by_vm_notify_(false), 13 52 : vm_config_name_(vm_name), vm_name_() { 14 26 : } 15 : 16 26 : VmUveEntryBase::~VmUveEntryBase() { 17 26 : } 18 : 19 0 : bool VmUveEntryBase::Update(const VmEntry *vm) { 20 0 : if (vm_config_name_ != vm->GetCfgName()) { 21 0 : vm_config_name_ = vm->GetCfgName(); 22 0 : return true; 23 : } 24 0 : return false; 25 : } 26 : 27 14 : void VmUveEntryBase::InterfaceAdd(const std::string &intf_cfg_name) { 28 14 : InterfaceSet::iterator it = interface_tree_.find(intf_cfg_name); 29 14 : if (it == interface_tree_.end()) { 30 14 : interface_tree_.insert(intf_cfg_name); 31 : } 32 14 : } 33 : 34 14 : void VmUveEntryBase::InterfaceDelete(const std::string &intf_cfg_name) { 35 14 : InterfaceSet::iterator intf_it = interface_tree_.find(intf_cfg_name); 36 14 : if (intf_it != interface_tree_.end()) { 37 14 : interface_tree_.erase(intf_it); 38 : } 39 14 : if (!add_by_vm_notify_ && (interface_tree_.size() == 0)) { 40 0 : renewed_ = false; 41 0 : deleted_ = true; 42 : } 43 14 : if (interface_tree_.size() == 0) { 44 12 : vm_name_ = ""; 45 : } 46 14 : } 47 : 48 0 : bool VmUveEntryBase::FrameVmMsg(const boost::uuids::uuid &u, 49 : UveVirtualMachineAgent *uve) { 50 0 : bool changed = false; 51 0 : assert(!deleted_); 52 0 : uve->set_name(vm_config_name_); 53 0 : vector<string> s_intf_list; 54 : 55 0 : if (!uve_info_.__isset.uuid) { 56 0 : uve->set_uuid(to_string(u)); 57 0 : uve_info_.set_uuid(to_string(u)); 58 0 : changed = true; 59 : } 60 0 : if (!uve_info_.__isset.vm_name || 61 0 : (uve_info_.get_vm_name() != vm_name_)) { 62 0 : uve->set_vm_name(vm_name_); 63 0 : uve_info_.set_vm_name(vm_name_); 64 : } 65 : 66 0 : InterfaceSet::iterator it = interface_tree_.begin(); 67 0 : while(it != interface_tree_.end()) { 68 0 : s_intf_list.push_back(*it); 69 0 : ++it; 70 : } 71 : 72 0 : if (UveVmInterfaceListChanged(s_intf_list)) { 73 0 : uve->set_interface_list(s_intf_list); 74 0 : uve_info_.set_interface_list(s_intf_list); 75 0 : changed = true; 76 : } 77 : 78 0 : string hostname = agent_->host_name(); 79 0 : if (UveVmVRouterChanged(hostname)) { 80 0 : uve->set_vrouter(hostname); 81 0 : uve_info_.set_vrouter(hostname); 82 0 : changed = true; 83 : } 84 : 85 0 : return changed; 86 0 : } 87 : 88 0 : bool VmUveEntryBase::UveVmVRouterChanged(const string &new_value) const { 89 0 : if (!uve_info_.__isset.vrouter) { 90 0 : return true; 91 : } 92 0 : if (new_value.compare(uve_info_.get_vrouter()) == 0) { 93 0 : return false; 94 : } 95 0 : return true; 96 : } 97 : 98 0 : bool VmUveEntryBase::UveVmInterfaceListChanged 99 : (const vector<string> &new_list) 100 : const { 101 0 : if (new_list != uve_info_.get_interface_list()) { 102 0 : return true; 103 : } 104 0 : return false; 105 : } 106 : 107 12 : void VmUveEntryBase::Reset() { 108 24 : UveVirtualMachineAgent uve; 109 : 110 12 : interface_tree_.clear(); 111 12 : uve_info_ = uve; 112 : 113 12 : deleted_ = true; 114 12 : renewed_ = false; 115 12 : add_by_vm_notify_ = false; 116 12 : }