Refactor AcInstanceState Update on ACM 68/132968/1
authorFrancescoFioraEst <francesco.fiora@est.tech>
Thu, 19 Jan 2023 10:08:35 +0000 (10:08 +0000)
committerFrancesco Fiora <francesco.fiora@est.tech>
Thu, 19 Jan 2023 10:50:25 +0000 (10:50 +0000)
Create AcInstanceStateResolver that will be used to Handle Deployment,
Undeployment, Locking and Unlocking.

Issue-ID: POLICY-4526
Change-Id: I9a7beb57dba9e9ea334974d63c1063acde7c977d
Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
models/src/main/java/org/onap/policy/clamp/models/acm/concepts/DeployState.java [new file with mode: 0644]
models/src/main/java/org/onap/policy/clamp/models/acm/concepts/LockState.java [new file with mode: 0644]
models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/AcInstanceStateUpdate.java
models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/AutomationCompositionOrderStateResponse.java [deleted file]
models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/DeployOrder.java [new file with mode: 0644]
models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/InstancePropertiesResponse.java [deleted file]
models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/InstantiationOrderStateResponse.java [deleted file]
models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/LockOrder.java [new file with mode: 0644]
models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AcInstanceStateResolver.java [new file with mode: 0644]
models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/AcInstanceStateResolverTest.java [new file with mode: 0644]
models/src/test/java/org/onap/policy/clamp/models/acm/utils/StateDefinitionTest.java [new file with mode: 0644]

diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/DeployState.java b/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/DeployState.java
new file mode 100644 (file)
index 0000000..659b312
--- /dev/null
@@ -0,0 +1,28 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2023 Nordix 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.clamp.models.acm.concepts;
+
+public enum DeployState {
+    DEPLOYED,
+    DEPLOYING,
+    UNDEPLOYED,
+    UNDEPLOYING
+}
diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/LockState.java b/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/LockState.java
new file mode 100644 (file)
index 0000000..a619cc2
--- /dev/null
@@ -0,0 +1,29 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2023 Nordix 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.clamp.models.acm.concepts;
+
+public enum LockState {
+    LOCKED,
+    LOCKING,
+    UNLOCKED,
+    UNLOCKING,
+    NONE
+}
index ed83a49..e47947a 100644 (file)
@@ -24,18 +24,6 @@ import lombok.Data;
 
 @Data
 public class AcInstanceStateUpdate {
-    private enum DeployOrder {
-        NONE,
-        UNDEPLOY,
-        DEPLOYED
-    }
-
-    private enum LockOrder {
-        NONE,
-        UNLOCK,
-        LOCK
-    }
-
     private DeployOrder deployOrder;
     private LockOrder lockOrder;
 }
diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/AutomationCompositionOrderStateResponse.java b/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/AutomationCompositionOrderStateResponse.java
deleted file mode 100644 (file)
index a87433d..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * ============LICENSE_START=======================================================
- *  Copyright (C) 2021 Nordix 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.
- *
- *  SPDX-License-Identifier: Apache-2.0
- *  ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.clamp.models.acm.messages.rest.instantiation;
-
-import java.util.ArrayList;
-import java.util.List;
-import lombok.Getter;
-import lombok.Setter;
-import lombok.ToString;
-import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
-import org.onap.policy.clamp.models.acm.messages.rest.GenericNameVersion;
-import org.onap.policy.clamp.models.acm.messages.rest.SimpleResponse;
-
-
-/**
- * Response to Commissioning requests that affect a change.
- */
-@Getter
-@Setter
-@ToString(callSuper = true)
-public class AutomationCompositionOrderStateResponse extends SimpleResponse {
-
-    private AutomationCompositionOrderedState orderedState = AutomationCompositionOrderedState.UNINITIALISED;
-
-    private List<GenericNameVersion> automationCompositionIdentifierList = new ArrayList<>();
-}
diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/DeployOrder.java b/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/DeployOrder.java
new file mode 100644 (file)
index 0000000..6c1572d
--- /dev/null
@@ -0,0 +1,27 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2023 Nordix 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.clamp.models.acm.messages.rest.instantiation;
+
+public enum DeployOrder {
+    NONE,
+    UNDEPLOY,
+    DEPLOY
+}
diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/InstancePropertiesResponse.java b/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/InstancePropertiesResponse.java
deleted file mode 100644 (file)
index 0341d5d..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * ============LICENSE_START=======================================================
- *  Copyright (C) 2021 Nordix 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.
- *
- *  SPDX-License-Identifier: Apache-2.0
- *  ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.clamp.models.acm.messages.rest.instantiation;
-
-import java.util.ArrayList;
-import java.util.List;
-import lombok.Getter;
-import lombok.Setter;
-import lombok.ToString;
-import org.onap.policy.clamp.models.acm.messages.rest.SimpleResponse;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
-
-/**
- * Response to Instance Properties requests that affect a change.
- */
-@Getter
-@Setter
-@ToString(callSuper = true)
-public class InstancePropertiesResponse extends SimpleResponse {
-    private List<ToscaConceptIdentifier> affectedInstanceProperties = new ArrayList<>();
-}
diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/InstantiationOrderStateResponse.java b/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/InstantiationOrderStateResponse.java
deleted file mode 100644 (file)
index e6bdaee..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * ============LICENSE_START=======================================================
- *  Copyright (C) 2021 Nordix Foundation.
- *  ================================================================================
- * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
- *  ================================================================================
- *  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.
- *
- *  SPDX-License-Identifier: Apache-2.0
- *  ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.clamp.models.acm.messages.rest.instantiation;
-
-import lombok.Getter;
-import lombok.Setter;
-import lombok.ToString;
-import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
-import org.onap.policy.clamp.models.acm.messages.rest.SimpleResponse;
-
-/**
- * Response to instantiation OrderState.
- */
-@Getter
-@Setter
-@ToString(callSuper = true)
-public class InstantiationOrderStateResponse extends SimpleResponse {
-
-    private AutomationCompositionOrderedState orderedState;
-}
diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/LockOrder.java b/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/LockOrder.java
new file mode 100644 (file)
index 0000000..6b0a208
--- /dev/null
@@ -0,0 +1,27 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2023 Nordix 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.clamp.models.acm.messages.rest.instantiation;
+
+public enum LockOrder {
+    NONE,
+    UNLOCK,
+    LOCK
+}
diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AcInstanceStateResolver.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AcInstanceStateResolver.java
new file mode 100644 (file)
index 0000000..dba64f5
--- /dev/null
@@ -0,0 +1,82 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2023 Nordix 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.clamp.models.acm.persistence.provider;
+
+import org.onap.policy.clamp.models.acm.concepts.DeployState;
+import org.onap.policy.clamp.models.acm.concepts.LockState;
+import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
+import org.onap.policy.clamp.models.acm.messages.rest.instantiation.LockOrder;
+import org.onap.policy.clamp.models.acm.utils.StateDefinition;
+import org.springframework.stereotype.Component;
+
+@Component
+public class AcInstanceStateResolver {
+    private final StateDefinition<String> graph;
+
+    private static final String DEPLOYED = DeployState.DEPLOYED.name();
+    private static final String UNDEPLOYED = DeployState.UNDEPLOYED.name();
+
+    private static final String LOCKED = LockState.LOCKED.name();
+    private static final String UNLOCKED = LockState.UNLOCKED.name();
+    private static final String STATE_LOCKED_NONE = LockState.NONE.name();
+
+    private static final String DEPLOY_NONE = DeployOrder.NONE.name();
+    private static final String LOCK_NONE = LockOrder.NONE.name();
+
+    // list of results
+    public static final String DEPLOY = DeployOrder.DEPLOY.name();
+    public static final String UNDEPLOY = DeployOrder.UNDEPLOY.name();
+    public static final String LOCK = LockOrder.LOCK.name();
+    public static final String UNLOCK = LockOrder.UNLOCK.name();
+    public static final String NONE = "NONE";
+
+    /**
+     * Construct.
+     */
+    public AcInstanceStateResolver() {
+        this.graph = new StateDefinition<>(4, NONE);
+
+        this.graph.put(new String[] {DEPLOY, LOCK_NONE, UNDEPLOYED, STATE_LOCKED_NONE}, DEPLOY);
+        this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, DEPLOYED, LOCKED}, UNDEPLOY);
+        this.graph.put(new String[] {DEPLOY_NONE, UNLOCK, DEPLOYED, LOCKED}, UNLOCK);
+        this.graph.put(new String[] {DEPLOY_NONE, LOCK, DEPLOYED, UNLOCKED}, LOCK);
+    }
+
+    /**
+     * Check if Deploy Order and Lock Order are consistent with current DeployState and LockState.
+     *
+     * @param acDeployOrder the Deploy Ordered
+     * @param acLockOrder the Lock Ordered
+     * @param acDeployState then current Deploy State
+     * @param acLockState the  current Lock State
+     * @return the order (DEPLOY/UNDEPLOY/LOCK/UNLOCK) to send to participant or NONE if order is not consistent
+     */
+    public String resolve(DeployOrder acDeployOrder, LockOrder acLockOrder, DeployState acDeployState,
+            LockState acLockState) {
+        var deployOrder = acDeployOrder != null ? acDeployOrder : DeployOrder.NONE;
+        var lockOrder = acLockOrder != null ? acLockOrder : LockOrder.NONE;
+
+        var deployState = acDeployState != null ? acDeployState : DeployState.UNDEPLOYED;
+        var lockState = acLockState != null ? acLockState : LockState.NONE;
+        return this.graph
+                .get(new String[] {deployOrder.name(), lockOrder.name(), deployState.name(), lockState.name()});
+    }
+}
diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/AcInstanceStateResolverTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/AcInstanceStateResolverTest.java
new file mode 100644 (file)
index 0000000..103bca2
--- /dev/null
@@ -0,0 +1,65 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2023 Nordix 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.clamp.models.acm.persistence.provider;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.junit.jupiter.api.Test;
+import org.onap.policy.clamp.models.acm.concepts.DeployState;
+import org.onap.policy.clamp.models.acm.concepts.LockState;
+import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
+import org.onap.policy.clamp.models.acm.messages.rest.instantiation.LockOrder;
+
+class AcInstanceStateResolverTest {
+
+    @Test
+    void testResolve() {
+        var acTypeStateResolver = new AcInstanceStateResolver();
+        var result =
+                acTypeStateResolver.resolve(DeployOrder.DEPLOY, LockOrder.NONE, DeployState.UNDEPLOYED, LockState.NONE);
+        assertThat(result).isEqualTo(AcInstanceStateResolver.DEPLOY);
+        result = acTypeStateResolver.resolve(DeployOrder.UNDEPLOY, LockOrder.NONE, DeployState.DEPLOYED,
+                LockState.LOCKED);
+        assertThat(result).isEqualTo(AcInstanceStateResolver.UNDEPLOY);
+        result = acTypeStateResolver.resolve(DeployOrder.NONE, LockOrder.UNLOCK, DeployState.DEPLOYED,
+                LockState.LOCKED);
+        assertThat(result).isEqualTo(AcInstanceStateResolver.UNLOCK);
+        result = acTypeStateResolver.resolve(DeployOrder.NONE, LockOrder.LOCK, DeployState.DEPLOYED,
+                LockState.UNLOCKED);
+        assertThat(result).isEqualTo(AcInstanceStateResolver.LOCK);
+
+        result = acTypeStateResolver.resolve(DeployOrder.NONE, LockOrder.NONE, DeployState.UNDEPLOYED, LockState.NONE);
+        assertThat(result).isEqualTo(AcInstanceStateResolver.NONE);
+        result = acTypeStateResolver.resolve(DeployOrder.UNDEPLOY, LockOrder.UNLOCK, DeployState.DEPLOYED,
+                LockState.LOCKED);
+        assertThat(result).isEqualTo(AcInstanceStateResolver.NONE);
+        result = acTypeStateResolver.resolve(DeployOrder.NONE, LockOrder.UNLOCK, DeployState.UNDEPLOYED,
+                LockState.NONE);
+        assertThat(result).isEqualTo(AcInstanceStateResolver.NONE);
+        result = acTypeStateResolver.resolve(DeployOrder.UNDEPLOY, LockOrder.NONE, DeployState.DEPLOYING,
+                LockState.NONE);
+        assertThat(result).isEqualTo(AcInstanceStateResolver.NONE);
+
+        result = acTypeStateResolver.resolve(null, null, null, null);
+        assertThat(result).isEqualTo(AcInstanceStateResolver.NONE);
+    }
+
+}
diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/utils/StateDefinitionTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/utils/StateDefinitionTest.java
new file mode 100644 (file)
index 0000000..c2cbf4b
--- /dev/null
@@ -0,0 +1,50 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2023 Nordix 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.clamp.models.acm.utils;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import org.junit.jupiter.api.Test;
+
+class StateDefinitionTest {
+
+    @Test
+    void testNonNull() {
+        var stateDefinition = new StateDefinition<String>(2, null);
+        assertThatThrownBy(() -> stateDefinition.put(null, null))
+            .hasMessageMatching("keys is marked .*ull but is null");
+        assertThatThrownBy(() -> stateDefinition.put(new String[] {"", ""}, null))
+            .hasMessageMatching("value is marked .*ull but is null");
+        assertThatThrownBy(() -> stateDefinition.get(null))
+            .hasMessageMatching("keys is marked .*ull but is null");
+    }
+
+    @Test
+    void testWrongKeys() {
+        var stateDefinition = new StateDefinition<String>(2, "NONE", "@");
+        assertThatThrownBy(() -> stateDefinition.get(new String[] {"key"}))
+            .hasMessageMatching("wrong number of keys");
+        assertThatThrownBy(() -> stateDefinition.put(new String[] {"key", "@id"}, "value"))
+            .hasMessageMatching("wrong key @id");
+        assertThatThrownBy(() -> stateDefinition.put(new String[] {"key", null}, "value"))
+            .hasMessageMatching("wrong key null");
+    }
+}