HONEYCOMB-58 - Routing Api
[honeycomb.git] / lisp / lisp2vpp / src / test / java / io / fd / honeycomb / lisp / translate / read / RemoteMappingCustomizerTest.java
1 /*
2  * Copyright (c) 2016 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.read;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertTrue;
22 import static org.mockito.Matchers.any;
23 import static org.mockito.Mockito.when;
24
25 import com.google.common.collect.ImmutableList;
26 import io.fd.honeycomb.lisp.context.util.EidMappingContext;
27 import io.fd.honeycomb.lisp.translate.util.EidTranslator;
28 import io.fd.honeycomb.translate.MappingContext;
29 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
30 import io.fd.honeycomb.translate.vpp.util.NamingContext;
31 import io.fd.honeycomb.vpp.test.read.ListReaderCustomizerTest;
32 import io.fd.vpp.jvpp.core.dto.LispEidTableDetails;
33 import io.fd.vpp.jvpp.core.dto.LispEidTableDetailsReplyDump;
34 import io.fd.vpp.jvpp.core.dto.LispLocatorDetails;
35 import io.fd.vpp.jvpp.core.dto.LispLocatorDetailsReplyDump;
36 import java.util.Collections;
37 import java.util.List;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.mockito.Mock;
41 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.eid.mapping.context.rev160801.contexts.eid.mapping.context.mappings.mapping.EidBuilder;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4Builder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.MapReplyAction;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.MappingId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.RemoteMappings;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.RemoteMappingsBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.RemoteMapping;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.RemoteMappingBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.RemoteMappingKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.Eid;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.locator.list.NegativeMapping;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.locator.list.PositiveMapping;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.dp.subtable.grouping.remote.mappings.remote.mapping.locator.list.positive.mapping.rlocs.Locator;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.eid.table.grouping.EidTable;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.eid.table.grouping.eid.table.VniTable;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.eid.table.grouping.eid.table.VniTableKey;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.eid.table.grouping.eid.table.vni.table.VrfSubtable;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61
62 public class RemoteMappingCustomizerTest
63         extends ListReaderCustomizerTest<RemoteMapping, RemoteMappingKey, RemoteMappingBuilder>
64         implements EidTranslator {
65
66     private static final Ipv4
67             EID_ADDRESS = new Ipv4Builder().setIpv4(new Ipv4Address("192.168.2.1")).build();
68
69     @Mock
70     private EidMappingContext eidMappingContext;
71
72     private InstanceIdentifier<RemoteMapping> validId;
73
74     public RemoteMappingCustomizerTest() {
75         super(RemoteMapping.class, RemoteMappingsBuilder.class);
76     }
77
78     @Before
79     public void init() {
80
81         validId = InstanceIdentifier.create(EidTable.class)
82                 .child(VniTable.class, new VniTableKey(12L))
83                 .child(VrfSubtable.class)
84                 .child(RemoteMappings.class)
85                 .child(RemoteMapping.class, new RemoteMappingKey(new MappingId("remote-mapping")));
86         mockMappings();
87         defineMapping(mappingContext,"loc-set",1,"loc-set-context");
88     }
89
90
91     private void mockDumpDataActionZero() {
92         LispEidTableDetailsReplyDump replyDump = new LispEidTableDetailsReplyDump();
93         LispEidTableDetails detail = new LispEidTableDetails();
94         detail.action = 0;
95         detail.authoritative = 1;
96         detail.context = 4;
97         detail.eid = new byte[]{-64, -88, 2, 1};
98         detail.eidPrefixLen = 32;
99         detail.isLocal = 0;
100         detail.locatorSetIndex = 1;
101         detail.ttl = 7;
102         detail.vni = 12;
103
104         replyDump.lispEidTableDetails = ImmutableList.of(detail);
105
106         when(api.lispEidTableDump(any())).thenReturn(future(replyDump));
107
108         LispLocatorDetailsReplyDump rlocs = new LispLocatorDetailsReplyDump();
109         rlocs.lispLocatorDetails = Collections.emptyList();
110         when(api.lispLocatorDump(any())).thenReturn(future(rlocs));
111     }
112
113     private void mockDumpDataActionOne() {
114         LispEidTableDetailsReplyDump replyDump = new LispEidTableDetailsReplyDump();
115         LispEidTableDetails detail = new LispEidTableDetails();
116         detail.action = 1;
117         detail.authoritative = 1;
118         detail.context = 4;
119         detail.eid = new byte[]{-64, -88, 2, 1};
120         detail.eidPrefixLen = 32;
121         detail.isLocal = 0;
122         detail.locatorSetIndex = 1;
123         detail.ttl = 7;
124         detail.vni = 12;
125
126         replyDump.lispEidTableDetails = ImmutableList.of(detail);
127
128         when(api.lispEidTableDump(any())).thenReturn(future(replyDump));
129     }
130
131     private void mockDumpDataActionZeroWithRemotes() {
132         LispEidTableDetailsReplyDump replyDump = new LispEidTableDetailsReplyDump();
133         LispEidTableDetails detail = new LispEidTableDetails();
134         detail.action = 0;
135         detail.authoritative = 1;
136         detail.context = 4;
137         detail.eid = new byte[]{-64, -88, 2, 1};
138         detail.eidPrefixLen = 32;
139         detail.isLocal = 0;
140         detail.locatorSetIndex = 1;
141         detail.ttl = 7;
142         detail.vni = 12;
143
144         replyDump.lispEidTableDetails = ImmutableList.of(detail);
145
146         when(api.lispEidTableDump(any())).thenReturn(future(replyDump));
147
148         LispLocatorDetailsReplyDump rlocs = new LispLocatorDetailsReplyDump();
149         LispLocatorDetails rloc = new LispLocatorDetails();
150         rloc.ipAddress = new byte[]{-64, -88, 2, 1};
151         rloc.isIpv6 = 0;
152         rloc.priority = 1;
153         rloc.weight = 2;
154
155         rlocs.lispLocatorDetails = ImmutableList.of(rloc);
156
157         when(api.lispLocatorDump(any())).thenReturn(future(rlocs));
158     }
159
160
161     private void mockMappings() {
162
163         when(eidMappingContext.getId(any(Eid.class), any(MappingContext.class)))
164                 .thenReturn(new MappingId("remote-mapping"));
165         when(eidMappingContext.containsEid(new MappingId("remote-mapping"), mappingContext)).thenReturn(true);
166         when(eidMappingContext.getEid(new MappingId("remote-mapping"), mappingContext))
167                 .thenReturn(new EidBuilder().setAddress(EID_ADDRESS).build());
168
169     }
170
171     @Test
172     public void readCurrentAttributesNegativeMappingOne() throws Exception {
173         mockDumpDataActionOne();
174         RemoteMappingBuilder builder = new RemoteMappingBuilder();
175         getCustomizer().readCurrentAttributes(validId, builder, ctx);
176
177         RemoteMapping mapping = builder.build();
178
179         assertNotNull(mapping);
180         assertEquals(true, compareAddresses(EID_ADDRESS, mapping.getEid().getAddress()));
181         assertEquals(true, mapping.getAuthoritative().isA());
182         assertEquals(7L, mapping.getTtl().longValue());
183         assertTrue(mapping.getLocatorList() instanceof NegativeMapping);
184         assertEquals(MapReplyAction.NativelyForward,
185                 ((NegativeMapping) mapping.getLocatorList()).getMapReply().getMapReplyAction());
186     }
187
188     @Test
189     public void readCurrentAttributesNegativeMappingZero() throws Exception {
190         mockDumpDataActionZero();
191         RemoteMappingBuilder builder = new RemoteMappingBuilder();
192         getCustomizer().readCurrentAttributes(validId, builder, ctx);
193
194         RemoteMapping mapping = builder.build();
195
196         assertNotNull(mapping);
197         assertEquals(true, compareAddresses(EID_ADDRESS, mapping.getEid().getAddress()));
198         assertEquals(true, mapping.getAuthoritative().isA());
199         assertEquals(7L, mapping.getTtl().longValue());
200         assertEquals(MapReplyAction.NoAction,
201                 ((NegativeMapping) mapping.getLocatorList()).getMapReply().getMapReplyAction());
202     }
203
204     @Test
205     public void readCurrentAttributesPositiveMapping() throws Exception {
206         mockDumpDataActionZeroWithRemotes();
207         RemoteMappingBuilder builder = new RemoteMappingBuilder();
208         getCustomizer().readCurrentAttributes(validId, builder, ctx);
209
210         RemoteMapping mapping = builder.build();
211
212         assertNotNull(mapping);
213         assertEquals(true, compareAddresses(EID_ADDRESS, mapping.getEid().getAddress()));
214         assertEquals(true, mapping.getAuthoritative().isA());
215         assertEquals(7L, mapping.getTtl().longValue());
216         assertTrue(mapping.getLocatorList() instanceof PositiveMapping);
217
218         final List<Locator> locators = ((PositiveMapping) mapping.getLocatorList()).getRlocs().getLocator();
219         assertEquals(1, locators.size());
220         final Locator locator = locators.get(0);
221         assertEquals("192.168.2.1", locator.getAddress().getIpv4Address().getValue());
222         assertEquals(1, locator.getPriority().shortValue());
223         assertEquals(2, locator.getWeight().shortValue());
224     }
225
226
227     @Test
228     public void getAllIds() throws Exception {
229         mockDumpDataActionOne();
230         final List<RemoteMappingKey> keys = getCustomizer().getAllIds(validId, ctx);
231
232         assertNotNull(keys);
233         assertEquals(1, keys.size());
234         assertEquals("remote-mapping", keys.get(0).getId().getValue());
235     }
236
237     @Override
238     protected ReaderCustomizer<RemoteMapping, RemoteMappingBuilder> initCustomizer() {
239         return new RemoteMappingCustomizer(api, new NamingContext("loc-set", "loc-set-context"), eidMappingContext);
240     }
241 }