296bfc45d5b7676f99008f96556c71064c2b0f29
[hc2vpp.git] /
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.hc2vpp.v3po.interfacesstate.span;
18
19 import static org.hamcrest.MatcherAssert.assertThat;
20 import static org.hamcrest.Matchers.containsInAnyOrder;
21 import static org.hamcrest.Matchers.hasSize;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.Mockito.when;
24
25 import io.fd.hc2vpp.common.test.read.ReaderCustomizerTest;
26 import io.fd.hc2vpp.common.translate.util.NamingContext;
27 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
28 import io.fd.vpp.jvpp.core.dto.SwInterfaceSpanDetails;
29 import io.fd.vpp.jvpp.core.dto.SwInterfaceSpanDetailsReplyDump;
30 import java.util.Arrays;
31 import org.junit.Test;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.SpanState;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceStateAugmentation;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces.state._interface.Span;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces.state._interface.SpanBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.span.attributes.MirroredInterfaces;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.span.attributes.MirroredInterfacesBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.span.attributes.mirrored.interfaces.MirroredInterface;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.span.attributes.mirrored.interfaces.MirroredInterfaceBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.span.attributes.mirrored.interfaces.MirroredInterfaceKey;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45
46
47 public class MirroredInterfacesCustomizerTest
48         extends ReaderCustomizerTest<MirroredInterfaces, MirroredInterfacesBuilder> {
49
50     private static final String IFACE_NAME = "iface";
51
52     private static final String SRC_IFACE_NAME_1 = "src-one";
53     private static final String SRC_IFACE_NAME_2 = "src-two";
54     private static final String SRC_IFACE_NAME_3 = "src-three";
55
56     private static final int IFACE_INDEX = 3;
57
58     private NamingContext interfaceContext;
59     private InstanceIdentifier<MirroredInterfaces> validId;
60     private MirroredInterface validData;
61
62     public MirroredInterfacesCustomizerTest() {
63         super(MirroredInterfaces.class, SpanBuilder.class);
64     }
65
66     @Override
67     protected void setUp() throws Exception {
68         interfaceContext = new NamingContext("iface", "iface-context");
69         defineMapping(mappingContext, IFACE_NAME, IFACE_INDEX, "iface-context");
70         defineMapping(mappingContext, SRC_IFACE_NAME_1, 1, "iface-context");
71         defineMapping(mappingContext, SRC_IFACE_NAME_2, 2, "iface-context");
72         defineMapping(mappingContext, SRC_IFACE_NAME_3, 3, "iface-context");
73
74         validId = InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey(IFACE_NAME))
75                 .augmentation(VppInterfaceStateAugmentation.class).child(Span.class)
76                 .child(MirroredInterfaces.class);
77
78         SwInterfaceSpanDetailsReplyDump dump = new SwInterfaceSpanDetailsReplyDump();
79         SwInterfaceSpanDetails detail1 = new SwInterfaceSpanDetails();
80
81         detail1.swIfIndexTo = IFACE_INDEX;
82         detail1.swIfIndexFrom = 1;
83         detail1.state = 1;
84
85         SwInterfaceSpanDetails detail2 = new SwInterfaceSpanDetails();
86
87         detail2.swIfIndexTo = IFACE_INDEX;
88         detail2.swIfIndexFrom = 2;
89         detail2.state = 3;
90
91         SwInterfaceSpanDetails detail3 = new SwInterfaceSpanDetails();
92
93         detail3.swIfIndexTo = IFACE_INDEX;
94         detail3.swIfIndexFrom = 3;
95         detail3.state = 0;
96
97         dump.swInterfaceSpanDetails = Arrays.asList(detail1, detail2, detail3);
98
99         when(api.swInterfaceSpanDump(any())).thenReturn(future(dump));
100     }
101
102     @Test
103     public void readCurrentAttributes() throws Exception {
104         MirroredInterfacesBuilder builder = new MirroredInterfacesBuilder();
105         getCustomizer().readCurrentAttributes(validId, builder, ctx);
106
107
108         final MirroredInterfaces data = builder.build();
109         // 1,2 should be returned,0 should be filtered out because of disabled state
110
111         assertThat(data.getMirroredInterface(), hasSize(2));
112         assertThat(data.getMirroredInterface(), containsInAnyOrder(
113                 mirroredInterface(SRC_IFACE_NAME_1, SpanState.Receive),
114                 mirroredInterface(SRC_IFACE_NAME_2, SpanState.Both)));
115     }
116
117     private MirroredInterface mirroredInterface(final String ifaceName, final SpanState state) {
118         return new MirroredInterfaceBuilder()
119                 .setIfaceRef(ifaceName)
120                 .setKey(new MirroredInterfaceKey(ifaceName))
121                 .setState(state)
122                 .build();
123     }
124
125     @Override
126     protected ReaderCustomizer<MirroredInterfaces, MirroredInterfacesBuilder> initCustomizer() {
127         return new MirroredInterfacesCustomizer(api, interfaceContext);
128     }
129 }