d7f9c0c1c4bce5ba2a5953dce8dd87a27dce2a36
[hc2vpp.git] /
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.translate.v3po.interfaces.ip;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.mockito.Mockito.times;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23
24 import com.google.common.io.BaseEncoding;
25 import io.fd.honeycomb.translate.MappingContext;
26 import io.fd.honeycomb.translate.v3po.test.ContextTestUtils;
27 import io.fd.honeycomb.translate.v3po.util.NamingContext;
28 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
29 import io.fd.honeycomb.translate.write.WriteContext;
30 import io.fd.honeycomb.translate.write.WriteFailedException;
31 import java.util.concurrent.CompletableFuture;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.ArgumentCaptor;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.mockito.MockitoAnnotations;
38 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface1;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.Ipv4;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.Neighbor;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.NeighborBuilder;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.openvpp.jvpp.core.dto.IpNeighborAddDel;
51 import org.openvpp.jvpp.core.dto.IpNeighborAddDelReply;
52 import org.openvpp.jvpp.core.future.FutureJVppCore;
53
54 public class Ipv4NeighbourCustomizerTest {
55
56     private static final String IFC_CTX_NAME = "ifc-test-instance";
57     private static final String IFACE_NAME = "parent";
58     private static final int IFACE_ID = 5;
59
60     @Mock
61     private FutureJVppCore jvpp;
62
63     @Mock
64     private WriteContext context;
65
66     @Mock
67     private MappingContext mappingContext;
68
69     @Mock
70     private Mapping mapping;
71
72     private ArgumentCaptor<IpNeighborAddDel> requestCaptor;
73     private Ipv4NeighbourCustomizer customizer;
74     private NamingContext namingContext;
75
76
77     @Before
78     public void init() {
79         MockitoAnnotations.initMocks(this);
80         when(context.getMappingContext()).thenReturn(mappingContext);
81
82         namingContext = new NamingContext("prefix", IFC_CTX_NAME);
83         ContextTestUtils.mockMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_CTX_NAME);
84
85         customizer = new Ipv4NeighbourCustomizer(jvpp,namingContext);
86
87         requestCaptor = ArgumentCaptor.forClass(IpNeighborAddDel.class);
88         CompletableFuture<IpNeighborAddDelReply> future = new CompletableFuture<>();
89         future.complete(new IpNeighborAddDelReply());
90         when(jvpp.ipNeighborAddDel(Mockito.any(IpNeighborAddDel.class))).thenReturn(future);
91     }
92
93     @Test
94     public void testWriteCurrentAttributes() throws WriteFailedException {
95
96         InterfaceKey intfKey = new InterfaceKey(IFACE_NAME);
97
98         InstanceIdentifier<Neighbor> id = InstanceIdentifier.builder(Interfaces.class).child(Interface.class, intfKey)
99                 .augmentation(Interface1.class).child(Ipv4.class).child(Neighbor.class).build();
100
101         Ipv4AddressNoZone noZoneIp = new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"));
102         PhysAddress mac = new PhysAddress("aa:bb:cc:ee:11:22");
103
104         Neighbor data = new NeighborBuilder().setIp(noZoneIp).setLinkLayerAddress(mac).build();
105
106         customizer.writeCurrentAttributes(id, data, context);
107
108         verify(jvpp, times(1)).ipNeighborAddDel(requestCaptor.capture());
109
110         IpNeighborAddDel request = requestCaptor.getValue();
111
112         assertEquals(0, request.isIpv6);
113         assertEquals(1, request.isAdd);
114         assertEquals(1, request.isStatic);
115         assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(request.dstAddress).getValue());
116         assertEquals("aabbccee1122", BaseEncoding.base16().lowerCase().encode(request.macAddress));
117         assertEquals(5, request.swIfIndex);
118     }
119
120     @Test
121     public void testDeleteCurrentAttributes() throws WriteFailedException {
122         InterfaceKey intfKey = new InterfaceKey(IFACE_NAME);
123
124         InstanceIdentifier<Neighbor> id = InstanceIdentifier.builder(Interfaces.class).child(Interface.class, intfKey)
125                 .augmentation(Interface1.class).child(Ipv4.class).child(Neighbor.class).build();
126
127         Ipv4AddressNoZone noZoneIp = new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"));
128         PhysAddress mac = new PhysAddress("aa:bb:cc:ee:11:22");
129
130         Neighbor data = new NeighborBuilder().setIp(noZoneIp).setLinkLayerAddress(mac).build();
131
132         customizer.deleteCurrentAttributes(id, data, context);
133
134         verify(jvpp, times(1)).ipNeighborAddDel(requestCaptor.capture());
135
136         IpNeighborAddDel request = requestCaptor.getValue();
137
138         assertEquals(0, request.isIpv6);
139         assertEquals(0, request.isAdd);
140         assertEquals(1, request.isStatic);
141         assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(request.dstAddress).getValue());
142         assertEquals("aabbccee1122", BaseEncoding.base16().lowerCase().encode(request.macAddress));
143         assertEquals(5, request.swIfIndex);
144     }
145
146 }