summaryrefslogtreecommitdiffstats
path: root/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/translate/v3po/vppclassifier/VppClassifierContextManagerImplTest.java
blob: cbc0c3802224fdc2ede1283003bbd9dede41cca2 (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
/*
 * Copyright (c) 2016 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.honeycomb.translate.v3po.vppclassifier;

import static io.fd.honeycomb.translate.v3po.vppclassifier.VppClassifierContextManagerImpl.VPP_CLASSIFIER_CONTEXT_IID;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

import com.google.common.base.Optional;
import io.fd.honeycomb.translate.MappingContext;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppNodeName;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev160909.VppClassifierContext;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev160909.VppClassifierContextBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev160909.vpp.classifier.context.ClassifyTableContext;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev160909.vpp.classifier.context.ClassifyTableContextBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev160909.vpp.classifier.context.ClassifyTableContextKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev160909.vpp.classifier.context.classify.table.context.NodeContext;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev160909.vpp.classifier.context.classify.table.context.NodeContextBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev160909.vpp.classifier.context.classify.table.context.NodeContextKey;
import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;

public class VppClassifierContextManagerImplTest {

    private static final int TABLE_ID_0 = 0;
    private static final String TABLE_NAME_0 = "table0";
    private static final KeyedInstanceIdentifier<ClassifyTableContext, ClassifyTableContextKey> TABLE_IID_0 =
        VPP_CLASSIFIER_CONTEXT_IID.child(ClassifyTableContext.class, new ClassifyTableContextKey(TABLE_NAME_0));

    private static final int TABLE_ID_1 = 1;
    private static final String TABLE_NAME_1 = "table1";

    private VppClassifierContextManagerImpl vppClassfierContext;

    @Mock
    private MappingContext ctx;

    @Before
    public void setUp() throws Exception {
        initMocks(this);
        vppClassfierContext = new VppClassifierContextManagerImpl("classify-table-");
    }

    @Test
    public void testAddTable() throws Exception {
        final String classfierNodeName = "node123";
        vppClassfierContext.addTable(TABLE_ID_0, TABLE_NAME_0, new VppNodeName(classfierNodeName), ctx);
        verify(ctx).put(TABLE_IID_0, table(TABLE_ID_0, TABLE_NAME_0, classfierNodeName));
    }

    @Test
    public void testContainsTable() throws Exception {
        when(ctx.read(TABLE_IID_0)).thenReturn(Optional.absent());
        assertFalse(vppClassfierContext.containsTable(TABLE_NAME_0, ctx));
    }

    @Test
    public void testGetTableIndex() throws Exception {
        when(ctx.read(TABLE_IID_0)).thenReturn(Optional.of(table(TABLE_ID_0, TABLE_NAME_0)));
        assertEquals(TABLE_ID_0, vppClassfierContext.getTableIndex(TABLE_NAME_0, ctx));
    }

    @Test
    public void testGetTableName() throws Exception {
        when(ctx.read(VPP_CLASSIFIER_CONTEXT_IID))
            .thenReturn(Optional.of(context(table(TABLE_ID_0, TABLE_NAME_0), table(TABLE_ID_1, TABLE_NAME_1))));
        assertEquals(TABLE_NAME_0, (vppClassfierContext.getTableName(TABLE_ID_0, ctx)));
    }

    @Test
    public void testGetTableBaseNode() throws Exception {
        final String classfierNodeName = "node123";
        when(ctx.read(TABLE_IID_0)).thenReturn(Optional.of(table(TABLE_ID_0, TABLE_NAME_0, classfierNodeName)));
        vppClassfierContext.getTableBaseNode(TABLE_NAME_0, ctx);
        assertEquals(Optional.of(classfierNodeName), (vppClassfierContext.getTableBaseNode(TABLE_NAME_0, ctx)));
    }

    @Test
    public void testRemoveTable() throws Exception {
        vppClassfierContext.removeTable(TABLE_NAME_0, ctx);
        verify(ctx).delete(TABLE_IID_0);
    }

    @Test
    public void testAddNodeName() throws Exception {
        final String nodeName = "node123";
        final int nodeIndex = 1;

        vppClassfierContext.addNodeName(TABLE_NAME_0, nodeIndex, nodeName, ctx);
        verify(ctx).put(
            TABLE_IID_0.child(NodeContext.class, new NodeContextKey(nodeName)),
            node(nodeName, nodeIndex)
        );
    }

    @Test
    public void testGetNonExistingNodeName() throws Exception {
        when(ctx.read(VPP_CLASSIFIER_CONTEXT_IID)).thenReturn(Optional.of(context(table(TABLE_ID_1, TABLE_NAME_1))));
        assertFalse(vppClassfierContext.getNodeName(TABLE_ID_0, 123, ctx).isPresent());
    }

    @Test
    public void testGetNodeName() throws Exception {
        final ClassifyTableContext tableCtx = table(TABLE_ID_0, TABLE_NAME_0, "aa", node("node123", 123));
        when(ctx.read(VPP_CLASSIFIER_CONTEXT_IID)).thenReturn(Optional.of(context(tableCtx)));
        when(ctx.read(TABLE_IID_0)).thenReturn(Optional.of(tableCtx));
        assertEquals(Optional.of("node123"), vppClassfierContext.getNodeName(TABLE_ID_0, 123, ctx));
    }

    private VppClassifierContext context(ClassifyTableContext... tables) {
        VppClassifierContextBuilder context = new VppClassifierContextBuilder();
        context.setClassifyTableContext(Arrays.asList(tables));
        return context.build();
    }

    private static ClassifyTableContext table(final Integer id, final String name) {
        return table(id, name, null);
    }

    private static ClassifyTableContext table(final Integer id, final String name, final String classfierNodeName,
                                              final NodeContext... nodeContexts) {
        final ClassifyTableContextBuilder builder =
            new ClassifyTableContextBuilder().setIndex(id).setName(name).setClassifierNodeName(classfierNodeName);

        if (nodeContexts.length > 0) {
            builder.setNodeContext(Arrays.asList(nodeContexts));
        }

        return builder.build();
    }

    private NodeContext node(final String nodeName, final int nodeIndex) {
        return new NodeContextBuilder().setName(nodeName).setIndex(nodeIndex).build();
    }
}