Bug 8228 - metadata service fix made cleaner
[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.binding.api.WriteTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.groupbasedpolicy.renderer.vpp.event.VppEndpointConfEvent;
24 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.MountedDataBrokerProvider;
25 import org.opendaylight.groupbasedpolicy.test.CustomDataBrokerTest;
26 import org.opendaylight.groupbasedpolicy.util.IidFactory;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpoint;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpointBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpointKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContextId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.LocationProviders;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.LocationProvider;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocation;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.rev160427.AddressType;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.rev160427.ContextType;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.Config;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425._interface.attributes._interface.type.choice.VhostUserCaseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.VppEndpoint;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.VppEndpointBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.VppEndpointKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170315.VhostUser;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170315.VhostUserRole;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170315.VppInterfaceAugmentation;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev170315.BridgeDomains;
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.network.topology.topology.Node;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52
53 import com.google.common.base.Optional;
54 import com.google.common.collect.ImmutableList;
55
56 public class InterfaceManagerTest extends CustomDataBrokerTest {
57
58     private final static String ADDRESS = "1.1.1.1/32";
59     private final static ContextId CONTEXT_ID = new ContextId("ctx1");
60     private final static String IFACE_NAME = "ifaceName1";
61     private final static VppEndpointKey BASIC_VPP_EP_KEY =
62             new VppEndpointKey(ADDRESS, AddressType.class, CONTEXT_ID, ContextType.class);
63     private final static InstanceIdentifier<VppEndpoint> BASIC_VPP_EP_IID =
64             InstanceIdentifier.builder(Config.class).child(VppEndpoint.class, BASIC_VPP_EP_KEY).build();
65     private final static NodeKey NODE_KEY = new NodeKey(new NodeId("node1"));
66     private final static String SOCKET = "socket1";
67
68     private InterfaceManager manager;
69     private MountedDataBrokerProvider mountedDataProviderMock;
70     private DataBroker mountPointDataBroker;
71     private DataBroker dataBroker;
72
73     @Override
74     public Collection<Class<?>> getClassesFromModules() {
75         return Arrays.asList(Node.class, VppEndpoint.class, Interfaces.class, BridgeDomains.class,
76                 LocationProviders.class);
77     }
78
79     @Before
80     public void init() throws Exception {
81         mountedDataProviderMock = Mockito.mock(MountedDataBrokerProvider.class);
82         mountPointDataBroker = getDataBroker();
83         setup(); // initialize new data broker for ODL data store
84         dataBroker = getDataBroker();
85         Mockito.when(mountedDataProviderMock.getDataBrokerForMountPoint(Mockito.any(InstanceIdentifier.class)))
86             .thenReturn(Optional.of(mountPointDataBroker));
87         manager = new InterfaceManager(mountedDataProviderMock, dataBroker);
88     }
89
90     @Test
91     public void testVppEndpointChanged_created() throws Exception {
92         AddressEndpoint addrEp = new AddressEndpointBuilder()
93             .setKey(new AddressEndpointKey(vhostVppEpBuilder().getAddress(), vhostVppEpBuilder().getAddressType(),
94                     vhostVppEpBuilder().getContextId(), vhostVppEpBuilder().getContextType()))
95             .setEndpointGroup(ImmutableList.of())
96             .build();
97         VppEndpointLocationProvider vppEpLocProvider = new VppEndpointLocationProvider(dataBroker);
98         WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
99         wTx.put(LogicalDatastoreType.OPERATIONAL, IidFactory.addressEndpointIid(addrEp.getKey()), addrEp, true);
100         wTx.submit().get();
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.rev170315.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         Assert.assertNull(vppIface.getL2()); //TODO test case for adding interface to BD
123         // assert state on ODL data store
124         ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
125         Optional<LocationProvider> optLocationProvider = rTx.read(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VppEndpointLocationProvider.VPP_ENDPOINT_LOCATION_PROVIDER)).get();
126         Assert.assertTrue(optLocationProvider.isPresent());
127         List<ProviderAddressEndpointLocation> epLocs = optLocationProvider.get().getProviderAddressEndpointLocation();
128         Assert.assertNotNull(epLocs);
129         Assert.assertEquals(1, epLocs.size());
130         vppEpLocProvider.close();
131     }
132
133     @Test
134     public void testVppEndpointChanged_deleted() throws Exception {
135         VppEndpoint vhostEp = vhostVppEpBuilder().build();
136         VppEndpointConfEvent createVppEpEvent = new VppEndpointConfEvent(BASIC_VPP_EP_IID, null, vhostEp);
137         VppEndpointConfEvent deleteVppEpEvent = new VppEndpointConfEvent(BASIC_VPP_EP_IID, vhostEp, null);
138
139         manager.vppEndpointChanged(createVppEpEvent);
140         manager.vppEndpointChanged(deleteVppEpEvent);
141         // assert state on data store behind mount point
142         ReadOnlyTransaction rTxMount = mountPointDataBroker.newReadOnlyTransaction();
143         Optional<Interface> potentialIface =
144                 rTxMount.read(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.builder(Interfaces.class)
145                     .child(Interface.class, new InterfaceKey(vhostEp.getVppInterfaceName()))
146                     .build()).get();
147         Assert.assertFalse(potentialIface.isPresent());
148         // assert state on ODL data store
149         ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
150         VppEndpointLocationProvider.createProviderAddressEndpointLocationKey(vhostEp);
151         InstanceIdentifier<ProviderAddressEndpointLocation> providerAddressEndpointLocationIid = IidFactory
152             .providerAddressEndpointLocationIid(VppEndpointLocationProvider.VPP_ENDPOINT_LOCATION_PROVIDER,
153                     VppEndpointLocationProvider.createProviderAddressEndpointLocationKey(vhostEp));
154         Optional<ProviderAddressEndpointLocation> optProvEpLoc =
155                 rTx.read(LogicalDatastoreType.CONFIGURATION, providerAddressEndpointLocationIid).get();
156         Assert.assertFalse(optProvEpLoc.isPresent());
157     }
158
159     private VppEndpointBuilder vhostVppEpBuilder() {
160         return basicVppEpBuilder().setVppInterfaceName(IFACE_NAME)
161             .setVppNodeId(NODE_KEY.getNodeId())
162             .setInterfaceTypeChoice(new VhostUserCaseBuilder().setSocket(SOCKET).build());
163     }
164
165     private VppEndpointBuilder basicVppEpBuilder() {
166         return new VppEndpointBuilder().setAddress(ADDRESS)
167             .setAddressType(AddressType.class)
168             .setContextId(CONTEXT_ID)
169             .setContextType(ContextType.class);
170     }
171 }