@Nullable @Override public Comparable apply(@Nullable final EndpointGroupId input) { if (input == null) { return EMPTY_COMPARABLE; } return MoreObjects.firstNonNull(input.getValue(), EMPTY_COMPARABLE); } });
@Override public void onDeleted(SecurityGroup deletedSecGroup, Neutron oldNeutron, Neutron newNeutron) { LOG.trace("deleted securityGroup - {}", deletedSecGroup); ReadWriteTransaction rwTx = dataProvider.newReadWriteTransaction(); TenantId tenantId = new TenantId(deletedSecGroup.getTenantId().getValue()); EndpointGroupId epgId = new EndpointGroupId(deletedSecGroup.getUuid().getValue()); Optional<EndpointGroup> potentialEpg = DataStoreHelper.removeIfExists(LogicalDatastoreType.CONFIGURATION, IidFactory.endpointGroupIid(tenantId, epgId), rwTx); if (!potentialEpg.isPresent()) { LOG.warn("Illegal state - Endpoint group {} does not exist.", epgId.getValue()); rwTx.cancel(); return; } DataStoreHelper.submitToDs(rwTx); }
/** * An endpoint is internal if none of its endpoint-groups is external implicit group. * * @param ep an endpoint * @param eigs external implicit groups * @return {@code true} if the given endpoint does not have EPG representing external implicit * group; * {@code false} otherwise * @throws NullPointerException if the given endpoint is {@code null} * @throws IllegalArgumentException if the given endpoint does not contain any endpoint-group */ public static boolean isInternal(Endpoint ep, @Nullable Collection<ExternalImplicitGroup> eigs) { Preconditions.checkNotNull(ep); if (eigs == null || eigs.isEmpty()) { return true; } Set<EndpointGroupId> epgs = getEpgs(ep); Preconditions.checkArgument(!epgs.isEmpty()); for (EndpointGroupId epg : epgs) { for (ExternalImplicitGroup eig : eigs) { if (epg.equals(eig.getId())) { return false; } } } return true; }
private static List<EndpointGroupId> resolveEpgIdsFromSecGroups(@Nullable List<Uuid> securityGroups) { List<EndpointGroupId> epgIds = new ArrayList<>(); if ((securityGroups == null || securityGroups.isEmpty())) { return epgIds; } for (Uuid secGrp : securityGroups) { epgIds.add(new EndpointGroupId(secGrp.getValue())); } return epgIds; }
private List<EndpointGroupId> concatEndpointGroups(List<Uuid> securityGroups, @Nullable List<EndpointGroupId> endpointGroupsToAdd) { List<EndpointGroupId> epgs = new ArrayList<>(); if (securityGroups != null) { for (Uuid sgId : securityGroups) { epgs.add(new EndpointGroupId(sgId.getValue())); } } if (endpointGroupsToAdd != null) { epgs.addAll(endpointGroupsToAdd); } return epgs; }
public static int getContextOrdinal(Endpoint ep, NetworkDomainId networkContainment) { Set<String> epgs = new TreeSet<>(); // Get EPGs and add to ordered Set if (ep.getEndpointGroup() != null) { epgs.add(ep.getEndpointGroup().getValue()); } if (ep.getEndpointGroups() != null) { for (EndpointGroupId epgId : ep.getEndpointGroups()) { epgs.add(epgId.getValue()); } } StringBuilder key = new StringBuilder(ep.getTenant().getValue()); for (String epg : epgs) { key.append('|'); key.append(epg); } key.append("|").append(networkContainment); return getContextOrdinalFromString(key.toString()); }
public boolean addNeutronSecurityGroup(SecurityGroup secGroup, ReadWriteTransaction rwTx) { TenantId tId = new TenantId(secGroup.getTenantId().getValue()); EndpointGroupId epgId = new EndpointGroupId(secGroup.getUuid().getValue()); if (epgId.getValue().equals(MappingUtils.EIG_UUID.getValue())) { ExternalImplicitGroup eig = new ExternalImplicitGroupBuilder().setId(epgId).build(); rwTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.externalImplicitGroupIid(tId, epgId), eig, true); } EndpointGroupBuilder epgBuilder = new EndpointGroupBuilder().setId(epgId); if (!Strings.isNullOrEmpty(secGroup.getName())) { try { epgBuilder.setName(new Name(secGroup.getName())); } catch (Exception e) { LOG.info("Name '{}' of Neutron Security-group '{}' is ignored.", secGroup.getName(), secGroup.getUuid().getValue()); LOG.debug("Name exception", e); } } epgBuilder.setIntraGroupPolicy(IntraGroupPolicy.RequireContract); rwTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.endpointGroupIid(tId, epgId), epgBuilder.build(), true); return true; }
@VisibleForTesting void undesignContractsBetweenProviderAndConsumer(TenantId tenantId, Uuid provSecGroupId, Uuid consSecGroupId, SecurityRule removedSecRule, Neutron neutron, ReadWriteTransaction rwTx) { Set<SecurityRule> provSecRules = getProvidedSecRulesBetween(provSecGroupId, consSecGroupId, neutron); Set<SecurityRule> consSecRules = getProvidedSecRulesBetween(consSecGroupId, provSecGroupId, neutron); EndpointGroupId consEpgId = new EndpointGroupId(consSecGroupId.getValue()); for (SecurityRule provSecRule : provSecRules) { if (isProvidersSecRuleSuitableForConsumersSecRulesAndGoodToRemove(provSecRule, consSecRules, removedSecRule)) { SelectorName consumerSelector = getSelectorNameWithProvider(provSecRule, neutron); deleteConsumerNamedSelector(consumerSelector, new EpgKeyDto(consEpgId, tenantId), rwTx); } // TODO add case when port ranges overlap } }
private boolean isSameEpg(RegisterEndpointInput epInput, AddressEndpoint input) { if (epInput == null || epInput.getAddressEndpointReg() == null || epInput.getAddressEndpointReg().isEmpty()) { return true; } final AddressEndpointReg epInputAddressEndpoint = epInput.getAddressEndpointReg().get(0); if (epInputAddressEndpoint.getEndpointGroup() == null || epInputAddressEndpoint.getEndpointGroup().isEmpty()) { return true; } if (input == null || input.getEndpointGroup() == null || input.getEndpointGroup().isEmpty()) { return true; } final EndpointGroupId addressEndpointGroupId = epInputAddressEndpoint.getEndpointGroup().get(0); final EndpointGroupId existingEndpointGroupId = input.getEndpointGroup().get(0); return addressEndpointGroupId.equals(existingEndpointGroupId); }
public static int getContextOrdinal(Endpoint ep) { Set<String> epgs = new TreeSet<>(); // Get EPGs and add to ordered Set if (ep.getEndpointGroup() != null) { epgs.add(ep.getEndpointGroup().getValue()); } if (ep.getEndpointGroups() != null) { for (EndpointGroupId epgId : ep.getEndpointGroups()) { epgs.add(epgId.getValue()); } } StringBuilder key = new StringBuilder(ep.getTenant().getValue()); for (String epg : epgs) { key.append('|'); key.append(epg); } return getContextOrdinalFromString(key.toString()); }
@VisibleForTesting void designContractsBetweenProviderAndConsumer(TenantId tenantId, Uuid provSecGroupId, Uuid consSecGroupId, Neutron neutron, ReadWriteTransaction rwTx) { Set<SecurityRule> provSecRules = getProvidedSecRulesBetween(provSecGroupId, consSecGroupId, neutron); Set<SecurityRule> consSecRules = getProvidedSecRulesBetween(consSecGroupId, provSecGroupId, neutron); EndpointGroupId consEpgId = new EndpointGroupId(consSecGroupId.getValue()); for (SecurityRule provSecRule : provSecRules) { if (isProviderSecRuleSuitableForConsumerSecRules(provSecRule, consSecRules)) { SelectorName consumerSelector = getSelectorNameWithProvider(provSecRule, neutron); ContractId contractId = SecRuleEntityDecoder.getContractId(provSecRule); writeConsumerNamedSelectorToEpg(consumerSelector, contractId, new EpgKeyDto(consEpgId, tenantId), rwTx); } // TODO add case when port ranges overlap } }
private void mapAllTenantResolvedPolicies(TenantId gbpTenantId, EndpointGroupId epgId) { Optional<ResolvedPolicies> resolvedPoliciesOptional = DataStoreHelper.readFromDs( LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.builder(ResolvedPolicies.class).build(), dataProvider.newReadOnlyTransaction()); if (!resolvedPoliciesOptional.isPresent() || resolvedPoliciesOptional.get().getResolvedPolicy() == null) { return; } List<ResolvedPolicy> resolvedPolicies = resolvedPoliciesOptional.get().getResolvedPolicy(); for (ResolvedPolicy policy : resolvedPolicies) { if (policy.getConsumerTenantId().equals(gbpTenantId)) { if (epgId == null || epgId.equals(policy.getConsumerEpgId()) || epgId.equals(policy.getProviderEpgId())) { // if any epg or a specific epg policy updateLogicalNetwork(policy); } } } }
/*** * Gets the list of endpoints that matches an intent subject id * @param subjectId * @return */ private List<Endpoint> readEPNodes(String subjectId) { List<Endpoint> matchingGroup = new ArrayList<>(); InstanceIdentifier<Endpoints> nodePath = GBPRendererHelper.createEndpointsIdentifier(); Endpoints node = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, nodePath); if (node != null) { for (Endpoint endpoint : node.getEndpoint()) { if (endpoint.getEndpointGroup().getValue().equalsIgnoreCase(subjectId)) { matchingGroup.add(endpoint); } } } else { LOG.error("Endpoints not found for the path {}", nodePath); } return matchingGroup; } }
.setId(new EndpointGroupId(this.getEndpointIdentifier(subjects1))) .setNetworkDomain(consumerNetworkDomainId) .setConsumerNamedSelector(ImmutableList.of(new ConsumerNamedSelectorBuilder() .setId(new EndpointGroupId(this.getEndpointIdentifier(subjects2))) .setNetworkDomain(providerNetworkDomainId) .setProviderNamedSelector(ImmutableList.of(new ProviderNamedSelectorBuilder()
private boolean isEqualService(ResolvedPolicy newPolicy, ResolvedPolicy oldPolicy) { return oldPolicy != null && newPolicy.getConsumerEpgId().equals(oldPolicy.getConsumerEpgId()) && newPolicy.getProviderEpgId().equals(oldPolicy.getProviderEpgId()) && newPolicy.getConsumerTenantId().equals(oldPolicy.getConsumerTenantId()) && newPolicy.getProviderTenantId().equals(oldPolicy.getProviderTenantId()); }
@VisibleForTesting void registerFollowedEndpointgroup(TenantId gbpTenantId, EndpointGroupId epgId) { if (epgId == null) { return; } FollowedEndpointGroupBuilder fEpgBuilder = new FollowedEndpointGroupBuilder(); fEpgBuilder.setId(epgId); WriteTransaction wTx = dataProvider.newWriteOnlyTransaction(); wTx.put(LogicalDatastoreType.OPERATIONAL, IidFactory.followedEndpointgroupIid(rendererName, gbpTenantId, epgId), fEpgBuilder.build()); if (DataStoreHelper.submitToDs(wTx)) { LOG.trace("EPG {} in Tenant {} is followed by renderer {}", epgId.getValue(), gbpTenantId.getValue(), rendererName.getValue()); } else { LOG.info("Couldn't register EPG {} in Tenant {} that is followed by renderer {}", epgId.getValue(), gbpTenantId.getValue(), rendererName.getValue()); } }
@Override public CheckedFuture<Void, TransactionCommitFailedException> processSgtInfo(final TenantId tenantId, final List<SgtInfo> sgtInfos) { final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction(); // create and write endpointgroups boolean createParent = true; for (SgtInfo sgtInfo : sgtInfos) { final Integer sgtValue = sgtInfo.getSgt().getValue(); final String sgtName = sgtInfo.getName(); LOG.trace("processing sgtInfo: {} - {}", sgtValue, sgtName); final EndpointGroupId epgId = new EndpointGroupId(sgtName); final InstanceIdentifier<EndpointGroup> epgPath = IidFactory.endpointGroupIid(tenantId, epgId); final EndpointGroup epg = new EndpointGroupBuilder() .setId(epgId) .setDescription(new Description("imported from ISE for sgt=" + sgtValue)) .setName(new Name(sgtName.replaceAll(" ", "_") + "--" + sgtValue)) .build(); wTx.put(LogicalDatastoreType.CONFIGURATION, epgPath, epg, createParent); createParent = false; } return wTx.submit(); } }
private LogicalSwitchBuilder initLogicalSwitchBuilder(EndpointGroup epg, Uuid tenantId) { LogicalSwitchBuilder builder = new LogicalSwitchBuilder(); builder.setAdminStateUp(true); builder.setName(new Text(epg.getId().getValue())); if (epg.getDescription() != null) builder.setDescription(new Text("gbp-epg: " + epg.getDescription().getValue())); else builder.setDescription(new Text("gbp-epg")); builder.setTenantId(tenantId); builder.setUuid(new Uuid(UUID.randomUUID().toString())); return builder; }
@Override public CheckedFuture<Void, TransactionCommitFailedException> processSgtInfo(final TenantId tenantId, final List<SgtInfo> sgtInfos) { final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction(); // write endpoint-policy-templates boolean createParent = true; for (SgtInfo sgtInfo : sgtInfos) { final Sgt sgt = sgtInfo.getSgt(); final String sgtName = sgtInfo.getName(); LOG.trace("processing sgtInfo: {} - {}", sgt.getValue(), sgtName); final EndpointGroupId epgId = new EndpointGroupId(sgtInfo.getName()); final InstanceIdentifier<EndpointPolicyTemplateBySgt> epPolicyTemplatePath = InstanceIdentifier .create(SxpMapper.class) .child(EndpointPolicyTemplateBySgt.class, new EndpointPolicyTemplateBySgtKey(sgt)); final EndpointPolicyTemplateBySgt epPolicyTemplate = new EndpointPolicyTemplateBySgtBuilder() .setSgt(sgt) .setEndpointGroups(Collections.singletonList(epgId)) .setTenant(tenantId) .build(); wTx.put(LogicalDatastoreType.CONFIGURATION, epPolicyTemplatePath, epPolicyTemplate, createParent); createParent = false; } return wTx.submit(); } }
private LogicalRouterBuilder initLogicalRouterBuilder(EndpointGroup epg, Uuid tenantId, boolean isPublic) { LogicalRouterBuilder builder = new LogicalRouterBuilder(); builder.setAdminStateUp(true); builder.setName(new Text(epg.getId().getValue())); if (epg.getDescription() != null) builder.setDescription(new Text("gbp-epg: " + epg.getDescription().getValue())); else builder.setDescription(new Text("gbp-epg")); builder.setPublic(isPublic); builder.setTenantId(tenantId); builder.setUuid(new Uuid(UUID.randomUUID().toString())); return builder; }