Merge "Changed in config will add and recover Rics"
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / tasks / RicRecoveryTask.java
index 3883585..fb41e26 100644 (file)
@@ -24,6 +24,7 @@ import java.util.Collection;
 import java.util.Vector;
 
 import org.oransc.policyagent.clients.A1Client;
+import org.oransc.policyagent.clients.AsyncRestClient;
 import org.oransc.policyagent.exceptions.ServiceException;
 import org.oransc.policyagent.repository.ImmutablePolicyType;
 import org.oransc.policyagent.repository.Policies;
@@ -31,6 +32,9 @@ import org.oransc.policyagent.repository.Policy;
 import org.oransc.policyagent.repository.PolicyType;
 import org.oransc.policyagent.repository.PolicyTypes;
 import org.oransc.policyagent.repository.Ric;
+import org.oransc.policyagent.repository.Rics;
+import org.oransc.policyagent.repository.Service;
+import org.oransc.policyagent.repository.Services;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -47,16 +51,20 @@ public class RicRecoveryTask {
     private final A1Client a1Client;
     private final PolicyTypes policyTypes;
     private final Policies policies;
+    private final Services services;
 
-    public RicRecoveryTask(A1Client a1Client, PolicyTypes policyTypes, Policies policies) {
+    public RicRecoveryTask(A1Client a1Client, PolicyTypes policyTypes, Policies policies, Services services) {
         this.a1Client = a1Client;
         this.policyTypes = policyTypes;
         this.policies = policies;
+        this.services = services;
     }
 
-    public void run(Collection<Ric> rics) {
-        for (Ric ric : rics) {
-            run(ric);
+    public void run(Rics rics) {
+        synchronized (rics) {
+            for (Ric ric : rics.getRics()) {
+                run(ric);
+            }
         }
     }
 
@@ -80,20 +88,39 @@ public class RicRecoveryTask {
 
     private void onComplete(Ric ric) {
         logger.debug("Recovery completed for:" + ric.name());
-        ric.setState(Ric.RicState.ACTIVE);
+        ric.setState(Ric.RicState.IDLE);
+        notifyAllServices("Recovery completed for:" + ric.name());
+    }
 
+    private void notifyAllServices(String body) {
+        synchronized (services) {
+            for (Service service : services.getAll()) {
+                String url = service.getCallbackUrl();
+                if (service.getCallbackUrl().length() > 0) {
+                    createClient(url) //
+                        .put("", body) //
+                        .subscribe(rsp -> logger.debug("Service called"),
+                            throwable -> logger.warn("Service called failed", throwable),
+                            () -> logger.debug("Service called complete"));
+                }
+            }
+        }
     }
 
     private void onError(Ric ric, Throwable t) {
-        logger.debug("Recovery failed for: {}, reason: {}", ric.name(), t.getMessage());
-        ric.setState(Ric.RicState.NOT_REACHABLE);
+        logger.warn("Recovery failed for: {}, reason: {}", ric.name(), t.getMessage());
+        ric.setState(Ric.RicState.UNDEFINED);
+    }
+
+    private AsyncRestClient createClient(final String url) {
+        return new AsyncRestClient(url);
     }
 
     private Flux<PolicyType> recoverPolicyTypes(Ric ric) {
         ric.clearSupportedPolicyTypes();
         return a1Client.getPolicyTypeIdentities(ric.getConfig().baseUrl()) //
             .flatMapMany(types -> Flux.fromIterable(types)) //
-            .doOnNext(typeId -> logger.debug("For ric: {}, handling type: {}", ric.getConfig().name(), typeId))
+            .doOnNext(typeId -> logger.debug("For ric: {}, handling type: {}", ric.getConfig().name(), typeId)) //
             .flatMap((policyTypeId) -> getPolicyType(ric, policyTypeId)) //
             .doOnNext(policyType -> ric.addSupportedPolicyType(policyType)); //
     }
@@ -117,9 +144,11 @@ public class RicRecoveryTask {
     }
 
     private Flux<String> deletePolicies(Ric ric) {
-        Collection<Policy> ricPolicies = new Vector<>(policies.getForRic(ric.name()));
-        for (Policy policy : ricPolicies) {
-            this.policies.remove(policy);
+        synchronized (policies) {
+            Collection<Policy> ricPolicies = new Vector<>(policies.getForRic(ric.name()));
+            for (Policy policy : ricPolicies) {
+                this.policies.remove(policy);
+            }
         }
 
         return a1Client.getPolicyIdentities(ric.getConfig().baseUrl()) //