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.NshAddDelEntry;
30 import io.fd.vpp.jvpp.nsh.dto.NshAddDelEntryReply;
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.Ethernet;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.MdType1;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.NshMdType1Augment;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.NshMdType1AugmentBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.vpp.nsh.NshEntries;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.vpp.nsh.nsh.entries.NshEntry;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.vpp.nsh.nsh.entries.NshEntryBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.nsh.rev160624.vpp.nsh.nsh.entries.NshEntryKey;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 public class NshEntryWriterCustomizerTest extends WriterCustomizerTest {
46 private static final String ENTRY_CTX_NAME = "nsh-entry-instance";
47 private static final int ENTRY_INDEX = 1;
48 private static final String ENTRY_NAME = "entry1";
51 protected FutureJVppNsh jvppNsh;
53 private NamingContext nshContext;
55 private NshEntryWriterCustomizer customizer;
58 public void setUp() throws Exception {
59 nshContext = new NamingContext("nsh_entry", ENTRY_CTX_NAME);
60 defineMapping(mappingContext, ENTRY_NAME, ENTRY_INDEX, ENTRY_CTX_NAME);
62 customizer = new NshEntryWriterCustomizer(jvppNsh, nshContext);
65 private static NshEntry generateNshEntry(final String name) {
66 final NshEntryBuilder builder = new NshEntryBuilder();
67 builder.setName(name);
68 builder.setKey(new NshEntryKey(name));
69 builder.setVersion((short) 0);
70 builder.setLength((short) 6);
71 builder.setMdType(MdType1.class);
72 builder.setNextProtocol(Ethernet.class);
74 builder.setNsi((short) 4);
76 final NshMdType1AugmentBuilder augmentBuilder = new NshMdType1AugmentBuilder();
77 augmentBuilder.setC1((long) 1);
78 augmentBuilder.setC2((long) 2);
79 augmentBuilder.setC3((long) 3);
80 augmentBuilder.setC4((long) 4);
81 builder.addAugmentation(NshMdType1Augment.class, augmentBuilder.build());
83 return builder.build();
86 private static InstanceIdentifier<NshEntry> getNshEntryId(final String name) {
87 return InstanceIdentifier.create(NshEntries.class)
88 .child(NshEntry.class, new NshEntryKey(name));
91 private void whenNshAddDelEntryThenSuccess() {
92 final NshAddDelEntryReply reply = new NshAddDelEntryReply();
93 reply.entryIndex = ENTRY_INDEX;
94 doReturn(future(reply)).when(jvppNsh).nshAddDelEntry(any(NshAddDelEntry.class));
97 private void whenNshAddDelEntryThenFailure() {
98 doReturn(failedFuture()).when(jvppNsh).nshAddDelEntry(any(NshAddDelEntry.class));
101 private static NshAddDelEntry generateNshAddDelEntry(final byte isAdd) {
102 final NshAddDelEntry request = new NshAddDelEntry();
103 request.isAdd = isAdd;
107 request.nextProtocol = 3;
108 request.nspNsi = 123<<8 | 4;
118 public void testCreate() throws Exception {
119 final NshEntry nshEntry = generateNshEntry(ENTRY_NAME);
120 final InstanceIdentifier<NshEntry> id = getNshEntryId(ENTRY_NAME);
122 whenNshAddDelEntryThenSuccess();
124 customizer.writeCurrentAttributes(id, nshEntry, writeContext);
126 verify(jvppNsh).nshAddDelEntry(generateNshAddDelEntry((byte) 1));
131 public void testCreateFailed() throws Exception {
132 final NshEntry nshEntry = generateNshEntry(ENTRY_NAME);
133 final InstanceIdentifier<NshEntry> id = getNshEntryId(ENTRY_NAME);
135 whenNshAddDelEntryThenFailure();
138 customizer.writeCurrentAttributes(id, nshEntry, writeContext);
139 } catch (WriteFailedException e) {
140 assertTrue(e.getCause() instanceof VppBaseCallException);
141 verify(jvppNsh).nshAddDelEntry(generateNshAddDelEntry((byte) 1));
145 fail("WriteFailedException.CreateFailedException was expected");
149 public void testDelete() throws Exception {
150 final NshEntry nshEntry = generateNshEntry(ENTRY_NAME);
151 final InstanceIdentifier<NshEntry> id = getNshEntryId(ENTRY_NAME);
153 whenNshAddDelEntryThenSuccess();
155 customizer.deleteCurrentAttributes(id, nshEntry, writeContext);
157 verify(jvppNsh).nshAddDelEntry(generateNshAddDelEntry((byte) 0));
161 public void testDeleteFailed() throws Exception {
162 final NshEntry nshEntry = generateNshEntry(ENTRY_NAME);
163 final InstanceIdentifier<NshEntry> id = getNshEntryId(ENTRY_NAME);
165 whenNshAddDelEntryThenFailure();
168 customizer.deleteCurrentAttributes(id, nshEntry, writeContext);
169 } catch (WriteFailedException e) {
170 assertTrue(e.getCause() instanceof VppBaseCallException);
171 verify(jvppNsh).nshAddDelEntry(generateNshAddDelEntry((byte) 0));
174 fail("WriteFailedException.DeleteFailedException was expected");
176 customizer.deleteCurrentAttributes(id, nshEntry, writeContext);
179 @Test(expected = UnsupportedOperationException.class)
180 public void testUpdate() throws Exception {
181 final NshEntry nshEntryBefore = generateNshEntry(ENTRY_NAME);
182 final InstanceIdentifier<NshEntry> id = getNshEntryId(ENTRY_NAME);
183 customizer.updateCurrentAttributes(id, nshEntryBefore, new NshEntryBuilder().build(), writeContext);