2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.translate.v3po.interfaces;
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.mock;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.verifyZeroInteractions;
25 import static org.mockito.Mockito.when;
27 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
28 import io.fd.honeycomb.translate.vpp.util.NamingContext;
29 import io.fd.honeycomb.translate.write.WriteFailedException;
30 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
31 import org.junit.Test;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceBuilder;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import io.fd.vpp.jvpp.VppBaseCallException;
38 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetFlags;
39 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetFlagsReply;
41 public class InterfaceCustomizerTest extends WriterCustomizerTest implements ByteDataTranslator {
42 private static final String IFACE_CTX_NAME = "interface-ctx";
43 private static final String IF_NAME = "eth1";
44 private static final int IF_INDEX = 1;
46 private InterfaceCustomizer customizer;
47 private InstanceIdentifier<Interface> IID =
48 InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IF_NAME));
51 protected void setUp() throws Exception {
52 customizer = new InterfaceCustomizer(api, new NamingContext("ifacePrefix", IFACE_CTX_NAME));
53 defineMapping(mappingContext, IF_NAME, IF_INDEX, IFACE_CTX_NAME);
57 public void testWrite() throws WriteFailedException {
58 final boolean enabled = true;
59 when(api.swInterfaceSetFlags(any())).thenReturn(future(new SwInterfaceSetFlagsReply()));
60 customizer.writeCurrentAttributes(IID, iface(enabled), writeContext);
61 verify(api).swInterfaceSetFlags(expectedRequest(enabled));
65 public void testWriteFailed() {
66 final boolean enabled = false;
67 when(api.swInterfaceSetFlags(any())).thenReturn(failedFuture());
69 customizer.writeCurrentAttributes(IID, iface(enabled), writeContext);
70 } catch (WriteFailedException e) {
71 assertTrue(e.getCause() instanceof VppBaseCallException);
72 verify(api).swInterfaceSetFlags(expectedRequest(enabled));
75 fail("WriteFailedException expected");
79 public void testUpdate() throws WriteFailedException {
80 when(api.swInterfaceSetFlags(any())).thenReturn(future(new SwInterfaceSetFlagsReply()));
81 customizer.updateCurrentAttributes(IID, iface(false), iface(true), writeContext);
82 verify(api).swInterfaceSetFlags(expectedRequest(true));
86 public void testUpdateFailed() {
87 when(api.swInterfaceSetFlags(any())).thenReturn(failedFuture());
89 customizer.updateCurrentAttributes(IID, iface(false), iface(true), writeContext);
90 } catch (WriteFailedException e) {
91 assertTrue(e.getCause() instanceof VppBaseCallException);
92 verify(api).swInterfaceSetFlags(expectedRequest(true));
95 fail("WriteFailedException expected");
99 public void testDelete() throws WriteFailedException {
100 customizer.deleteCurrentAttributes(IID, mock(Interface.class), writeContext);
101 verifyZeroInteractions(api);
104 private Interface iface(final boolean enabled) {
105 return new InterfaceBuilder().setName(IF_NAME).setEnabled(enabled).build();
108 private SwInterfaceSetFlags expectedRequest(final boolean enabled) {
109 final SwInterfaceSetFlags request = new SwInterfaceSetFlags();
111 request.adminUpDown = booleanToByte(enabled);
112 request.linkUpDown = booleanToByte(enabled);
113 request.swIfIndex = IF_INDEX;