HONEYCOMB-58 - Routing Api
[honeycomb.git] / nsh / impl / src / test / java / io / fd / honeycomb / vppnsh / impl / config / NshMapWriterCustomizerTest.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.config;
18
19 import static org.junit.Assert.assertTrue;
20 import static org.junit.Assert.fail;
21 import static org.mockito.Matchers.any;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.verify;
24
25 import io.fd.honeycomb.translate.vpp.util.NamingContext;
26 import io.fd.honeycomb.translate.write.WriteFailedException;
27 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
28 import io.fd.vpp.jvpp.VppBaseCallException;
29 import io.fd.vpp.jvpp.nsh.dto.NshAddDelMap;
30 import io.fd.vpp.jvpp.nsh.dto.NshAddDelMapReply;
31 import io.fd.vpp.jvpp.nsh.future.FutureJVppNsh;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.Swap;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.VxlanGpe;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.vpp.nsh.NshMaps;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.vpp.nsh.nsh.maps.NshMap;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.vpp.nsh.nsh.maps.NshMapBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.vpp.nsh.nsh.maps.NshMapKey;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41
42 public class NshMapWriterCustomizerTest extends WriterCustomizerTest {
43
44     private static final String MAP_CTX_NAME = "nsh-map-instance";
45     private static final int MAP_INDEX = 1;
46     private static final String MAP_NAME = "map";
47
48     private static final String INT_CTX_NAME = "interface-instance";
49     private static final int ITF_INDEX = 3;
50     private static final String ITF_NAME = "vxlanGpeTun3";
51
52     @Mock
53     protected FutureJVppNsh jvppNsh;
54
55     private NamingContext nshContext;
56
57     private NamingContext interfaceContext;
58
59     private NshMapWriterCustomizer customizer;
60
61     @Override
62     public void setUp() throws Exception {
63         nshContext = new NamingContext("nsh_map", MAP_CTX_NAME);
64         defineMapping(mappingContext, MAP_NAME, MAP_INDEX, MAP_CTX_NAME);
65         interfaceContext = new NamingContext("interface", INT_CTX_NAME);
66         defineMapping(mappingContext, ITF_NAME, ITF_INDEX, INT_CTX_NAME);
67
68         customizer = new NshMapWriterCustomizer(jvppNsh, nshContext, interfaceContext);
69     }
70
71     private static NshMap generateNshMap(final String name) {
72         final NshMapBuilder builder = new NshMapBuilder();
73         builder.setName(name);
74         builder.setKey(new NshMapKey(name));
75         builder.setNsp(184L);
76         builder.setNsi((short) 255);
77         builder.setMappedNsp(183L);
78         builder.setMappedNsi((short) 254);
79         builder.setNshAction(Swap.class);
80         builder.setEncapType(VxlanGpe.class);
81         builder.setEncapIfName("vxlanGpeTun3");
82
83         return builder.build();
84     }
85
86     private static InstanceIdentifier<NshMap> getNshMapId(final String name) {
87         return InstanceIdentifier.create(NshMaps.class)
88                 .child(NshMap.class, new NshMapKey(name));
89     }
90
91     private void whenNshAddDelMapThenSuccess() {
92         final NshAddDelMapReply reply = new NshAddDelMapReply();
93         reply.mapIndex = MAP_INDEX;
94         doReturn(future(reply)).when(jvppNsh).nshAddDelMap(any(NshAddDelMap.class));
95     }
96
97     private void whenNshAddDelMapThenFailure() {
98         doReturn(failedFuture()).when(jvppNsh).nshAddDelMap(any(NshAddDelMap.class));
99     }
100
101     private static NshAddDelMap generateNshAddDelMap(final byte isAdd) {
102         final NshAddDelMap request = new NshAddDelMap();
103         request.isAdd = isAdd;
104         request.nspNsi = 184<<8 | 255;
105         request.mappedNspNsi = 183<<8 | 254;
106         request.nshAction = 0;
107         request.swIfIndex = ITF_INDEX;
108         request.nextNode = 2;
109
110         return request;
111     }
112
113     @Test
114     public void testCreate() throws Exception {
115         final NshMap nshMap = generateNshMap(MAP_NAME);
116         final InstanceIdentifier<NshMap> id = getNshMapId(MAP_NAME);
117
118         whenNshAddDelMapThenSuccess();
119
120         customizer.writeCurrentAttributes(id, nshMap, writeContext);
121
122         verify(jvppNsh).nshAddDelMap(generateNshAddDelMap((byte) 1));
123
124     }
125
126     @Test
127     public void testCreateFailed() throws Exception {
128         final NshMap nshMap = generateNshMap(MAP_NAME);
129         final InstanceIdentifier<NshMap> id = getNshMapId(MAP_NAME);
130
131         whenNshAddDelMapThenFailure();
132
133         try {
134             customizer.writeCurrentAttributes(id, nshMap, writeContext);
135         } catch (WriteFailedException e) {
136             assertTrue(e.getCause() instanceof VppBaseCallException);
137             verify(jvppNsh).nshAddDelMap(generateNshAddDelMap((byte) 1));
138
139             return;
140         }
141         fail("WriteFailedException.CreateFailedException was expected");
142     }
143
144     @Test
145     public void testDelete() throws Exception {
146         final NshMap nshMap = generateNshMap(MAP_NAME);
147         final InstanceIdentifier<NshMap> id = getNshMapId(MAP_NAME);
148
149         whenNshAddDelMapThenSuccess();
150
151         customizer.deleteCurrentAttributes(id, nshMap, writeContext);
152
153         verify(jvppNsh).nshAddDelMap(generateNshAddDelMap((byte) 0));
154     }
155
156     @Test
157     public void testDeleteFailed() throws Exception {
158         final NshMap nshMap = generateNshMap(MAP_NAME);
159         final InstanceIdentifier<NshMap> id = getNshMapId(MAP_NAME);
160
161         whenNshAddDelMapThenFailure();
162
163         try {
164             customizer.deleteCurrentAttributes(id, nshMap, writeContext);
165         } catch (WriteFailedException e) {
166             assertTrue(e.getCause() instanceof VppBaseCallException);
167             verify(jvppNsh).nshAddDelMap(generateNshAddDelMap((byte) 0));
168             return;
169         }
170         fail("WriteFailedException.DeleteFailedException was expected");
171
172         customizer.deleteCurrentAttributes(id, nshMap, writeContext);
173     }
174
175     @Test(expected = UnsupportedOperationException.class)
176     public void testUpdate() throws Exception {
177         final NshMap nshMapBefore = generateNshMap(MAP_NAME);
178         final InstanceIdentifier<NshMap> id = getNshMapId(MAP_NAME);
179         customizer.updateCurrentAttributes(id, nshMapBefore, new NshMapBuilder().build(), writeContext);
180     }
181 }