2 * Copyright © 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.neutron.utils;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.List;
14 import java.util.UUID;
16 import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
17 import org.opendaylight.neutron.spi.INeutronPortCRUD;
18 import org.opendaylight.neutron.spi.INeutronSecurityGroupCRUD;
19 import org.opendaylight.neutron.spi.INeutronSecurityRuleCRUD;
20 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
21 import org.opendaylight.neutron.spi.NeutronNetwork;
22 import org.opendaylight.neutron.spi.NeutronPort;
23 import org.opendaylight.neutron.spi.NeutronSecurityGroup;
24 import org.opendaylight.neutron.spi.NeutronSecurityRule;
25 import org.opendaylight.neutron.spi.NeutronSubnet;
26 import org.opendaylight.netvirt.utils.servicehelper.ServiceHelper;
28 public class NeutronUtils {
29 public NeutronPort createNeutronPort(String networkId, String subnetId,
30 String id, String owner, String ipaddr, String mac, NeutronSecurityGroup... secGroups) {
31 INeutronPortCRUD iNeutronPortCRUD =
32 (INeutronPortCRUD) ServiceHelper.getGlobalInstance(INeutronPortCRUD.class, this);
33 NeutronPort np = new NeutronPort();
36 np.setDeviceOwner(owner);
37 np.setMacAddress(mac);
38 np.setNetworkUUID(networkId);
39 List<org.opendaylight.neutron.spi.Neutron_IPs> srcAddressList =
41 org.opendaylight.neutron.spi.Neutron_IPs nip = new org.opendaylight.neutron.spi.Neutron_IPs();
42 nip.setIpAddress(ipaddr);
43 nip.setSubnetUUID(subnetId);
44 srcAddressList.add(nip);
45 np.setFixedIPs(srcAddressList);
46 List<NeutronSecurityGroup> nsgs = Arrays.asList(secGroups);
47 np.setSecurityGroups(nsgs);
48 iNeutronPortCRUD.add(np);
52 public boolean removeNeutronPort(String uuid) {
53 INeutronPortCRUD iNeutronPortCRUD =
54 (INeutronPortCRUD) ServiceHelper.getGlobalInstance(INeutronPortCRUD.class, this);
55 return iNeutronPortCRUD.removePort(uuid);
58 public NeutronSubnet createNeutronSubnet(String subnetId, String tenantId,
59 String networkId, String cidr) {
60 INeutronSubnetCRUD iNeutronSubnetCRUD =
61 (INeutronSubnetCRUD) ServiceHelper.getGlobalInstance(INeutronSubnetCRUD.class, this);
62 NeutronSubnet ns = new NeutronSubnet();
66 ns.setNetworkUUID(networkId);
67 ns.setTenantID(tenantId);
68 iNeutronSubnetCRUD.add(ns);
72 public boolean removeNeutronSubnet(String uuid) {
73 INeutronSubnetCRUD iNeutronSubnetCRUD =
74 (INeutronSubnetCRUD) ServiceHelper.getGlobalInstance(INeutronSubnetCRUD.class, this);
75 return iNeutronSubnetCRUD.removeSubnet(uuid);
78 public NeutronNetwork createNeutronNetwork(String uuid, String tenantID, String networkTypeVxlan, String segId) {
79 INeutronNetworkCRUD iNeutronNetworkCRUD =
80 (INeutronNetworkCRUD) ServiceHelper.getGlobalInstance(INeutronNetworkCRUD.class, this);
81 NeutronNetwork nn = new NeutronNetwork();
84 nn.setTenantID(tenantID);
85 nn.setProviderNetworkType(networkTypeVxlan);
86 nn.setProviderSegmentationID(segId);
87 iNeutronNetworkCRUD.addNetwork(nn);
91 public boolean removeNeutronNetwork(String uuid) {
92 INeutronNetworkCRUD iNeutronNetworkCRUD =
93 (INeutronNetworkCRUD) ServiceHelper.getGlobalInstance(INeutronNetworkCRUD.class, this);
94 return iNeutronNetworkCRUD.removeNetwork(uuid);
99 * Build a NeutronSecurityRule that can be passed in to createNeutronSecurityGroup.
100 * @param direction e.g., "ingress". May be null.
101 * @param ethertype e.g., "IPv4". May be null.
102 * @param protocol e.g., "TCP". May be null.
103 * @param ipPrefix e.g., "10.9.8.0/24". May be null.
104 * @param portMin or null
105 * @param portMax or null
106 * @return A new NeutronSecurityRule
108 public NeutronSecurityRule buildNeutronSecurityRule(String direction, String ethertype, String protocol,
109 String ipPrefix, Integer portMin, Integer portMax) {
110 NeutronSecurityRule rule = new NeutronSecurityRule();
111 rule.setID(UUID.randomUUID().toString());
112 rule.setSecurityRemoteGroupID(null);
113 rule.setSecurityRuleDirection(direction);
114 rule.setSecurityRuleEthertype(ethertype);
115 rule.setSecurityRuleProtocol(protocol);
116 rule.setSecurityRuleRemoteIpPrefix(ipPrefix);
117 rule.setSecurityRulePortMin(portMin);
118 rule.setSecurityRulePortMax(portMax);
124 * Create a new NeutronSecurityGroup and create the NeutronSecurityRules passed in. This method will first create
125 * teh NeutronSecurityRules and then the NeutronSecurityGroup in md-sal.
126 * @param tenantId The tenant ID for both the rules and groups
127 * @param rules NeutronSecurityRules. You can create them with buildNeutronSecurityRule.
128 * @return A new NeutronSecurityGroup
130 public NeutronSecurityGroup createNeutronSecurityGroup(String tenantId, NeutronSecurityRule... rules) {
131 INeutronSecurityGroupCRUD groupCRUD =
132 (INeutronSecurityGroupCRUD) ServiceHelper.getGlobalInstance(INeutronSecurityGroupCRUD.class, this);
133 INeutronSecurityRuleCRUD ruleCRUD =
134 (INeutronSecurityRuleCRUD) ServiceHelper.getGlobalInstance(INeutronSecurityRuleCRUD.class, this);
136 String id = UUID.randomUUID().toString();
137 NeutronSecurityGroup sg = new NeutronSecurityGroup();
138 sg.setSecurityGroupName("SG-" + id);
140 sg.setTenantID(tenantId);
142 List<NeutronSecurityRule> ruleList = new ArrayList<>(rules.length);
143 for (NeutronSecurityRule rule : rules) {
144 rule.setTenantID(tenantId);
145 rule.setSecurityRuleGroupID(id);
147 ruleCRUD.addNeutronSecurityRule(rule);
156 * Remove the NeutronSecurityGroup and its associated NeutronSecurityRules from md-sal
157 * @param sg NeutronSecurityGroup to remove
159 public void removeNeutronSecurityGroupAndRules(NeutronSecurityGroup sg) {
160 INeutronSecurityGroupCRUD groupCRUD =
161 (INeutronSecurityGroupCRUD) ServiceHelper.getGlobalInstance(INeutronSecurityGroupCRUD.class, this);
163 groupCRUD.removeNeutronSecurityGroup(sg.getID());
167 * Get the NeutronSecurityRule and its associated NeutronSecurityGroup
168 * @param sg NeutronSecurityGroup to to get the rules for
169 * @return List of NeutronSecurityRule
171 public List<NeutronSecurityRule> getNeutronSecurityGroupRules(NeutronSecurityGroup sg) {
172 INeutronSecurityRuleCRUD ruleCRUD =
173 (INeutronSecurityRuleCRUD) ServiceHelper.getGlobalInstance(INeutronSecurityRuleCRUD.class, this);
175 List<NeutronSecurityRule> rules = new ArrayList<>();
176 List<NeutronSecurityRule> securityRules = ruleCRUD.getAllNeutronSecurityRules();
177 for (NeutronSecurityRule securityRule : securityRules) {
178 if (sg.getID().equals(securityRule.getSecurityRuleGroupID())) {
179 rules.add(securityRule);