Bug 3738: GBP coexistence with SFC
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / OfContext.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay;
10
11 import java.util.Collection;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.Map;
15 import java.util.Set;
16 import java.util.concurrent.ScheduledExecutorService;
17
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;
34
35 import com.google.common.annotations.VisibleForTesting;
36 import com.google.common.base.Optional;
37
38 public class OfContext {
39
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;
48
49     private final ScheduledExecutorService executor;
50
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.");
60         } else {
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));
67                 }
68                 // TODO we should read resolved-policy from DS instead of this
69                 policyInfo = resolvePolicy(resolvedTenants);
70             }
71             rTx.close();
72         }
73     }
74
75     @VisibleForTesting
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);
81     }
82
83     private static PolicyInfo resolvePolicy(Map<TenantId, IndexedTenant> resolvedTenants) {
84         if (resolvedTenants.isEmpty()) {
85             return null;
86         }
87         return PolicyResolverUtils.resolvePolicyInfo(getIndexedTenants(resolvedTenants.values()));
88     }
89
90     private static Set<IndexedTenant> getIndexedTenants(Collection<IndexedTenant> tenants) {
91         Set<IndexedTenant> result = new HashSet<>();
92         for (IndexedTenant t : tenants) {
93             if (t != null) {
94                 result.add(t);
95             }
96         }
97         return result;
98     }
99
100     public PolicyManager getPolicyManager() {
101         return this.policyManager;
102     }
103
104     public SwitchManager getSwitchManager() {
105         return this.switchManager;
106     }
107
108     public EndpointManager getEndpointManager() {
109         return this.epManager;
110     }
111
112     public DataBroker getDataBroker() {
113         return this.dataBroker;
114     }
115
116     public IndexedTenant getTenant(TenantId tenant) {
117         return resolvedTenants.get(tenant);
118     }
119
120     /**
121      * Get a snapshot of the current policy
122      *
123      * @return the {@link PolicyInfo} object representing an immutable snapshot
124      *         of the policy state
125      */
126     public PolicyInfo getCurrentPolicy() {
127         return policyInfo;
128     }
129
130     public ScheduledExecutorService getExecutor() {
131         return this.executor;
132     }
133
134 }