Phased recovery

- Implemented a set of CLI commands
    - Enable/disable group of ports
    - List recovery phase of each device
    - Force a specific device to enter given phase
- Return CompletableFuture in RRP
- Introduce completeAfter method in Tools
- Introduce submit method in PredictableExecutor which returns a CompletableFuture

Change-Id: I60b0fb7b67e392b33b52d908d2b53f7acbddc565
diff --git a/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/phasedrecovery/api/OsgiPropertyConstants.java b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/phasedrecovery/api/OsgiPropertyConstants.java
new file mode 100644
index 0000000..cdeb8a2
--- /dev/null
+++ b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/phasedrecovery/api/OsgiPropertyConstants.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.segmentrouting.phasedrecovery.api;
+
+/**
+ * Constants for default values of configurable properties.
+ */
+public final class OsgiPropertyConstants {
+    private OsgiPropertyConstants() {}
+
+    public static final String PROP_PHASED_RECOVERY = "phasedRecovery";
+    public static final boolean PHASED_RECOVERY_DEFAULT = false;
+}
diff --git a/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/phasedrecovery/api/Phase.java b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/phasedrecovery/api/Phase.java
new file mode 100644
index 0000000..7f18685
--- /dev/null
+++ b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/phasedrecovery/api/Phase.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.segmentrouting.phasedrecovery.api;
+
+/**
+ * Phases of recovering devices.
+ */
+public enum Phase {
+
+    /**
+     * Device is waiting for ports to be reported.
+     */
+    PENDING,
+
+    /**
+     * Pair port enabled. This is the initial state for paired devices.
+     */
+    PAIR,
+
+    /**
+     * Infrastructure ports enabled.
+     */
+    INFRA,
+
+    /**
+     * Edge ports enabled. This is the initial state for non-paired and spine devices.
+     */
+    EDGE
+}
diff --git a/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/phasedrecovery/api/PhasedRecoveryService.java b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/phasedrecovery/api/PhasedRecoveryService.java
new file mode 100644
index 0000000..705ecb7
--- /dev/null
+++ b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/phasedrecovery/api/PhasedRecoveryService.java
@@ -0,0 +1,139 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.segmentrouting.phasedrecovery.api;
+
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Service that provides functionality related to phased recovery.
+ */
+public interface PhasedRecoveryService {
+
+    // TODO Make timeout values configurable via Component Config Service
+    /**
+     * Timeout for PAIR phase in seconds.
+     */
+    int PAIR_TIMEOUT = 30;
+
+    /**
+     * Timeout for INFRA phase in seconds.
+     */
+    int INFRA_TIMEOUT = 30;
+
+    /**
+     * Timeout for EDGE phase in seconds.
+     */
+    int EDGE_TIMEOUT = 30;
+
+    //
+    // Phased recovery APIs.
+    //
+
+
+    /**
+     * Returns true if phased recovery is enabled.
+     *
+     * @return true if phased recovery is enabled.
+     */
+    boolean isEnabled();
+
+    /**
+     * Initializes a device. Only the master of the device is allowed to do this.
+     *
+     * @param deviceId device ID
+     * @return true if the device is initialized successfully and the caller should proceed,
+     *         false if the device initialization has failed and the caller should abort.
+     */
+    boolean init(DeviceId deviceId);
+
+    /**
+     * Resets a device. Only the master of the device is allowed to do this.
+     *
+     * @param deviceId device ID
+     * @return true if the device is reset successfully.
+     *         false if the device has not been previously initialized.
+     */
+    boolean reset(DeviceId deviceId);
+
+    /**
+     * Gets recovery phase of every devices.
+     *
+     * @return a map between device ID and recovery phase
+     */
+    Map<DeviceId, Phase> getPhases();
+
+    /**
+     * Gets recovery phase of given device.
+     *
+     * @param deviceId device ID
+     * @return current phase or null if the device wasn't seen before
+     */
+    Phase getPhase(DeviceId deviceId);
+
+    /**
+     * Sets given device with given recovery phase. Only the master of the device is allowed to do this.
+     *
+     * @param deviceId device ID
+     * @param newPhase recovery phase
+     * @return new phase if transition succeeded, otherwise return current phase.
+     */
+    Phase setPhase(DeviceId deviceId, Phase newPhase);
+
+    //
+    // Port manipulation APIs.
+    //
+
+    /**
+     * Enables every ports on the given device.
+     *
+     * @param deviceId device id
+     * @param enabled true to enable, false to disable
+     * @return ports that have been enabled
+     */
+    Set<PortNumber> changeAllPorts(DeviceId deviceId, boolean enabled);
+
+    /**
+     * Enables pair port on the given device.
+     *
+     * @param deviceId device id
+     * @param enabled true to enable, false to disable
+     * @return ports that have been enabled
+     */
+    Set<PortNumber> changePairPort(DeviceId deviceId, boolean enabled);
+
+    /**
+     * Enables infrastructure ports on the given device.
+     *
+     * @param deviceId device id
+     * @param enabled true to enable, false to disable
+     * @return ports that have been enabled
+     */
+    Set<PortNumber> changeInfraPorts(DeviceId deviceId, boolean enabled);
+
+    /**
+     * Enables edge ports on the given device.
+     *
+     * @param deviceId device id
+     * @param enabled true to enable, false to disable
+     * @return ports that have been enabled
+     */
+    Set<PortNumber> changeEdgePorts(DeviceId deviceId, boolean enabled);
+}
diff --git a/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/phasedrecovery/api/package-info.java b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/phasedrecovery/api/package-info.java
new file mode 100644
index 0000000..cc556be
--- /dev/null
+++ b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/phasedrecovery/api/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Phased recovery API.
+ */
+package org.onosproject.segmentrouting.phasedrecovery.api;
\ No newline at end of file