Test the client with the reference provider Proposal
Run the OpenCapacity client against the reference provider, a local test provider with invented data, and set what it discloses, what it has available, and when it fails.
The reference provider is the Java class org.opencapacity.reference.ReferenceProvider. It serves the whole OpenCapacity API on a local port and answers from an inventory of three invented metros and four invented facilities. The tests of the client run against it.
Prerequisites#
- A JDK, version 21 or later, and the SDK. See Install the SDK.
- Git and Maven.
Build the reference provider#
The reference provider is in the specification repository, under reference-provider/. It isn't in the SDK jar, and it isn't on Maven Central. It depends on the JDK and Jackson only.
The following command clones the specification repository:
git clone https://github.com/Open-Capacity/opencapacity.gitThe following command runs the tests of the reference provider and installs it in your local Maven repository:
mvn -f opencapacity/reference-provider/pom.xml installThe following dependency adds the reference provider to the tests of your project:
<dependency>
<groupId>org.opencapacity</groupId>
<artifactId>opencapacity-reference-provider</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>Start the provider and connect the client#
The following example starts a provider and creates a client for it:
import com.eqixiac.opencapacity.OpenCapacity;
import org.opencapacity.reference.ReferenceProvider;
ReferenceProvider provider = ReferenceProvider.start();
OpenCapacity capacity = OpenCapacity.builder()
.endpoint(provider.endpoint())
.credentials(ReferenceProvider.CLIENT_ID, ReferenceProvider.CLIENT_SECRET)
.build();start() opens a free port on the loopback address. endpoint() returns the URL of that port. The URL is HTTP, which build() accepts for localhost only.
CLIENT_ID and CLIENT_SECRET are the one pair of credentials that the provider accepts. A token that it issues is valid for 3,600 seconds.
Each call to start() returns a new provider with the example inventory and no signals or subscriptions.
Set how much the provider discloses#
A provider chooses how much of an answer it discloses. The reference provider has three profiles, and it starts with BANDED.
| Profile | An answer contains |
|---|---|
GRADE_ONLY | A grade per location and one entry per desired feature. |
BANDED | Also timing, a density range, a cabinet band, and interconnection presence. |
FULL | Also a power band, latency bands, and reasons. |
The following example sets the FULL profile and asks for 200 kVA in the metro LD:
import com.eqixiac.opencapacity.model.CapacityAnswer;
import com.eqixiac.opencapacity.model.CapacityRequirement;
import com.eqixiac.opencapacity.model.Location;
import com.eqixiac.opencapacity.model.LocationResult;
import com.eqixiac.opencapacity.model.Power;
provider.disclosure(ReferenceProvider.Disclosure.FULL);
CapacityRequirement requirement = CapacityRequirement.define()
.addLocation(Location.metro("LD"))
.power(Power.kva(200))
.create();
CapacityAnswer answer = capacity.queries().ask(requirement);
for (LocationResult result : answer.results()) {
System.out.println(result.location().code() + " " + result.grade() + " " + result.reasons());
}For what each grade and each disclosed field means, see Graded disclosure.
Change the inventory and send events#
To test how your code handles a change in an answer, change the power that a facility has available and call reevaluate(). The following example creates a pull subscription, raises the available power of the facility LD-A to 500 kW, and reads the events:
import com.eqixiac.opencapacity.Subscription;
import com.eqixiac.opencapacity.model.Event;
Subscription subscription = capacity.subscriptions().define(requirement).pull().create();
provider.inventory().setAvailableKw("LD-A", 500); // kW available today
provider.reevaluate();
for (Event event : subscription.events(0, 50)) { // after sequence 0, at most 50 events
System.out.println(event.sequence() + " " + event.type());
}reevaluate() evaluates every active subscription again and sends an event where the answer changed. A webhook subscription receives the event at its URL.
Act as the provider on a signal#
A signal changes state when the provider acts on it. The reference provider has one method for each action. The following example creates a signal, takes it into planning, and closes it:
import com.eqixiac.opencapacity.Signal;
Signal signal = capacity.signals().define(requirement).create();
provider.acknowledge(signal.id());
signal.refresh();
System.out.println(signal.state()); // ACKNOWLEDGED
provider.close(signal.id(), "FULFILLED");
signal.refresh();
System.out.println(signal.state()); // CLOSEDacknowledge also attaches the current answer to the signal. The close reasons are FULFILLED, DECLINED, DUPLICATE, and OTHER.
Make a request fail#
| Call | Effect |
|---|---|
provider.queryQuota(n) | The provider answers n queries. It answers each later query with 429, the code QUOTA_EXCEEDED, and Retry-After: 3600. |
provider.revokeTokens() | Every token that the provider issued becomes invalid. The next request gets a 401. The client then requests a new token and repeats the request once. |
provider.areaLocations(false) | The provider answers a query for a point and a radius with 422 and the code UNSUPPORTED_LOCATION_LEVEL. |
The following example sets a quota of one query and prints the status and the code of the second query:
import com.eqixiac.opencapacity.OpenCapacityException;
provider.queryQuota(1);
capacity.queries().ask(requirement);
try {
capacity.queries().ask(requirement);
} catch (OpenCapacityException e) {
System.err.println(e.status() + " " + e.code().orElse("no code"));
}Set the clock#
The provider reads the date and time from a java.time.Clock. The following example fixes the clock, so that dates in an answer don't change between test runs:
provider.clock(Clock.fixed(Instant.parse("2027-01-15T09:00:00Z"), ZoneOffset.UTC));An event stream stays open for 300 ms after the last event. To change that time, call provider.streamHoldMillis(millis).
Stop the provider#
The following example stops the provider and releases the port:
provider.close();ReferenceProvider implements AutoCloseable, so you can also start it in a try-with-resources statement.
Example inventory#
Every name, code, and number in the inventory is invented. None describes a real provider or a real site.
| Metro | Name | Country |
|---|---|---|
FR | Frankfurt | DE |
AM | Amsterdam | NL |
LD | London | GB |
| Facility | Metro | Campus | Available (kW) | Planned (kW) | Planned for | Maximum density (kW per cabinet) | Redundancy | Features |
|---|---|---|---|---|---|---|---|---|
FR-A | FR | FR-CAMPUS-1 | 1,200 | 4,000 | 2027-Q3 | 80 | TWO_N | LIQUID_COOLING, DIRECT_TO_CHIP_COOLING, PRIVATE_CAGE |
FR-B | FR | FR-CAMPUS-1 | 300 | 0 | 15 | N_PLUS_1 | PRIVATE_CAGE, PRIVATE_SUITE | |
AM-A | AM | 600 | 2,000 | 2028-Q1 | 40 | TWO_N | LIQUID_COOLING, REAR_DOOR_HEAT_EXCHANGER, PRIVATE_CAGE | |
LD-A | LD | 150 | 800 | 2027-Q1 | 20 | N_PLUS_1 | PRIVATE_CAGE |
The provider converts kVA to kW with a power factor of 0.95.
Limitations#
- The reference provider keeps nothing on disk. A signal or a subscription ends when the provider stops.
- It isn't for production use.
- It isn't a conformance suite. A client that passes its tests against the reference provider isn't shown to work with any other provider.
Related pages#
- To create a client and send a first query, see Get started with the OpenCapacity client.
- To create and amend signals, see Create, amend, and delete a signal.
- To receive events at a webhook or over a stream, see Subscribe to capacity events.
- For the status of the proposal, see Proposal status and feedback.