translator: remove dependency of neutron.spi
[netvirt.git] / utils / netvirt-it-utils / src / main / java / org / opendaylight / netvirt / utils / netvirt / it / utils / NeutronNetItUtil.java
1 /*
2  * Copyright (c) 2016 Red Hat, 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.netvirt.utils.netvirt.it.utils;
10
11 import java.util.Map;
12 import java.util.UUID;
13 import java.util.Vector;
14
15 import com.google.common.collect.Maps;
16 import org.junit.Assert;
17 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronNetwork;
18 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronPort;
19 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronSecurityGroup;
20 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronSubnet;
21 import org.opendaylight.netvirt.utils.neutron.utils.NeutronUtils;
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;
24
25 /**
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
28  */
29 public class NeutronNetItUtil {
30
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";
37     public String macPfx;
38     public String ipPfx;
39     public String cidr;
40
41     public SouthboundUtils southboundUtils;
42     public NeutronUtils neutronUtils;
43     public Vector<NeutronPort> neutronPorts = new Vector();
44
45     /**
46      * Construct a new NeutronNetItUtil.
47      * @param southboundUtils used to create termination points
48      * @param tenantId tenant ID
49      */
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");
52     }
53
54     /**
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"
62      */
63     public NeutronNetItUtil(SouthboundUtils southboundUtils, String tenantId,
64                             String segId, String macPfx, String ipPfx, String cidr) {
65         this.tenantId = tenantId;
66         this.segId = segId;
67         this.macPfx = macPfx;
68         this.ipPfx = ipPfx;
69         this.cidr = cidr;
70
71         this.id = UUID.randomUUID().toString();
72         this.subnetId = UUID.randomUUID().toString();
73
74         this.southboundUtils = southboundUtils;
75         neutronUtils = new NeutronUtils();
76         neutronNetwork = null;
77         neutronSubnet = null;
78     }
79
80     /**
81      * Create the network and subnet.
82      */
83     public void create() {
84         neutronNetwork = neutronUtils.createNeutronNetwork(id, tenantId, "vxlan", segId);
85         neutronSubnet = neutronUtils.createNeutronSubnet(subnetId, tenantId, id, "10.0.0.0/24");
86     }
87
88     /**
89      * Clean up all created neutron objects.
90      */
91     public void destroy() {
92         for (NeutronPort port : neutronPorts) {
93             neutronUtils.removeNeutronPort(port.getID());
94         }
95         neutronPorts.clear();
96
97         if (neutronSubnet != null) {
98             neutronUtils.removeNeutronSubnet(neutronSubnet.getID());
99             neutronSubnet = null;
100         }
101
102         if (neutronNetwork != null) {
103             neutronUtils.removeNeutronNetwork(neutronNetwork.getID());
104             neutronNetwork = null;
105         }
106     }
107
108     /**
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
113      */
114     public void createPort(Node bridge, String portName) throws InterruptedException {
115         createPort(bridge, portName, "compute:None");
116     }
117
118     /**
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
125      */
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));
133
134         //TBD: Use NotifyingDataChangeListener
135         Thread.sleep(1000);
136
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);
141     }
142
143     /**
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
147      */
148     public String macFor(long portNum) {
149         return macPfx + String.format("%02x", portNum);
150     }
151
152     /**
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
156      */
157     public String ipFor(long portNum) {
158         return ipPfx + portNum;
159     }
160 }
161