2ebd64776c356df41a4bf8d2e4973a0b13770343
[groupbasedpolicy.git] / renderers / vpp / src / test / java / org / opendaylight / groupbasedpolicy / renderer / vpp / iface / InterfaceManagerTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.iface;
10
11 import java.util.Arrays;
12 import java.util.Collection;
13 import java.util.List;
14
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Mockito;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.groupbasedpolicy.renderer.vpp.event.VppEndpointConfEvent;
23 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.MountedDataBrokerProvider;
24 import org.opendaylight.groupbasedpolicy.test.CustomDataBrokerTest;
25 import org.opendaylight.groupbasedpolicy.util.IidFactory;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContextId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.LocationProviders;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.LocationProvider;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocation;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.rev160427.AddressType;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.rev160427.ContextType;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.Config;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.VppEndpoint;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.VppEndpointBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.VppEndpointKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.vpp.endpoint._interface.type.choice.VhostUserCaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUser;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUserRole;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.L2;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.base.attributes.Interconnection;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.base.attributes.interconnection.BridgeBased;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.BridgeDomains;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55
56 import com.google.common.base.Optional;
57 import com.google.common.base.Strings;
58 import com.google.common.util.concurrent.MoreExecutors;
59
60 public class InterfaceManagerTest extends CustomDataBrokerTest {
61
62     private final static String ADDRESS = "1.1.1.1/32";
63     private final static ContextId CONTEXT_ID = new ContextId("ctx1");
64     private final static String IFACE_NAME = "ifaceName1";
65     private final static VppEndpointKey BASIC_VPP_EP_KEY =
66             new VppEndpointKey(ADDRESS, AddressType.class, CONTEXT_ID, ContextType.class);
67     private final static InstanceIdentifier<VppEndpoint> BASIC_VPP_EP_IID =
68             InstanceIdentifier.builder(Config.class).child(VppEndpoint.class, BASIC_VPP_EP_KEY).build();
69     private final static TopologyKey TOPO_KEY = new TopologyKey(new TopologyId("topo1"));
70     private final static NodeKey NODE_KEY = new NodeKey(new NodeId("node1"));
71     private final static InstanceIdentifier<Node> NODE_IID = InstanceIdentifier.builder(NetworkTopology.class)
72         .child(Topology.class, TOPO_KEY)
73         .child(Node.class, NODE_KEY)
74         .build();
75     private final static String SOCKET = "socket1";
76
77     private InterfaceManager manager;
78     private MountedDataBrokerProvider mountedDataProviderMock;
79     private DataBroker mountPointDataBroker;
80     private DataBroker dataBroker;
81
82     @Override
83     public Collection<Class<?>> getClassesFromModules() {
84         return Arrays.asList(Node.class, VppEndpoint.class, Interfaces.class, BridgeDomains.class,
85                 LocationProviders.class);
86     }
87
88     @Before
89     public void init() throws Exception {
90         mountedDataProviderMock = Mockito.mock(MountedDataBrokerProvider.class);
91         mountPointDataBroker = getDataBroker();
92         setup(); // initialize new data broker for ODL data store
93         dataBroker = getDataBroker();
94         Mockito.when(mountedDataProviderMock.getDataBrokerForMountPoint(Mockito.any(InstanceIdentifier.class)))
95             .thenReturn(Optional.of(mountPointDataBroker));
96         manager = new InterfaceManager(mountedDataProviderMock, dataBroker, MoreExecutors.newDirectExecutorService());
97     }
98
99     @Test
100     public void testVppEndpointChanged_created() throws Exception {
101         VppEndpoint vhostEp = vhostVppEpBuilder().build();
102         VppEndpointConfEvent event = new VppEndpointConfEvent(BASIC_VPP_EP_IID, null, vhostEp);
103
104         manager.vppEndpointChanged(event);
105         // assert state on data store behind mount point
106         ReadOnlyTransaction rTxMount = mountPointDataBroker.newReadOnlyTransaction();
107         Optional<Interface> potentialIface =
108                 rTxMount.read(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.builder(Interfaces.class)
109                     .child(Interface.class, new InterfaceKey(vhostEp.getVppInterfaceName()))
110                     .build()).get();
111         Assert.assertTrue(potentialIface.isPresent());
112         Interface iface = potentialIface.get();
113         Assert.assertEquals(VhostUser.class, iface.getType());
114         Assert.assertTrue(iface.isEnabled());
115         VppInterfaceAugmentation vppIface = iface.getAugmentation(VppInterfaceAugmentation.class);
116         Assert.assertNotNull(vppIface);
117         org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.VhostUser vhostUserIface =
118                 vppIface.getVhostUser();
119         Assert.assertNotNull(vhostUserIface);
120         Assert.assertEquals(VhostUserRole.Client, vhostUserIface.getRole());
121         Assert.assertEquals(SOCKET, vhostUserIface.getSocket());
122         L2 l2Iface = vppIface.getL2();
123         if (l2Iface != null) {
124             Interconnection interconnection = l2Iface.getInterconnection();
125             if (interconnection != null) {
126                 if (interconnection instanceof BridgeBased) {
127                     BridgeBased bridgeL2Iface = (BridgeBased) interconnection;
128                     Assert.assertTrue(Strings.isNullOrEmpty(bridgeL2Iface.getBridgeDomain()));
129                 }
130             }
131         }
132         // assert state on ODL data store
133         ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
134         Optional<LocationProvider> optLocationProvider = rTx.read(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VppEndpointLocationProvider.VPP_ENDPOINT_LOCATION_PROVIDER)).get();
135         Assert.assertTrue(optLocationProvider.isPresent());
136         List<ProviderAddressEndpointLocation> epLocs = optLocationProvider.get().getProviderAddressEndpointLocation();
137         Assert.assertNotNull(epLocs);
138         Assert.assertEquals(1, epLocs.size());
139         ProviderAddressEndpointLocation epLoc = VppEndpointLocationProvider.createProviderAddressEndpointLocation(vhostEp);
140         Assert.assertEquals(epLoc, epLocs.get(0));
141     }
142
143     @Test
144     public void testVppEndpointChanged_deleted() throws Exception {
145         VppEndpoint vhostEp = vhostVppEpBuilder().build();
146         VppEndpointConfEvent createVppEpEvent = new VppEndpointConfEvent(BASIC_VPP_EP_IID, null, vhostEp);
147         VppEndpointConfEvent deleteVppEpEvent = new VppEndpointConfEvent(BASIC_VPP_EP_IID, vhostEp, null);
148
149         manager.vppEndpointChanged(createVppEpEvent);
150         manager.vppEndpointChanged(deleteVppEpEvent);
151         // assert state on data store behind mount point
152         ReadOnlyTransaction rTxMount = mountPointDataBroker.newReadOnlyTransaction();
153         Optional<Interface> potentialIface =
154                 rTxMount.read(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.builder(Interfaces.class)
155                     .child(Interface.class, new InterfaceKey(vhostEp.getVppInterfaceName()))
156                     .build()).get();
157         Assert.assertFalse(potentialIface.isPresent());
158         // assert state on ODL data store
159         ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
160         ProviderAddressEndpointLocation providerAddressEndpointLocation =
161                 VppEndpointLocationProvider.createProviderAddressEndpointLocation(vhostEp);
162         InstanceIdentifier<ProviderAddressEndpointLocation> providerAddressEndpointLocationIid = IidFactory
163             .providerAddressEndpointLocationIid(VppEndpointLocationProvider.VPP_ENDPOINT_LOCATION_PROVIDER,
164                     providerAddressEndpointLocation.getKey());
165         Optional<ProviderAddressEndpointLocation> optProvEpLoc =
166                 rTx.read(LogicalDatastoreType.CONFIGURATION, providerAddressEndpointLocationIid).get();
167         Assert.assertFalse(optProvEpLoc.isPresent());
168     }
169
170     private VppEndpointBuilder vhostVppEpBuilder() {
171         return basicVppEpBuilder().setVppInterfaceName(IFACE_NAME)
172             .setVppNodePath(NODE_IID)
173             .setInterfaceTypeChoice(new VhostUserCaseBuilder().setSocket(SOCKET).build());
174     }
175
176     private VppEndpointBuilder basicVppEpBuilder() {
177         return new VppEndpointBuilder().setAddress(ADDRESS)
178             .setAddressType(AddressType.class)
179             .setContextId(CONTEXT_ID)
180             .setContextType(ContextType.class);
181     }
182 }