Upgrade ietf-{inet,yang}-types to 2013-07-15
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / OfContext.java
index c2fd12b18a28302d570c3f7b5e0480db9d2e1b23..0b738bc61454141fb368e768b8e74dd25a9d310a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -8,40 +8,93 @@
 
 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay;
 
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.ScheduledExecutorService;
 
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
+import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.groupbasedpolicy.dto.IndexedTenant;
+import org.opendaylight.groupbasedpolicy.dto.PolicyInfo;
 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.endpoint.EndpointManager;
 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.node.SwitchManager;
-import org.opendaylight.groupbasedpolicy.resolver.PolicyResolver;
+import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
+import org.opendaylight.groupbasedpolicy.util.InheritanceUtils;
+import org.opendaylight.groupbasedpolicy.util.PolicyResolverUtils;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.Tenants;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.Tenant;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Optional;
 
 public class OfContext {
-    private final DataBroker dataBroker;
-    private final RpcProviderRegistry rpcRegistry;
 
+    private static final Logger LOG = LoggerFactory.getLogger(OfContext.class);
+    private static final InstanceIdentifier<Tenants> TENANTS_IID = InstanceIdentifier.builder(Tenants.class).build();
+    private final DataBroker dataBroker;
     private final PolicyManager policyManager;
     private final SwitchManager switchManager;
     private final EndpointManager epManager;
-
-    private final PolicyResolver policyResolver;
+    private final Map<TenantId, IndexedTenant> resolvedTenants = new HashMap<>();
+    private PolicyInfo policyInfo;
 
     private final ScheduledExecutorService executor;
 
-    public OfContext(DataBroker dataBroker,
-                      RpcProviderRegistry rpcRegistry,
-                      PolicyManager policyManager,
-                      PolicyResolver policyResolver,
-                      SwitchManager switchManager,
-                      EndpointManager endpointManager,
-                      ScheduledExecutorService executor) {
+    public OfContext(DataBroker dataBroker, PolicyManager policyManager, SwitchManager switchManager,
+            EndpointManager endpointManager, ScheduledExecutorService executor) {
         this.dataBroker = dataBroker;
-        this.rpcRegistry = rpcRegistry;
         this.policyManager = policyManager;
         this.switchManager = switchManager;
         this.epManager = endpointManager;
-        this.policyResolver = policyResolver;
         this.executor = executor;
+        if (dataBroker == null) {
+            LOG.error("DataBroker is null. Cannot read resolved tenants and resolved policy from DS.");
+        } else {
+            ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
+            Optional<Tenants> potentialTenants =
+                    DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL, TENANTS_IID, rTx);
+            if (potentialTenants.isPresent() && potentialTenants.get().getTenant() != null) {
+                for (Tenant tenant : potentialTenants.get().getTenant()) {
+                    resolvedTenants.put(tenant.getId(), new IndexedTenant(tenant));
+                }
+                // TODO we should read resolved-policy from DS instead of this
+                policyInfo = resolvePolicy(resolvedTenants);
+            }
+            rTx.close();
+        }
+    }
+
+    @VisibleForTesting
+    void addTenantAndResolvePolicy(Tenant unresolvedTenant) {
+        Tenant t = InheritanceUtils.resolveTenant(unresolvedTenant);
+        IndexedTenant it = new IndexedTenant(t);
+        resolvedTenants.put(unresolvedTenant.getId(), it);
+        policyInfo = resolvePolicy(resolvedTenants);
+    }
+
+    private static PolicyInfo resolvePolicy(Map<TenantId, IndexedTenant> resolvedTenants) {
+        if (resolvedTenants.isEmpty()) {
+            return null;
+        }
+        return PolicyResolverUtils.resolvePolicyInfo(getIndexedTenants(resolvedTenants.values()));
+    }
+
+    private static Set<IndexedTenant> getIndexedTenants(Collection<IndexedTenant> tenants) {
+        Set<IndexedTenant> result = new HashSet<>();
+        for (IndexedTenant t : tenants) {
+            if (t != null) {
+                result.add(t);
+            }
+        }
+        return result;
     }
 
     public PolicyManager getPolicyManager() {
@@ -56,16 +109,22 @@ public class OfContext {
         return this.epManager;
     }
 
-    public PolicyResolver getPolicyResolver() {
-        return this.policyResolver;
-    }
-
     public DataBroker getDataBroker() {
         return this.dataBroker;
     }
 
-    public RpcProviderRegistry getRpcRegistry() {
-        return this.rpcRegistry;
+    public IndexedTenant getTenant(TenantId tenant) {
+        return resolvedTenants.get(tenant);
+    }
+
+    /**
+     * Get a snapshot of the current policy
+     *
+     * @return the {@link PolicyInfo} object representing an immutable snapshot
+     *         of the policy state
+     */
+    public PolicyInfo getCurrentPolicy() {
+        return policyInfo;
     }
 
     public ScheduledExecutorService getExecutor() {