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.vppnsh.impl.config;
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;
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;
42 public class NshMapWriterCustomizerTest extends WriterCustomizerTest {
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";
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";
53 protected FutureJVppNsh jvppNsh;
55 private NamingContext nshContext;
57 private NamingContext interfaceContext;
59 private NshMapWriterCustomizer customizer;
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);
68 customizer = new NshMapWriterCustomizer(jvppNsh, nshContext, interfaceContext);
71 private static NshMap generateNshMap(final String name) {
72 final NshMapBuilder builder = new NshMapBuilder();
73 builder.setName(name);
74 builder.setKey(new NshMapKey(name));
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");
83 return builder.build();
86 private static InstanceIdentifier<NshMap> getNshMapId(final String name) {
87 return InstanceIdentifier.create(NshMaps.class)
88 .child(NshMap.class, new NshMapKey(name));
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));
97 private void whenNshAddDelMapThenFailure() {
98 doReturn(failedFuture()).when(jvppNsh).nshAddDelMap(any(NshAddDelMap.class));
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;
114 public void testCreate() throws Exception {
115 final NshMap nshMap = generateNshMap(MAP_NAME);
116 final InstanceIdentifier<NshMap> id = getNshMapId(MAP_NAME);
118 whenNshAddDelMapThenSuccess();
120 customizer.writeCurrentAttributes(id, nshMap, writeContext);
122 verify(jvppNsh).nshAddDelMap(generateNshAddDelMap((byte) 1));
127 public void testCreateFailed() throws Exception {
128 final NshMap nshMap = generateNshMap(MAP_NAME);
129 final InstanceIdentifier<NshMap> id = getNshMapId(MAP_NAME);
131 whenNshAddDelMapThenFailure();
134 customizer.writeCurrentAttributes(id, nshMap, writeContext);
135 } catch (WriteFailedException e) {
136 assertTrue(e.getCause() instanceof VppBaseCallException);
137 verify(jvppNsh).nshAddDelMap(generateNshAddDelMap((byte) 1));
141 fail("WriteFailedException.CreateFailedException was expected");
145 public void testDelete() throws Exception {
146 final NshMap nshMap = generateNshMap(MAP_NAME);
147 final InstanceIdentifier<NshMap> id = getNshMapId(MAP_NAME);
149 whenNshAddDelMapThenSuccess();
151 customizer.deleteCurrentAttributes(id, nshMap, writeContext);
153 verify(jvppNsh).nshAddDelMap(generateNshAddDelMap((byte) 0));
157 public void testDeleteFailed() throws Exception {
158 final NshMap nshMap = generateNshMap(MAP_NAME);
159 final InstanceIdentifier<NshMap> id = getNshMapId(MAP_NAME);
161 whenNshAddDelMapThenFailure();
164 customizer.deleteCurrentAttributes(id, nshMap, writeContext);
165 } catch (WriteFailedException e) {
166 assertTrue(e.getCause() instanceof VppBaseCallException);
167 verify(jvppNsh).nshAddDelMap(generateNshAddDelMap((byte) 0));
170 fail("WriteFailedException.DeleteFailedException was expected");
172 customizer.deleteCurrentAttributes(id, nshMap, writeContext);
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);