Add INFO.yaml for GBP
[groupbasedpolicy.git] / neutron-vpp-mapper / src / test / java / org / opendaylight / groupbasedpolicy / neutron / vpp / mapper / processors / NeutronListenerTest.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.neutron.vpp.mapper.processors;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.eq;
13 import static org.mockito.Mockito.verify;
14
15 import java.util.concurrent.ExecutionException;
16
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;
34
35 public class NeutronListenerTest extends AbstractDataBrokerTest {
36
37     private DataBroker dataBroker;
38
39     private NodeId routingNode;
40     private Port port;
41     private BaseEndpointByPort bebp;
42     private NeutronListener neutronListener;
43     private PortAware baseEpByPortListener;
44
45     @Before
46     public void init() {
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);
55     }
56
57     @Test
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();
73     }
74
75     @Test
76     public void testProcessCreatedNeutronDto() throws Exception {
77         putPortAndBaseEndpointByPort();
78         neutronListener.close();
79         verify(baseEpByPortListener).processCreatedNeutronDto(port);
80     }
81
82     @Test
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);
88         wTx.submit().get();
89         neutronListener.close();
90         verify(baseEpByPortListener).processUpdatedNeutronDto(port, updatedPort);
91     }
92
93     @Test
94     public void testProcessDeletedNeutronDto() throws Exception {
95         putPortAndBaseEndpointByPort();
96         WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
97         wTx.delete(LogicalDatastoreType.CONFIGURATION, TestUtils.createPortIid(port.getKey()));
98         wTx.submit().get();
99         verify(baseEpByPortListener).processDeletedNeutronDto(port);
100     }
101
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);
106         wTx.submit().get();
107     }
108 }