2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.neutron.vpp.mapper.processors;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.eq;
13 import static org.mockito.Mockito.verify;
15 import java.util.concurrent.ExecutionException;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mockito;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
22 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
23 import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.Mappings;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.GbpByNeutronMappings;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.gbp.by.neutron.mappings.BaseEndpointsByPorts;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.gbp.by.neutron.mappings.base.endpoints.by.ports.BaseEndpointByPort;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.PortBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 public class NeutronListenerTest extends AbstractDataBrokerTest {
37 private DataBroker dataBroker;
39 private NodeId routingNode;
41 private BaseEndpointByPort bebp;
42 private NeutronListener neutronListener;
43 private PortAware baseEpByPortListener;
47 port = TestUtils.createValidVppPort();
48 bebp = TestUtils.createBaseEndpointByPortForPort();
49 dataBroker = getDataBroker();
50 neutronListener = new NeutronListener(dataBroker, routingNode);
51 neutronListener.clearDataChangeProviders();
52 baseEpByPortListener = Mockito.spy(new PortAware(new PortHandler(
53 dataBroker, routingNode), dataBroker));
54 neutronListener.addDataChangeProvider(baseEpByPortListener);
58 public void constructorTest() {
59 dataBroker = Mockito.spy(dataBroker);
60 NeutronListener neutronListener = new NeutronListener(dataBroker, routingNode);
61 verify(dataBroker).registerDataTreeChangeListener(
62 eq(new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION,
63 InstanceIdentifier.builder(Neutron.class)
64 .build())), any(NeutronListener.class));
65 verify(dataBroker).registerDataTreeChangeListener(
66 eq(new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL,
67 InstanceIdentifier.builder(Mappings.class)
68 .child(GbpByNeutronMappings.class)
69 .child(BaseEndpointsByPorts.class)
70 .child(BaseEndpointByPort.class)
71 .build())), any(PortAware.class));
72 neutronListener.close();
76 public void testProcessCreatedNeutronDto() throws Exception {
77 putPortAndBaseEndpointByPort();
78 neutronListener.close();
79 verify(baseEpByPortListener).processCreatedNeutronDto(port);
83 public void testProcessUpdatedNeutronDto() throws Exception {
84 putPortAndBaseEndpointByPort();
85 Port updatedPort = new PortBuilder(port).setName("renamed").build();
86 WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
87 wTx.put(LogicalDatastoreType.CONFIGURATION, TestUtils.createPortIid(updatedPort.getKey()), updatedPort);
89 neutronListener.close();
90 verify(baseEpByPortListener).processUpdatedNeutronDto(port, updatedPort);
94 public void testProcessDeletedNeutronDto() throws Exception {
95 putPortAndBaseEndpointByPort();
96 WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
97 wTx.delete(LogicalDatastoreType.CONFIGURATION, TestUtils.createPortIid(port.getKey()));
99 verify(baseEpByPortListener).processDeletedNeutronDto(port);
102 private void putPortAndBaseEndpointByPort() throws InterruptedException, ExecutionException {
103 WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
104 wTx.put(LogicalDatastoreType.CONFIGURATION, TestUtils.createPortIid(port.getKey()), port);
105 wTx.put(LogicalDatastoreType.OPERATIONAL, TestUtils.createBaseEpByPortIid(port.getUuid()), bebp);