Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #include <uve/vn_uve_entry_base.h> 6 : #include <uve/agent_uve_base.h> 7 : 8 26 : VnUveEntryBase::VnUveEntryBase(Agent *agent, const VnEntry *vn) 9 26 : : agent_(agent), vn_(vn), uve_info_(), interface_tree_(), vm_tree_(), 10 26 : add_by_vn_notify_(false), changed_(true), deleted_(false), 11 26 : renewed_(false) { 12 26 : } 13 : 14 0 : VnUveEntryBase::VnUveEntryBase(Agent *agent) 15 0 : : agent_(agent), vn_(NULL), uve_info_(), interface_tree_(), vm_tree_(), 16 0 : add_by_vn_notify_(false), changed_(true), deleted_(false), 17 0 : renewed_(false) { 18 0 : } 19 : 20 26 : VnUveEntryBase::~VnUveEntryBase() { 21 26 : } 22 : 23 14 : void VnUveEntryBase::VmAdd(const string &vm) { 24 14 : if (vm != agent_->NullString()) { 25 14 : VmSet::iterator it = vm_tree_.find(vm); 26 14 : if (it == vm_tree_.end()) { 27 12 : vm_tree_.insert(vm); 28 : } 29 : } 30 14 : } 31 : 32 14 : void VnUveEntryBase::VmDelete(const string &vm) { 33 14 : if (vm != agent_->NullString()) { 34 14 : VmSet::iterator vm_it = vm_tree_.find(vm); 35 14 : if (vm_it != vm_tree_.end()) { 36 12 : vm_tree_.erase(vm_it); 37 : } 38 : } 39 14 : } 40 : 41 14 : void VnUveEntryBase::InterfaceAdd(const Interface *intf) { 42 14 : InterfaceSet::iterator it = interface_tree_.find(intf); 43 14 : if (it == interface_tree_.end()) { 44 14 : interface_tree_.insert(intf); 45 : } 46 14 : } 47 : 48 14 : void VnUveEntryBase::InterfaceDelete(const Interface *intf) { 49 14 : InterfaceSet::iterator intf_it = interface_tree_.find(intf); 50 14 : if (intf_it != interface_tree_.end()) { 51 14 : interface_tree_.erase(intf_it); 52 : } 53 14 : if (!add_by_vn_notify_ && (interface_tree_.size() == 0)) { 54 0 : renewed_ = false; 55 0 : deleted_ = true; 56 : } 57 14 : } 58 : 59 0 : bool VnUveEntryBase::BuildInterfaceVmList(UveVirtualNetworkAgent &s_vn) { 60 0 : bool changed = false; 61 0 : assert(!deleted_); 62 : 63 0 : s_vn.set_name(vn_->GetName()); 64 0 : vector<string> vm_list; 65 : 66 0 : VmSet::iterator vm_it = vm_tree_.begin(); 67 0 : while (vm_it != vm_tree_.end()) { 68 0 : vm_list.push_back(*vm_it); 69 0 : ++vm_it; 70 : } 71 0 : if (UveVnVmListChanged(vm_list)) { 72 0 : s_vn.set_virtualmachine_list(vm_list); 73 0 : uve_info_.set_virtualmachine_list(vm_list); 74 0 : changed = true; 75 : } 76 : 77 0 : vector<string> intf_list; 78 0 : InterfaceSet::iterator intf_it = interface_tree_.begin(); 79 0 : while (intf_it != interface_tree_.end()) { 80 0 : const Interface *intf = *intf_it; 81 0 : const VmInterface *vm_port = 82 : static_cast<const VmInterface *>(intf); 83 0 : intf_list.push_back(vm_port->cfg_name()); 84 0 : intf_it++; 85 : } 86 0 : if (UveVnInterfaceListChanged(intf_list)) { 87 0 : s_vn.set_interface_list(intf_list); 88 0 : uve_info_.set_interface_list(intf_list); 89 0 : changed = true; 90 : } 91 : 92 0 : return changed; 93 0 : } 94 : 95 0 : bool VnUveEntryBase::UveVnAclChanged(const string &name) const { 96 0 : if (!uve_info_.__isset.acl) { 97 0 : return true; 98 : } 99 0 : if (name.compare(uve_info_.get_acl()) != 0) { 100 0 : return true; 101 : } 102 0 : return false; 103 : } 104 : 105 0 : bool VnUveEntryBase::UveVnMirrorAclChanged(const string &name) const { 106 0 : if (!uve_info_.__isset.mirror_acl) { 107 0 : return true; 108 : } 109 0 : if (name.compare(uve_info_.get_mirror_acl()) != 0) { 110 0 : return true; 111 : } 112 0 : return false; 113 : } 114 : 115 0 : bool VnUveEntryBase::UveVnInterfaceListChanged(const vector<string> &new_list) 116 : const { 117 0 : if (!uve_info_.__isset.interface_list) { 118 0 : return true; 119 : } 120 0 : if (new_list != uve_info_.get_interface_list()) { 121 0 : return true; 122 : } 123 0 : return false; 124 : } 125 : 126 0 : bool VnUveEntryBase::UveVnVmListChanged(const vector<string> &new_list) const { 127 0 : if (!uve_info_.__isset.virtualmachine_list) { 128 0 : return true; 129 : } 130 0 : if (new_list != uve_info_.get_virtualmachine_list()) { 131 0 : return true; 132 : } 133 0 : return false; 134 : } 135 : 136 0 : bool VnUveEntryBase::UveVnAclRuleCountChanged(int32_t size) const { 137 0 : if (!uve_info_.__isset.total_acl_rules) { 138 0 : return true; 139 : } 140 0 : if (size != uve_info_.get_total_acl_rules()) { 141 0 : return true; 142 : } 143 0 : return false; 144 : } 145 : 146 0 : bool VnUveEntryBase::FrameVnAclRuleCountMsg(const VnEntry *vn, 147 : UveVirtualNetworkAgent *uve) { 148 0 : bool changed = false; 149 0 : assert(!deleted_); 150 0 : uve->set_name(vn->GetName()); 151 : 152 : int acl_rule_count; 153 0 : if (vn->GetAcl()) { 154 0 : acl_rule_count = vn->GetAcl()->Size(); 155 : } else { 156 0 : acl_rule_count = 0; 157 : } 158 : 159 0 : if (UveVnAclRuleCountChanged(acl_rule_count)) { 160 0 : uve->set_total_acl_rules(acl_rule_count); 161 0 : uve_info_.set_total_acl_rules(acl_rule_count); 162 0 : changed = true; 163 : } 164 : 165 0 : return changed; 166 : } 167 : 168 0 : bool VnUveEntryBase::FrameVnMsg(const VnEntry *vn, UveVirtualNetworkAgent &uve) { 169 0 : bool changed = false; 170 0 : assert(!deleted_); 171 0 : uve.set_name(vn->GetName()); 172 : 173 0 : string acl_name; 174 : int acl_rule_count; 175 0 : if (vn->GetAcl()) { 176 0 : acl_name = vn->GetAcl()->GetName(); 177 0 : acl_rule_count = vn->GetAcl()->Size(); 178 : } else { 179 0 : acl_name = agent_->NullString(); 180 0 : acl_rule_count = 0; 181 : } 182 : 183 0 : if (UveVnAclChanged(acl_name)) { 184 0 : uve.set_acl(acl_name); 185 0 : uve_info_.set_acl(acl_name); 186 0 : changed = true; 187 : } 188 : 189 0 : if (UveVnAclRuleCountChanged(acl_rule_count)) { 190 0 : uve.set_total_acl_rules(acl_rule_count); 191 0 : uve_info_.set_total_acl_rules(acl_rule_count); 192 0 : changed = true; 193 : } 194 : 195 0 : if (vn->GetMirrorCfgAcl()) { 196 0 : acl_name = vn->GetMirrorCfgAcl()->GetName(); 197 : } else { 198 0 : acl_name = agent_->NullString(); 199 : } 200 0 : if (UveVnMirrorAclChanged(acl_name)) { 201 0 : uve.set_mirror_acl(acl_name); 202 0 : uve_info_.set_mirror_acl(acl_name); 203 0 : changed = true; 204 : } 205 : 206 0 : if (BuildInterfaceVmList(uve)) { 207 0 : changed = true; 208 : } 209 0 : return changed; 210 0 : } 211 : 212 6 : void VnUveEntryBase::Reset() { 213 12 : UveVirtualNetworkAgent uve; 214 : 215 6 : interface_tree_.clear(); 216 6 : vm_tree_.clear(); 217 6 : uve_info_ = uve; 218 6 : vn_ = NULL; 219 : 220 6 : deleted_ = true; 221 6 : renewed_ = false; 222 6 : add_by_vn_notify_ = false; 223 6 : }