fb97dce5db0af71edbe48c1219f193f3ae264cb7
[honeycomb.git] / lisp / lisp2vpp / src / test / java / io / fd / honeycomb / lisp / translate / read / InterfaceCustomizerTest.java
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.lisp.translate.read;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.mockito.Mockito.when;
21
22 import com.google.common.collect.ImmutableList;
23 import io.fd.honeycomb.translate.read.ReadFailedException;
24 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
25 import io.fd.honeycomb.translate.vpp.util.NamingContext;
26 import io.fd.honeycomb.vpp.test.read.ListReaderCustomizerTest;
27 import java.util.List;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.Mockito;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.LocatorSets;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.LocatorSet;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.LocatorSetBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.LocatorSetKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.locator.set.Interface;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.locator.set.InterfaceBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.locator.set.InterfaceKey;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import io.fd.vpp.jvpp.core.dto.LispLocatorDetails;
40 import io.fd.vpp.jvpp.core.dto.LispLocatorDetailsReplyDump;
41
42 public class InterfaceCustomizerTest
43         extends ListReaderCustomizerTest<Interface, InterfaceKey, InterfaceBuilder> {
44
45     public InterfaceCustomizerTest() {
46         super(Interface.class, LocatorSetBuilder.class);
47     }
48
49     private InstanceIdentifier<Interface> validId;
50
51     @Before
52     public void init() {
53         validId = InstanceIdentifier.create(LocatorSets.class).child(LocatorSet.class, new LocatorSetKey("loc-set-1"))
54                 .child(Interface.class, new InterfaceKey("interface-1"));
55
56         //mappings
57         defineMappings();
58         //dump data
59         defineDumpData();
60     }
61
62     private void defineDumpData() {
63         final LispLocatorDetailsReplyDump dump = new LispLocatorDetailsReplyDump();
64
65         final LispLocatorDetails detail1 = new LispLocatorDetails();
66         detail1.swIfIndex = 1;
67         detail1.ipAddress = new byte[]{-64, -88, 2, 1};
68         detail1.isIpv6 = 0;
69         detail1.local = 0;
70         detail1.priority = 1;
71         detail1.weight = 2;
72
73         final LispLocatorDetails detail2 = new LispLocatorDetails();
74         detail2.swIfIndex = 2;
75         detail2.ipAddress = new byte[]{-64, -88, 2, 2};
76         detail2.isIpv6 = 0;
77         detail2.local = 0;
78         detail2.priority = 2;
79         detail2.weight = 3;
80
81         dump.lispLocatorDetails = ImmutableList.of(detail1, detail2);
82
83         when(api.lispLocatorDump(Mockito.any())).thenReturn(future(dump));
84     }
85
86     private void defineMappings() {
87         defineMapping(mappingContext, "interface-1", 1, "interface-context");
88         defineMapping(mappingContext, "interface-2", 2, "interface-context");
89         defineMapping(mappingContext, "loc-set-1", 3, "locator-set-context");
90     }
91
92     @Test
93     public void testGetAllIds() throws ReadFailedException {
94
95         final List<InterfaceKey> keys = getCustomizer().getAllIds(validId, ctx);
96
97         assertEquals(2, keys.size());
98         assertEquals("interface-1", keys.get(0).getInterfaceRef());
99         assertEquals("interface-2", keys.get(1).getInterfaceRef());
100     }
101
102     @Test
103     public void testReadCurrentAttributes() throws ReadFailedException {
104         InterfaceBuilder builder = new InterfaceBuilder();
105         getCustomizer().readCurrentAttributes(validId, builder, ctx);
106
107         final Interface iface = builder.build();
108         assertEquals("interface-1", iface.getInterfaceRef());
109         assertEquals("interface-1", iface.getKey().getInterfaceRef());
110
111     }
112
113     @Override
114     protected ReaderCustomizer<Interface, InterfaceBuilder> initCustomizer() {
115         return new InterfaceCustomizer(api, new NamingContext("interface", "interface-context"),
116                 new NamingContext("loc-set", "locator-set-context"));
117     }
118 }