VPP-378: update jvpp package names
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / vppclassifier / ClassifySessionWriterTest.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.translate.v3po.vppclassifier;
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.mock;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26
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 org.junit.Test;
31 import org.mockito.Mock;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.HexString;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.OpaqueIndex;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.PacketHandlingAction;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppClassifier;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppNode;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppNodeName;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.classify.table.base.attributes.ClassifySession;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.classify.table.base.attributes.ClassifySessionBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.classify.table.base.attributes.ClassifySessionKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTable;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTableKey;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import io.fd.vpp.jvpp.VppBaseCallException;
45 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSession;
46 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSessionReply;
47
48 public class ClassifySessionWriterTest extends WriterCustomizerTest {
49
50     private static final int TABLE_INDEX = 123;
51     private static final String TABLE_NAME = "table123";
52
53     @Mock
54     private VppClassifierContextManager classfierContext;
55
56     private ClassifySessionWriter customizer;
57     private static final int SESSION_INDEX = 456;
58
59     @Override
60     public void setUp() throws Exception {
61         customizer = new ClassifySessionWriter(api, classfierContext);
62
63         when(classfierContext.containsTable(TABLE_NAME, mappingContext)).thenReturn(true);
64         when(classfierContext.getTableIndex(TABLE_NAME, mappingContext)).thenReturn(TABLE_INDEX);
65
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));
70     }
71
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();
79     }
80
81     private static InstanceIdentifier<ClassifySession> getClassifySessionId(final String tableName,
82                                                                             final String match) {
83         return InstanceIdentifier.create(VppClassifier.class)
84             .child(ClassifyTable.class, new ClassifyTableKey(tableName))
85             .child(ClassifySession.class, new ClassifySessionKey(new HexString(match)));
86     }
87
88     private void whenClassifyAddDelSessionThenSuccess() {
89         doReturn(future(new ClassifyAddDelSessionReply())).when(api)
90             .classifyAddDelSession(any(ClassifyAddDelSession.class));
91     }
92
93     private void whenClassifyAddDelSessionThenFailure() {
94         doReturn(failedFuture()).when(api).classifyAddDelSession(any(ClassifyAddDelSession.class));
95     }
96
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;
105         request.match =
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};
108         return request;
109     }
110
111     @Test
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);
116
117         whenClassifyAddDelSessionThenSuccess();
118
119         customizer.writeCurrentAttributes(id, classifySession, writeContext);
120
121         verify(api).classifyAddDelSession(generateClassifyAddDelSession((byte) 1, TABLE_INDEX, SESSION_INDEX));
122     }
123
124     @Test
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);
129
130         whenClassifyAddDelSessionThenFailure();
131
132         try {
133             customizer.writeCurrentAttributes(id, classifySession, writeContext);
134         } catch (WriteFailedException.CreateFailedException e) {
135             assertTrue(e.getCause() instanceof VppBaseCallException);
136             verify(api).classifyAddDelSession(generateClassifyAddDelSession((byte) 1, TABLE_INDEX, SESSION_INDEX));
137             return;
138         }
139         fail("WriteFailedException.CreateFailedException was expected");
140     }
141
142     @Test(expected = UnsupportedOperationException.class)
143     public void testUpdate() throws Exception {
144         customizer.updateCurrentAttributes(null, null, null, writeContext);
145     }
146
147     @Test
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);
152
153         whenClassifyAddDelSessionThenSuccess();
154
155         customizer.deleteCurrentAttributes(id, classifySession, writeContext);
156
157         verify(api).classifyAddDelSession(
158             generateClassifyAddDelSession((byte) 0, TABLE_INDEX, SESSION_INDEX));
159     }
160
161     @Test
162     public void testDeleteFailed() throws Exception {
163         final String match = "00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00";
164         final ClassifySession classifySession = generateClassifySession(SESSION_INDEX, match);
165         final InstanceIdentifier<ClassifySession> id = getClassifySessionId(TABLE_NAME, match);
166
167         whenClassifyAddDelSessionThenFailure();
168
169         try {
170             customizer.deleteCurrentAttributes(id, classifySession, writeContext);
171         } catch (WriteFailedException.DeleteFailedException e) {
172             assertTrue(e.getCause() instanceof VppBaseCallException);
173             verify(api).classifyAddDelSession(
174                 generateClassifyAddDelSession((byte) 0, TABLE_INDEX, SESSION_INDEX));
175             return;
176         }
177         fail("WriteFailedException.DeleteFailedException was expected");
178
179         customizer.deleteCurrentAttributes(id, classifySession, writeContext);
180     }
181 }