868f851a36aa86faee379647bf8bdc27b0b98272
[honeycomb.git] / nsh / impl / src / test / java / io / fd / honeycomb / vppnsh / impl / oper / NshMapReaderCustomizerTest.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.vppnsh.impl.oper;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.mockito.Matchers.any;
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.verify;
23
24 import com.google.common.collect.Lists;
25 import io.fd.honeycomb.translate.read.ReadFailedException;
26 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
27 import io.fd.honeycomb.translate.vpp.util.NamingContext;
28 import io.fd.honeycomb.vpp.test.read.ListReaderCustomizerTest;
29 import io.fd.vpp.jvpp.VppBaseCallException;
30 import io.fd.vpp.jvpp.nsh.dto.NshMapDetails;
31 import io.fd.vpp.jvpp.nsh.dto.NshMapDetailsReplyDump;
32 import io.fd.vpp.jvpp.nsh.dto.NshMapDump;
33 import io.fd.vpp.jvpp.nsh.future.FutureJVppNsh;
34 import java.util.List;
35 import org.junit.Test;
36 import org.mockito.Mock;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.Swap;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.VxlanGpe;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.vpp.nsh.state.NshMaps;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.vpp.nsh.state.NshMapsBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.vpp.nsh.state.nsh.maps.NshMap;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.vpp.nsh.state.nsh.maps.NshMapBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.vpp.nsh.state.nsh.maps.NshMapKey;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45
46
47 public class NshMapReaderCustomizerTest extends
48     ListReaderCustomizerTest<NshMap, NshMapKey, NshMapBuilder> {
49
50     private static final String MAP_CTX_NAME = "nsh-map-instance";
51     private static final int MAP_INDEX_1 = 1;
52     private static final String MAP_NAME_1 = "map1";
53
54     private static final int MAP_INDEX_2 = 2;
55     private static final String MAP_NAME_2 = "map2";
56
57     private static final String INT_CTX_NAME = "interface-instance";
58     private static final int ITF_INDEX = 3;
59     private static final String ITF_NAME = "vxlanGpeTun3";
60
61     @Mock
62     protected FutureJVppNsh jvppNsh;
63
64     private NamingContext nshContext;
65
66     private NamingContext interfaceContext;
67
68     public NshMapReaderCustomizerTest() {
69         super(NshMap.class, NshMapsBuilder.class);
70     }
71
72     @Override
73     protected ReaderCustomizer<NshMap, NshMapBuilder> initCustomizer() {
74         return new NshMapReaderCustomizer(jvppNsh, nshContext, interfaceContext);
75     }
76
77     private static InstanceIdentifier<NshMap> getNshMapId(final String name) {
78         return InstanceIdentifier.create(NshMaps.class)
79             .child(NshMap.class, new NshMapKey(name));
80     }
81
82     @Override
83     public void setUp() throws VppBaseCallException {
84         nshContext = new NamingContext("nsh_map", MAP_CTX_NAME);
85         defineMapping(mappingContext, MAP_NAME_1, MAP_INDEX_1, MAP_CTX_NAME);
86         defineMapping(mappingContext, MAP_NAME_2, MAP_INDEX_2, MAP_CTX_NAME);
87
88         interfaceContext = new NamingContext("interface", INT_CTX_NAME);
89         defineMapping(mappingContext, ITF_NAME, ITF_INDEX, INT_CTX_NAME);
90
91         final NshMapDetailsReplyDump reply = new NshMapDetailsReplyDump();
92         final NshMapDetails nshMapDetails = new NshMapDetails();
93         nshMapDetails.nspNsi = (184<<8 | 255);
94         nshMapDetails.mappedNspNsi = (183<<8 | 254);
95         nshMapDetails.nshAction = 0;
96         nshMapDetails.swIfIndex = ITF_INDEX;
97         nshMapDetails.nextNode = 2;
98         reply.nshMapDetails = Lists.newArrayList(nshMapDetails);
99         doReturn(future(reply)).when(jvppNsh).nshMapDump(any(NshMapDump.class));
100     }
101
102     @Test
103     public void testreadCurrentAttributes() throws ReadFailedException {
104
105         NshMapBuilder builder = new NshMapBuilder();
106         getCustomizer().readCurrentAttributes(getNshMapId(MAP_NAME_1), builder, ctx);
107
108         assertEquals(184, builder.getNsp().intValue());
109         assertEquals(255, builder.getNsi().intValue());
110         assertEquals(183, builder.getMappedNsp().intValue());
111         assertEquals(254, builder.getMappedNsi().intValue());
112         assertEquals(Swap.class, builder.getNshAction());
113         assertEquals(VxlanGpe.class, builder.getEncapType());
114         assertEquals("vxlanGpeTun3", builder.getEncapIfName());
115
116         verify(jvppNsh).nshMapDump(any(NshMapDump.class));
117     }
118
119     @Test
120     public void testGetAllIds() throws ReadFailedException {
121         final NshMapDetailsReplyDump reply = new NshMapDetailsReplyDump();
122
123         final NshMapDetails nshMapDetails_1 = new NshMapDetails();
124         nshMapDetails_1.mapIndex = MAP_INDEX_1;
125         nshMapDetails_1.nspNsi = (184<<8 | 255);
126         nshMapDetails_1.mappedNspNsi = (183<<8 | 254);
127         nshMapDetails_1.swIfIndex = ITF_INDEX;
128         nshMapDetails_1.nextNode = 2;
129         reply.nshMapDetails = Lists.newArrayList(nshMapDetails_1);
130
131         final NshMapDetails nshMapDetails_2 = new NshMapDetails();
132         nshMapDetails_2.mapIndex = MAP_INDEX_2;
133         nshMapDetails_2.nspNsi = (84<<8 | 255);
134         nshMapDetails_2.mappedNspNsi = (83<<8 | 254);
135         nshMapDetails_2.swIfIndex = ITF_INDEX;
136         nshMapDetails_2.nextNode = 1;
137         reply.nshMapDetails = Lists.newArrayList(nshMapDetails_2);
138
139         doReturn(future(reply)).when(jvppNsh).nshMapDump(any(NshMapDump.class));
140
141         final List<NshMapKey> allIds = getCustomizer().getAllIds(getNshMapId(MAP_NAME_1), ctx);
142
143         assertEquals(reply.nshMapDetails.size(), allIds.size());
144
145     }
146 }