@Override
public Set<ComputedValue> execute(final FunctionExecutionContext executionContext, final FunctionInputs inputs, final ComputationTarget target, final Set<ValueRequirement> desiredValues) {
final Clock snapshotClock = executionContext.getValuationClock();
final ZonedDateTime now = ZonedDateTime.now(snapshotClock);
final SecuritySource securitySource = OpenGammaExecutionContext.getSecuritySource(executionContext);
final SwaptionSecurity security = (SwaptionSecurity) target.getSecurity();
final ValueRequirement desiredValue = desiredValues.iterator().next();
final Currency currency = FinancialSecurityUtils.getCurrency(security);
final String curveName = desiredValue.getConstraint(ValuePropertyNames.CURVE);
final String fullCurveName = curveName + "_" + currency.getCode();
final String surfaceName = desiredValue.getConstraint(ValuePropertyNames.SURFACE);
final Object volatilitySurfaceObject = inputs.getValue(getVolatilityRequirement(surfaceName, currency));
if (volatilitySurfaceObject == null) {
throw new OpenGammaRuntimeException("Could not get volatility surface");
}
final VolatilitySurface volatilitySurface = (VolatilitySurface) volatilitySurfaceObject;
if (!(volatilitySurface.getSurface() instanceof InterpolatedDoublesSurface)) {
throw new OpenGammaRuntimeException("Expecting an InterpolatedDoublesSurface; got " + volatilitySurface.getSurface().getClass());
}
final String curveCalculationConfigName = desiredValue.getConstraint(ValuePropertyNames.CURVE_CALCULATION_CONFIG);
final ConfigSource configSource = OpenGammaExecutionContext.getConfigSource(executionContext);
final ConfigDBCurveCalculationConfigSource curveCalculationConfigSource = new ConfigDBCurveCalculationConfigSource(configSource);
final MultiCurveCalculationConfig curveCalculationConfig = curveCalculationConfigSource.getConfig(curveCalculationConfigName);
if (curveCalculationConfig == null) {
throw new OpenGammaRuntimeException("Could not find curve calculation configuration named " + curveCalculationConfigName);
}
String[] curveNames = curveCalculationConfig.getYieldCurveNames();
if (curveNames.length == 1) {
curveNames = new String[] {curveNames[0], curveNames[0] };
}
final String[] fullCurveNames = new String[curveNames.length];
for (int i = 0; i < curveNames.length; i++) {
fullCurveNames[i] = curveNames[i] + "_" + currency.getCode();
}
final String curveCalculationMethod = curveCalculationConfig.getCalculationMethod();
if (curveCalculationMethod.equals(FXImpliedYieldCurveFunction.FX_IMPLIED)) {
throw new UnsupportedOperationException("Cannot handle FX implied curves");
}
final InstrumentDefinition<?> definition = security.accept(getVisitor());
final InstrumentDerivative swaption = definition.toDerivative(now, fullCurveNames); //TODO
final ValueRequirement curveSpecRequirement = getCurveSpecRequirement(currency, curveName);
final Object curveSpecObject = inputs.getValue(curveSpecRequirement);
if (curveSpecObject == null) {
throw new OpenGammaRuntimeException("Could not get " + curveSpecRequirement);