2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay;
11 import java.util.Collection;
12 import java.util.HashMap;
13 import java.util.HashSet;
16 import java.util.concurrent.ScheduledExecutorService;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.groupbasedpolicy.dto.IndexedTenant;
22 import org.opendaylight.groupbasedpolicy.dto.PolicyInfo;
23 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.endpoint.EndpointManager;
24 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.node.SwitchManager;
25 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
26 import org.opendaylight.groupbasedpolicy.util.InheritanceUtils;
27 import org.opendaylight.groupbasedpolicy.util.PolicyResolverUtils;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.Tenants;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.Tenant;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import com.google.common.annotations.VisibleForTesting;
36 import com.google.common.base.Optional;
38 public class OfContext {
40 private static final Logger LOG = LoggerFactory.getLogger(OfContext.class);
41 private static final InstanceIdentifier<Tenants> TENANTS_IID = InstanceIdentifier.builder(Tenants.class).build();
42 private final DataBroker dataBroker;
43 private final PolicyManager policyManager;
44 private final SwitchManager switchManager;
45 private final EndpointManager epManager;
46 private final Map<TenantId, IndexedTenant> resolvedTenants = new HashMap<>();
47 private PolicyInfo policyInfo;
49 private final ScheduledExecutorService executor;
51 public OfContext(DataBroker dataBroker, PolicyManager policyManager, SwitchManager switchManager,
52 EndpointManager endpointManager, ScheduledExecutorService executor) {
53 this.dataBroker = dataBroker;
54 this.policyManager = policyManager;
55 this.switchManager = switchManager;
56 this.epManager = endpointManager;
57 this.executor = executor;
58 if (dataBroker == null) {
59 LOG.error("DataBroker is null. Cannot read resolved tenants and resolved policy from DS.");
61 ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
62 Optional<Tenants> potentialTenants =
63 DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL, TENANTS_IID, rTx);
64 if (potentialTenants.isPresent() && potentialTenants.get().getTenant() != null) {
65 for (Tenant tenant : potentialTenants.get().getTenant()) {
66 resolvedTenants.put(tenant.getId(), new IndexedTenant(tenant));
68 // TODO we should read resolved-policy from DS instead of this
69 policyInfo = resolvePolicy(resolvedTenants);
76 void addTenantAndResolvePolicy(Tenant unresolvedTenant) {
77 Tenant t = InheritanceUtils.resolveTenant(unresolvedTenant);
78 IndexedTenant it = new IndexedTenant(t);
79 resolvedTenants.put(unresolvedTenant.getId(), it);
80 policyInfo = resolvePolicy(resolvedTenants);
83 private static PolicyInfo resolvePolicy(Map<TenantId, IndexedTenant> resolvedTenants) {
84 if (resolvedTenants.isEmpty()) {
87 return PolicyResolverUtils.resolvePolicyInfo(getIndexedTenants(resolvedTenants.values()));
90 private static Set<IndexedTenant> getIndexedTenants(Collection<IndexedTenant> tenants) {
91 Set<IndexedTenant> result = new HashSet<>();
92 for (IndexedTenant t : tenants) {
100 public PolicyManager getPolicyManager() {
101 return this.policyManager;
104 public SwitchManager getSwitchManager() {
105 return this.switchManager;
108 public EndpointManager getEndpointManager() {
109 return this.epManager;
112 public DataBroker getDataBroker() {
113 return this.dataBroker;
116 public IndexedTenant getTenant(TenantId tenant) {
117 return resolvedTenants.get(tenant);
121 * Get a snapshot of the current policy
123 * @return the {@link PolicyInfo} object representing an immutable snapshot
124 * of the policy state
126 public PolicyInfo getCurrentPolicy() {
130 public ScheduledExecutorService getExecutor() {
131 return this.executor;