4e24b3f3f44d5be14e6f98995aa8af508878a609
[honeycomb.git] / lisp / lisp2vpp / src / test / java / io / fd / honeycomb / lisp / translate / write / LocalMappingCustomizerTest.java
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.lisp.translate.write;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.mockito.Matchers.any;
22 import static org.mockito.Matchers.eq;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.times;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27
28 import io.fd.honeycomb.lisp.context.util.EidMappingContext;
29 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
30 import io.fd.honeycomb.translate.vpp.util.Ipv4Translator;
31 import io.fd.honeycomb.translate.write.WriteFailedException;
32 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
33 import java.util.concurrent.ExecutionException;
34 import org.junit.Test;
35 import org.mockito.ArgumentCaptor;
36 import org.mockito.Captor;
37 import org.mockito.Mock;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4Afi;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4Builder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.Lisp;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.MappingId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.dp.subtable.grouping.LocalMappings;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.dp.subtable.grouping.local.mappings.LocalMapping;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.dp.subtable.grouping.local.mappings.LocalMappingBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.dp.subtable.grouping.local.mappings.LocalMappingKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.dp.subtable.grouping.local.mappings.local.mapping.Eid;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.dp.subtable.grouping.local.mappings.local.mapping.EidBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.EidTable;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.VniTable;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.VniTableKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.VrfSubtable;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.lisp.feature.data.grouping.LispFeatureData;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 import io.fd.vpp.jvpp.core.dto.LispAddDelLocalEid;
56 import io.fd.vpp.jvpp.core.dto.LispAddDelLocalEidReply;
57
58 public class LocalMappingCustomizerTest extends WriterCustomizerTest implements ByteDataTranslator, Ipv4Translator {
59
60     @Mock
61     private EidMappingContext eidMappingContext;
62     @Captor
63     private ArgumentCaptor<LispAddDelLocalEid> mappingCaptor;
64
65     private InstanceIdentifier<LocalMapping> id;
66     private LocalMapping mapping;
67     private LocalMappingCustomizer customizer;
68
69     @Override
70     public void setUp() {
71         final Eid
72                 eid = new EidBuilder()
73                 .setAddressType(Ipv4Afi.class)
74                 .setAddress(
75                         new Ipv4Builder().setIpv4(
76                                 new Ipv4Address("192.168.2.1"))
77                                 .build())
78                 .build();
79
80         mapping = new LocalMappingBuilder()
81                 .setEid(eid)
82                 .setLocatorSet("Locator")
83                 .build();
84
85         id = InstanceIdentifier.builder(Lisp.class)
86                 .child(LispFeatureData.class)
87                 .child(EidTable.class)
88                 .child(VniTable.class, new VniTableKey(25L))
89                 .child(VrfSubtable.class)
90                 .child(LocalMappings.class)
91                 .child(LocalMapping.class, new LocalMappingKey(new MappingId("local")))
92                 .build();
93
94         customizer = new LocalMappingCustomizer(api, eidMappingContext);
95
96         when(api.lispAddDelLocalEid(any(LispAddDelLocalEid.class))).thenReturn(future(new LispAddDelLocalEidReply()));
97     }
98
99
100     @Test(expected = NullPointerException.class)
101     public void testWriteCurrentAttributesNullData() throws WriteFailedException {
102         customizer.writeCurrentAttributes(null, null, writeContext);
103     }
104
105     @Test(expected = NullPointerException.class)
106     public void testWriteCurrentAttributesNullEid() throws WriteFailedException {
107         LocalMapping mapping = mock(LocalMapping.class);
108         when(mapping.getEid()).thenReturn(null);
109         when(mapping.getLocatorSet()).thenReturn("Locator");
110
111         customizer.writeCurrentAttributes(null, mapping, writeContext);
112     }
113
114     @Test(expected = NullPointerException.class)
115     public void testWriteCurrentAttributesNullLocator() throws WriteFailedException {
116         LocalMapping mapping = mock(LocalMapping.class);
117         when(mapping.getEid()).thenReturn(mock(Eid.class));
118         when(mapping.getLocatorSet()).thenReturn(null);
119
120         customizer.writeCurrentAttributes(null, mapping, writeContext);
121     }
122
123
124     @Test
125     public void testWriteCurrentAttributes() throws WriteFailedException {
126         customizer.writeCurrentAttributes(id, mapping, writeContext);
127
128         verify(api, times(1)).lispAddDelLocalEid(mappingCaptor.capture());
129
130         LispAddDelLocalEid request = mappingCaptor.getValue();
131
132         assertNotNull(request);
133         assertEquals("Locator", new String(request.locatorSetName));
134         assertEquals("1.2.168.192", arrayToIpv4AddressNoZone(request.eid).getValue());
135         assertEquals(0, request.eidType);
136         assertEquals(1, request.isAdd);
137         assertEquals(25, request.vni);
138         assertEquals("Locator", toString(request.locatorSetName));
139     }
140
141     @Test(expected = UnsupportedOperationException.class)
142     public void testUpdateCurrentAttributes() throws WriteFailedException {
143         customizer.updateCurrentAttributes(null, null, null, writeContext);
144     }
145
146     @Test
147     public void testDeleteCurrentAttributes() throws WriteFailedException, InterruptedException, ExecutionException {
148         when(eidMappingContext.containsEid(any(), eq(mappingContext))).thenReturn(true);
149         customizer.deleteCurrentAttributes(id, mapping, writeContext);
150
151         verify(api, times(1)).lispAddDelLocalEid(mappingCaptor.capture());
152
153         LispAddDelLocalEid request = mappingCaptor.getValue();
154
155         assertNotNull(request);
156         assertEquals("Locator", new String(request.locatorSetName));
157         assertEquals("1.2.168.192", arrayToIpv4AddressNoZone(request.eid).getValue());
158         assertEquals(0, request.eidType);
159         assertEquals(0, request.isAdd);
160         assertEquals(25, request.vni);
161         assertEquals("Locator", toString(request.locatorSetName));
162     }
163 }