7026ee2a4d7176df903e20baa68c8f9dcd59880b
[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.v3po.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.base.Optional;
25 import com.google.common.io.BaseEncoding;
26 import io.fd.honeycomb.v3po.translate.MappingContext;
27 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
28 import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils;
29 import io.fd.honeycomb.v3po.translate.write.WriteContext;
30 import io.fd.honeycomb.v3po.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.dto.IpNeighborAddDel;
51 import org.openvpp.jvpp.dto.IpNeighborAddDelReply;
52 import org.openvpp.jvpp.future.FutureJVpp;
53
54 public class Ipv4NeighbourCustomizerTest {
55
56     @Mock
57     private FutureJVpp jvpp;
58
59     @Mock
60     private WriteContext context;
61
62     @Mock
63     private MappingContext mappingContext;
64
65     @Mock
66     private Mapping mapping;
67
68     private ArgumentCaptor<IpNeighborAddDel> requestCaptor;
69     private Ipv4NeighbourCustomizer customizer;
70     private NamingContext namingContext;
71
72
73     @Before
74     public void init() {
75         MockitoAnnotations.initMocks(this);
76
77         namingContext = new NamingContext("prefix", "instance");
78         namingContext.addName(5, "parent", mappingContext);
79
80         customizer = new Ipv4NeighbourCustomizer(jvpp,namingContext);
81
82         requestCaptor = ArgumentCaptor.forClass(IpNeighborAddDel.class);
83
84         CompletableFuture<IpNeighborAddDelReply> future = new CompletableFuture<>();
85         future.complete(new IpNeighborAddDelReply());
86
87         when(context.getMappingContext()).thenReturn(mappingContext);
88         when(mapping.getIndex()).thenReturn(5);
89         when(mapping.getName()).thenReturn("parent");
90         when(mappingContext.read(Mockito.any())).thenReturn(Optional.fromNullable(mapping));
91         when(jvpp.ipNeighborAddDel(Mockito.any(IpNeighborAddDel.class))).thenReturn(future);
92     }
93
94     @Test
95     public void testWriteCurrentAttributes() throws WriteFailedException {
96
97         InterfaceKey intfKey = new InterfaceKey("parent");
98
99         InstanceIdentifier<Neighbor> id = InstanceIdentifier.builder(Interfaces.class).child(Interface.class, intfKey)
100                 .augmentation(Interface1.class).child(Ipv4.class).child(Neighbor.class).build();
101
102         Ipv4AddressNoZone noZoneIp = new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"));
103         PhysAddress mac = new PhysAddress("aa:bb:cc:ee:11:22");
104
105         Neighbor data = new NeighborBuilder().setIp(noZoneIp).setLinkLayerAddress(mac).build();
106
107         customizer.writeCurrentAttributes(id, data, context);
108
109         verify(jvpp, times(1)).ipNeighborAddDel(requestCaptor.capture());
110
111         IpNeighborAddDel request = requestCaptor.getValue();
112
113         assertEquals(0, request.isIpv6);
114         assertEquals(1, request.isAdd);
115         assertEquals(1, request.isStatic);
116         assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(request.dstAddress).getValue());
117         assertEquals("aabbccee1122", BaseEncoding.base16().lowerCase().encode(request.macAddress));
118         assertEquals(5, request.swIfIndex);
119     }
120
121     @Test
122     public void testDeleteCurrentAttributes() throws WriteFailedException {
123         InterfaceKey intfKey = new InterfaceKey("parent");
124
125         InstanceIdentifier<Neighbor> id = InstanceIdentifier.builder(Interfaces.class).child(Interface.class, intfKey)
126                 .augmentation(Interface1.class).child(Ipv4.class).child(Neighbor.class).build();
127
128         Ipv4AddressNoZone noZoneIp = new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"));
129         PhysAddress mac = new PhysAddress("aa:bb:cc:ee:11:22");
130
131         Neighbor data = new NeighborBuilder().setIp(noZoneIp).setLinkLayerAddress(mac).build();
132
133         customizer.deleteCurrentAttributes(id, data, context);
134
135         verify(jvpp, times(1)).ipNeighborAddDel(requestCaptor.capture());
136
137         IpNeighborAddDel request = requestCaptor.getValue();
138
139         assertEquals(0, request.isIpv6);
140         assertEquals(0, request.isAdd);
141         assertEquals(1, request.isStatic);
142         assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(request.dstAddress).getValue());
143         assertEquals("aabbccee1122", BaseEncoding.base16().lowerCase().encode(request.macAddress));
144         assertEquals(5, request.swIfIndex);
145     }
146
147 }