2 * Copyright (c) 2016 Red Hat, 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.netvirt.utils.netvirt.it.utils;
12 import java.util.UUID;
13 import java.util.Vector;
15 import com.google.common.collect.Maps;
16 import org.junit.Assert;
17 import org.opendaylight.netvirt.utils.neutron.utils.NeutronUtils;
18 import org.opendaylight.neutron.spi.NeutronNetwork;
19 import org.opendaylight.neutron.spi.NeutronPort;
20 import org.opendaylight.neutron.spi.NeutronSecurityGroup;
21 import org.opendaylight.neutron.spi.NeutronSubnet;
22 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
26 * A utility class used in integration tests that need to create neutron networks with some ports.
27 * Please see NetvirtIT#testNeutronNet for an example of how this class is used
29 public class NeutronNetItUtil {
31 public final String tenantId;
32 public final String id;
33 public final String subnetId;
34 public NeutronNetwork neutronNetwork;
35 public NeutronSubnet neutronSubnet;
36 public String segId = "100";
41 public SouthboundUtils southboundUtils;
42 public NeutronUtils neutronUtils;
43 public Vector<NeutronPort> neutronPorts = new Vector();
46 * Construct a new NeutronNetItUtil.
47 * @param southboundUtils used to create termination points
48 * @param tenantId tenant ID
50 public NeutronNetItUtil(SouthboundUtils southboundUtils, String tenantId) {
51 this(southboundUtils, tenantId, "101", "f7:00:00:0f:00:", "10.0.0.", "10.0.0.0/24");
55 * Construct a new NeutronNetItUtil.
56 * @param southboundUtils used to create termination points
57 * @param tenantId tenant ID
58 * @param segId the segmentation id to use for the neutron network
59 * @param macPfx the first characters of the mac addresses generated for ports. Format is "f7:00:00:0f:00:"
60 * @param ipPfx the first characters of the ip addresses generated for ports. Format is "10.0.0."
61 * @param cidr the cidr for this network, e.g., "10.0.0.0/24"
63 public NeutronNetItUtil(SouthboundUtils southboundUtils, String tenantId,
64 String segId, String macPfx, String ipPfx, String cidr) {
65 this.tenantId = tenantId;
71 this.id = UUID.randomUUID().toString();
72 this.subnetId = UUID.randomUUID().toString();
74 this.southboundUtils = southboundUtils;
75 neutronUtils = new NeutronUtils();
76 neutronNetwork = null;
81 * Create the network and subnet.
83 public void create() {
84 neutronNetwork = neutronUtils.createNeutronNetwork(id, tenantId, "vxlan", segId);
85 neutronSubnet = neutronUtils.createNeutronSubnet(subnetId, tenantId, id, "10.0.0.0/24");
89 * Clean up all created neutron objects.
91 public void destroy() {
92 for (NeutronPort port : neutronPorts) {
93 neutronUtils.removeNeutronPort(port.getID());
97 if (neutronSubnet != null) {
98 neutronUtils.removeNeutronSubnet(neutronSubnet.getID());
102 if (neutronNetwork != null) {
103 neutronUtils.removeNeutronNetwork(neutronNetwork.getID());
104 neutronNetwork = null;
109 * Create a port on the network. The deviceOwner will be set to "compute:None".
110 * @param bridge bridge where the port will be created on OVS
111 * @param portName name for this port
112 * @throws InterruptedException if we're interrupted while waiting for objects to be created
114 public void createPort(Node bridge, String portName) throws InterruptedException {
115 createPort(bridge, portName, "compute:None");
119 * Create a port on the network. The deviceOwner will be set to "compute:None".
120 * @param bridge bridge where the port will be created on OVS
121 * @param portName name for this port
122 * @param owner deviceOwner, e.g., "network:dhcp"
123 * @param secGroups Optional NeutronSecurityGroup objects see NeutronUtils.createNeutronSecurityGroup()
124 * @throws InterruptedException if we're interrupted while waiting for objects to be created
126 public void createPort(Node bridge, String portName, String owner, NeutronSecurityGroup... secGroups) throws InterruptedException {
127 long idx = neutronPorts.size() + 1;
128 Assert.assertTrue(idx < 256);
129 String mac = macFor(idx);
130 String ip = ipFor(idx);
131 String portId = UUID.randomUUID().toString();
132 neutronPorts.add(neutronUtils.createNeutronPort(id, subnetId, portId, owner, ip, mac, secGroups));
134 //TBD: Use NotifyingDataChangeListener
137 Map<String, String> externalIds = Maps.newHashMap();
138 externalIds.put("attached-mac", mac);
139 externalIds.put("iface-id", portId);
140 southboundUtils.addTerminationPoint(bridge, portName, "internal", null, externalIds, idx);
144 * Get the mac address for the n'th port created on this network (starts at 1).
145 * @param portNum index of port created
146 * @return the mac address
148 public String macFor(long portNum) {
149 return macPfx + String.format("%02x", portNum);
153 * Get the ip address for the n'th port created on this network (starts at 1).
154 * @param portNum index of port created
155 * @return the mac address
157 public String ipFor(long portNum) {
158 return ipPfx + portNum;