Added range type to subject-feature-definition/parameter
[groupbasedpolicy.git] / renderers / opflex / src / test / java / org / opendaylight / groupbasedpolicy / renderer / opflex / L2EprOperationTest.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.L2BridgeDomainId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3Address;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3AddressBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 import com.google.common.base.Optional;
47 import com.google.common.util.concurrent.CheckedFuture;
48
49
50
51 /**
52  *
53  */
54 public class L2EprOperationTest implements EprOperation.EprOpCallback  {
55     protected static final Logger logger = LoggerFactory.getLogger(L2EprOperationTest.class);
56
57     private static final int TEST_SIZE = 1;
58     L2EprOperation op = null;
59     private int callbacks;
60
61    @Mock
62     private WriteTransaction mockWriter;
63     @Mock
64     private ReadOnlyTransaction mockReader;
65     @Mock
66     private Identity mockId;
67     @Mock
68     private L2BridgeDomainId mockL2Context;
69     @Mock
70     private MacAddress mockMac;
71     @Mock
72     private CheckedFuture<Optional<Endpoint>,ReadFailedException> mockFuture;
73     @Mock
74     private Optional<Endpoint> mockOption;
75     @Mock
76     private Endpoint 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_BD_ID = "badac187-2f98-416b-a931-2d0ee58be6b9";
84     private static final String TEST_CONTEXT = "eef1f1de-18f8-4adc-910d-2141bf3e6699";
85     private static final String TEST_IP1 = "192.168.194.131";
86     private static final String TEST_IP2 = "192.168.194.132";
87     private static final String TEST_IP3 = "192.168.194.133";
88     private static final String TEST_MAC = "de:ad:be:ef:00:ba";
89     private static final int TEST_PRR = 100;
90
91     private static Ipv4Address TEST_IPV4_1, TEST_IPV4_2, TEST_IPV4_3;
92     private static TenantId tid;
93     private static EndpointGroupId egid;
94     private static L2BridgeDomainId l2bdid;
95     private static L3ContextId l3cid;
96     private static MacAddress mac;
97     private static List<L3Address> l3List;
98     private static Long prr;
99
100
101         @Override
102         public void callback(EprOperation op) {
103                 this.callbacks += 1;
104
105         }
106
107         @Before
108     public void setUp() throws Exception {
109         MockitoAnnotations.initMocks(this);
110         TEST_IPV4_1 = new Ipv4Address(TEST_IP1);
111         TEST_IPV4_2 = new Ipv4Address(TEST_IP2);
112         TEST_IPV4_3 = new Ipv4Address(TEST_IP3);
113         op =  new L2EprOperation(TEST_PRR);
114         op.setCallback(this);
115
116         tid = new TenantId(TEST_TENANT_ID);
117         egid = new EndpointGroupId(TEST_EPG_ID);
118         l3cid = new L3ContextId(TEST_CONTEXT);
119         mac = new MacAddress(TEST_MAC);
120         prr = new Long(TEST_PRR);
121         l2bdid = new L2BridgeDomainId(TEST_BD_ID);
122
123         op.setTenantId(tid);
124         op.setEndpointGroupId(egid);
125         op.setContextId(l2bdid);
126         op.setMacAddress(mac);
127
128         // Add 3 contexts
129         L3AddressBuilder l3ab = new L3AddressBuilder();
130         l3ab.setL3Context(l3cid);
131         l3ab.setIpAddress(new IpAddress(TEST_IPV4_1));
132         l3List = new ArrayList<L3Address>();
133         l3List.add(l3ab.build());
134         l3ab.setIpAddress(new IpAddress(TEST_IPV4_2));
135         l3List.add(l3ab.build());
136         l3ab.setIpAddress(new IpAddress(TEST_IPV4_3));
137         l3List.add(l3ab.build());
138         op.setL3AddressList(l3List);
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<Endpoint>>any(), Matchers.<Endpoint>any());
147         Endpoint ep = op.getEp();
148         assertTrue(ep != null);
149         assertTrue(ep.getEndpointGroup().equals(egid));
150         assertTrue(ep.getTenant().equals(tid));
151         assertTrue(ep.getL3Address().equals(l3List));
152         assertTrue(ep.getMacAddress().equals(mac));
153         assertTrue(ep.getTimestamp().equals(prr));
154
155
156     }
157
158     @Test
159     public void testEpDelete() throws Exception {
160         op.delete(mockWriter);
161         verify(mockWriter).delete(eq(LogicalDatastoreType.OPERATIONAL),
162                         Matchers.<InstanceIdentifier<Endpoint>>any());
163
164     }
165
166     @Test
167     public void testRead() throws Exception {
168         executor = Executors.newScheduledThreadPool(1);
169         assertTrue(executor != null);
170         when(mockReader.read(eq(LogicalDatastoreType.OPERATIONAL),
171                         Matchers.<InstanceIdentifier<Endpoint>>any())).thenReturn(mockFuture);
172         op.read(mockReader, executor);
173         verify(mockReader).read(eq(LogicalDatastoreType.OPERATIONAL),
174                         Matchers.<InstanceIdentifier<Endpoint>>any());
175         Endpoint ep = op.getEp();
176         assertTrue(ep != null);
177         assertTrue(ep.getEndpointGroup().equals(egid));
178         assertTrue(ep.getTenant().equals(tid));
179         assertTrue(ep.getL3Address().equals(l3List));
180         assertTrue(ep.getMacAddress().equals(mac));
181         assertTrue(ep.getTimestamp().equals(prr));
182     }
183
184     @Test
185     public void testCallback() throws Exception {
186         this.callbacks = 0;
187
188         // pre-seed the EP
189         op.setEp(op.buildEp());
190
191         when(mockOption.isPresent()).thenReturn(true);
192         when(mockOption.get()).thenReturn(op.getEp());
193
194         op.onSuccess(mockOption);
195         Endpoint ep = op.getEp();
196         assertTrue(ep != null);
197         assertTrue(ep.getEndpointGroup().equals(egid));
198         assertTrue(ep.getTenant().equals(tid));
199         assertTrue(ep.getL3Address().equals(l3List));
200         assertTrue(ep.getMacAddress().equals(mac));
201         assertTrue(ep.getTimestamp().equals(prr));
202         assertTrue(this.callbacks == TEST_SIZE);
203
204     }
205
206 }