2 * Copyright (c) 2016 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.translate.v3po.interfacesstate.ip;
19 import static org.hamcrest.MatcherAssert.assertThat;
20 import static org.hamcrest.Matchers.containsInAnyOrder;
21 import static org.hamcrest.Matchers.hasSize;
22 import static org.mockito.Matchers.any;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
27 import io.fd.honeycomb.translate.read.ReadFailedException;
28 import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
29 import io.fd.honeycomb.translate.vpp.util.NamingContext;
30 import io.fd.honeycomb.vpp.test.read.ListReaderCustomizerTest;
31 import java.util.List;
32 import org.junit.Test;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.SubinterfaceStateAugmentation;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.SubInterfaces;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterface;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterfaceKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.Ipv4;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.Ipv4Builder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.Address;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.AddressBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.AddressKey;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.address.subnet.PrefixLengthBuilder;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import io.fd.vpp.jvpp.core.dto.IpAddressDetails;
49 import io.fd.vpp.jvpp.core.dto.IpAddressDetailsReplyDump;
51 public class SubInterfaceIpv4AddressCustomizerTest extends ListReaderCustomizerTest<Address, AddressKey, AddressBuilder> {
53 private static final String IFC_CTX_NAME = "ifc-test-instance";
54 private static final String IF_NAME = "local0";
55 private static final int IF_INDEX = 1;
56 private static final String SUB_IF_NAME = "local0.1";
57 private static final long SUB_IF_ID = 1;
58 private static final int SUB_IF_INDEX = 11;
59 private static final InstanceIdentifier<Ipv4> IP4_IID =
60 InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey(IF_NAME))
61 .augmentation(SubinterfaceStateAugmentation.class)
62 .child(SubInterfaces.class).child(SubInterface.class, new SubInterfaceKey(SUB_IF_ID))
64 private static final Ipv4AddressNoZone IP1 = new Ipv4AddressNoZone("10.1.1.1");
65 private static final Ipv4AddressNoZone IP2 = new Ipv4AddressNoZone("10.1.1.2");
66 private static final short PREFIX_LENGTH = 16;
68 private NamingContext interfaceContext;
70 public SubInterfaceIpv4AddressCustomizerTest() {
71 super(Address.class, Ipv4Builder.class);
75 protected void setUp() throws Exception {
76 interfaceContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME);
77 defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_CTX_NAME);
78 defineMapping(mappingContext, SUB_IF_NAME, SUB_IF_INDEX, IFC_CTX_NAME);
82 protected ListReaderCustomizer<Address, AddressKey, AddressBuilder> initCustomizer() {
83 return new SubInterfaceIpv4AddressCustomizer(api, interfaceContext);
86 private static InstanceIdentifier<Address> getId() {
87 return IP4_IID.child(Address.class);
90 private static InstanceIdentifier<Address> getId(final Ipv4AddressNoZone ip) {
91 return IP4_IID.child(Address.class, new AddressKey(ip));
95 public void testRead() throws ReadFailedException {
96 final AddressBuilder builder = mock(AddressBuilder.class);
97 when(api.ipAddressDump(any())).thenReturn(future(dump()));
98 getCustomizer().readCurrentAttributes(getId(IP2), builder, ctx);
99 verify(builder).setIp(IP2);
100 verify(builder).setSubnet(new PrefixLengthBuilder().setPrefixLength(PREFIX_LENGTH).build());
103 @Test(expected = ReadFailedException.class)
104 public void testReadFailed() throws ReadFailedException {
105 when(api.ipAddressDump(any())).thenReturn(failedFuture());
106 getCustomizer().readCurrentAttributes(getId(IP1), mock(AddressBuilder.class), ctx);
110 public void testGetAllIds() throws ReadFailedException {
111 when(api.ipAddressDump(any())).thenReturn(future(dump()));
112 final List<AddressKey> allIds = getCustomizer().getAllIds(getId(), ctx);
113 assertThat(allIds, hasSize(2));
114 assertThat(allIds, containsInAnyOrder(new AddressKey(IP1), new AddressKey(IP2)));
117 @Test(expected = ReadFailedException.class)
118 public void testGetAllIdsFailed() throws ReadFailedException {
119 when(api.ipAddressDump(any())).thenReturn(failedFuture());
120 getCustomizer().getAllIds(getId(), ctx);
123 private IpAddressDetailsReplyDump dump() {
124 final IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump();
126 final IpAddressDetails details1 = new IpAddressDetails();
127 details1.ip = new byte[] {1, 1, 1, 10};
128 details1.prefixLength = (byte) PREFIX_LENGTH;
129 reply.ipAddressDetails.add(details1);
131 final IpAddressDetails details2 = new IpAddressDetails();
132 details2.ip = new byte[] {2, 1, 1, 10};
133 details2.prefixLength = (byte) PREFIX_LENGTH;
134 reply.ipAddressDetails.add(details2);