HONEYCOMB-204 exclude deleted interfaces from operational data
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / DisabledInterfacesManagerTest.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;
18
19 import static org.hamcrest.CoreMatchers.hasItems;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertThat;
22 import static org.junit.Assert.assertTrue;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.verify;
25
26 import com.google.common.base.Optional;
27 import io.fd.honeycomb.translate.MappingContext;
28 import java.util.Arrays;
29 import java.util.List;
30 import java.util.stream.Collectors;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.context.rev160909.DisabledInterfaces;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.context.rev160909.DisabledInterfacesBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.context.rev160909.disabled.interfaces.DisabledInterfaceIndex;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.context.rev160909.disabled.interfaces.DisabledInterfaceIndexBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.context.rev160909.disabled.interfaces.DisabledInterfaceIndexKey;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
42
43 public class DisabledInterfacesManagerTest {
44
45     private static final InstanceIdentifier<DisabledInterfaces>
46             ROOT_ID = InstanceIdentifier.create(DisabledInterfaces.class);
47     private static final KeyedInstanceIdentifier<DisabledInterfaceIndex, DisabledInterfaceIndexKey> SPECIFIC_ID_1 =
48             ROOT_ID.child(DisabledInterfaceIndex.class, new DisabledInterfaceIndexKey(1));
49     private static final KeyedInstanceIdentifier<DisabledInterfaceIndex, DisabledInterfaceIndexKey> SPECIFIC_ID_4 =
50             ROOT_ID.child(DisabledInterfaceIndex.class, new DisabledInterfaceIndexKey(4));
51
52     @Mock
53     private MappingContext mappingContext;
54     private DisabledInterfacesManager manager;
55
56     @Before
57     public void setUp() throws Exception {
58         MockitoAnnotations.initMocks(this);
59         manager = new DisabledInterfacesManager();
60         doReturn(Optional.of(new DisabledInterfacesBuilder().setDisabledInterfaceIndex(toIndices(1, 2, 3)).build()))
61                 .when(mappingContext)
62                 .read(ROOT_ID);
63         doReturn(Optional.of(toIndex(1)))
64                 .when(mappingContext)
65                 .read(SPECIFIC_ID_1);
66         doReturn(Optional.absent())
67                 .when(mappingContext)
68                 .read(SPECIFIC_ID_4);
69     }
70
71     @Test
72     public void testGetAll() throws Exception {
73         final List<Integer> disabledInterfaces = manager.getDisabledInterfaces(mappingContext);
74         assertThat(disabledInterfaces, hasItems(1, 2, 3));
75     }
76
77     @Test
78     public void testCheckOne() throws Exception {
79         assertTrue(manager.isInterfaceDisabled(1, mappingContext));
80         assertFalse(manager.isInterfaceDisabled(4, mappingContext));
81     }
82
83     @Test
84     public void testDisable() throws Exception {
85         manager.disableInterface(1, mappingContext);
86         verify(mappingContext).put(SPECIFIC_ID_1, toIndex(1));
87     }
88
89     @Test
90     public void testRemoveDisability() throws Exception {
91         manager.removeDisabledInterface(1, mappingContext);
92         verify(mappingContext).delete(SPECIFIC_ID_1);
93     }
94
95     private List<DisabledInterfaceIndex> toIndices(final int... indices) {
96         return Arrays.stream(indices)
97                 .mapToObj(this::toIndex)
98                 .collect(Collectors.toList());
99     }
100
101     private DisabledInterfaceIndex toIndex(final int idx) {
102         return new DisabledInterfaceIndexBuilder()
103                 .setIndex(idx)
104                 .build();
105     }
106 }