Remove code smells and increase code coverage
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / repository / Ric.java
index 82d84f1..6eece5e 100644 (file)
 
 package org.oransc.policyagent.repository;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Vector;
-
+import lombok.Getter;
+import lombok.Setter;
+import org.oransc.policyagent.clients.A1Client.A1ProtocolType;
 import org.oransc.policyagent.configuration.RicConfig;
 
 /**
@@ -32,8 +36,14 @@ import org.oransc.policyagent.configuration.RicConfig;
  */
 public class Ric {
     private final RicConfig ricConfig;
-    private RicState state = RicState.NOT_INITIATED;
+    private final List<String> managedElementIds;
+
+    private RicState state = RicState.UNDEFINED;
     private Map<String, PolicyType> supportedPolicyTypes = new HashMap<>();
+    @Getter
+    @Setter
+    private A1ProtocolType protocolVersion = A1ProtocolType.UNKNOWN;
+
 
     /**
      * Creates the Ric. Initial state is {@link RicState.NOT_INITIATED}.
@@ -42,22 +52,23 @@ public class Ric {
      */
     public Ric(RicConfig ricConfig) {
         this.ricConfig = ricConfig;
+        this.managedElementIds = new ArrayList<>(ricConfig.managedElementIds());
     }
 
     public String name() {
         return ricConfig.name();
     }
 
-    public RicState state() {
-        return state;
+    public RicConfig getConfig() {
+        return this.ricConfig;
     }
 
-    public void setState(RicState newState) {
-        state = newState;
+    public synchronized RicState getState() {
+        return this.state;
     }
 
-    public RicConfig getConfig() {
-        return this.ricConfig;
+    public synchronized void setState(RicState state) {
+        this.state = state;
     }
 
     /**
@@ -65,8 +76,8 @@ public class Ric {
      *
      * @return a vector containing the nodes managed by this Ric.
      */
-    public Vector<String> getManagedElementIds() {
-        return ricConfig.managedElementIds();
+    public synchronized Collection<String> getManagedElementIds() {
+        return managedElementIds;
     }
 
     /**
@@ -75,8 +86,8 @@ public class Ric {
      * @param managedElementId the node name to check.
      * @return true if the given node is managed by this Ric.
      */
-    public boolean isManaging(String managedElementId) {
-        return ricConfig.managedElementIds().contains(managedElementId);
+    public synchronized boolean isManaging(String managedElementId) {
+        return managedElementIds.contains(managedElementId);
     }
 
     /**
@@ -84,9 +95,9 @@ public class Ric {
      *
      * @param managedElementId the node to add.
      */
-    public void addManagedElement(String managedElementId) {
-        if (!ricConfig.managedElementIds().contains(managedElementId)) {
-            ricConfig.managedElementIds().add(managedElementId);
+    public synchronized void addManagedElement(String managedElementId) {
+        if (!managedElementIds.contains(managedElementId)) {
+            managedElementIds.add(managedElementId);
         }
     }
 
@@ -95,8 +106,8 @@ public class Ric {
      *
      * @param managedElementId the node to remove.
      */
-    public void removeManagedElement(String managedElementId) {
-        ricConfig.managedElementIds().remove(managedElementId);
+    public synchronized void removeManagedElement(String managedElementId) {
+        managedElementIds.remove(managedElementId);
     }
 
     /**
@@ -104,12 +115,12 @@ public class Ric {
      *
      * @return the policy types supported by this Ric in an unmodifiable list.
      */
-    public Collection<PolicyType> getSupportedPolicyTypes() {
-        return supportedPolicyTypes.values();
+    public synchronized Collection<PolicyType> getSupportedPolicyTypes() {
+        return new Vector<>(supportedPolicyTypes.values());
     }
 
-    public Collection<String> getSupportedPolicyTypeNames() {
-        return supportedPolicyTypes.keySet();
+    public synchronized Collection<String> getSupportedPolicyTypeNames() {
+        return new Vector<>(supportedPolicyTypes.keySet());
     }
 
     /**
@@ -117,14 +128,14 @@ public class Ric {
      *
      * @param type the policy type to support.
      */
-    public void addSupportedPolicyType(PolicyType type) {
+    public synchronized void addSupportedPolicyType(PolicyType type) {
         supportedPolicyTypes.put(type.name(), type);
     }
 
     /**
      * Removes all policy type as supported by this Ric.
      */
-    public void clearSupportedPolicyTypes() {
+    public synchronized void clearSupportedPolicyTypes() {
         supportedPolicyTypes.clear();
     }
 
@@ -135,31 +146,31 @@ public class Ric {
      *
      * @return true if the given type is supported by this Ric, false otherwise.
      */
-    public boolean isSupportingType(String typeName) {
+    public synchronized boolean isSupportingType(String typeName) {
         return supportedPolicyTypes.containsKey(typeName);
     }
 
     @Override
-    public String toString() {
+    public synchronized String toString() {
         return Ric.class.getSimpleName() + ": " + "name: " + name() + ", state: " + state + ", baseUrl: "
-            + ricConfig.baseUrl() + ", managedNodes: " + ricConfig.managedElementIds();
+            + ricConfig.baseUrl() + ", managedNodes: " + managedElementIds;
     }
 
     /**
      * Represents the states possible for a Ric.
      */
-    public static enum RicState {
+    public enum RicState {
         /**
-         * The Ric has not been initiated yet.
+         * The agent view of the Ric may be inconsistent.
          */
-        NOT_INITIATED,
+        UNDEFINED,
         /**
-         * The Ric is working fine.
+         * The normal state. Policies can be configured.
          */
-        ACTIVE,
+        IDLE,
         /**
-         * The Ric cannot be contacted.
+         * The agent is synchronizing the view of the Ric.
          */
-        NOT_REACHABLE, RECOVERING
+        SYNCHRONIZING
     }
 }