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