23f8610738d2c39c0bc4ea59bd056a44e5b7af1e
[honeycomb.git] / nsh / impl / src / test / java / io / fd / honeycomb / vppnsh / impl / config / NshEntryWriterCustomizerTest.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.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;
43
44 public class NshEntryWriterCustomizerTest extends WriterCustomizerTest {
45
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";
49
50     @Mock
51     protected FutureJVppNsh jvppNsh;
52
53     private NamingContext nshContext;
54
55     private NshEntryWriterCustomizer customizer;
56
57     @Override
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);
61
62         customizer = new NshEntryWriterCustomizer(jvppNsh, nshContext);
63     }
64
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);
73         builder.setNsp(123L);
74         builder.setNsi((short) 4);
75
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());
82
83         return builder.build();
84     }
85
86     private static InstanceIdentifier<NshEntry> getNshEntryId(final String name) {
87         return InstanceIdentifier.create(NshEntries.class)
88                 .child(NshEntry.class, new NshEntryKey(name));
89     }
90
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));
95     }
96
97     private void whenNshAddDelEntryThenFailure() {
98         doReturn(failedFuture()).when(jvppNsh).nshAddDelEntry(any(NshAddDelEntry.class));
99     }
100
101     private static NshAddDelEntry generateNshAddDelEntry(final byte isAdd) {
102         final NshAddDelEntry request = new NshAddDelEntry();
103         request.isAdd = isAdd;
104         request.verOC = 0;
105         request.length = 6;
106         request.mdType = 1;
107         request.nextProtocol = 3;
108         request.nspNsi = 123<<8 | 4;
109         request.c1 = 1;
110         request.c2 = 2;
111         request.c3 = 3;
112         request.c4 = 4;
113
114         return request;
115     }
116
117     @Test
118     public void testCreate() throws Exception {
119         final NshEntry nshEntry = generateNshEntry(ENTRY_NAME);
120         final InstanceIdentifier<NshEntry> id = getNshEntryId(ENTRY_NAME);
121
122         whenNshAddDelEntryThenSuccess();
123
124         customizer.writeCurrentAttributes(id, nshEntry, writeContext);
125
126         verify(jvppNsh).nshAddDelEntry(generateNshAddDelEntry((byte) 1));
127
128     }
129
130     @Test
131     public void testCreateFailed() throws Exception {
132         final NshEntry nshEntry = generateNshEntry(ENTRY_NAME);
133         final InstanceIdentifier<NshEntry> id = getNshEntryId(ENTRY_NAME);
134
135         whenNshAddDelEntryThenFailure();
136
137         try {
138             customizer.writeCurrentAttributes(id, nshEntry, writeContext);
139         } catch (WriteFailedException e) {
140             assertTrue(e.getCause() instanceof VppBaseCallException);
141             verify(jvppNsh).nshAddDelEntry(generateNshAddDelEntry((byte) 1));
142
143             return;
144         }
145         fail("WriteFailedException.CreateFailedException was expected");
146     }
147
148     @Test
149     public void testDelete() throws Exception {
150         final NshEntry nshEntry = generateNshEntry(ENTRY_NAME);
151         final InstanceIdentifier<NshEntry> id = getNshEntryId(ENTRY_NAME);
152
153         whenNshAddDelEntryThenSuccess();
154
155         customizer.deleteCurrentAttributes(id, nshEntry, writeContext);
156
157         verify(jvppNsh).nshAddDelEntry(generateNshAddDelEntry((byte) 0));
158     }
159
160     @Test
161     public void testDeleteFailed() throws Exception {
162         final NshEntry nshEntry = generateNshEntry(ENTRY_NAME);
163         final InstanceIdentifier<NshEntry> id = getNshEntryId(ENTRY_NAME);
164
165         whenNshAddDelEntryThenFailure();
166
167         try {
168             customizer.deleteCurrentAttributes(id, nshEntry, writeContext);
169         } catch (WriteFailedException e) {
170             assertTrue(e.getCause() instanceof VppBaseCallException);
171             verify(jvppNsh).nshAddDelEntry(generateNshAddDelEntry((byte) 0));
172             return;
173         }
174         fail("WriteFailedException.DeleteFailedException was expected");
175
176         customizer.deleteCurrentAttributes(id, nshEntry, writeContext);
177     }
178
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);
184     }
185 }