2 * Copyright (c) 2015 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.lisp.translate.write;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.times;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
26 import io.fd.honeycomb.lisp.context.util.EidMappingContext;
27 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
28 import io.fd.honeycomb.translate.write.WriteFailedException;
29 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
30 import java.util.concurrent.CompletableFuture;
31 import java.util.concurrent.ExecutionException;
32 import org.junit.Test;
33 import org.mockito.ArgumentCaptor;
34 import org.mockito.Captor;
35 import org.mockito.Mockito;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4Builder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.Lisp;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.MapReplyAction;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.MappingId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.EidTable;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.VniTable;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.VniTableKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.RemoteMappings;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.remote.mappings.RemoteMapping;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.remote.mappings.RemoteMappingBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.remote.mappings.RemoteMappingKey;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.remote.mappings.remote.mapping.Eid;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.remote.mappings.remote.mapping.EidBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.remote.mappings.remote.mapping.locator.list.NegativeMappingBuilder;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52 import org.openvpp.jvpp.core.dto.LispAddDelRemoteMapping;
53 import org.openvpp.jvpp.core.dto.LispAddDelRemoteMappingReply;
56 public class RemoteMappingCustomizerTest extends WriterCustomizerTest {
59 private ArgumentCaptor<LispAddDelRemoteMapping> mappingCaptor;
61 private MappingId mappingId;
62 private RemoteMappingCustomizer customizer;
63 private RemoteMapping intf;
64 private LispAddDelRemoteMappingReply fakeReply;
65 private CompletableFuture<LispAddDelRemoteMappingReply> completeFuture;
66 private InstanceIdentifier<RemoteMapping> id;
67 private EidMappingContext remoteMappingContext;
71 final Eid eid = new EidBuilder().setAddress(
72 new Ipv4Builder().setIpv4(
73 new Ipv4Address("192.168.2.1"))
77 mappingId = new MappingId("REMOTE");
78 final RemoteMappingKey key = new RemoteMappingKey(mappingId);
79 remoteMappingContext = new EidMappingContext("remote");
82 intf = new RemoteMappingBuilder()
85 .setLocatorList(new NegativeMappingBuilder().setMapReplyAction(MapReplyAction.Drop).build())
88 id = InstanceIdentifier.builder(Lisp.class).child(EidTable.class)
89 .child(VniTable.class, new VniTableKey(25L))
90 .child(RemoteMappings.class)
91 .child(RemoteMapping.class, key).build();
93 fakeReply = new LispAddDelRemoteMappingReply();
94 completeFuture = new CompletableFuture<>();
95 completeFuture.complete(fakeReply);
96 customizer = new RemoteMappingCustomizer(api, remoteMappingContext);
98 when(api.lispAddDelRemoteMapping(Mockito.any())).thenReturn(completeFuture);
99 when(mappingContext.read(Mockito.any())).thenReturn(com.google.common.base.Optional
100 .of(new RemoteMappingBuilder().setKey(key).setId(mappingId).setEid(eid).build()));
103 @Test(expected = NullPointerException.class)
104 public void testWriteCurrentAttributesNullData() throws WriteFailedException {
105 customizer.writeCurrentAttributes(null, null, writeContext);
108 @Test(expected = NullPointerException.class)
109 public void testWriteCurrentAttributesBadData() throws WriteFailedException {
111 .writeCurrentAttributes(null, mock(RemoteMapping.class), writeContext);
115 public void testWriteCurrentAttributes() throws WriteFailedException, InterruptedException, ExecutionException {
116 //to simulate no mapping
117 when(mappingContext.read(Mockito.any())).thenReturn(com.google.common.base.Optional.absent());
119 customizer.writeCurrentAttributes(id, intf, writeContext);
121 verify(api, times(1)).lispAddDelRemoteMapping(mappingCaptor.capture());
123 LispAddDelRemoteMapping request = mappingCaptor.getValue();
125 assertNotNull(request);
126 assertEquals(1, request.isAdd);
127 assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(request.eid).getValue());
128 assertEquals(25, request.vni);
131 @Test(expected = UnsupportedOperationException.class)
132 public void testUpdateCurrentAttributes() throws WriteFailedException {
133 customizer.updateCurrentAttributes(null, null, null, writeContext);
136 @Test(expected = NullPointerException.class)
137 public void testDeleteCurrentAttributesNullData() throws WriteFailedException {
138 customizer.deleteCurrentAttributes(null, null, writeContext);
142 public void testDeleteCurrentAttributes() throws WriteFailedException, InterruptedException, ExecutionException {
143 customizer.deleteCurrentAttributes(id, intf, writeContext);
145 verify(api, times(1)).lispAddDelRemoteMapping(mappingCaptor.capture());
147 LispAddDelRemoteMapping request = mappingCaptor.getValue();
149 assertNotNull(request);
150 assertEquals(0, request.isAdd);
151 assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(request.eid).getValue());
152 assertEquals(25, request.vni);