5c8138f85fbd12a5b74c46ab993b03d6860ab3cb
[groupbasedpolicy.git] / renderers / faas / src / test / java / org / opendaylight / groupbasedpolicy / renderer / faas / MockFaasPolicyManager.java
1 /*
2  * Copyright (c) 2016 Huawei Technologies 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 package org.opendaylight.groupbasedpolicy.renderer.faas;
9
10 import static org.junit.Assert.assertTrue;
11
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
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.DataChangeListener;
20 import org.opendaylight.faas.uln.datastore.api.Pair;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.common.rev151013.Uuid;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContractId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2FloodDomainId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.ServiceCommunicationLayer;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2BridgeDomain;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2FloodDomain;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L3Context;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.Subnet;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.ResolvedPolicy.ExternalImplicitGroup;
36 import org.opendaylight.yangtools.concepts.ListenerRegistration;
37
38 public class MockFaasPolicyManager extends FaasPolicyManager {
39
40     private Map<EndpointGroupId, EndpointGroup> testEndpointGroups = new HashMap<>();
41     private Map<SubnetId, Subnet> testSubnets = new HashMap<>();
42     private Map<L2FloodDomainId, L2FloodDomain> testL2FloodDomains = new HashMap<>();
43     private Map<L2BridgeDomainId, L2BridgeDomain> testL2BridgeDomains = new HashMap<>();
44     private Map<L3ContextId, L3Context> testL3Contextes = new HashMap<>();
45     private Map<ContractId, Uuid> testSecIdPerContract = new HashMap<>();
46     private Map<TenantId, Uuid> testFaasTenantId = new HashMap<>();
47     private ServiceCommunicationLayer comLayer;
48     private ExternalImplicitGroup externalImplicitGroup;
49
50     public MockFaasPolicyManager(DataBroker dataBroker, ScheduledExecutorService executor) {
51         super(dataBroker, executor);
52     }
53
54     // *******************************************************
55     // Test Stubs
56     // *******************************************************
57     @Override
58     public void registerTenant(TenantId tenantId, EndpointGroupId epgId) {
59         assertTrue("FaasPolicyManager.registerTenant: epgId != null", epgId != null);
60         assertTrue("FaasPolicyManager.registerTenant: tenantId != null", tenantId != null);
61
62         assertTrue("FaasPolicyManager.registerTenant: testEndpointGroups.containsKey(epgId)",
63                 testEndpointGroups.containsKey(epgId));
64
65         registeredTenants.putIfAbsent(tenantId, new ArrayList<ListenerRegistration<DataChangeListener>>());
66     }
67
68     @Override
69     public void registerSubnetWithEpg(EndpointGroupId epgId, TenantId tenantId, SubnetId subnetId) {
70         assertTrue("FaasPolicyManager.registerTenant: epgId != null", epgId != null);
71         assertTrue("FaasPolicyManager.registerTenant: tenantId != null", tenantId != null);
72         assertTrue("FaasPolicyManager.registerTenant: subnetId != null", subnetId != null);
73
74         assertTrue("FaasPolicyManager.registerTenant: testEndpointGroups.containsKey(epgId)",
75                 testEndpointGroups.containsKey(epgId));
76         assertTrue("FaasPolicyManager.registerTenant: registeredTenants.containsKey(tenantId)",
77                 registeredTenants.containsKey(tenantId));
78         assertTrue("FaasPolicyManager.registerTenant: testSubnets.containsKey(subnetId)",
79                 testSubnets.containsKey(subnetId));
80         List<SubnetId> subnets = epgSubnetsMap.get(new Pair<>(epgId, tenantId));
81         if (subnets == null) {
82             subnets = new ArrayList<>();
83         }
84         for (SubnetId id : subnets) {
85             if (id.equals(subnetId)) {
86                 return;
87             }
88         }
89         subnets.add(subnetId);
90         epgSubnetsMap.put(new Pair<>(epgId, tenantId), subnets);
91     }
92
93     @Override
94     public EndpointGroup readEndpointGroup(EndpointGroupId epgId, TenantId tenantId) {
95         return testEndpointGroups.get(epgId);
96     }
97
98     @Override
99     public Subnet readSubnet(SubnetId subnetId, TenantId tenantId) {
100         return testSubnets.get(subnetId);
101     }
102
103     @Override
104     protected L2FloodDomain readL2FloodDomain(L2FloodDomainId l2fId, TenantId tenantId) {
105         return testL2FloodDomains.get(l2fId);
106     }
107
108     @Override
109     protected L2BridgeDomain readL2BridgeDomainInstance(TenantId tenantId, L2BridgeDomainId l2bId) {
110         return testL2BridgeDomains.get(l2bId);
111     }
112
113     @Override
114     protected L3Context readL3ContextInstance(TenantId tenantId, L3ContextId l3cId) {
115         return testL3Contextes.get(l3cId);
116     }
117
118     @Override
119     protected boolean needToCreateLogicalNetwork(ServiceCommunicationLayer comLayer, List<SubnetId> consSubnetIds,
120             List<SubnetId> provSubnetIds, TenantId tenantId, ContractId contractId, EndpointGroup providerEpg,
121             EndpointGroup consumerEpg, ExternalImplicitGroup externalImplicitGroup) {
122         return true;
123     }
124
125     @Override
126     protected Uuid getFaasSecRulesId(ContractId contractId, TenantId gbpTenantId) {
127         return testSecIdPerContract.get(contractId);
128     }
129
130     @Override
131     public Uuid getFaasTenantId(TenantId tenantId) {
132         return testFaasTenantId.get(tenantId);
133     }
134
135     @Override
136     protected void createLayer3LogicalNetwork(EndpointGroup consEpg, ContractId contractId, EndpointGroup provEpg,
137             TenantId gbpTenantId, ServiceCommunicationLayer comLayer, ExternalImplicitGroup externalImplicitGroup) {
138         this.comLayer = comLayer;
139         this.externalImplicitGroup = externalImplicitGroup;
140         assertTrue("FaasPolicyManager.createLayer3LogicalNetwork", testEndpointGroups.containsKey(consEpg.getId()));
141         assertTrue("FaasPolicyManager.createLayer3LogicalNetwork", testEndpointGroups.containsKey(provEpg.getId()));
142         assertTrue("FaasPolicyManager.createLayer3LogicalNetwork", registeredTenants.containsKey(gbpTenantId));
143
144     }
145
146     @Override
147     protected void createLayer2LogicalNetwork(EndpointGroup consEpg, ContractId contractId, EndpointGroup provEpg,
148             TenantId gbpTenantId, ServiceCommunicationLayer comLayer, ExternalImplicitGroup externalImplicitGroup) {
149         this.comLayer = comLayer;
150         this.externalImplicitGroup = externalImplicitGroup;
151         assertTrue("FaasPolicyManager.createLayer2LogicalNetwork", testEndpointGroups.containsKey(consEpg.getId()));
152         assertTrue("FaasPolicyManager.createLayer2LogicalNetwork", testEndpointGroups.containsKey(provEpg.getId()));
153         assertTrue("FaasPolicyManager.createLayer2LogicalNetwork", registeredTenants.containsKey(gbpTenantId));
154     }
155
156     // *******************************************************
157     // The following Methods are to input test data
158     // *******************************************************
159
160     public void storeTestFaasTenantId(TenantId gbpTenantId, Uuid faasTenantId) {
161         testFaasTenantId.put(gbpTenantId, faasTenantId);
162     }
163
164     public void storeTestSecIdPerContract(ContractId contractId, Uuid secId) {
165         testSecIdPerContract.put(contractId, secId);
166     }
167
168     public void storeTestL2BridgeDomain(L2BridgeDomain brdg) {
169         if (brdg.getId() != null) {
170             testL2BridgeDomains.put(brdg.getId(), brdg);
171         }
172     }
173
174     public void storeTestL3Contextes(L3Context l3Context) {
175         if (l3Context.getId() != null) {
176             testL3Contextes.put(l3Context.getId(), l3Context);
177         }
178     }
179
180     public void storeTestL2FloodDomain(L2FloodDomain fld) {
181         if (fld.getId() != null) {
182             testL2FloodDomains.put(fld.getId(), fld);
183         }
184     }
185
186     public void storeTestSubnet(Subnet subnet) {
187         if (subnet.getId() != null) {
188             testSubnets.put(subnet.getId(), subnet);
189         }
190     }
191
192     public void storeTestEpg(EndpointGroup epg) {
193         if (epg.getId() != null) {
194             testEndpointGroups.put(epg.getId(), epg);
195         }
196     }
197
198     public ServiceCommunicationLayer getComLayer() {
199         return comLayer;
200     }
201
202     public ExternalImplicitGroup getExternalImplicitGroup() {
203         return externalImplicitGroup;
204     }
205 }