2 * Copyright (c) 2014 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.resolver;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.when;
18 import java.util.Arrays;
19 import java.util.Collection;
20 import java.util.List;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.groupbasedpolicy.dto.IndexedTenant;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ActionName;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierName;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContextId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContractId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2FloodDomainId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.Tenant;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.TenantBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.ForwardingContext;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.ForwardingContextBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.Policy;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2BridgeDomain;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2BridgeDomainBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2FloodDomain;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2FloodDomainBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L3Context;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L3ContextBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.Subnet;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.SubnetBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.Contract;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.SubjectFeatureInstances;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ActionInstance;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ClassifierInstance;
53 import com.google.common.collect.ImmutableList;
55 public class IndexedTenantTest {
57 private Tenant tenant;
58 private Policy policy;
59 private ForwardingContext fwCtx;
62 public void before() {
63 tenant = mock(Tenant.class);
64 policy = mock(Policy.class);
65 fwCtx = mock(ForwardingContext.class);
66 when(tenant.getPolicy()).thenReturn(policy);
67 when(tenant.getForwardingContext()).thenReturn(fwCtx);
71 public void testResolveND() throws Exception {
72 SubnetId sid = new SubnetId("dd25397d-d829-4c8d-8c01-31f129b8de8f");
73 SubnetId sid2 = new SubnetId("c752ba40-40aa-4a47-8138-9b7175b854fa");
74 L3ContextId l3id = new L3ContextId("f2311f52-890f-4095-8b85-485ec8b92b3c");
75 L2BridgeDomainId bdid = new L2BridgeDomainId("70aeb9ea-4ca1-4fb9-9780-22b04b84a0d6");
76 L2FloodDomainId fdid = new L2FloodDomainId("252fbac6-bb6e-4d16-808d-6f56d20e5cca");
78 L3Context l3c = new L3ContextBuilder().setId(l3id).build();
79 L2BridgeDomain bd = new L2BridgeDomainBuilder().setParent(l3id).setId(bdid).build();
80 L2FloodDomain fd = new L2FloodDomainBuilder().setParent(bdid).setId(fdid).build();
81 Subnet s = new SubnetBuilder().setParent(fdid).setId(sid).build();
82 Subnet s2 = new SubnetBuilder().setParent(bdid).setId(sid2).build();
83 Tenant t = new TenantBuilder()
84 .setForwardingContext(new ForwardingContextBuilder().setSubnet(ImmutableList.of(s, s2))
85 .setL2BridgeDomain(ImmutableList.of(bd))
86 .setL3Context(ImmutableList.of(l3c))
87 .setL2FloodDomain(ImmutableList.of(fd))
90 IndexedTenant it = new IndexedTenant(t);
92 assertNotNull(it.getNetworkDomain(sid));
93 Collection<Subnet> sns = it.resolveSubnets(sid);
94 assertTrue(sns.contains(s));
95 assertTrue(sns.contains(s2));
96 assertEquals(l3id, it.resolveL3Context(sid).getId());
97 assertEquals(bdid, it.resolveL2BridgeDomain(sid).getId());
98 assertEquals(fdid, it.resolveL2FloodDomain(sid).getId());
102 public void constructorTest() {
103 EndpointGroup eg = mock(EndpointGroup.class);
104 List<EndpointGroup> egList = Arrays.asList(eg);
105 when(policy.getEndpointGroup()).thenReturn(egList);
106 EndpointGroupId egId = mock(EndpointGroupId.class);
107 when(eg.getId()).thenReturn(egId);
109 Contract contract = mock(Contract.class);
110 List<Contract> contractList = Arrays.asList(contract);
111 when(policy.getContract()).thenReturn(contractList);
112 ContractId contractId = mock(ContractId.class);
113 when(contract.getId()).thenReturn(contractId);
115 L3Context l3Context = mock(L3Context.class);
116 List<L3Context> l3ContextList = Arrays.asList(l3Context);
117 when(fwCtx.getL3Context()).thenReturn(l3ContextList);
118 L3ContextId l3ContextId = mock(L3ContextId.class);
119 when(l3Context.getId()).thenReturn(l3ContextId);
120 String l3ContextValue = "contextID";
121 when(l3ContextId.getValue()).thenReturn(l3ContextValue);
123 L2BridgeDomain l2BridgeDomain = mock(L2BridgeDomain.class);
124 List<L2BridgeDomain> l2BridgeDomainList = Arrays.asList(l2BridgeDomain);
125 when(fwCtx.getL2BridgeDomain()).thenReturn(l2BridgeDomainList);
126 L2BridgeDomainId l2BridgeDomainId = mock(L2BridgeDomainId.class);
127 when(l2BridgeDomain.getId()).thenReturn(l2BridgeDomainId);
128 String l2BridgeDomainIdValue = "bridgeDomainID";
129 when(l2BridgeDomainId.getValue()).thenReturn(l2BridgeDomainIdValue);
131 L2FloodDomain l2FloodDomain = mock(L2FloodDomain.class);
132 List<L2FloodDomain> l2FloodDomainList = Arrays.asList(l2FloodDomain);
133 when(fwCtx.getL2FloodDomain()).thenReturn(l2FloodDomainList);
134 L2FloodDomainId l2FloodDomainId = mock(L2FloodDomainId.class);
135 when(l2FloodDomain.getId()).thenReturn(l2FloodDomainId);
136 String cValue = "floodDomainID";
137 when(l2FloodDomainId.getValue()).thenReturn(cValue);
139 Subnet subnet = mock(Subnet.class);
140 List<Subnet> subnetList = Arrays.asList(subnet);
141 when(fwCtx.getSubnet()).thenReturn(subnetList);
142 SubnetId subnetId = mock(SubnetId.class);
143 when(subnet.getId()).thenReturn(subnetId);
144 String subnetIdValue = "subnetID";
145 when(subnetId.getValue()).thenReturn(subnetIdValue);
146 ContextId sParent = mock(ContextId.class);
147 when(subnet.getParent()).thenReturn(sParent);
148 String sParentValue = "sParentValue";
149 when(sParent.getValue()).thenReturn(sParentValue);
151 SubjectFeatureInstances sfi = mock(SubjectFeatureInstances.class);
152 when(policy.getSubjectFeatureInstances()).thenReturn(sfi);
154 ClassifierInstance ci = mock(ClassifierInstance.class);
155 List<ClassifierInstance> ciList = Arrays.asList(ci);
156 when(sfi.getClassifierInstance()).thenReturn(ciList);
157 ClassifierName ciName = mock(ClassifierName.class);
158 when(ci.getName()).thenReturn(ciName);
160 ActionInstance ai = mock(ActionInstance.class);
161 List<ActionInstance> actionList = Arrays.asList(ai);
162 when(sfi.getActionInstance()).thenReturn(actionList);
163 ActionName actionName = mock(ActionName.class);
164 when(ai.getName()).thenReturn(actionName);
166 IndexedTenant it = new IndexedTenant(tenant);
168 assertEquals(tenant.hashCode(), it.hashCode());
169 assertEquals(tenant, it.getTenant());
170 assertEquals(eg, it.getEndpointGroup(egId));
171 assertEquals(contract, it.getContract(contractId));
172 assertEquals(l3Context, it.getNetworkDomain(l3ContextId));
173 assertEquals(l2BridgeDomain, it.getNetworkDomain(l2BridgeDomainId));
174 assertEquals(l2FloodDomain, it.getNetworkDomain(l2FloodDomainId));
175 assertEquals(ci, it.getClassifier(ciName));
176 assertEquals(ai, it.getAction(actionName));
180 public void constructorTestNullValues() {
181 when(fwCtx.getL3Context()).thenReturn(null);
182 when(fwCtx.getL2BridgeDomain()).thenReturn(null);
183 when(fwCtx.getL2FloodDomain()).thenReturn(null);
184 when(fwCtx.getSubnet()).thenReturn(null);
186 SubjectFeatureInstances sfi = mock(SubjectFeatureInstances.class);
187 when(policy.getSubjectFeatureInstances()).thenReturn(sfi);
188 when(sfi.getClassifierInstance()).thenReturn(null);
189 when(sfi.getActionInstance()).thenReturn(null);
191 IndexedTenant it = new IndexedTenant(tenant);
192 assertEquals(tenant.hashCode(), it.hashCode());
193 assertEquals(tenant, it.getTenant());
197 public void equalsTest() {
198 Tenant tenant = mock(Tenant.class);
199 IndexedTenant indexedTenant = new IndexedTenant(tenant);
201 assertTrue(indexedTenant.equals(indexedTenant));
202 assertFalse(indexedTenant.equals(null));
203 assertFalse(indexedTenant.equals(new Object()));
205 Tenant tenantOther = mock(Tenant.class);
207 other = new IndexedTenant(tenantOther);
208 assertFalse(indexedTenant.equals(other));
209 other = new IndexedTenant(tenant);
210 assertTrue(indexedTenant.equals(other));