2 * Copyright (C) 2014 Cisco Systems, Inc.
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
8 * Authors : Thomas Bachman
11 package org.opendaylight.groupbasedpolicy.renderer.opflex;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Matchers.eq;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.concurrent.Executors;
21 import java.util.concurrent.ScheduledExecutorService;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.Matchers;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
29 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3Address;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3AddressBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import com.google.common.base.Optional;
46 import com.google.common.util.concurrent.CheckedFuture;
53 public class L3EprOperationTest implements EprOperation.EprOpCallback {
54 protected static final Logger logger = LoggerFactory.getLogger(L3EprOperationTest.class);
56 private static final int TEST_SIZE = 1;
57 L3EprOperation op = null;
58 private int callbacks;
60 private WriteTransaction mockWriter;
62 private ReadOnlyTransaction mockReader;
64 private Identity mockId;
66 private L3ContextId mockL3Context;
68 private IpAddress mockIp;
70 private List<L3Address> mockAddresses;
72 private CheckedFuture<Optional<EndpointL3>,ReadFailedException> mockFuture;
74 private Optional<EndpointL3> mockOption;
76 private EndpointL3 mockEp;
78 private List<String> mockIdentityList;
79 private ScheduledExecutorService executor;
81 private static final String TEST_TENANT_ID = "e9fbd015-df23-4749-abec-8ba63bc0e738";
82 private static final String TEST_EPG_ID = "8e359239-a253-42c3-9858-9acc039f1913";
83 private static final String TEST_CONTEXT = "4ac7e31a-5775-46ac-b228-f40cdfeeabe7";
84 private static final String TEST_IP1 = "192.168.194.131";
85 private static final String TEST_IP2 = "192.168.194.132";
86 private static final String TEST_IP3 = "192.168.194.133";
87 private static final String TEST_MAC = "de:ad:be:ef:00:ba";
88 private static final int TEST_PRR = 100;
90 private static Ipv4Address TEST_IPV4_1, TEST_IPV4_2, TEST_IPV4_3;
91 private static TenantId tid;
92 private static EndpointGroupId egid;
93 private static L3ContextId l3cid;
94 private static MacAddress mac;
95 private static List<L3Address> l3List;
96 private static IpAddress ip;
97 private static Long prr;
100 public void callback(EprOperation op) {
106 public void setUp() throws Exception {
107 MockitoAnnotations.initMocks(this);
108 TEST_IPV4_1 = new Ipv4Address(TEST_IP1);
109 TEST_IPV4_2 = new Ipv4Address(TEST_IP2);
110 TEST_IPV4_3 = new Ipv4Address(TEST_IP3);
111 op = new L3EprOperation(TEST_PRR);
112 op.setCallback(this);
114 tid = new TenantId(TEST_TENANT_ID);
115 egid = new EndpointGroupId(TEST_EPG_ID);
116 l3cid = new L3ContextId(TEST_CONTEXT);
117 mac = new MacAddress(TEST_MAC);
118 ip = new IpAddress(TEST_IPV4_1);
119 prr = new Long(TEST_PRR);
122 op.setEndpointGroupId(egid);
123 op.setIpAddress(new IpAddress(TEST_IPV4_1));
124 op.setContextId(l3cid);
125 op.setMacAddress(mac);
128 L3AddressBuilder l3ab = new L3AddressBuilder();
129 l3ab.setL3Context(l3cid);
130 l3ab.setIpAddress(new IpAddress(TEST_IPV4_1));
131 l3List = new ArrayList<L3Address>();
132 l3List.add(l3ab.build());
133 l3ab.setIpAddress(new IpAddress(TEST_IPV4_2));
134 l3List.add(l3ab.build());
135 l3ab.setIpAddress(new IpAddress(TEST_IPV4_3));
136 l3List.add(l3ab.build());
137 op.setL3AddressList(l3List);
142 public void testEpPut() throws Exception {
145 verify(mockWriter).put(eq(LogicalDatastoreType.OPERATIONAL),
146 Matchers.<InstanceIdentifier<EndpointL3>>any(), Matchers.<EndpointL3>any());
147 EndpointL3 epl3 = op.getEp();
148 assertTrue(epl3 != null);
149 assertTrue(epl3.getEndpointGroup().equals(egid));
150 assertTrue(epl3.getTenant().equals(tid));
151 assertTrue(epl3.getIpAddress().equals(ip));
152 assertTrue(epl3.getL3Address().equals(l3List));
153 assertTrue(epl3.getMacAddress().equals(mac));
154 assertTrue(epl3.getTimestamp().equals(prr));
155 assertTrue(epl3.getL3Context().equals(l3cid));
161 public void testEpDelete() throws Exception {
162 op.delete(mockWriter);
163 verify(mockWriter).delete(eq(LogicalDatastoreType.OPERATIONAL),
164 Matchers.<InstanceIdentifier<EndpointL3>>any());
169 public void testEpRead() throws Exception {
170 executor = Executors.newScheduledThreadPool(1);
171 assertTrue(executor != null);
172 when(mockReader.read(eq(LogicalDatastoreType.OPERATIONAL),
173 Matchers.<InstanceIdentifier<EndpointL3>>any())).thenReturn(mockFuture);
174 op.read(mockReader, executor);
175 verify(mockReader).read(eq(LogicalDatastoreType.OPERATIONAL),
176 Matchers.<InstanceIdentifier<EndpointL3>>any());
177 EndpointL3 epl3 = op.getEp();
178 assertTrue(epl3 != null);
179 assertTrue(epl3.getEndpointGroup().equals(egid));
180 assertTrue(epl3.getTenant().equals(tid));
181 assertTrue(epl3.getIpAddress().equals(ip));
182 assertTrue(epl3.getL3Address().equals(l3List));
183 assertTrue(epl3.getMacAddress().equals(mac));
184 assertTrue(epl3.getTimestamp().equals(prr));
185 assertTrue(epl3.getL3Context().equals(l3cid));
190 public void testCallback() throws Exception {
194 op.setEp(op.buildEp());
196 when(mockOption.isPresent()).thenReturn(true);
197 when(mockOption.get()).thenReturn(op.getEp());
199 op.onSuccess(mockOption);
200 EndpointL3 epl3 = op.getEp();
201 assertTrue(epl3 != null);
202 assertTrue(epl3.getEndpointGroup().equals(egid));
203 assertTrue(epl3.getTenant().equals(tid));
204 assertTrue(epl3.getIpAddress().equals(ip));
205 assertTrue(epl3.getL3Address().equals(l3List));
206 assertTrue(epl3.getMacAddress().equals(mac));
207 assertTrue(epl3.getTimestamp().equals(prr));
208 assertTrue(epl3.getL3Context().equals(l3cid));
209 assertTrue(this.callbacks == TEST_SIZE);