Fix checkstyle
[neutron.git] / neutron-hostconfig / vpp / src / test / java / org / opendaylight / neutron / hostconfig / vpp / NeutronHostconfigVppListenerTest.java
1 /*
2  * Copyright (c) 2017 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 package org.opendaylight.neutron.hostconfig.vpp;
9
10 import com.google.common.util.concurrent.SettableFuture;
11 import java.util.Arrays;
12 import java.util.Collection;
13 import java.util.List;
14 import java.util.concurrent.ExecutionException;
15 import java.util.stream.Collectors;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.junit.After;
18 import org.junit.Assert;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
22 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
23 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
24 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
25 import org.opendaylight.mdsal.binding.api.DataTreeModification;
26 import org.opendaylight.mdsal.binding.api.WriteTransaction;
27 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.AvailableCapabilitiesBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapabilityBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.Hostconfigs;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.hostconfigs.Hostconfig;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.hostconfigs.HostconfigKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
46 import org.opendaylight.yangtools.concepts.ListenerRegistration;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48
49 public class NeutronHostconfigVppListenerTest extends HostconfigsDataBrokerTest
50         implements ClusteredDataTreeChangeListener<Hostconfig> {
51
52     private static final String V3PO_1704_CAPABILITY =
53         "(urn:opendaylight:params:xml:ns:yang:v3po?revision=2017-03-15)v3po";
54     private static final String V3PO_1701_CAPABILITY =
55         "(urn:opendaylight:params:xml:ns:yang:v3po?revision=2016-12-14)v3po";
56     private static final String INTERFACES =
57             "(urn:ietf:params:xml:ns:yang:ietf-interfaces?revision=2014-05-08)ietf-interfaces";
58     private static final NodeId NODE_ID = new NodeId("node1");
59     private static final String SOCKET_PATH = "/tmp";
60     private static final String SOCKET_PREFIX = "socket_";
61     private static final String VHOSTUSER_MODE = "server";
62     private SettableFuture<Integer> sf;
63     private ListenerRegistration<DataTreeChangeListener<Hostconfig>> listenerRegistration;
64     private NeutronHostconfigVppListener neutronHostconfigVppListener;
65
66     @Before
67     public void init() throws InterruptedException, ExecutionException {
68         DataTreeIdentifier<Hostconfig> dataTreeIdentifier = DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
69                 hostConfigIid(NODE_ID).firstIdentifierOf(Hostconfigs.class).builder().child(Hostconfig.class).build());
70         listenerRegistration = getDataBroker().registerDataTreeChangeListener(dataTreeIdentifier,
71                 NeutronHostconfigVppListenerTest.this);
72         neutronHostconfigVppListener =
73                 new NeutronHostconfigVppListener(getDataBroker(), SOCKET_PATH, SOCKET_PREFIX, VHOSTUSER_MODE);
74         neutronHostconfigVppListener.init();
75         sf = SettableFuture.create();
76     }
77
78     @Test
79     public void testPutCreateParentsSuccess() throws Exception {
80         InstanceIdentifier<Node> iid = InstanceIdentifier.builder(NetworkTopology.class)
81             .child(Topology.class, new TopologyKey(new TopologyId("topology-netconf")))
82             .child(Node.class, new NodeKey(new NodeId(NODE_ID)))
83             .build();
84         Node node1 =
85             createNetconfNode(NODE_ID, V3PO_1704_CAPABILITY, V3PO_1701_CAPABILITY, INTERFACES);
86         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
87         writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, node1, true);
88         writeTx.commit().get();
89         Assert.assertEquals(sf.get(), Integer.valueOf(1));
90         sf = SettableFuture.create();
91         writeTx = getDataBroker().newWriteOnlyTransaction();
92         writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid);
93         writeTx.commit().get();
94         Assert.assertEquals(sf.get(), Integer.valueOf(2));
95     }
96
97     private InstanceIdentifier<Hostconfig> hostConfigIid(@NonNull NodeId nodeId) {
98         return InstanceIdentifier.builder(Neutron.class)
99             .child(Hostconfigs.class)
100             .child(Hostconfig.class, new HostconfigKey(nodeId.getValue(), HostconfigUtil.L2_HOST_TYPE))
101             .build();
102     }
103
104     private Node createNetconfNode(NodeId nodeId, String... capabilities) {
105         List<AvailableCapability> caps = Arrays.asList(capabilities)
106             .stream()
107             .map(name -> new AvailableCapabilityBuilder().setCapability(name).build())
108             .collect(Collectors.toList());
109         NetconfNode netconfNode = new NetconfNodeBuilder().setConnectionStatus(ConnectionStatus.Connected)
110             .setAvailableCapabilities(new AvailableCapabilitiesBuilder().setAvailableCapability(caps).build())
111             .build();
112         return new NodeBuilder().setNodeId(nodeId).addAugmentation(NetconfNode.class, netconfNode).build();
113     }
114
115     @Override
116     public void onDataTreeChanged(Collection<DataTreeModification<Hostconfig>> hostConfigDtm) {
117         for (DataTreeModification<Hostconfig> dtm : hostConfigDtm) {
118             ModificationType mod = dtm.getRootNode().getModificationType();
119             switch (mod) {
120                 case WRITE: {
121                     sf.set(1);
122                     break;
123                 }
124                 case DELETE: {
125                     sf.set(2);
126                     break;
127                 }
128                 default:
129             }
130         }
131     }
132
133     @After
134     public void close() throws Exception {
135         if (listenerRegistration != null) {
136             listenerRegistration = null;
137         }
138         if (neutronHostconfigVppListener != null) {
139             neutronHostconfigVppListener.close();
140         }
141     }
142 }