// EndpointGroupDelete deletes end point group func (ac *APIController) EndpointGroupDelete(endpointGroup *contivModel.EndpointGroup) error { log.Infof("Received EndpointGroupDelete: %+v", endpointGroup) // delete the endpoint group state err := master.DeleteEndpointGroup(endpointGroup.EndpointGroupID) if err != nil { log.Errorf("Error creating endpoing group %+v. Err: %v", endpointGroup, err) } // Detach the endpoint group from the Policies for _, policyName := range endpointGroup.Policies { policyKey := endpointGroup.TenantName + ":" + policyName // find the policy policy := contivModel.FindPolicy(policyKey) if policy == nil { log.Errorf("Could not find policy %s", policyName) continue } // detach policy to epg err := master.PolicyDetach(endpointGroup, policy) if err != nil && err != master.EpgPolicyExists { log.Errorf("Error detaching policy %s from epg %s", policyName, endpointGroup.Key) } // Remove links modeldb.RemoveLinkSet(&policy.LinkSets.EndpointGroups, endpointGroup) modeldb.RemoveLinkSet(&endpointGroup.LinkSets.Policies, policy) policy.Write() } return nil }
// Cleans up state off endpointGroup and related objects. func endpointGroupCleanup(endpointGroup *contivModel.EndpointGroup) { // delete the endpoint group state err := master.DeleteEndpointGroup(endpointGroup.TenantName, endpointGroup.GroupName) if err != nil { log.Errorf("Error deleting endpoint group %+v. Err: %v", endpointGroup, err) } // Detach the endpoint group from the Policies for _, policyName := range endpointGroup.Policies { policyKey := endpointGroup.TenantName + ":" + policyName // find the policy policy := contivModel.FindPolicy(policyKey) if policy == nil { log.Errorf("Could not find policy %s", policyName) continue } // detach policy to epg err := master.PolicyDetach(endpointGroup, policy) if err != nil && err != master.EpgPolicyExists { log.Errorf("Error detaching policy %s from epg %s", policyName, endpointGroup.Key) } // Remove links modeldb.RemoveLinkSet(&policy.LinkSets.EndpointGroups, endpointGroup) modeldb.RemoveLinkSet(&endpointGroup.LinkSets.Policies, policy) policy.Write() } // Cleanup any external contracts err = cleanupExternalContracts(endpointGroup) if err != nil { log.Errorf("Error cleaning up external contracts for epg %s", endpointGroup.Key) } // Remove the endpoint group from network and tenant link sets. nwObjKey := endpointGroup.TenantName + ":" + endpointGroup.NetworkName network := contivModel.FindNetwork(nwObjKey) if network != nil { modeldb.RemoveLinkSet(&network.LinkSets.EndpointGroups, endpointGroup) network.Write() } tenant := contivModel.FindTenant(endpointGroup.TenantName) if tenant != nil { modeldb.RemoveLinkSet(&tenant.LinkSets.EndpointGroups, endpointGroup) tenant.Write() } }
// RuleDelete deletes the rule within a policy func (ac *APIController) RuleDelete(rule *contivModel.Rule) error { log.Infof("Received RuleDelete: %+v", rule) policyKey := rule.TenantName + ":" + rule.PolicyName // find the policy policy := contivModel.FindPolicy(policyKey) if policy == nil { log.Errorf("Error finding policy %s", policyKey) return core.Errorf("Policy not found") } // unlink the rule from policy modeldb.RemoveLinkSet(&policy.LinkSets.Rules, rule) err := policy.Write() if err != nil { return err } // Trigger policyDB Update err = master.PolicyDelRule(policy, rule) if err != nil { log.Errorf("Error deleting rule %s to policy %s. Err: %v", rule.Key, policy.Key, err) return err } return nil }
// NetworkDelete deletes network func (ac *APIController) NetworkDelete(network *contivModel.Network) error { log.Infof("Received NetworkDelete: %+v", network) // Find the tenant tenant := contivModel.FindTenant(network.TenantName) if tenant == nil { return core.Errorf("Tenant not found") } // Remove link modeldb.RemoveLinkSet(&tenant.LinkSets.Networks, network) // Save the tenant too since we removed the links err := tenant.Write() if err != nil { return err } // Get the state driver stateDriver, err := utils.GetStateDriver() if err != nil { return err } // Delete the network networkID := network.NetworkName + "." + network.TenantName err = master.DeleteNetworkID(stateDriver, networkID) if err != nil { log.Errorf("Error deleting network %s. Err: %v", network.NetworkName, err) } return nil }
// AppProfileDelete delete the app func (ac *APIController) AppProfileDelete(prof *contivModel.AppProfile) error { log.Infof("Received AppProfileDelete: %+v", prof) tenant := contivModel.FindTenant(prof.TenantName) if tenant == nil { return core.Errorf("Tenant %s not found", prof.TenantName) } DeleteAppNw(prof) // remove all links for _, epg := range prof.EndpointGroups { epgKey := prof.TenantName + ":" + epg epgObj := contivModel.FindEndpointGroup(epgKey) if epgObj == nil { log.Errorf("EndpointGroup %s not found", epgKey) continue } modeldb.RemoveLink(&epgObj.Links.AppProfile, prof) err := epgObj.Write() if err != nil { log.Errorf("Error updating epg state(%+v). Err: %v", epgObj, err) } } modeldb.RemoveLinkSet(&tenant.LinkSets.AppProfiles, prof) tenant.Write() return nil }
// AppProfileUpdate updates app func (ac *APIController) AppProfileUpdate(oldProf, newProf *contivModel.AppProfile) error { log.Infof("Received AppProfileUpdate: %+v, newProf: %+v", oldProf, newProf) // handle any epg addition for _, epg := range newProf.EndpointGroups { epgKey := newProf.TenantName + ":" + epg log.Infof("Add %s to %s", epgKey, newProf.AppProfileName) epgObj := contivModel.FindEndpointGroup(epgKey) if epgObj == nil { return core.Errorf("EndpointGroup %s not found", epgKey) } modeldb.AddLinkSet(&newProf.LinkSets.EndpointGroups, epgObj) // workaround for objdb update problem modeldb.AddLinkSet(&oldProf.LinkSets.EndpointGroups, epgObj) modeldb.AddLink(&epgObj.Links.AppProfile, newProf) err := epgObj.Write() if err != nil { log.Errorf("Error updating epg state(%+v). Err: %v", epgObj, err) return err } } // handle any epg removal for _, epg := range oldProf.EndpointGroups { if !stringInSlice(epg, newProf.EndpointGroups) { epgKey := newProf.TenantName + ":" + epg log.Infof("Remove %s from %s", epgKey, newProf.AppProfileName) epgObj := contivModel.FindEndpointGroup(epgKey) if epgObj == nil { return core.Errorf("EndpointGroup %s not found", epgKey) } modeldb.RemoveLink(&epgObj.Links.AppProfile, oldProf) err := epgObj.Write() if err != nil { log.Errorf("Error updating epg state(%+v). Err: %v", epgObj, err) return err } // workaround for objdb update problem modeldb.RemoveLinkSet(&oldProf.LinkSets.EndpointGroups, epgObj) } } // workaround for objdb update problem -- should fix model oldProf.EndpointGroups = newProf.EndpointGroups // update the app nw DeleteAppNw(oldProf) CreateAppNw(oldProf) return nil }
// Cleanup external contracts from an epg. func cleanupExternalContracts(endpointGroup *contivModel.EndpointGroup) error { tenant := endpointGroup.TenantName for _, contractsGrp := range endpointGroup.ExtContractsGrps { contractsGrpKey := tenant + ":" + contractsGrp contractsGrpObj := contivModel.FindExtContractsGroup(contractsGrpKey) if contractsGrpObj != nil { // Break any linkeage we might have set. modeldb.RemoveLinkSet(&contractsGrpObj.LinkSets.EndpointGroups, endpointGroup) modeldb.RemoveLinkSet(&endpointGroup.LinkSets.ExtContractsGrps, contractsGrpObj) // Links broken, update the contracts group object. err := contractsGrpObj.Write() if err != nil { return err } } else { log.Errorf("Error cleaning up consumed ext contract %s", contractsGrp) continue } } return nil }
// NetworkDelete deletes network func (ac *APIController) NetworkDelete(network *contivModel.Network) error { log.Infof("Received NetworkDelete: %+v", network) // Find the tenant tenant := contivModel.FindTenant(network.TenantName) if tenant == nil { return core.Errorf("Tenant not found") } // if the network has associated epgs, fail the delete epgCount := len(network.LinkSets.EndpointGroups) if epgCount != 0 { return core.Errorf("cannot delete %s has %d endpoint groups", network.NetworkName, epgCount) } // Remove link modeldb.RemoveLinkSet(&tenant.LinkSets.Networks, network) // Save the tenant too since we removed the links err := tenant.Write() if err != nil { return err } // Get the state driver stateDriver, err := utils.GetStateDriver() if err != nil { return err } // Delete the network networkID := network.NetworkName + "." + network.TenantName err = master.DeleteNetworkID(stateDriver, networkID) if err != nil { log.Errorf("Error deleting network %s. Err: %v", network.NetworkName, err) return err } return nil }
// EndpointGroupUpdate updates endpoint group func (ac *APIController) EndpointGroupUpdate(endpointGroup, params *contivModel.EndpointGroup) error { log.Infof("Received EndpointGroupUpdate: %+v, params: %+v", endpointGroup, params) // Only update policy attachments // Look for policy adds for _, policyName := range params.Policies { if !stringInSlice(policyName, endpointGroup.Policies) { policyKey := endpointGroup.TenantName + ":" + policyName // find the policy policy := contivModel.FindPolicy(policyKey) if policy == nil { log.Errorf("Could not find policy %s", policyName) return core.Errorf("Policy not found") } // attach policy to epg err := master.PolicyAttach(endpointGroup, policy) if err != nil && err != master.EpgPolicyExists { log.Errorf("Error attaching policy %s to epg %s", policyName, endpointGroup.Key) return err } // Setup links modeldb.AddLinkSet(&policy.LinkSets.EndpointGroups, endpointGroup) modeldb.AddLinkSet(&endpointGroup.LinkSets.Policies, policy) err = policy.Write() if err != nil { return err } } } // now look for policy removals for _, policyName := range endpointGroup.Policies { if !stringInSlice(policyName, params.Policies) { policyKey := endpointGroup.TenantName + ":" + policyName // find the policy policy := contivModel.FindPolicy(policyKey) if policy == nil { log.Errorf("Could not find policy %s", policyName) return core.Errorf("Policy not found") } // detach policy to epg err := master.PolicyDetach(endpointGroup, policy) if err != nil && err != master.EpgPolicyExists { log.Errorf("Error detaching policy %s from epg %s", policyName, endpointGroup.Key) return err } // Remove links modeldb.RemoveLinkSet(&policy.LinkSets.EndpointGroups, endpointGroup) modeldb.RemoveLinkSet(&endpointGroup.LinkSets.Policies, policy) err = policy.Write() if err != nil { return err } } } // Update the policy list endpointGroup.Policies = params.Policies return nil }
// EndpointGroupUpdate updates endpoint group func (ac *APIController) EndpointGroupUpdate(endpointGroup, params *contivModel.EndpointGroup) error { log.Infof("Received EndpointGroupUpdate: %+v, params: %+v", endpointGroup, params) // if the network association was changed, reject the update. if endpointGroup.NetworkName != params.NetworkName { return core.Errorf("Cannot change network association after epg is created.") } // Only update policy attachments // Look for policy adds for _, policyName := range params.Policies { if !stringInSlice(policyName, endpointGroup.Policies) { policyKey := endpointGroup.TenantName + ":" + policyName // find the policy policy := contivModel.FindPolicy(policyKey) if policy == nil { log.Errorf("Could not find policy %s", policyName) return core.Errorf("Policy not found") } // attach policy to epg err := master.PolicyAttach(endpointGroup, policy) if err != nil && err != master.EpgPolicyExists { log.Errorf("Error attaching policy %s to epg %s", policyName, endpointGroup.Key) return err } // Setup links modeldb.AddLinkSet(&policy.LinkSets.EndpointGroups, endpointGroup) modeldb.AddLinkSet(&endpointGroup.LinkSets.Policies, policy) err = policy.Write() if err != nil { return err } } } // now look for policy removals for _, policyName := range endpointGroup.Policies { if !stringInSlice(policyName, params.Policies) { policyKey := endpointGroup.TenantName + ":" + policyName // find the policy policy := contivModel.FindPolicy(policyKey) if policy == nil { log.Errorf("Could not find policy %s", policyName) return core.Errorf("Policy not found") } // detach policy to epg err := master.PolicyDetach(endpointGroup, policy) if err != nil && err != master.EpgPolicyExists { log.Errorf("Error detaching policy %s from epg %s", policyName, endpointGroup.Key) return err } // Remove links modeldb.RemoveLinkSet(&policy.LinkSets.EndpointGroups, endpointGroup) modeldb.RemoveLinkSet(&endpointGroup.LinkSets.Policies, policy) err = policy.Write() if err != nil { return err } } } // Update the policy list endpointGroup.Policies = params.Policies // For the external contracts, we can keep the update simple. Remove // all that we have now, and update the epg with the new list. // Step 1: Cleanup existing external contracts. err := cleanupExternalContracts(endpointGroup) if err != nil { return err } // Step 2: Add contracts from the update. // Consumed contracts err = setupExternalContracts(endpointGroup, params.ExtContractsGrps) if err != nil { return err } // Update the epg itself with the new contracts groups. endpointGroup.ExtContractsGrps = params.ExtContractsGrps // if there is an associated app profiles, update that as well profKey := endpointGroup.Links.AppProfile.ObjKey profObj := contivModel.FindAppProfile(profKey) if profObj == nil { log.Warnf("EndpointGroupUpdate prof %s not found", profKey) } else { log.Infof("EndpointGroupUpdate sync prof %s", profKey) DeleteAppNw(profObj) CreateAppNw(profObj) } return nil }