30c168e727b1370195069a63ebcd380d083bee58
[honeycomb.git] / lisp / lisp2vpp / src / test / java / io / fd / honeycomb / lisp / translate / write / RemoteMappingCustomizerTest.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.Mockito.mock;
22 import static org.mockito.Mockito.times;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25
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;
54
55
56 public class RemoteMappingCustomizerTest extends WriterCustomizerTest {
57
58     @Captor
59     private ArgumentCaptor<LispAddDelRemoteMapping> mappingCaptor;
60
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;
68
69     @Override
70     public void setUp() {
71         final Eid eid = new EidBuilder().setAddress(
72                 new Ipv4Builder().setIpv4(
73                         new Ipv4Address("192.168.2.1"))
74                         .build())
75                 .build();
76
77         mappingId = new MappingId("REMOTE");
78         final RemoteMappingKey key = new RemoteMappingKey(mappingId);
79         remoteMappingContext = new EidMappingContext("remote");
80
81
82         intf = new RemoteMappingBuilder()
83                 .setEid(
84                         eid)
85                 .setLocatorList(new NegativeMappingBuilder().setMapReplyAction(MapReplyAction.Drop).build())
86                 .build();
87
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();
92
93         fakeReply = new LispAddDelRemoteMappingReply();
94         completeFuture = new CompletableFuture<>();
95         completeFuture.complete(fakeReply);
96         customizer = new RemoteMappingCustomizer(api, remoteMappingContext);
97
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()));
101     }
102
103     @Test(expected = NullPointerException.class)
104     public void testWriteCurrentAttributesNullData() throws WriteFailedException {
105         customizer.writeCurrentAttributes(null, null, writeContext);
106     }
107
108     @Test(expected = NullPointerException.class)
109     public void testWriteCurrentAttributesBadData() throws WriteFailedException {
110         customizer
111                 .writeCurrentAttributes(null, mock(RemoteMapping.class), writeContext);
112     }
113
114     @Test
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());
118
119         customizer.writeCurrentAttributes(id, intf, writeContext);
120
121         verify(api, times(1)).lispAddDelRemoteMapping(mappingCaptor.capture());
122
123         LispAddDelRemoteMapping request = mappingCaptor.getValue();
124
125         assertNotNull(request);
126         assertEquals(1, request.isAdd);
127         assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(request.eid).getValue());
128         assertEquals(25, request.vni);
129     }
130
131     @Test(expected = UnsupportedOperationException.class)
132     public void testUpdateCurrentAttributes() throws WriteFailedException {
133         customizer.updateCurrentAttributes(null, null, null, writeContext);
134     }
135
136     @Test(expected = NullPointerException.class)
137     public void testDeleteCurrentAttributesNullData() throws WriteFailedException {
138         customizer.deleteCurrentAttributes(null, null, writeContext);
139     }
140
141     @Test
142     public void testDeleteCurrentAttributes() throws WriteFailedException, InterruptedException, ExecutionException {
143         customizer.deleteCurrentAttributes(id, intf, writeContext);
144
145         verify(api, times(1)).lispAddDelRemoteMapping(mappingCaptor.capture());
146
147         LispAddDelRemoteMapping request = mappingCaptor.getValue();
148
149         assertNotNull(request);
150         assertEquals(0, request.isAdd);
151         assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(request.eid).getValue());
152         assertEquals(25, request.vni);
153     }
154
155 }