summaryrefslogtreecommitdiffstats
path: root/vpp-classifier/impl/src/test/java/io/fd/hc2vpp/vpp/classifier/write/ClassifySessionWriterTest.java
blob: 31d7ad7f70d57bea7410f4eb60e47a2de61334a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.fd.hc2vpp.vpp.classifier.write;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.google.common.base.Optional;
import io.fd.hc2vpp.common.test.write.WriterCustomizerTest;
import io.fd.hc2vpp.common.translate.util.NamingContext;
import io.fd.hc2vpp.vpp.classifier.context.VppClassifierContextManager;
import io.fd.honeycomb.translate.write.WriteFailedException;
import io.fd.vpp.jvpp.VppBaseCallException;
import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSession;
import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSessionReply;
import org.junit.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.HexString;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.OpaqueIndex;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.PacketHandlingAction;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.VppClassifier;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.VppNode;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.VppNodeName;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.classify.session.attributes.next_node.StandardBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.classify.table.base.attributes.ClassifySession;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.classify.table.base.attributes.ClassifySessionBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.classify.table.base.attributes.ClassifySessionKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.vpp.classifier.ClassifyTable;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.vpp.classifier.ClassifyTableKey;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;

public class ClassifySessionWriterTest extends WriterCustomizerTest {

    private static final int TABLE_INDEX = 123;
    private static final String TABLE_NAME = "table123";
    private static final int SESSION_INDEX = 456;
    @Mock
    private VppClassifierContextManager classfierContext;
    private ClassifySessionWriter customizer;

    private static ClassifySession generateClassifySession(final long opaqueIndex, final String match) {
        final ClassifySessionBuilder builder = new ClassifySessionBuilder();
        builder.setNextNode(
            new StandardBuilder()
                .setOpaqueIndex(new OpaqueIndex(opaqueIndex))
                .setHitNext(new VppNode(PacketHandlingAction.Deny))
                .build());
        builder.setAdvance(123);
        builder.setMatch(new HexString(match));
        return builder.build();
    }

    private static InstanceIdentifier<ClassifySession> getClassifySessionId(final String tableName,
                                                                            final String match) {
        return InstanceIdentifier.create(VppClassifier.class)
                .child(ClassifyTable.class, new ClassifyTableKey(tableName))
                .child(ClassifySession.class, new ClassifySessionKey(new HexString(match)));
    }

    private static ClassifyAddDelSession generateClassifyAddDelSession(final byte isAdd, final int tableIndex,
                                                                       final int sessionIndex) {
        final ClassifyAddDelSession request = new ClassifyAddDelSession();
        request.isAdd = isAdd;
        request.tableIndex = tableIndex;
        request.opaqueIndex = sessionIndex;
        request.hitNextIndex = 0;
        request.advance = 123;
        request.match =
                new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
                        (byte) 0x05, (byte) 0x06, 0x00, 0x00, 0x00, 0x00};
        return request;
    }

    @Override
    public void setUpTest() throws Exception {
        customizer = new ClassifySessionWriter(api, classfierContext, new NamingContext("policer-", "policer-context-"));

        when(classfierContext.containsTable(TABLE_NAME, mappingContext)).thenReturn(true);
        when(classfierContext.getTableIndex(TABLE_NAME, mappingContext)).thenReturn(TABLE_INDEX);

        final ClassifyTable table = mock(ClassifyTable.class);
        when(table.getClassifierNode()).thenReturn(new VppNodeName("ip4-classifier"));
        when(writeContext.readAfter(ArgumentMatchers.any())).thenReturn(Optional.of(table));
        when(writeContext.readBefore(ArgumentMatchers.any())).thenReturn(Optional.of(table));
    }

    private void whenClassifyAddDelSessionThenSuccess() {
        doReturn(future(new ClassifyAddDelSessionReply())).when(api)
                .classifyAddDelSession(ArgumentMatchers.any(ClassifyAddDelSession.class));
    }

    private void whenClassifyAddDelSessionThenFailure() {
        doReturn(failedFuture()).when(api).classifyAddDelSession(ArgumentMatchers.any(ClassifyAddDelSession.class));
    }

    @Test
    public void testCreate() throws Exception {
        final String match = "00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00";
        final ClassifySession classifySession = generateClassifySession(SESSION_INDEX, match);
        final InstanceIdentifier<ClassifySession> id = getClassifySessionId(TABLE_NAME, match);

        whenClassifyAddDelSessionThenSuccess();

        customizer.writeCurrentAttributes(id, classifySession, writeContext);

        verify(api).classifyAddDelSession(generateClassifyAddDelSession((byte) 1, TABLE_INDEX, SESSION_INDEX));
    }

    @Test
    public void testCreateFailed() throws Exception {
        final String match = "00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00";
        final ClassifySession classifySession = generateClassifySession(SESSION_INDEX, match);
        final InstanceIdentifier<ClassifySession> id = getClassifySessionId(TABLE_NAME, match);

        whenClassifyAddDelSessionThenFailure();

        try {
            customizer.writeCurrentAttributes(id, classifySession, writeContext);
        } catch (WriteFailedException e) {
            assertTrue(e.getCause() instanceof VppBaseCallException);
            verify(api).classifyAddDelSession(generateClassifyAddDelSession((byte) 1, TABLE_INDEX, SESSION_INDEX));
            return;
        }
        fail("WriteFailedException.CreateFailedException was expected");
    }

    @Test(expected = UnsupportedOperationException.class)
    public void testUpdate() throws Exception {
        customizer.updateCurrentAttributes(null, null, null, writeContext);
    }

    @Test
    public void testDelete() throws Exception {
        final String match = "00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00";
        final ClassifySession classifySession = generateClassifySession(SESSION_INDEX, match);
        final InstanceIdentifier<ClassifySession> id = getClassifySessionId(TABLE_NAME, match);

        whenClassifyAddDelSessionThenSuccess();

        customizer.deleteCurrentAttributes(id, classifySession, writeContext);

        verify(api).classifyAddDelSession(generateClassifyAddDelSession((byte) 0, TABLE_INDEX, SESSION_INDEX));
    }

    @Test
    public void testDeleteFailed() throws Exception {
        final String match = "00:00:00:00:00:00:01:02:03:04:05:06:00:00:00:00";
        final ClassifySession classifySession = generateClassifySession(SESSION_INDEX, match);
        final InstanceIdentifier<ClassifySession> id = getClassifySessionId(TABLE_NAME, match);

        whenClassifyAddDelSessionThenFailure();

        try {
            customizer.deleteCurrentAttributes(id, classifySession, writeContext);
        } catch (WriteFailedException e) {
            assertTrue(e.getCause() instanceof VppBaseCallException);
            verify(api).classifyAddDelSession(generateClassifyAddDelSession((byte) 0, TABLE_INDEX, SESSION_INDEX));
            return;
        }
        fail("WriteFailedException.DeleteFailedException was expected");

        customizer.deleteCurrentAttributes(id, classifySession, writeContext);
    }
}