Commit a4642130 authored by Christos Tranoris's avatar Christos Tranoris
Browse files

fix for #38

parent c9334b49
Loading
Loading
Loading
Loading
Loading
+32
Changes for src/main/java/org/etsi/osl/tmf/ri639/model/ResourceStatusType.java: 32 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -26,6 +26,38 @@ public enum ResourceStatusType {
    return String.valueOf(value);
  }

  /**
   * Whether this status records that the resource has been <b>allocated to someone</b>, as opposed to
   * describing how the resource is doing.
   *
   * <p>{@code resourceStatus} carries two unrelated kinds of statement, and confusing them is how a
   * reservation gets destroyed:
   *
   * <ul>
   *   <li><b>Allocation</b> — {@code RESERVED} and {@code STANDBY}. Someone has taken this resource
   *       out of the free pool: an orchestrator reserved it for an order, or it is being held as a
   *       spare. Only whoever made that claim may retract it.</li>
   *   <li><b>Condition</b> — {@code AVAILABLE}, {@code ALARM}, {@code SUSPENDED}, {@code UNKNOWN}.
   *       An observation of the resource itself, which the party that manages it may restate at any
   *       time.</li>
   * </ul>
   *
   * <p>A deployment controller only ever observes condition. It must therefore declare its verdict
   * through {@link ResourceUpdate#declareResourceStatus(ResourceStatusType)}, which refuses to
   * overwrite an allocation the controller did not make — see the note there.
   *
   * <p>This says nothing about health. The service-state verdict is derived from the ITU-T X.731
   * pair {@code operationalState}/{@code administrativeState} by {@link Resource#deriveHealth()};
   * {@code resourceStatus} takes no part in it. It remains load-bearing for pool allocation,
   * automatic termination ({@code ResourceRepository.findActiveToTerminate}), partner sync and
   * metrics — which is exactly why it must not be clobbered.
   *
   * @return true for {@code RESERVED} and {@code STANDBY}
   */
  public boolean isAllocation() {
    return this == RESERVED || this == STANDBY;
  }

  @JsonCreator
  public static ResourceStatusType fromValue(String text) {
    for (ResourceStatusType b : ResourceStatusType.values()) {
+39
Changes for src/main/java/org/etsi/osl/tmf/ri639/model/ResourceUpdate.java: 39 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -548,6 +548,45 @@ public class ResourceUpdate {
		this.resourceStatus = resourceStatus;
	}

	/**
	 * Declares the {@code resourceStatus} this party has observed, <b>without overwriting an
	 * allocation someone else made</b>.
	 *
	 * <p>A deployment controller is asked to make a resource work and then to report what it found.
	 * It has no idea whether an orchestrator has meanwhile reserved that resource for an order: the
	 * request it is handed is built from the live catalog entry, so a {@code RESERVED} it never set
	 * arrives in this very object. A plain
	 * {@link #setResourceStatus(ResourceStatusType) setResourceStatus(AVAILABLE)} at the end of a
	 * successful deploy then writes the reservation away, and the resource silently rejoins the free
	 * pool while it is in use — visible as it being handed to a second order, and as
	 * {@code ResourceRepository.findActiveToTerminate} sweeping it up at its end-of-operating date.
	 *
	 * <p>So: the declaration is applied when the current value is unset or is a condition
	 * ({@code AVAILABLE}, {@code ALARM}, {@code SUSPENDED}, {@code UNKNOWN}) — a condition is an
	 * observation, and this party has just made a newer one, so a successful deploy legitimately
	 * supersedes an earlier {@code ALARM}. It is skipped when the current value is an allocation
	 * ({@code RESERVED}, {@code STANDBY} — see {@link ResourceStatusType#isAllocation()}), because
	 * ending an allocation is the business of whoever made it.
	 *
	 * <p>Use {@code setResourceStatus} directly, not this method, when you <i>are</i> that party:
	 * an orchestrator reserving or releasing, or a teardown path writing {@code UNKNOWN} — a
	 * teardown ends the allocation along with everything else.
	 *
	 * <p>This is orthogonal to health. Keep declaring {@code operationalState} /
	 * {@code administrativeState} unconditionally: those attributes are the reporting party's own
	 * observation of the resource, nobody else has a claim on them, and they — not this field — are
	 * what {@link Resource#deriveHealth()} turns into a service state.
	 *
	 * @param declared the status this party observed; ignored when null
	 * @return this, for chaining
	 */
	public ResourceUpdate declareResourceStatus(ResourceStatusType declared) {
		if (declared != null && (this.resourceStatus == null || !this.resourceStatus.isAllocation())) {
			this.resourceStatus = declared;
		}
		return this;
	}

	public ResourceUpdate usageState(ResourceUsageStateType usageState) {
		this.usageState = usageState;
		return this;
+150
Changes for src/test/java/org/etsi/osl/tmf/ri639/model/ResourceStatusDeclarationTest.java: 150 added lines, 0 removed lines.
Original line number Diff line number Diff line
package org.etsi.osl.tmf.ri639.model;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

/**
 * Pins the split between an <i>allocation</i> a controller must not touch and a <i>condition</i> it is
 * entitled to restate — see {@link ResourceUpdate#declareResourceStatus(ResourceStatusType)}.
 */
class ResourceStatusDeclarationTest {

    @Test
    @DisplayName("RESERVED and STANDBY are allocations; nothing else is")
    void allocationIsExactlyReservedAndStandby() {
        assertTrue(ResourceStatusType.RESERVED.isAllocation());
        assertTrue(ResourceStatusType.STANDBY.isAllocation());
        assertFalse(ResourceStatusType.AVAILABLE.isAllocation());
        assertFalse(ResourceStatusType.ALARM.isAllocation());
        assertFalse(ResourceStatusType.SUSPENDED.isAllocation());
        assertFalse(ResourceStatusType.UNKNOWN.isAllocation());
    }

    @Test
    @DisplayName("The regression: a deployed controller does not release someone else's reservation")
    void declaringAvailableLeavesAReservationAlone() {
        ResourceUpdate ru = new ResourceUpdate();
        ru.setResourceStatus(ResourceStatusType.RESERVED);

        ru.declareResourceStatus(ResourceStatusType.AVAILABLE);

        assertEquals(ResourceStatusType.RESERVED, ru.getResourceStatus());
    }

    @Test
    @DisplayName("A spare held in STANDBY is protected the same way")
    void declaringAvailableLeavesStandbyAlone() {
        ResourceUpdate ru = new ResourceUpdate();
        ru.setResourceStatus(ResourceStatusType.STANDBY);

        ru.declareResourceStatus(ResourceStatusType.AVAILABLE);

        assertEquals(ResourceStatusType.STANDBY, ru.getResourceStatus());
    }

    @Test
    @DisplayName("Nothing claimed yet: the declaration is applied")
    void declaringOverAnUnsetStatusApplies() {
        ResourceUpdate ru = new ResourceUpdate();
        assertNull(ru.getResourceStatus());

        ru.declareResourceStatus(ResourceStatusType.AVAILABLE);

        assertEquals(ResourceStatusType.AVAILABLE, ru.getResourceStatus());
    }

    @ParameterizedTest
    @EnumSource(value = ResourceStatusType.class,
            names = {"AVAILABLE", "ALARM", "SUSPENDED", "UNKNOWN"})
    @DisplayName("A condition is an observation, so a newer observation supersedes it")
    void declaringOverAConditionApplies(ResourceStatusType current) {
        ResourceUpdate ru = new ResourceUpdate();
        ru.setResourceStatus(current);

        ru.declareResourceStatus(ResourceStatusType.AVAILABLE);

        assertEquals(ResourceStatusType.AVAILABLE, ru.getResourceStatus());
    }

    @Test
    @DisplayName("A failure verdict is a declaration too, and is equally held off a reservation")
    void declaringAlarmFollowsTheSameRule() {
        ResourceUpdate onReserved = new ResourceUpdate();
        onReserved.setResourceStatus(ResourceStatusType.RESERVED);
        onReserved.declareResourceStatus(ResourceStatusType.ALARM);
        assertEquals(ResourceStatusType.RESERVED, onReserved.getResourceStatus());

        ResourceUpdate onAvailable = new ResourceUpdate();
        onAvailable.setResourceStatus(ResourceStatusType.AVAILABLE);
        onAvailable.declareResourceStatus(ResourceStatusType.ALARM);
        assertEquals(ResourceStatusType.ALARM, onAvailable.getResourceStatus());
    }

    @Test
    @DisplayName("A null declaration changes nothing")
    void declaringNullIsANoOp() {
        ResourceUpdate ru = new ResourceUpdate();
        ru.setResourceStatus(ResourceStatusType.AVAILABLE);

        ru.declareResourceStatus(null);

        assertEquals(ResourceStatusType.AVAILABLE, ru.getResourceStatus());
    }

    @Test
    @DisplayName("Teardown still forces: setResourceStatus is how the allocation is ended")
    void setResourceStatusStillOverwritesAnAllocation() {
        ResourceUpdate ru = new ResourceUpdate();
        ru.setResourceStatus(ResourceStatusType.RESERVED);

        ru.setResourceStatus(ResourceStatusType.UNKNOWN);

        assertEquals(ResourceStatusType.UNKNOWN, ru.getResourceStatus());
    }

    @Test
    @DisplayName("Returns this, so it chains with the health attributes it sits beside")
    void declareReturnsThisForChaining() {
        ResourceUpdate ru = new ResourceUpdate();

        assertSame(ru, ru.declareResourceStatus(ResourceStatusType.AVAILABLE));
    }

    @Test
    @DisplayName("ResourceCreate inherits the rule")
    void resourceCreateInheritsTheRule() {
        ResourceCreate rc = new ResourceCreate();
        rc.setResourceStatus(ResourceStatusType.RESERVED);

        rc.declareResourceStatus(ResourceStatusType.AVAILABLE);

        assertEquals(ResourceStatusType.RESERVED, rc.getResourceStatus());
    }

    @Test
    @DisplayName("Health is orthogonal: a preserved reservation does not hold back the X.731 verdict")
    void healthIsUnaffectedByThePreservedStatus() {
        ResourceUpdate ru = new ResourceUpdate();
        ru.setResourceStatus(ResourceStatusType.RESERVED);

        ru.declareResourceStatus(ResourceStatusType.AVAILABLE);
        ru.setOperationalState(ResourceOperationalStateType.ENABLE);
        ru.setAdministrativeState(ResourceAdministrativeStateType.UNLOCKED);

        Resource live = new Resource();
        live.setResourceStatus(ru.getResourceStatus());
        live.setOperationalState(ru.getOperationalState());
        live.setAdministrativeState(ru.getAdministrativeState());

        assertEquals(ResourceStatusType.RESERVED, live.getResourceStatus());
        assertEquals(ResourceHealth.UP, live.deriveHealth());
    }
}