5811907a4fdce6a7caf60d5c04cdfe793d5db182
[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.mock;
21 import static org.mockito.Mockito.times;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24
25 import com.google.common.base.Optional;
26 import com.google.common.io.BaseEncoding;
27 import io.fd.honeycomb.v3po.translate.MappingContext;
28 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
29 import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils;
30 import io.fd.honeycomb.v3po.translate.write.WriteContext;
31 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.concurrent.CompletableFuture;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.ArgumentCaptor;
38 import org.mockito.Mock;
39 import org.mockito.Mockito;
40 import org.mockito.MockitoAnnotations;
41 import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
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.inet.types.rev130715.Ipv4AddressNoZone;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface1;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.Ipv4;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.Ipv4Builder;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.Neighbor;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.NeighborBuilder;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
54 import org.openvpp.jvpp.dto.IpNeighborAddDel;
55 import org.openvpp.jvpp.dto.IpNeighborAddDelReply;
56 import org.openvpp.jvpp.future.FutureJVpp;
57
58 public class Ipv4NeighbourCustomizerTest {
59
60     @Mock
61     private FutureJVpp 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
81         namingContext = new NamingContext("prefix", "instance");
82         namingContext.addName(5, "parent", mappingContext);
83
84         customizer = new Ipv4NeighbourCustomizer(jvpp,namingContext);
85
86         requestCaptor = ArgumentCaptor.forClass(IpNeighborAddDel.class);
87
88         CompletableFuture<IpNeighborAddDelReply> future = new CompletableFuture<>();
89         future.complete(new IpNeighborAddDelReply());
90
91         when(context.getMappingContext()).thenReturn(mappingContext);
92         when(mapping.getIndex()).thenReturn(5);
93         when(mapping.getName()).thenReturn("parent");
94         when(mappingContext.read(Mockito.any())).thenReturn(Optional.fromNullable(mapping));
95         when(jvpp.ipNeighborAddDel(Mockito.any(IpNeighborAddDel.class))).thenReturn(future);
96     }
97
98     @Test
99     public void testWriteCurrentAttributes() throws WriteFailedException {
100
101         InterfaceKey intfKey = new InterfaceKey("parent");
102
103         InstanceIdentifier<Neighbor> id = InstanceIdentifier.builder(Interfaces.class).child(Interface.class, intfKey)
104                 .augmentation(Interface1.class).child(Ipv4.class).child(Neighbor.class).build();
105
106         Ipv4AddressNoZone noZoneIp = new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"));
107         PhysAddress mac = new PhysAddress("aa:bb:cc:ee:11:22");
108
109         Neighbor data = new NeighborBuilder().setIp(noZoneIp).setLinkLayerAddress(mac).build();
110
111         customizer.writeCurrentAttributes(id, data, context);
112
113         verify(jvpp, times(1)).ipNeighborAddDel(requestCaptor.capture());
114
115         IpNeighborAddDel request = requestCaptor.getValue();
116
117         assertEquals(0, request.isIpv6);
118         assertEquals(1, request.isAdd);
119         assertEquals(1, request.isStatic);
120         assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(request.dstAddress).getValue());
121         assertEquals("aabbccee1122", BaseEncoding.base16().lowerCase().encode(request.macAddress));
122         assertEquals(5, request.swIfIndex);
123     }
124
125     @Test
126     public void testDeleteCurrentAttributes() throws WriteFailedException {
127         InterfaceKey intfKey = new InterfaceKey("parent");
128
129         InstanceIdentifier<Neighbor> id = InstanceIdentifier.builder(Interfaces.class).child(Interface.class, intfKey)
130                 .augmentation(Interface1.class).child(Ipv4.class).child(Neighbor.class).build();
131
132         Ipv4AddressNoZone noZoneIp = new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"));
133         PhysAddress mac = new PhysAddress("aa:bb:cc:ee:11:22");
134
135         Neighbor data = new NeighborBuilder().setIp(noZoneIp).setLinkLayerAddress(mac).build();
136
137         customizer.deleteCurrentAttributes(id, data, context);
138
139         verify(jvpp, times(1)).ipNeighborAddDel(requestCaptor.capture());
140
141         IpNeighborAddDel request = requestCaptor.getValue();
142
143         assertEquals(0, request.isIpv6);
144         assertEquals(0, request.isAdd);
145         assertEquals(1, request.isStatic);
146         assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(request.dstAddress).getValue());
147         assertEquals("aabbccee1122", BaseEncoding.base16().lowerCase().encode(request.macAddress));
148         assertEquals(5, request.swIfIndex);
149     }
150
151     @Test
152     public void testExtract() {
153         Neighbor data = new NeighborBuilder().build();
154         Ipv4 parentData = new Ipv4Builder().setNeighbor(Collections.singletonList(data)).build();
155
156         Optional<List<Neighbor>> optionalData = new Ipv4NeighbourCustomizer(mock(FutureJVpp.class), null).extract(null,
157                 parentData);
158         assertEquals(true, optionalData.isPresent());
159         assertEquals(true, optionalData.get().contains(data));
160     }
161
162 }