Refactor AcTypeState Update

Refactor AcTypeState Update and add AcTypeState Resolved that will used by priming/depriming.

Issue-ID: POLICY-4503
Change-Id: I2dd70cd8f569f29d35709e741616079499757bfd
Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/AcTypeState.java b/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/AcTypeState.java
new file mode 100644
index 0000000..76851b4
--- /dev/null
+++ b/models/src/main/java/org/onap/policy/clamp/models/acm/concepts/AcTypeState.java
@@ -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 AcTypeState {
+
+    COMMISSIONED,
+    PRIMING,
+    PRIMED,
+    DEPRIMING
+}
diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/commissioning/AcTypeStateUpdate.java b/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/commissioning/AcTypeStateUpdate.java
index b5f2428..842f1ae 100644
--- a/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/commissioning/AcTypeStateUpdate.java
+++ b/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/commissioning/AcTypeStateUpdate.java
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- * Copyright (C) 2021-2022 Nordix Foundation.
+ * Copyright (C) 2021-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.
@@ -24,11 +24,6 @@
 
 @Data
 public class AcTypeStateUpdate {
-    public enum PrimeOrder {
-        NONE,
-        PRIME,
-        DEPRIME
-    }
 
     private PrimeOrder primeOrder;
 }
diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/commissioning/PrimeOrder.java b/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/commissioning/PrimeOrder.java
new file mode 100644
index 0000000..be6aa8f
--- /dev/null
+++ b/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/commissioning/PrimeOrder.java
@@ -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.commissioning;
+
+public enum PrimeOrder {
+    NONE,
+    PRIME,
+    DEPRIME
+}
diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/AutomationCompositionPrimed.java b/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/AutomationCompositionPrimed.java
deleted file mode 100644
index 3455665..0000000
--- a/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/AutomationCompositionPrimed.java
+++ /dev/null
@@ -1,32 +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 lombok.Data;
-
-@Data
-public class AutomationCompositionPrimed {
-
-    private String name;
-
-    private String version;
-
-    private boolean primed;
-}
diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/AutomationCompositionPrimedResponse.java b/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/AutomationCompositionPrimedResponse.java
deleted file mode 100644
index fa3e8fa..0000000
--- a/models/src/main/java/org/onap/policy/clamp/models/acm/messages/rest/instantiation/AutomationCompositionPrimedResponse.java
+++ /dev/null
@@ -1,39 +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;
-
-
-/**
- * Response to provide all primed automation compositions.
- */
-@Getter
-@Setter
-@ToString(callSuper = true)
-public class AutomationCompositionPrimedResponse extends SimpleResponse {
-
-    private List<AutomationCompositionPrimed> primedAutomationCompositionsList = new ArrayList<>();
-}
diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AcTypeStateResolver.java b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AcTypeStateResolver.java
new file mode 100644
index 0000000..7070a31
--- /dev/null
+++ b/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AcTypeStateResolver.java
@@ -0,0 +1,61 @@
+/*-
+ * ============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.AcTypeState;
+import org.onap.policy.clamp.models.acm.messages.rest.commissioning.PrimeOrder;
+import org.onap.policy.clamp.models.acm.utils.StateDefinition;
+import org.springframework.stereotype.Component;
+
+@Component
+public class AcTypeStateResolver {
+
+    private final StateDefinition<PrimeOrder> graph;
+
+    private static final String PRIME = PrimeOrder.PRIME.toString();
+    private static final String DEPRIME = PrimeOrder.DEPRIME.toString();
+    private static final String PRIMED = AcTypeState.PRIMED.toString();
+    private static final String COMMISSIONED = AcTypeState.COMMISSIONED.toString();
+
+    /**
+     * Construct.
+     */
+    public AcTypeStateResolver() {
+        this.graph = new StateDefinition<>(2, PrimeOrder.NONE);
+        this.graph.put(new String[] {PRIME, PRIMED}, PrimeOrder.PRIME);
+        this.graph.put(new String[] {PRIME, COMMISSIONED}, PrimeOrder.PRIME);
+        this.graph.put(new String[] {DEPRIME, PRIMED}, PrimeOrder.DEPRIME);
+        this.graph.put(new String[] {DEPRIME, COMMISSIONED}, PrimeOrder.DEPRIME);
+    }
+
+    /**
+     * Check if Prime Order is consistent with current AcTypeState.
+     *
+     * @param primeOrder the PrimeOrder
+     * @param acTypeState then current AcTypeState
+     * @return primeOrder or NONE if the primeOrder is not consistent
+     */
+    public PrimeOrder resolve(PrimeOrder primeOrder, AcTypeState acTypeState) {
+        var po = primeOrder != null ? primeOrder : PrimeOrder.NONE;
+        var state = acTypeState != null ? acTypeState : AcTypeState.COMMISSIONED;
+        return this.graph.get(new String[] {po.toString(), state.toString()});
+    }
+}
diff --git a/models/src/main/java/org/onap/policy/clamp/models/acm/utils/StateDefinition.java b/models/src/main/java/org/onap/policy/clamp/models/acm/utils/StateDefinition.java
new file mode 100644
index 0000000..4593484
--- /dev/null
+++ b/models/src/main/java/org/onap/policy/clamp/models/acm/utils/StateDefinition.java
@@ -0,0 +1,81 @@
+/*-
+ * ============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 java.util.HashMap;
+import java.util.Map;
+import javax.ws.rs.core.Response.Status;
+import lombok.NonNull;
+import org.onap.policy.models.base.PfModelRuntimeException;
+
+public class StateDefinition<V> {
+
+    private final Map<String, V> map = new HashMap<>();
+
+    private final int size;
+    private final V defaultValue;
+    private final String separator;
+
+    /**
+     * Constructor.
+     *
+     * @param size the number of keys
+     * @param defaultValue the default Value
+     * @param separator the separator used to concatenate keys
+     */
+    public StateDefinition(int size, V defaultValue, String separator) {
+        this.size = size;
+        this.defaultValue = defaultValue;
+        this.separator = separator;
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param size the number of keys
+     * @param defaultValue the default Value
+     */
+    public StateDefinition(int size, V defaultValue) {
+        this(size, defaultValue, "@");
+    }
+
+    public void put(@NonNull String[] keys, @NonNull V value) {
+        map.put(createKey(keys), value);
+    }
+
+    public V get(@NonNull String[] keys) {
+        return map.getOrDefault(createKey(keys), defaultValue);
+    }
+
+    private String createKey(String[] keys) {
+        if (keys.length != size) {
+            throw new PfModelRuntimeException(Status.INTERNAL_SERVER_ERROR, "wrong number of keys");
+        }
+        var sb = new StringBuilder();
+        for (var key : keys) {
+            if (key == null || key.contains(separator)) {
+                throw new PfModelRuntimeException(Status.INTERNAL_SERVER_ERROR, "wrong key " + key);
+            }
+            sb.append(key + separator);
+        }
+        return sb.toString();
+    }
+}
diff --git a/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/AcTypeStateResolverTest.java b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/AcTypeStateResolverTest.java
new file mode 100644
index 0000000..bf27c3e
--- /dev/null
+++ b/models/src/test/java/org/onap/policy/clamp/models/acm/persistence/provider/AcTypeStateResolverTest.java
@@ -0,0 +1,55 @@
+/*-
+ * ============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.AcTypeState;
+import org.onap.policy.clamp.models.acm.messages.rest.commissioning.PrimeOrder;
+
+class AcTypeStateResolverTest {
+
+    @Test
+    void testAcTypeState() {
+        var acTypeStateResolver = new AcTypeStateResolver();
+        var result = acTypeStateResolver.resolve(PrimeOrder.PRIME, AcTypeState.COMMISSIONED);
+        assertThat(result).isEqualTo(PrimeOrder.PRIME);
+        result = acTypeStateResolver.resolve(PrimeOrder.PRIME, AcTypeState.PRIMED);
+        assertThat(result).isEqualTo(PrimeOrder.PRIME);
+        result = acTypeStateResolver.resolve(PrimeOrder.PRIME, AcTypeState.PRIMING);
+        assertThat(result).isEqualTo(PrimeOrder.NONE);
+        result = acTypeStateResolver.resolve(PrimeOrder.PRIME, AcTypeState.DEPRIMING);
+        assertThat(result).isEqualTo(PrimeOrder.NONE);
+
+        result = acTypeStateResolver.resolve(PrimeOrder.DEPRIME, AcTypeState.COMMISSIONED);
+        assertThat(result).isEqualTo(PrimeOrder.DEPRIME);
+        result = acTypeStateResolver.resolve(PrimeOrder.DEPRIME, AcTypeState.PRIMED);
+        assertThat(result).isEqualTo(PrimeOrder.DEPRIME);
+        result = acTypeStateResolver.resolve(PrimeOrder.DEPRIME, AcTypeState.PRIMING);
+        assertThat(result).isEqualTo(PrimeOrder.NONE);
+        result = acTypeStateResolver.resolve(PrimeOrder.DEPRIME, AcTypeState.DEPRIMING);
+        assertThat(result).isEqualTo(PrimeOrder.NONE);
+
+        result = acTypeStateResolver.resolve(null, null);
+        assertThat(result).isEqualTo(PrimeOrder.NONE);
+    }
+}