Added range type to subject-feature-definition/parameter
[groupbasedpolicy.git] / groupbasedpolicy / src / test / java / org / opendaylight / groupbasedpolicy / renderer / opflex / L3EprOperationTest.java
1 /*
2  * Copyright (C) 2014 Cisco Systems, Inc.
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  *  Authors : Thomas Bachman
9  */
10
11 package org.opendaylight.groupbasedpolicy.renderer.opflex;
12
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;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.concurrent.Executors;
21 import java.util.concurrent.ScheduledExecutorService;
22
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;
44
45 import com.google.common.base.Optional;
46 import com.google.common.util.concurrent.CheckedFuture;
47
48
49
50 /**
51  *
52  */
53 public class L3EprOperationTest implements EprOperation.EprOpCallback {
54     protected static final Logger logger = LoggerFactory.getLogger(L3EprOperationTest.class);
55
56     private static final int TEST_SIZE = 1;
57     L3EprOperation op = null;
58     private int callbacks;
59     @Mock
60     private WriteTransaction mockWriter;
61     @Mock
62     private ReadOnlyTransaction mockReader;
63     @Mock
64     private Identity mockId;
65     @Mock
66     private L3ContextId mockL3Context;
67     @Mock
68     private IpAddress mockIp;
69     @Mock
70     private List<L3Address> mockAddresses;
71     @Mock
72     private CheckedFuture<Optional<EndpointL3>,ReadFailedException> mockFuture;
73     @Mock
74     private Optional<EndpointL3> mockOption;
75     @Mock
76     private EndpointL3 mockEp;
77     @Mock
78     private List<String> mockIdentityList;
79     private ScheduledExecutorService executor;
80
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;
89
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;
98
99         @Override
100         public void callback(EprOperation op) {
101                 this.callbacks += 1;
102
103         }
104
105         @Before
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);
113
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);
120
121         op.setTenantId(tid);
122         op.setEndpointGroupId(egid);
123         op.setIpAddress(new IpAddress(TEST_IPV4_1));
124         op.setContextId(l3cid);
125         op.setMacAddress(mac);
126
127         // Add 3 contexts
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);
138     }
139
140
141     @Test
142     public void testEpPut() throws Exception {
143
144         op.put(mockWriter);
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));
156
157     }
158
159
160     @Test
161     public void testEpDelete() throws Exception {
162         op.delete(mockWriter);
163         verify(mockWriter).delete(eq(LogicalDatastoreType.OPERATIONAL),
164                         Matchers.<InstanceIdentifier<EndpointL3>>any());
165
166     }
167
168     @Test
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));
186
187     }
188
189     @Test
190     public void testCallback() throws Exception {
191         this.callbacks = 0;
192
193         // pre-seed the EP
194         op.setEp(op.buildEp());
195
196         when(mockOption.isPresent()).thenReturn(true);
197         when(mockOption.get()).thenReturn(op.getEp());
198
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);
210     }
211
212
213 }