import org.opendaylight.groupbasedpolicy.sxp.ep.provider.api.EPPolicyTemplateProvider;
import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.SgtGeneratorImpl;
import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.SimpleCachedDao;
-import org.opendaylight.groupbasedpolicy.util.IidFactory;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Description;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Name;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroupBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ep.provider.model.rev160302.SxpEpMapper;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ep.provider.model.rev160302.TemplateGenerated;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ep.provider.model.rev160302.sxp.ep.mapper.EndpointPolicyTemplateBySgt;
return templateOpt.transform(template -> Futures.immediateFuture(templateOpt))
// failed to read template -> invoke fallback if available
.or(() -> java.util.Optional.ofNullable(templateProvider)
- .flatMap(provider -> templateProvider.provideTemplate(key))
- .map(template -> storeTemplateAndEpg(template))
+ .map(provider -> templateProvider.provideTemplate(key))
+ .map(template -> rewrapOptionalToGuavaOptional(template))
.orElse(Futures.immediateFuture(Optional.absent()))
);
}
});
}
- private ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> storeTemplateAndEpg(final EndpointPolicyTemplateBySgt template) {
- // store EPG (presume that it does not exist)
- final Sgt sgtValue = template.getSgt();
- LOG.trace("storing EPGs for generated epPolicyTemplate: {} [{}]",
- sgtValue.getValue(), template.getEndpointGroups().size());
- final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
-
- boolean createParent = true;
- for (EndpointGroupId epgId : template.getEndpointGroups()) {
- final InstanceIdentifier<EndpointGroup> epgPath = IidFactory.endpointGroupIid(template.getTenant(), epgId);
- final EndpointGroup epg = new EndpointGroupBuilder()
- .setId(epgId)
- .setDescription(new Description("imported from ISE for sgt=" + sgtValue.getValue()))
- .setName(new Name(String.format("%s_ISE_SGT_%d", epgId.getValue(), sgtValue.getValue())))
- .build();
- wTx.put(LogicalDatastoreType.CONFIGURATION, epgPath, epg, createParent);
- createParent = false;
- }
-
- // store ep-policy-template
- LOG.trace("storing generated epPolicyTemplate: {}", sgtValue.getValue());
- final InstanceIdentifier<EndpointPolicyTemplateBySgt> epPolicyTemplatePath = InstanceIdentifier
- .create(SxpEpMapper.class)
- .child(EndpointPolicyTemplateBySgt.class, new EndpointPolicyTemplateBySgtKey(sgtValue));
- wTx.put(LogicalDatastoreType.CONFIGURATION, epPolicyTemplatePath, template, true);
-
- return Futures.transform(wTx.submit(), createStoreOutcomeHandlerToOptional(template));
+ private <T> ListenableFuture<Optional<T>> rewrapOptionalToGuavaOptional(final ListenableFuture<java.util.Optional<T>> templateFu) {
+ return Futures.transform(templateFu, new Function<java.util.Optional<T>, Optional<T>>() {
+ @Nullable
+ @Override
+ public Optional<T> apply(@Nullable final java.util.Optional<T> input) {
+ return java.util.Optional.ofNullable(input)
+ .map(origNonnullInput -> Optional.fromNullable(origNonnullInput.orElse(null)))
+ .orElse(Optional.absent());
+ }
+ }
+ );
}
- private Function<Void, Optional<EndpointPolicyTemplateBySgt>> createStoreOutcomeHandlerToOptional(final EndpointPolicyTemplateBySgt template) {
- return new Function<Void, Optional<EndpointPolicyTemplateBySgt>>() {
- @Nullable
- @Override
- public Optional<EndpointPolicyTemplateBySgt> apply(@Nullable final Void aVoid) {
- return Optional.of(template);
- }
- };
- }
private Function<Void, Collection<EndpointPolicyTemplateBySgt>> createStoreOutcomeHandlerToCollection(final EndpointPolicyTemplateBySgt template) {
return new Function<Void, Collection<EndpointPolicyTemplateBySgt>>() {