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