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.translate.v3po.vppclassifier;
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.mock;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
27 import com.google.common.base.Optional;
28 import io.fd.honeycomb.translate.write.WriteFailedException;
29 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
30 import io.fd.vpp.jvpp.VppBaseCallException;
31 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSession;
32 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSessionReply;
33 import org.junit.Test;
34 import org.mockito.Mock;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.HexString;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.OpaqueIndex;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.PacketHandlingAction;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.VppClassifier;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.VppNode;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.VppNodeName;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.classify.table.base.attributes.ClassifySession;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.classify.table.base.attributes.ClassifySessionBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.classify.table.base.attributes.ClassifySessionKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.vpp.classifier.ClassifyTable;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.vpp.classifier.ClassifyTableKey;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 public class ClassifySessionWriterTest extends WriterCustomizerTest {
50 private static final int TABLE_INDEX = 123;
51 private static final String TABLE_NAME = "table123";
54 private VppClassifierContextManager classfierContext;
56 private ClassifySessionWriter customizer;
57 private static final int SESSION_INDEX = 456;
60 public void setUp() throws Exception {
61 customizer = new ClassifySessionWriter(api, classfierContext);
63 when(classfierContext.containsTable(TABLE_NAME, mappingContext)).thenReturn(true);
64 when(classfierContext.getTableIndex(TABLE_NAME, mappingContext)).thenReturn(TABLE_INDEX);
66 final ClassifyTable table = mock(ClassifyTable.class);
67 when(table.getClassifierNode()).thenReturn(new VppNodeName("ip4-classifier"));
68 when(writeContext.readAfter(any())).thenReturn(Optional.of(table));
69 when(writeContext.readBefore(any())).thenReturn(Optional.of(table));
72 private static ClassifySession generateClassifySession(final long opaqueIndex, final String match) {
73 final ClassifySessionBuilder builder = new ClassifySessionBuilder();
74 builder.setOpaqueIndex(new OpaqueIndex(opaqueIndex));
75 builder.setHitNext(new VppNode(PacketHandlingAction.Deny));
76 builder.setAdvance(123);
77 builder.setMatch(new HexString(match));
78 return builder.build();
81 private static InstanceIdentifier<ClassifySession> getClassifySessionId(final String tableName,
83 return InstanceIdentifier.create(VppClassifier.class)
84 .child(ClassifyTable.class, new ClassifyTableKey(tableName))
85 .child(ClassifySession.class, new ClassifySessionKey(new HexString(match)));
88 private void whenClassifyAddDelSessionThenSuccess() {
89 doReturn(future(new ClassifyAddDelSessionReply())).when(api)
90 .classifyAddDelSession(any(ClassifyAddDelSession.class));
93 private void whenClassifyAddDelSessionThenFailure() {
94 doReturn(failedFuture()).when(api).classifyAddDelSession(any(ClassifyAddDelSession.class));
97 private static ClassifyAddDelSession generateClassifyAddDelSession(final byte isAdd, final int tableIndex,
98 final int sessionIndex) {
99 final ClassifyAddDelSession request = new ClassifyAddDelSession();
100 request.isAdd = isAdd;
101 request.tableIndex = tableIndex;
102 request.opaqueIndex = sessionIndex;
103 request.hitNextIndex = 0;
104 request.advance = 123;
106 new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
107 (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00};
112 public void testCreate() throws Exception {
113 final String match = "00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00";
114 final ClassifySession classifySession = generateClassifySession(SESSION_INDEX, match);
115 final InstanceIdentifier<ClassifySession> id = getClassifySessionId(TABLE_NAME, match);
117 whenClassifyAddDelSessionThenSuccess();
119 customizer.writeCurrentAttributes(id, classifySession, writeContext);
121 verify(api).classifyAddDelSession(generateClassifyAddDelSession((byte) 1, TABLE_INDEX, SESSION_INDEX));
125 public void testCreateFailed() throws Exception {
126 final String match = "00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00";
127 final ClassifySession classifySession = generateClassifySession(SESSION_INDEX, match);
128 final InstanceIdentifier<ClassifySession> id = getClassifySessionId(TABLE_NAME, match);
130 whenClassifyAddDelSessionThenFailure();
133 customizer.writeCurrentAttributes(id, classifySession, writeContext);
134 } catch (WriteFailedException e) {
135 assertTrue(e.getCause() instanceof VppBaseCallException);
136 verify(api).classifyAddDelSession(generateClassifyAddDelSession((byte) 1, TABLE_INDEX, SESSION_INDEX));
139 fail("WriteFailedException.CreateFailedException was expected");
142 @Test(expected = UnsupportedOperationException.class)
143 public void testUpdate() throws Exception {
144 customizer.updateCurrentAttributes(null, null, null, writeContext);
148 public void testDelete() throws Exception {
149 final String match = "00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00";
150 final ClassifySession classifySession = generateClassifySession(SESSION_INDEX, match);
151 final InstanceIdentifier<ClassifySession> id = getClassifySessionId(TABLE_NAME, match);
153 whenClassifyAddDelSessionThenSuccess();
155 customizer.deleteCurrentAttributes(id, classifySession, writeContext);
157 verify(api).classifyAddDelSession(generateClassifyAddDelSession((byte) 0, TABLE_INDEX, SESSION_INDEX));
161 public void testDeleteFailed() throws Exception {
162 final String match = "00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00";
163 final ClassifySession classifySession = generateClassifySession(SESSION_INDEX, match);
164 final InstanceIdentifier<ClassifySession> id = getClassifySessionId(TABLE_NAME, match);
166 whenClassifyAddDelSessionThenFailure();
169 customizer.deleteCurrentAttributes(id, classifySession, writeContext);
170 } catch (WriteFailedException e) {
171 assertTrue(e.getCause() instanceof VppBaseCallException);
172 verify(api).classifyAddDelSession(generateClassifyAddDelSession((byte) 0, TABLE_INDEX, SESSION_INDEX));
175 fail("WriteFailedException.DeleteFailedException was expected");
177 customizer.deleteCurrentAttributes(id, classifySession, writeContext);