You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by wi...@apache.org on 2015/04/15 10:05:03 UTC
[03/53] [abbrv] git commit: updated refs/heads/reporter to 5c99784
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/01864ef7/plugins/network-elements/bigswitch/src/com/cloud/network/bigswitch/NetworkData.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/bigswitch/src/com/cloud/network/bigswitch/NetworkData.java b/plugins/network-elements/bigswitch/src/com/cloud/network/bigswitch/NetworkData.java
new file mode 100644
index 0000000..cafbc90
--- /dev/null
+++ b/plugins/network-elements/bigswitch/src/com/cloud/network/bigswitch/NetworkData.java
@@ -0,0 +1,217 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package com.cloud.network.bigswitch;
+
+import java.util.List;
+
+import com.google.gson.annotations.SerializedName;
+
+/**
+ * NetworkData contains information expected by Big Switch Controller
+ * in CreateBcfSegmentCommand
+ */
+public class NetworkData {
+ private final Network network;
+
+ public Network getNetwork() {
+ return network;
+ }
+
+ public NetworkData() {
+ network = new Network();
+ }
+
+ public class Network {
+ @SerializedName("id") private String id;
+ @SerializedName("name") private String name = null;
+ @SerializedName("tenant_id") private String tenantId = null;
+ @SerializedName("tenant_name") private String tenantName = null;
+ @SerializedName("state") private String state = null;
+ @SerializedName("subnets") private List<Segment> segments = null;
+ @SerializedName("provider:segmentation_id") private Integer vlan = null;
+
+ public String getId() {
+ return id;
+ }
+
+ public void setId(String id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getTenantId() {
+ return tenantId;
+ }
+
+ public void setTenantId(String tenantId) {
+ this.tenantId = tenantId;
+ }
+
+ public String getTenantName() {
+ return tenantName;
+ }
+
+ public void setTenantName(String tenantName) {
+ this.tenantName = tenantName;
+ }
+
+ public String getState() {
+ return state;
+ }
+
+ public void setState(String state) {
+ this.state = state;
+ }
+
+ public List<Segment> getSegments() {
+ return segments;
+ }
+
+ public void setSegments(List<Segment> segments) {
+ this.segments = segments;
+ }
+
+ public Integer getVlan() {
+ return vlan;
+ }
+
+ public void setVlan(Integer vlan) {
+ this.vlan = vlan;
+ }
+ }
+
+ public class Segment {
+ @SerializedName("cidr") private String cidr;
+ @SerializedName("gateway_ip") private String gatewayIp;
+ @SerializedName("id") private String id;
+ @SerializedName("name") private String name;
+ @SerializedName("state") private String state;
+ @SerializedName("tenant_id") private String tenantId;
+
+ public String getCidr() {
+ return cidr;
+ }
+
+ public void setCidr(String cidr) {
+ this.cidr = cidr;
+ }
+
+ public String getGatewayIp() {
+ return gatewayIp;
+ }
+
+ public void setGatewayIp(String gatewayIp) {
+ this.gatewayIp = gatewayIp;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public void setId(String id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getState() {
+ return state;
+ }
+
+ public void setState(String state) {
+ this.state = state;
+ }
+
+ public String getTenantId() {
+ return tenantId;
+ }
+
+ public void setTenantId(String tenantId) {
+ this.tenantId = tenantId;
+ }
+ }
+
+
+
+ public class AttachmentInfo {
+ @SerializedName("id") private String id = null;
+ @SerializedName("mac") private String mac = null;
+
+ public AttachmentInfo(String id, String mac){
+ this.id = id;
+ this.mac = mac;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public void setId(String id) {
+ this.id = id;
+ }
+
+ public String getMac() {
+ return mac;
+ }
+
+ public void setMac(String mac) {
+ this.mac = mac;
+ }
+ }
+
+ public class SegmentInfo {
+ @SerializedName("network_type") private String networkType = null;
+ @SerializedName("segmentation_id") private Integer segmentationId = 0;
+
+ public SegmentInfo(String networkType, Integer segmentationId){
+ this.networkType = networkType;
+ this.segmentationId = segmentationId;
+ }
+
+ public String getNetworkType() {
+ return networkType;
+ }
+
+ public void setNetworkType(String networkType) {
+ this.networkType = networkType;
+ }
+
+ public Integer getSegmentationId() {
+ return segmentationId;
+ }
+
+ public void setSegmentationId(Integer segmentationId) {
+ this.segmentationId = segmentationId;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/01864ef7/plugins/network-elements/bigswitch/src/com/cloud/network/bigswitch/RouterData.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/bigswitch/src/com/cloud/network/bigswitch/RouterData.java b/plugins/network-elements/bigswitch/src/com/cloud/network/bigswitch/RouterData.java
new file mode 100644
index 0000000..2d61653
--- /dev/null
+++ b/plugins/network-elements/bigswitch/src/com/cloud/network/bigswitch/RouterData.java
@@ -0,0 +1,131 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package com.cloud.network.bigswitch;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.google.gson.annotations.SerializedName;
+
+public class RouterData {
+ @SerializedName("router") private Router router;
+
+ public RouterData(String tenantId){
+ router = new Router(tenantId);
+ }
+
+ public class Router {
+ @SerializedName("tenant_id") private String tenantId;
+ @SerializedName("id") private String id;
+ @SerializedName("external_gateway_info") private ExternalGateway externalGateway;
+ @SerializedName("router_rules") private List<AclData> acls;
+ @SerializedName("interfaces") private List<RouterInterfaceData.RouterInterface> interfaces;
+
+ public Router(String tenantId){
+ this.tenantId = tenantId;
+ this.id = tenantId;
+ this.externalGateway = null;
+ this.acls = new ArrayList<AclData>();
+ this.interfaces = null;
+ }
+
+ public List<AclData> getAcls() {
+ return acls;
+ }
+
+ public class ExternalGateway{
+ @SerializedName("tenant_id") private String tenantId;
+ @SerializedName("network_id") private String networkId;
+ @SerializedName("external_fixed_ips") private List<ExternalIp> externalIps;
+
+ public ExternalGateway(String gatewayIp){
+ this.tenantId = "external";
+ this.networkId = "external";
+ this.externalIps = new ArrayList<ExternalIp>();
+ if(gatewayIp!=null){
+ this.externalIps.add(new ExternalIp(gatewayIp));
+ }
+ }
+
+ public class ExternalIp{
+ @SerializedName("subnet_id") private String subnetId; //assume don't care for now
+ @SerializedName("ip_address") private String ipAddress;
+
+ public ExternalIp(String ipAddress){
+ this.subnetId = "";
+ this.ipAddress = ipAddress;
+ }
+
+ public String getSubnetId() {
+ return subnetId;
+ }
+
+ public String getIpAddress() {
+ return ipAddress;
+ }
+ }
+
+ public String getNetworkId() {
+ return networkId;
+ }
+
+ public List<ExternalIp> getExternalIps() {
+ return externalIps;
+ }
+ }
+
+ public void addExternalGateway(String gatewayIp){
+ if(gatewayIp != null){
+ this.externalGateway = new ExternalGateway(gatewayIp);
+ }
+ }
+
+ public String getTenantId() {
+ return tenantId;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public ExternalGateway getExternalGateway() {
+ return externalGateway;
+ }
+
+ public List<RouterInterfaceData.RouterInterface> getInterfaces() {
+ return interfaces;
+ }
+
+ public void addInterface(RouterInterfaceData intf){
+ if(this.interfaces == null){
+ this.interfaces = new ArrayList<RouterInterfaceData.RouterInterface>();
+ }
+ this.interfaces.add(intf.getRouterInterface());
+ }
+
+ public void setInterfaces(List<RouterInterfaceData.RouterInterface> interfaces) {
+ this.interfaces = interfaces;
+ }
+ }
+
+ public Router getRouter() {
+ return router;
+ }
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/01864ef7/plugins/network-elements/bigswitch/src/com/cloud/network/bigswitch/RouterInterfaceData.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/bigswitch/src/com/cloud/network/bigswitch/RouterInterfaceData.java b/plugins/network-elements/bigswitch/src/com/cloud/network/bigswitch/RouterInterfaceData.java
new file mode 100644
index 0000000..a00535e
--- /dev/null
+++ b/plugins/network-elements/bigswitch/src/com/cloud/network/bigswitch/RouterInterfaceData.java
@@ -0,0 +1,67 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package com.cloud.network.bigswitch;
+
+import com.google.gson.annotations.SerializedName;
+
+public class RouterInterfaceData {
+ @SerializedName("interface") private RouterInterface routerInterface;
+
+ public RouterInterfaceData(String tenantId, String gatewayIp, String cidr, String id, String name){
+ routerInterface = new RouterInterface(tenantId, gatewayIp, cidr, id, name);
+ }
+
+ public class RouterInterface {
+ @SerializedName("subnet") private NetworkData.Segment subnet;
+ @SerializedName("id") private String id;
+ @SerializedName("network") private NetworkData.Network network;
+
+ public RouterInterface(String tenantId, String gatewayIp, String cidr, String id, String name){
+ NetworkData data = new NetworkData();
+ this.subnet = data.new Segment();
+ this.subnet.setTenantId(tenantId);
+ this.subnet.setGatewayIp(gatewayIp);
+ this.subnet.setCidr(cidr);
+
+ this.id = id;
+
+ this.network = data.getNetwork();
+ this.network.setTenantId(tenantId);
+ this.network.setId(id);
+ this.network.setName(name);
+ }
+
+ public NetworkData.Segment getSubnet() {
+ return subnet;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public NetworkData.Network getNetwork() {
+ return network;
+ }
+ }
+
+ public RouterInterface getRouterInterface() {
+ return routerInterface;
+ }
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/01864ef7/plugins/network-elements/bigswitch/src/com/cloud/network/bigswitch/TopologyData.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/bigswitch/src/com/cloud/network/bigswitch/TopologyData.java b/plugins/network-elements/bigswitch/src/com/cloud/network/bigswitch/TopologyData.java
new file mode 100644
index 0000000..cbaeeff
--- /dev/null
+++ b/plugins/network-elements/bigswitch/src/com/cloud/network/bigswitch/TopologyData.java
@@ -0,0 +1,249 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package com.cloud.network.bigswitch;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.cloud.network.bigswitch.AttachmentData.Attachment.IpAddress;
+import com.cloud.network.bigswitch.NetworkData.AttachmentInfo;
+import com.cloud.network.bigswitch.NetworkData.Segment;
+import com.cloud.network.bigswitch.NetworkData.SegmentInfo;
+import com.cloud.network.bigswitch.RouterData.Router;
+import com.google.gson.annotations.SerializedName;
+
+public class TopologyData {
+ @SerializedName("networks") private final List<Network> networks;
+ @SerializedName("routers") private final List<Router> routers;
+
+ public void addNetwork(Network network) {
+ networks.add(network);
+ }
+
+ public void setNetworks(List<Network> networks) {
+ this.networks.clear();
+ this.networks.addAll(networks);
+ }
+
+ public void clearNetworks() {
+ this.networks.clear();
+ }
+
+ public void addRouter(Router router) {
+ routers.add(router);
+ }
+
+ public void setRouters(List<Router> routers) {
+ this.routers.clear();
+ this.routers.addAll(routers);
+ }
+
+ public void clearRouters() {
+ this.routers.clear();
+ }
+
+ public TopologyData() {
+ networks = new ArrayList<Network>();
+ routers = new ArrayList<Router>();
+ }
+
+ public class Network {
+ @SerializedName("id") private String id;
+ @SerializedName("name") private String name;
+ @SerializedName("tenant_id") private String tenantId;
+ @SerializedName("tenant_name") private String tenantName;
+ @SerializedName("provider:segmentation_id") private Integer vlan = null;
+ @SerializedName("state") private String state;
+ @SerializedName("ports") private List<Port> ports;
+ @SerializedName("subnets") private List<Segment> segments;
+
+ public String getId() {
+ return id;
+ }
+ public void setId(String id) {
+ this.id = id;
+ }
+ public String getName() {
+ return name;
+ }
+ public void setName(String name) {
+ this.name = name;
+ }
+ public String getTenantId() {
+ return tenantId;
+ }
+ public void setTenantId(String tenantId) {
+ this.tenantId = tenantId;
+ }
+ public String getTenantName() {
+ return tenantName;
+ }
+ public void setTenantName(String tenantName) {
+ this.tenantName = tenantName;
+ }
+ public Integer getVlan() {
+ return vlan;
+ }
+ public void setVlan(Integer vlan) {
+ this.vlan = vlan;
+ }
+ public String getState() {
+ return state;
+ }
+ public void setState(String state) {
+ this.state = state;
+ }
+ public List<Port> getPorts() {
+ return ports;
+ }
+ public void setPorts(List<Port> ports) {
+ this.ports = ports;
+ }
+ public List<Segment> getSegments() {
+ return segments;
+ }
+ public void setSegments(List<Segment> segments) {
+ this.segments = segments;
+ }
+ @Override
+ public String toString() {
+ return "Network [id=" + id + ", name=" + name + ", tenantId="
+ + tenantId + ", tenantName=" + tenantName + ", vlan="
+ + vlan + ", state=" + state + ", ports=" + ports
+ + ", segments=" + segments + "]";
+ }
+ }
+
+ public class Port {
+ @SerializedName("attachment") private AttachmentInfo attachment;
+ @SerializedName("binding:host_id") private String hostId;
+ @SerializedName("bound_segment") private SegmentInfo segmentInfo;
+ @SerializedName("device_owner") private String owner;
+ @SerializedName("fixed_ips") private List<IpAddress> ipAddresses;
+ @SerializedName("id") private String id;
+ @SerializedName("mac_address") private String mac;
+ @SerializedName("network") private NetworkData.Network network;
+ @SerializedName("state") private String state;
+ @SerializedName("tenant_id") private String tenantId;
+ @SerializedName("tenant_name") private String tenantName;
+
+ public AttachmentInfo getAttachment() {
+ return attachment;
+ }
+
+ public void setAttachmentInfo(AttachmentInfo attachment) {
+ this.attachment = attachment;
+ }
+
+ public String getHostId() {
+ return hostId;
+ }
+
+ public void setHostId(String hostId) {
+ this.hostId = hostId;
+ }
+
+ public SegmentInfo getSegmentInfo() {
+ return segmentInfo;
+ }
+
+ public void setSegmentInfo(SegmentInfo segmentInfo) {
+ this.segmentInfo = segmentInfo;
+ }
+
+ public String getOwner() {
+ return owner;
+ }
+
+ public void setOwner(String owner) {
+ this.owner = owner;
+ }
+
+ public List<IpAddress> getIpAddresses() {
+ return ipAddresses;
+ }
+
+ public void setIpAddresses(List<IpAddress> ipAddresses) {
+ this.ipAddresses = ipAddresses;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public void setId(String id) {
+ this.id = id;
+ }
+
+ public String getMac() {
+ return mac;
+ }
+
+ public void setMac(String mac) {
+ this.mac = mac;
+ }
+
+ public NetworkData.Network getNetwork() {
+ return network;
+ }
+
+ public void setNetwork(NetworkData.Network network) {
+ this.network = network;
+ }
+
+ public String getState() {
+ return state;
+ }
+
+ public void setState(String state) {
+ this.state = state;
+ }
+
+ public String getTenantId() {
+ return tenantId;
+ }
+
+ public void setTenantId(String tenantId) {
+ this.tenantId = tenantId;
+ }
+
+ public String getTenantName() {
+ return tenantName;
+ }
+
+ public void setTenantName(String tenantName) {
+ this.tenantName = tenantName;
+ }
+ }
+
+ public List<Network> getNetworks() {
+ return networks;
+ }
+
+ public List<Router> getRouters() {
+ return routers;
+ }
+
+ @Override
+ public String toString() {
+ return "TopologyData [networks=" + networks + ", routers=" + routers
+ + "]";
+ }
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/01864ef7/plugins/network-elements/bigswitch/src/com/cloud/network/bigswitch/TrustingProtocolSocketFactory.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/bigswitch/src/com/cloud/network/bigswitch/TrustingProtocolSocketFactory.java b/plugins/network-elements/bigswitch/src/com/cloud/network/bigswitch/TrustingProtocolSocketFactory.java
new file mode 100644
index 0000000..09c444b
--- /dev/null
+++ b/plugins/network-elements/bigswitch/src/com/cloud/network/bigswitch/TrustingProtocolSocketFactory.java
@@ -0,0 +1,114 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package com.cloud.network.bigswitch;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.net.UnknownHostException;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.X509Certificate;
+
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLSocketFactory;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
+
+import org.apache.commons.httpclient.ConnectTimeoutException;
+import org.apache.commons.httpclient.params.HttpConnectionParams;
+import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
+
+/*
+ * The TrustingProtocolSocketFactory will accept any provided
+ * certificate, i.e., in a sticky mode certificate management scheme,
+ * when making an SSL connection to the Big Switch Controller
+ */
+public class TrustingProtocolSocketFactory implements SecureProtocolSocketFactory {
+ private SSLSocketFactory ssf;
+
+ public TrustingProtocolSocketFactory() throws IOException {
+ // Create a trust manager that does not validate certificate chains
+ TrustManager[] trustAllCerts = new TrustManager[] {
+ new X509TrustManager() {
+ @Override
+ public X509Certificate[] getAcceptedIssuers() {
+ return null;
+ }
+
+ @Override
+ public void checkClientTrusted(X509Certificate[] certs, String authType) {
+ // Trust always
+ }
+
+ @Override
+ public void checkServerTrusted(X509Certificate[] certs, String authType) {
+ // Trust always
+ }
+ }
+ };
+
+ try {
+ // Install the all-trusting trust manager
+ SSLContext sc = SSLContext.getInstance("SSL");
+ sc.init(null, trustAllCerts, new java.security.SecureRandom());
+ ssf = sc.getSocketFactory();
+ } catch (KeyManagementException e) {
+ throw new IOException(e);
+ } catch (NoSuchAlgorithmException e) {
+ throw new IOException(e);
+ }
+ }
+
+ @Override
+ public Socket createSocket(String host, int port) throws IOException,
+ UnknownHostException {
+ return ssf.createSocket(host, port);
+ }
+
+ @Override
+ public Socket createSocket(String address, int port, InetAddress localAddress,
+ int localPort) throws IOException, UnknownHostException {
+ return ssf.createSocket(address, port, localAddress, localPort);
+ }
+
+ @Override
+ public Socket createSocket(Socket socket, String host, int port,
+ boolean autoClose) throws IOException, UnknownHostException {
+ return ssf.createSocket(socket, host, port, autoClose);
+ }
+
+ @Override
+ public Socket createSocket(String host, int port, InetAddress localAddress,
+ int localPort, HttpConnectionParams params) throws IOException,
+ UnknownHostException, ConnectTimeoutException {
+ int timeout = params.getConnectionTimeout();
+ if (timeout == 0) {
+ return createSocket(host, port, localAddress, localPort);
+ }
+ else {
+ Socket s = ssf.createSocket();
+ s.bind(new InetSocketAddress(localAddress, localPort));
+ s.connect(new InetSocketAddress(host, port), timeout);
+ return s;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/01864ef7/plugins/network-elements/bigswitch/src/com/cloud/network/dao/BigSwitchBcfDao.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/bigswitch/src/com/cloud/network/dao/BigSwitchBcfDao.java b/plugins/network-elements/bigswitch/src/com/cloud/network/dao/BigSwitchBcfDao.java
new file mode 100644
index 0000000..8cf84e4
--- /dev/null
+++ b/plugins/network-elements/bigswitch/src/com/cloud/network/dao/BigSwitchBcfDao.java
@@ -0,0 +1,35 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package com.cloud.network.dao;
+
+import java.util.List;
+
+import com.cloud.network.BigSwitchBcfDeviceVO;
+import com.cloud.utils.db.GenericDao;
+
+public interface BigSwitchBcfDao extends GenericDao<BigSwitchBcfDeviceVO, Long> {
+ /**
+ * list all the BigSwitch BCF Controller devices added in to this physical network
+ * @param physicalNetworkId physical Network Id
+ * @return list of BigSwitchBcfDeviceVO for this physical network.
+ */
+ List<BigSwitchBcfDeviceVO> listByPhysicalNetwork(long physicalNetworkId);
+
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/01864ef7/plugins/network-elements/bigswitch/src/com/cloud/network/dao/BigSwitchBcfDaoImpl.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/bigswitch/src/com/cloud/network/dao/BigSwitchBcfDaoImpl.java b/plugins/network-elements/bigswitch/src/com/cloud/network/dao/BigSwitchBcfDaoImpl.java
new file mode 100644
index 0000000..f0b4a06
--- /dev/null
+++ b/plugins/network-elements/bigswitch/src/com/cloud/network/dao/BigSwitchBcfDaoImpl.java
@@ -0,0 +1,53 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package com.cloud.network.dao;
+
+import java.util.List;
+
+import javax.ejb.Local;
+
+import org.springframework.stereotype.Component;
+
+import com.cloud.network.BigSwitchBcfDeviceVO;
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.SearchCriteria.Op;
+
+@Component
+@Local(value = BigSwitchBcfDao.class)
+public class BigSwitchBcfDaoImpl extends GenericDaoBase<BigSwitchBcfDeviceVO, Long> implements BigSwitchBcfDao {
+
+ protected final SearchBuilder<BigSwitchBcfDeviceVO> physicalNetworkIdSearch;
+
+ public BigSwitchBcfDaoImpl() {
+ physicalNetworkIdSearch = createSearchBuilder();
+ physicalNetworkIdSearch.and("physicalNetworkId", physicalNetworkIdSearch.entity().getPhysicalNetworkId(), Op.EQ);
+ physicalNetworkIdSearch.done();
+ }
+
+ @Override
+ public List<BigSwitchBcfDeviceVO> listByPhysicalNetwork(long physicalNetworkId) {
+ SearchCriteria<BigSwitchBcfDeviceVO> sc = physicalNetworkIdSearch.create();
+ sc.setParameters("physicalNetworkId", physicalNetworkId);
+ return search(sc, null);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/01864ef7/plugins/network-elements/bigswitch/src/com/cloud/network/element/BigSwitchBcfElement.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/bigswitch/src/com/cloud/network/element/BigSwitchBcfElement.java b/plugins/network-elements/bigswitch/src/com/cloud/network/element/BigSwitchBcfElement.java
new file mode 100644
index 0000000..2de447e
--- /dev/null
+++ b/plugins/network-elements/bigswitch/src/com/cloud/network/element/BigSwitchBcfElement.java
@@ -0,0 +1,785 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package com.cloud.network.element;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+
+import javax.ejb.Local;
+import javax.inject.Inject;
+import javax.naming.ConfigurationException;
+
+import org.apache.log4j.Logger;
+import org.springframework.stereotype.Component;
+import org.apache.cloudstack.network.ExternalNetworkDeviceManager.NetworkDevice;
+import org.apache.commons.net.util.SubnetUtils;
+
+import com.cloud.agent.AgentManager;
+import com.cloud.agent.api.BcfAnswer;
+import com.cloud.agent.api.UpdateBcfRouterCommand;
+import com.cloud.agent.api.CreateBcfAttachmentCommand;
+import com.cloud.agent.api.CreateBcfStaticNatCommand;
+import com.cloud.agent.api.DeleteBcfAttachmentCommand;
+import com.cloud.agent.api.DeleteBcfStaticNatCommand;
+import com.cloud.agent.api.StartupBigSwitchBcfCommand;
+import com.cloud.agent.api.StartupCommand;
+import com.cloud.api.ApiDBUtils;
+import com.cloud.api.commands.AddBigSwitchBcfDeviceCmd;
+import com.cloud.api.commands.DeleteBigSwitchBcfDeviceCmd;
+import com.cloud.api.commands.ListBigSwitchBcfDevicesCmd;
+import com.cloud.api.commands.BcfConstants;
+import com.cloud.api.response.BigSwitchBcfDeviceResponse;
+import com.cloud.configuration.ConfigurationManager;
+import com.cloud.dc.DataCenterVO;
+import com.cloud.dc.dao.DataCenterDao;
+import com.cloud.dc.dao.VlanDao;
+import com.cloud.deploy.DeployDestination;
+import com.cloud.exception.ConcurrentOperationException;
+import com.cloud.exception.InsufficientCapacityException;
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.exception.ResourceUnavailableException;
+import com.cloud.host.DetailVO;
+import com.cloud.host.Host;
+import com.cloud.host.HostVO;
+import com.cloud.host.dao.HostDao;
+import com.cloud.host.dao.HostDetailsDao;
+import com.cloud.hypervisor.Hypervisor.HypervisorType;
+import com.cloud.network.BigSwitchBcfDeviceVO;
+import com.cloud.network.IpAddressManager;
+import com.cloud.network.Network;
+import com.cloud.network.Network.Capability;
+import com.cloud.network.Network.Provider;
+import com.cloud.network.Network.Service;
+import com.cloud.network.NetworkModel;
+import com.cloud.network.Networks.BroadcastDomainType;
+import com.cloud.network.PhysicalNetwork;
+import com.cloud.network.PhysicalNetworkServiceProvider;
+import com.cloud.network.PublicIpAddress;
+import com.cloud.network.bigswitch.AclData;
+import com.cloud.network.bigswitch.BigSwitchBcfApi;
+import com.cloud.network.bigswitch.BigSwitchBcfUtils;
+import com.cloud.network.bigswitch.TopologyData;
+import com.cloud.network.dao.BigSwitchBcfDao;
+import com.cloud.network.dao.FirewallRulesCidrsDao;
+import com.cloud.network.dao.FirewallRulesDao;
+import com.cloud.network.dao.IPAddressDao;
+import com.cloud.network.dao.NetworkDao;
+import com.cloud.network.dao.NetworkServiceMapDao;
+import com.cloud.network.dao.PhysicalNetworkDao;
+import com.cloud.network.dao.PhysicalNetworkServiceProviderDao;
+import com.cloud.network.dao.PhysicalNetworkServiceProviderVO;
+import com.cloud.network.dao.PhysicalNetworkVO;
+import com.cloud.network.resource.BigSwitchBcfResource;
+import com.cloud.network.rules.FirewallRule;
+import com.cloud.network.rules.StaticNat;
+import com.cloud.network.vpc.NetworkACLItem;
+import com.cloud.network.vpc.NetworkACLItemCidrsDao;
+import com.cloud.network.vpc.NetworkACLItemDao;
+import com.cloud.network.vpc.Vpc;
+import com.cloud.network.vpc.dao.VpcDao;
+import com.cloud.offering.NetworkOffering;
+import com.cloud.resource.ResourceManager;
+import com.cloud.resource.ResourceState;
+import com.cloud.resource.ResourceStateAdapter;
+import com.cloud.resource.ServerResource;
+import com.cloud.resource.UnableDeleteHostException;
+import com.cloud.user.dao.AccountDao;
+import com.cloud.utils.component.AdapterBase;
+import com.cloud.utils.db.DB;
+import com.cloud.utils.db.Transaction;
+import com.cloud.utils.db.TransactionCallback;
+import com.cloud.utils.db.TransactionStatus;
+import com.cloud.utils.exception.CloudRuntimeException;
+import com.cloud.vm.NicProfile;
+import com.cloud.vm.ReservationContext;
+import com.cloud.vm.VirtualMachineProfile;
+import com.cloud.vm.dao.DomainRouterDao;
+import com.cloud.vm.dao.NicDao;
+import com.cloud.vm.dao.VMInstanceDao;
+
+/**
+ * BigSwitchBcfElement is responsible for creating and plugging a nic into a BCF Segment network.
+ * When a VM is created and needs to be plugged into a BCF Segment network, BigSwitchBcfElement is
+ * called by NetworkOrchestrator to create a "port" and an "attachment" for each nic, and
+ * register them with the controller to be plugged into the corresponding network. It also
+ * removes them when the VM is destroyed.
+ */
+@Component
+@Local(value = {NetworkElement.class, ConnectivityProvider.class, IpDeployer.class,
+ SourceNatServiceProvider.class, StaticNatServiceProvider.class,
+ NetworkACLServiceProvider.class, FirewallServiceProvider.class})
+public class BigSwitchBcfElement extends AdapterBase implements BigSwitchBcfElementService,
+ConnectivityProvider, IpDeployer, SourceNatServiceProvider, StaticNatServiceProvider,
+NetworkACLServiceProvider, FirewallServiceProvider, ResourceStateAdapter {
+ private static final Logger s_logger = Logger.getLogger(BigSwitchBcfElement.class);
+
+ private static final Map<Service, Map<Capability, String>> capabilities = setCapabilities();
+
+ @Inject
+ ResourceManager _resourceMgr;
+ @Inject
+ PhysicalNetworkDao _physicalNetworkDao;
+ @Inject
+ PhysicalNetworkServiceProviderDao _physicalNetworkServiceProviderDao;
+ @Inject
+ BigSwitchBcfDao _bigswitchBcfDao;
+ @Inject
+ HostDetailsDao _hostDetailsDao;
+ @Inject
+ HostDao _hostDao;
+ @Inject
+ AgentManager _agentMgr;
+ @Inject
+ NetworkDao _networkDao;
+ @Inject
+ DomainRouterDao _routerDao;
+ @Inject
+ NicDao _nicDao;
+ @Inject
+ VMInstanceDao _vmDao;
+ @Inject
+ AccountDao _accountDao;
+ @Inject
+ VpcDao _vpcDao;
+ @Inject
+ NetworkModel _networkModel;
+ @Inject
+ ConfigurationManager _configMgr;
+ @Inject
+ NetworkServiceMapDao _ntwkSrvcDao;
+ @Inject
+ DataCenterDao _zoneDao;
+ @Inject
+ IPAddressDao _ipAddressDao;
+ @Inject
+ IpAddressManager _ipAddrMgr;
+ @Inject
+ VlanDao _vlanDao;
+ @Inject
+ FirewallRulesDao _fwRulesDao;
+ @Inject
+ FirewallRulesCidrsDao _fwCidrsDao;
+ @Inject
+ NetworkACLItemDao _aclItemDao;
+ @Inject
+ NetworkACLItemCidrsDao _aclItemCidrsDao;
+
+ private BigSwitchBcfUtils _bcfUtils = null;
+
+ @Override
+ public Map<Service, Map<Capability, String>> getCapabilities() {
+ return capabilities;
+ }
+
+ @Override
+ public Provider getProvider() {
+ return Provider.BigSwitchBcf;
+ }
+
+ private boolean canHandle(Network network, Service service) {
+ s_logger.debug("Checking if BigSwitchBcfElement can handle service " + service.getName() + " on network " + network.getDisplayText());
+ if (network.getBroadcastDomainType() != BroadcastDomainType.Vlan) {
+ return false;
+ }
+
+ if (!_networkModel.isProviderForNetwork(getProvider(), network.getId())) {
+ s_logger.debug("BigSwitchBcfElement is not a provider for network " + network.getDisplayText());
+ return false;
+ }
+
+ if (!_ntwkSrvcDao.canProviderSupportServiceInNetwork(network.getId(), service, BcfConstants.BigSwitchBcf)) {
+ s_logger.debug("BigSwitchBcfElement can't provide the " + service.getName() + " service on network " + network.getDisplayText());
+ return false;
+ }
+
+ return true;
+ }
+
+ @Override
+ public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
+ super.configure(name, params);
+ _resourceMgr.registerResourceStateAdapter(this.getClass().getSimpleName(), this);
+ return true;
+ }
+
+ @Override
+ public boolean implement(Network network, NetworkOffering offering, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException,
+ ResourceUnavailableException, InsufficientCapacityException {
+ updateBcfRouter(network);
+ return true;
+ }
+
+ @Override
+ public boolean prepare(Network network, NicProfile nic, VirtualMachineProfile vm, DeployDestination dest, ReservationContext context)
+ throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
+
+ if (!canHandle(network, Service.Connectivity)) {
+ return false;
+ }
+
+ bcfUtilsInit();
+
+ // get arguments for CreateBcfAttachmentCommand
+ // determine whether this is VPC network or stand-alone network
+ Vpc vpc = null;
+ if(network.getVpcId()!=null){
+ vpc = _vpcDao.acquireInLockTable(network.getVpcId());
+ }
+
+ String tenantId;
+ String tenantName;
+ if (vpc != null) {
+ tenantId = vpc.getUuid();
+ tenantName = vpc.getName();
+ _vpcDao.releaseFromLockTable(vpc.getId());
+ } else {
+ // use account in CS as tenant in BSN
+ // use network id/name as tenant id/name for non-VPC networks
+ tenantId = network.getUuid();
+ tenantName = network.getName();
+ }
+
+ String networkId = network.getUuid();
+ String hostname = dest.getHost().getName();
+ String nicId = nic.getUuid();
+ Integer vlan = Integer.valueOf(BroadcastDomainType.getValue(nic.getIsolationUri()));
+ String ipv4 = nic.getIp4Address();
+ String mac = nic.getMacAddress();
+ long zoneId = network.getDataCenterId();
+ String vmwareVswitchLabel = _networkModel.getDefaultGuestTrafficLabel(zoneId, HypervisorType.VMware);
+ String[] labelArray = null;
+ String vswitchName = null;
+ if(vmwareVswitchLabel!=null){
+ labelArray=vmwareVswitchLabel.split(",");
+ vswitchName = labelArray[0];
+ }
+
+ // hypervisor type:
+ // kvm: ivs port name
+ // vmware: specific portgroup naming convention
+ String pgName = "";
+ if (dest.getHost().getHypervisorType() == HypervisorType.KVM){
+ pgName = hostname;
+ } else if (dest.getHost().getHypervisorType() == HypervisorType.VMware){
+ pgName = hostname + "-" + vswitchName;
+ }
+
+ CreateBcfAttachmentCommand cmd = new CreateBcfAttachmentCommand(tenantId,
+ tenantName, networkId, pgName, nicId, vlan, ipv4, mac);
+
+ _bcfUtils.sendBcfCommandWithNetworkSyncCheck(cmd, network);
+
+ return true;
+ }
+
+ @Override
+ public boolean release(Network network, NicProfile nic, VirtualMachineProfile vm, ReservationContext context) throws ConcurrentOperationException,
+ ResourceUnavailableException {
+
+ if (!canHandle(network, Service.Connectivity)) {
+ return false;
+ }
+
+ if (network.getBroadcastUri() == null) {
+ s_logger.error("Nic has no broadcast Uri");
+ return false;
+ }
+
+ bcfUtilsInit();
+
+ String networkId = network.getUuid();
+ String nicId = nic.getUuid();
+
+ String tenantId;
+ if(network.getVpcId()!=null) {
+ tenantId = network.getNetworkDomain();
+ } else {
+ tenantId = networkId;
+ }
+
+ DeleteBcfAttachmentCommand cmd = new DeleteBcfAttachmentCommand(tenantId, networkId, nicId);
+
+ _bcfUtils.sendBcfCommandWithNetworkSyncCheck(cmd, network);
+ return true;
+ }
+
+ @Override
+ public boolean shutdown(Network network, ReservationContext context, boolean cleanup) throws ConcurrentOperationException, ResourceUnavailableException {
+ if (!canHandle(network, Service.Connectivity)) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public boolean destroy(Network network, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException {
+ if (!canHandle(network, Service.Connectivity)) {
+ return false;
+ }
+
+ return true;
+ }
+
+ @Override
+ public boolean isReady(PhysicalNetworkServiceProvider provider) {
+ return true;
+ }
+
+ @Override
+ public boolean shutdownProviderInstances(PhysicalNetworkServiceProvider provider, ReservationContext context) throws ConcurrentOperationException,
+ ResourceUnavailableException {
+ return true;
+ }
+
+ @Override
+ public boolean canEnableIndividualServices() {
+ return true;
+ }
+
+ @Override
+ public boolean verifyServicesCombination(Set<Service> services) {
+ if (!services.contains(Service.Connectivity)) {
+ s_logger.warn("Unable to provide services without Connectivity service enabled for this element");
+ return false;
+ }
+ return true;
+ }
+
+ private static Map<Service, Map<Capability, String>> setCapabilities() {
+ Map<Service, Map<Capability, String>> capabilities = new HashMap<Service, Map<Capability, String>>();
+
+ // L2 Support
+ capabilities.put(Service.Connectivity, null);
+
+ // L3 Support
+ capabilities.put(Service.Gateway, null);
+
+ // L3 Support : SourceNat
+ Map<Capability, String> sourceNatCapabilities = new HashMap<Capability, String>();
+ sourceNatCapabilities.put(Capability.SupportedSourceNatTypes, "peraccount");
+ sourceNatCapabilities.put(Capability.RedundantRouter, "false");
+ capabilities.put(Service.SourceNat, sourceNatCapabilities);
+
+ // L3 support : StaticNat
+ capabilities.put(Service.StaticNat, null);
+
+ //add network ACL capability
+ Map<Network.Capability, String> networkACLCapabilities = new HashMap<Network.Capability, String>();
+ networkACLCapabilities.put(Network.Capability.SupportedProtocols, "tcp,udp,icmp");
+ capabilities.put(Network.Service.NetworkACL, networkACLCapabilities);
+
+ // Set capabilities for Firewall service
+ Map<Capability, String> firewallCapabilities = new HashMap<Capability, String>();
+ firewallCapabilities.put(Capability.TrafficStatistics, "per public ip");
+ firewallCapabilities.put(Capability.SupportedProtocols, "tcp,udp,icmp");
+ firewallCapabilities.put(Capability.SupportedEgressProtocols, "tcp,udp,icmp, all");
+ firewallCapabilities.put(Capability.SupportedTrafficDirection, "ingress, egress");
+ firewallCapabilities.put(Capability.MultipleIps, "true");
+ capabilities.put(Service.Firewall, firewallCapabilities);
+
+ return capabilities;
+ }
+
+ @Override
+ @DB
+ public BigSwitchBcfDeviceVO addBigSwitchBcfDevice(AddBigSwitchBcfDeviceCmd cmd) {
+ BigSwitchBcfDeviceVO newBcfDevice;
+
+ bcfUtilsInit();
+
+ ServerResource resource = new BigSwitchBcfResource();
+
+ final String deviceName = BcfConstants.BigSwitchBcf.getName();
+ NetworkDevice networkDevice = NetworkDevice.getNetworkDevice(deviceName);
+ final Long physicalNetworkId = cmd.getPhysicalNetworkId();
+ final String hostname = cmd.getHost();
+ final String username = cmd.getUsername();
+ final String password = cmd.getPassword();
+ final Boolean nat = cmd.getNat();
+
+ PhysicalNetworkVO physicalNetwork = _physicalNetworkDao.findById(physicalNetworkId);
+ if (physicalNetwork == null) {
+ throw new InvalidParameterValueException("Could not find phyical network with ID: " + physicalNetworkId);
+ }
+ long zoneId = physicalNetwork.getDataCenterId();
+
+ final PhysicalNetworkServiceProviderVO ntwkSvcProvider =
+ _physicalNetworkServiceProviderDao.findByServiceProvider(physicalNetwork.getId(), networkDevice.getNetworkServiceProvder());
+ if (ntwkSvcProvider == null) {
+ throw new CloudRuntimeException("Network Service Provider: " + networkDevice.getNetworkServiceProvder() + " is not enabled in the physical network: " +
+ physicalNetworkId + "to add this device");
+ } else if (ntwkSvcProvider.getState() == PhysicalNetworkServiceProvider.State.Shutdown) {
+ throw new CloudRuntimeException("Network Service Provider: " + ntwkSvcProvider.getProviderName() + " is in shutdown state in the physical network: " +
+ physicalNetworkId + "to add this device");
+ }
+ ntwkSvcProvider.setFirewallServiceProvided(true);
+ ntwkSvcProvider.setGatewayServiceProvided(true);
+ ntwkSvcProvider.setNetworkAclServiceProvided(true);
+ ntwkSvcProvider.setSourcenatServiceProvided(true);
+ ntwkSvcProvider.setStaticnatServiceProvided(true);
+
+ if (_bigswitchBcfDao.listByPhysicalNetwork(physicalNetworkId).size() > 1) {
+ throw new CloudRuntimeException("At most two BCF controllers can be configured");
+ }
+
+ DataCenterVO zone = _zoneDao.findById(physicalNetwork.getDataCenterId());
+ String zoneName;
+ if(zone!= null){
+ zoneName = zone.getName();
+ } else {
+ zoneName = String.valueOf(zoneId);
+ }
+
+ Boolean natNow = _bcfUtils.isNatEnabled();
+ if( natNow != null)
+ if (!nat && natNow){
+ throw new CloudRuntimeException("NAT is enabled in existing controller. Enable NAT for new controller or remove existing controller first.");
+ } else if (nat && !natNow){
+ throw new CloudRuntimeException("NAT is disabled in existing controller. Disable NAT for new controller or remove existing controller first.");
+ }
+
+ Map<String, String> params = new HashMap<String, String>();
+ params.put("guid", UUID.randomUUID().toString());
+ params.put("zoneId", zoneName);
+ params.put("physicalNetworkId", String.valueOf(physicalNetwork.getId()));
+ params.put("name", "BigSwitch Controller - " + cmd.getHost());
+ params.put("hostname", cmd.getHost());
+ params.put("username", username);
+ params.put("password", password);
+ params.put("nat", nat.toString());
+
+ // FIXME What to do with multiple isolation types
+ params.put("transportzoneisotype", physicalNetwork.getIsolationMethods().get(0).toLowerCase());
+ Map<String, Object> hostdetails = new HashMap<String, Object>();
+ hostdetails.putAll(params);
+
+ try {
+ resource.configure(cmd.getHost(), hostdetails);
+
+ // store current topology in bcf resource
+ TopologyData topo = _bcfUtils.getTopology(physicalNetwork.getId());
+ ((BigSwitchBcfResource) resource).setTopology(topo);
+
+ final Host host = _resourceMgr.addHost(zoneId, resource, Host.Type.L2Networking, params);
+ if (host != null) {
+ newBcfDevice = Transaction.execute(new TransactionCallback<BigSwitchBcfDeviceVO>() {
+ @Override
+ public BigSwitchBcfDeviceVO doInTransaction(TransactionStatus status) {
+ BigSwitchBcfDeviceVO bigswitchBcfDevice =
+ new BigSwitchBcfDeviceVO(host.getId(), physicalNetworkId, ntwkSvcProvider.getProviderName(),
+ deviceName, hostname, username, password, nat, BigSwitchBcfApi.HASH_IGNORE);
+ _bigswitchBcfDao.persist(bigswitchBcfDevice);
+
+ DetailVO detail = new DetailVO(host.getId(), "bigswitchbcfdeviceid", String.valueOf(bigswitchBcfDevice.getId()));
+ _hostDetailsDao.persist(detail);
+
+ return bigswitchBcfDevice;
+ }
+ });
+ } else {
+ throw new CloudRuntimeException("Failed to add BigSwitch BCF Controller Device due to internal error.");
+ }
+ } catch (ConfigurationException e) {
+ throw new CloudRuntimeException(e.getMessage());
+ }
+
+ // initial topology sync to newly added BCF controller
+ HostVO bigswitchBcfHost = _hostDao.findById(newBcfDevice.getHostId());
+ _bcfUtils.syncTopologyToBcfHost(bigswitchBcfHost, nat);
+
+ return newBcfDevice;
+ }
+
+ @Override
+ public BigSwitchBcfDeviceResponse createBigSwitchBcfDeviceResponse(BigSwitchBcfDeviceVO bigswitchBcfDeviceVO) {
+ HostVO bigswitchBcfHost = _hostDao.findById(bigswitchBcfDeviceVO.getHostId());
+ _hostDao.loadDetails(bigswitchBcfHost);
+
+ BigSwitchBcfDeviceResponse response = new BigSwitchBcfDeviceResponse();
+ response.setDeviceName(bigswitchBcfDeviceVO.getDeviceName());
+ PhysicalNetwork pnw = ApiDBUtils.findPhysicalNetworkById(bigswitchBcfDeviceVO.getPhysicalNetworkId());
+ if (pnw != null) {
+ response.setPhysicalNetworkId(pnw.getUuid());
+ }
+ response.setId(bigswitchBcfDeviceVO.getUuid());
+ response.setProviderName(bigswitchBcfDeviceVO.getProviderName());
+ response.setHostName(bigswitchBcfHost.getDetail("hostname"));
+ response.setObjectName("bigswitchbcfdevice");
+ return response;
+ }
+
+ @Override
+ public boolean deleteBigSwitchBcfDevice(DeleteBigSwitchBcfDeviceCmd cmd) {
+ Long bigswitchBcfDeviceId = cmd.getBigSwitchBcfDeviceId();
+ BigSwitchBcfDeviceVO bigswitchBcfDevice = _bigswitchBcfDao.findById(bigswitchBcfDeviceId);
+ if (bigswitchBcfDevice == null) {
+ throw new InvalidParameterValueException("Could not find a BigSwitch Controller with id " + bigswitchBcfDevice);
+ }
+
+ HostVO bigswitchHost = _hostDao.findById(bigswitchBcfDevice.getHostId());
+ Long hostId = bigswitchHost.getId();
+
+ bigswitchHost.setResourceState(ResourceState.Maintenance);
+ _hostDao.update(hostId, bigswitchHost);
+ _resourceMgr.deleteHost(hostId, false, false);
+
+ _bigswitchBcfDao.remove(bigswitchBcfDeviceId);
+ return true;
+ }
+
+ @Override
+ public List<BigSwitchBcfDeviceVO> listBigSwitchBcfDevices(ListBigSwitchBcfDevicesCmd cmd) {
+ Long physicalNetworkId = cmd.getPhysicalNetworkId();
+ Long bigswitchBcfDeviceId = cmd.getBigSwitchBcfDeviceId();
+ List<BigSwitchBcfDeviceVO> responseList = new ArrayList<BigSwitchBcfDeviceVO>();
+
+ if (physicalNetworkId == null && bigswitchBcfDeviceId == null) {
+ throw new InvalidParameterValueException("Either physical network Id or bigswitch device Id must be specified");
+ }
+
+ if (bigswitchBcfDeviceId != null) {
+ BigSwitchBcfDeviceVO bigswitchBcfDevice = _bigswitchBcfDao.findById(bigswitchBcfDeviceId);
+ if (bigswitchBcfDevice == null) {
+ throw new InvalidParameterValueException("Could not find BigSwitch controller with id: " + bigswitchBcfDevice);
+ }
+ responseList.add(bigswitchBcfDevice);
+ } else {
+ PhysicalNetworkVO physicalNetwork = _physicalNetworkDao.findById(physicalNetworkId);
+ if (physicalNetwork == null) {
+ throw new InvalidParameterValueException("Could not find a physical network with id: " + physicalNetworkId);
+ }
+ responseList = _bigswitchBcfDao.listByPhysicalNetwork(physicalNetworkId);
+ }
+
+ return responseList;
+ }
+
+ @Override
+ public HostVO createHostVOForConnectedAgent(HostVO host, StartupCommand[] cmd) {
+ return null;
+ }
+
+ @Override
+ public HostVO createHostVOForDirectConnectAgent(HostVO host, StartupCommand[] startup, ServerResource resource, Map<String, String> details, List<String> hostTags) {
+ if (!(startup[0] instanceof StartupBigSwitchBcfCommand)) {
+ return null;
+ }
+
+ BigSwitchBcfResource bcfResource = (BigSwitchBcfResource) resource;
+ bcfUtilsInit();
+
+ if(_bcfUtils.getTopology()!=null){
+ bcfResource.setTopology(_bcfUtils.getTopology());
+ }
+
+ host.setType(Host.Type.L2Networking);
+ return host;
+ }
+
+ @Override
+ public DeleteHostAnswer deleteHost(HostVO host, boolean isForced, boolean isForceDeleteStorage) throws UnableDeleteHostException {
+ if (!(host.getType() == Host.Type.L2Networking)) {
+ return null;
+ }
+ return new DeleteHostAnswer(true);
+ }
+
+ @Override
+ public List<Class<?>> getCommands() {
+ List<Class<?>> cmdList = new ArrayList<Class<?>>();
+ cmdList.add(AddBigSwitchBcfDeviceCmd.class);
+ cmdList.add(DeleteBigSwitchBcfDeviceCmd.class);
+ cmdList.add(ListBigSwitchBcfDevicesCmd.class);
+ return cmdList;
+ }
+
+ @Override
+ public IpDeployer getIpDeployer(Network network) {
+ return this;
+ }
+
+ @Override
+ public boolean applyStaticNats(Network network,
+ List<? extends StaticNat> rules)
+ throws ResourceUnavailableException {
+ bcfUtilsInit();
+
+ _bcfUtils.listACLbyNetwork(network);
+
+ Vpc vpc = null;
+ if(network.getVpcId()!=null){
+ vpc = _vpcDao.acquireInLockTable(network.getVpcId());
+ }
+
+ String tenantId;
+ if (vpc != null) {
+ tenantId = vpc.getUuid();
+ _vpcDao.releaseFromLockTable(vpc.getId());
+ } else {
+ // use account in CS as tenant in BSN
+ // use network uuid as tenantId for non-VPC networks
+ tenantId = network.getUuid();
+ }
+
+ for (StaticNat rule: rules){
+ String srcIp = _ipAddressDao.findById(rule.getSourceIpAddressId()).getAddress().addr();
+ String dstIp = rule.getDestIpAddress();
+ String mac = rule.getSourceMacAddress();
+ if(!rule.isForRevoke()) {
+ s_logger.debug("BCF enables static NAT for public IP: " + srcIp + " private IP " + dstIp
+ + " mac " + mac);
+ CreateBcfStaticNatCommand cmd = new CreateBcfStaticNatCommand(
+ tenantId, network.getUuid(), dstIp, srcIp, mac);
+
+ _bcfUtils.sendBcfCommandWithNetworkSyncCheck(cmd, network);
+ } else {
+ s_logger.debug("BCF removes static NAT for public IP: " + srcIp + " private IP " + dstIp
+ + " mac " + mac);
+ DeleteBcfStaticNatCommand cmd = new DeleteBcfStaticNatCommand(tenantId, srcIp);
+
+ _bcfUtils.sendBcfCommandWithNetworkSyncCheck(cmd, network);
+ }
+ }
+ return true;
+ }
+
+ @Override
+ public boolean applyIps(Network network,
+ List<? extends PublicIpAddress> ipAddress, Set<Service> services)
+ throws ResourceUnavailableException {
+ return false;
+ }
+
+ @Override
+ public boolean applyNetworkACLs(Network network,
+ List<? extends NetworkACLItem> rules)
+ throws ResourceUnavailableException {
+ SubnetUtils utils;
+ String cidr = null;
+ List<String> cidrList;
+ for(NetworkACLItem r: rules){
+ if(r.getState()==NetworkACLItem.State.Revoke){
+ continue;
+ }
+ cidrList = r.getSourceCidrList();
+ if(cidrList != null){
+ if(cidrList.size()>1 || r.getSourcePortEnd()!=r.getSourcePortStart()){
+ throw new ResourceUnavailableException("One CIDR and one port only please.",
+ Network.class, network.getId());
+ } else {
+ cidr = cidrList.get(0);
+ }
+ }
+ if (cidr == null || cidr.equalsIgnoreCase("0.0.0.0/0")) {
+ cidr = "";
+ } else {
+ utils = new SubnetUtils(cidr);
+ if(!utils.getInfo().getNetworkAddress().equals(utils.getInfo().getAddress())){
+ throw new ResourceUnavailableException("Invalid CIDR in Network ACL rule.",
+ Network.class, network.getId());
+ }
+ }
+ }
+ updateBcfRouter(network);
+ return true;
+ }
+
+ @Override
+ public boolean applyFWRules(Network network,
+ List<? extends FirewallRule> rules)
+ throws ResourceUnavailableException {
+ SubnetUtils utils;
+ String cidr = null;
+ List<String> cidrList;
+ for(FirewallRule r: rules){
+ if(r.getState()==FirewallRule.State.Revoke){
+ continue;
+ }
+ cidrList = r.getSourceCidrList();
+ if(cidrList != null){
+ if(cidrList.size()>1 || r.getSourcePortEnd()!=r.getSourcePortStart()){
+ throw new ResourceUnavailableException("One CIDR and one port only please.",
+ Network.class, network.getId());
+ } else {
+ cidr = cidrList.get(0);
+ }
+ }
+ if (cidr == null || cidr.equalsIgnoreCase("0.0.0.0/0")) {
+ cidr = "";
+ } else {
+ utils = new SubnetUtils(cidr);
+ if(!utils.getInfo().getNetworkAddress().equals(utils.getInfo().getAddress())){
+ throw new ResourceUnavailableException("Invalid CIDR in Firewall rule.",
+ Network.class, network.getId());
+ }
+ }
+ }
+ updateBcfRouter(network);
+ return true;
+ }
+
+ private void updateBcfRouter(Network network) throws IllegalArgumentException{
+ bcfUtilsInit();
+
+ Vpc vpc = null;
+ if(network.getVpcId()!=null){
+ vpc = _vpcDao.acquireInLockTable(network.getVpcId());
+ }
+
+ String tenantId;
+ if (vpc != null) {
+ tenantId = vpc.getUuid();
+ _vpcDao.releaseFromLockTable(vpc.getId());
+ } else {
+ tenantId = network.getUuid();
+ }
+
+ UpdateBcfRouterCommand cmd = new UpdateBcfRouterCommand(tenantId);
+
+ List<AclData> aclList = _bcfUtils.listACLbyNetwork(network);
+ for(AclData acl: aclList){
+ cmd.addAcl(acl);
+ }
+
+ if(vpc != null){
+ cmd.setPublicIp(_bcfUtils.getPublicIpByVpc(vpc));
+ } else {
+ cmd.setPublicIp(_bcfUtils.getPublicIpByNetwork(network));
+ }
+
+ BcfAnswer answer = _bcfUtils.sendBcfCommandWithNetworkSyncCheck(cmd, network);
+ if(answer != null && !answer.getResult()){
+ throw new IllegalArgumentException("Illegal router update arguments");
+ }
+ }
+
+ private void bcfUtilsInit(){
+ if (_bcfUtils == null) {
+ _bcfUtils = new BigSwitchBcfUtils(_networkDao, _nicDao,
+ _vmDao, _hostDao, _vpcDao, _bigswitchBcfDao,
+ _agentMgr, _vlanDao, _ipAddressDao, _fwRulesDao,
+ _fwCidrsDao, _aclItemDao, _aclItemCidrsDao, _networkModel);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/01864ef7/plugins/network-elements/bigswitch/src/com/cloud/network/element/BigSwitchBcfElementService.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/bigswitch/src/com/cloud/network/element/BigSwitchBcfElementService.java b/plugins/network-elements/bigswitch/src/com/cloud/network/element/BigSwitchBcfElementService.java
new file mode 100644
index 0000000..3a64472
--- /dev/null
+++ b/plugins/network-elements/bigswitch/src/com/cloud/network/element/BigSwitchBcfElementService.java
@@ -0,0 +1,41 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package com.cloud.network.element;
+
+import java.util.List;
+
+import com.cloud.api.commands.AddBigSwitchBcfDeviceCmd;
+import com.cloud.api.commands.DeleteBigSwitchBcfDeviceCmd;
+import com.cloud.api.commands.ListBigSwitchBcfDevicesCmd;
+import com.cloud.api.response.BigSwitchBcfDeviceResponse;
+import com.cloud.network.BigSwitchBcfDeviceVO;
+import com.cloud.utils.component.PluggableService;
+
+public interface BigSwitchBcfElementService extends PluggableService {
+
+ public BigSwitchBcfDeviceVO addBigSwitchBcfDevice(AddBigSwitchBcfDeviceCmd cmd);
+
+ public BigSwitchBcfDeviceResponse createBigSwitchBcfDeviceResponse(BigSwitchBcfDeviceVO bigswitchDeviceVO);
+
+ public boolean deleteBigSwitchBcfDevice(DeleteBigSwitchBcfDeviceCmd cmd);
+
+ public List<BigSwitchBcfDeviceVO> listBigSwitchBcfDevices(ListBigSwitchBcfDevicesCmd cmd);
+
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/01864ef7/plugins/network-elements/bigswitch/src/com/cloud/network/guru/BigSwitchBcfGuestNetworkGuru.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/bigswitch/src/com/cloud/network/guru/BigSwitchBcfGuestNetworkGuru.java b/plugins/network-elements/bigswitch/src/com/cloud/network/guru/BigSwitchBcfGuestNetworkGuru.java
new file mode 100644
index 0000000..7625b4b
--- /dev/null
+++ b/plugins/network-elements/bigswitch/src/com/cloud/network/guru/BigSwitchBcfGuestNetworkGuru.java
@@ -0,0 +1,423 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package com.cloud.network.guru;
+
+import java.util.List;
+
+import javax.ejb.Local;
+import javax.inject.Inject;
+
+import org.apache.cloudstack.context.CallContext;
+import org.apache.commons.lang.StringUtils;
+import org.apache.log4j.Logger;
+
+import com.cloud.agent.AgentManager;
+import com.cloud.agent.api.CreateBcfAttachmentCommand;
+import com.cloud.agent.api.CreateBcfRouterCommand;
+import com.cloud.agent.api.CreateBcfRouterInterfaceCommand;
+import com.cloud.agent.api.CreateBcfSegmentCommand;
+import com.cloud.agent.api.DeleteBcfSegmentCommand;
+import com.cloud.dc.DataCenter;
+import com.cloud.dc.DataCenter.NetworkType;
+import com.cloud.dc.VlanVO;
+import com.cloud.dc.dao.HostPodDao;
+import com.cloud.dc.dao.VlanDao;
+import com.cloud.deploy.DeployDestination;
+import com.cloud.deploy.DeploymentPlan;
+import com.cloud.event.ActionEventUtils;
+import com.cloud.event.EventTypes;
+import com.cloud.event.EventVO;
+import com.cloud.exception.InsufficientAddressCapacityException;
+import com.cloud.exception.InsufficientVirtualNetworkCapacityException;
+import com.cloud.host.dao.HostDao;
+import com.cloud.hypervisor.Hypervisor.HypervisorType;
+import com.cloud.network.BigSwitchBcfDeviceVO;
+import com.cloud.network.Network;
+import com.cloud.network.Network.GuestType;
+import com.cloud.network.Network.State;
+import com.cloud.network.NetworkMigrationResponder;
+import com.cloud.network.NetworkProfile;
+import com.cloud.network.NetworkService;
+import com.cloud.network.Networks.BroadcastDomainType;
+import com.cloud.network.PhysicalNetwork;
+import com.cloud.network.PhysicalNetwork.IsolationMethod;
+import com.cloud.network.bigswitch.BigSwitchBcfUtils;
+import com.cloud.network.dao.BigSwitchBcfDao;
+import com.cloud.network.dao.FirewallRulesCidrsDao;
+import com.cloud.network.dao.FirewallRulesDao;
+import com.cloud.network.dao.IPAddressDao;
+import com.cloud.network.dao.NetworkVO;
+import com.cloud.network.dao.PhysicalNetworkDao;
+import com.cloud.network.dao.PhysicalNetworkVO;
+import com.cloud.network.vpc.NetworkACLItemCidrsDao;
+import com.cloud.network.vpc.NetworkACLItemDao;
+import com.cloud.network.vpc.Vpc;
+import com.cloud.network.vpc.dao.VpcDao;
+import com.cloud.offering.NetworkOffering;
+import com.cloud.user.Account;
+import com.cloud.user.dao.AccountDao;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.vm.NicProfile;
+import com.cloud.vm.ReservationContext;
+import com.cloud.vm.VirtualMachineProfile;
+import com.cloud.vm.dao.DomainRouterDao;
+import com.cloud.vm.dao.VMInstanceDao;
+
+/**
+ * BigSwitchBcfGuestNetworkGuru is responsible for creating and removing virtual networks.
+ * Current implementation leverages GuestNetworkGuru to create VLAN based virtual networks
+ * and map it to a Big Switch Big Cloud Fabric (BCF) Segment. It is called by the NetworkOrchestrator.
+ *
+ * When a VM is created and needs to be plugged into a BCF segment network, BigSwitchBcfElement is
+ * called by NetworkOrchestrator to create a "port" and an "attachment" for each nic, and
+ * register them with the controller to be plugged into the corresponding network. It also
+ * removes them when the VM is destroyed.
+ */
+@Local(value = NetworkGuru.class)
+public class BigSwitchBcfGuestNetworkGuru extends GuestNetworkGuru implements NetworkMigrationResponder {
+ private static final Logger s_logger = Logger.getLogger(BigSwitchBcfGuestNetworkGuru.class);
+
+ @Inject
+ PhysicalNetworkDao _physicalNetworkDao;
+ @Inject
+ BigSwitchBcfDao _bigswitchBcfDao;
+ @Inject
+ HostDao _hostDao;
+ @Inject
+ AgentManager _agentMgr;
+ @Inject
+ DomainRouterDao _routerDao;
+ @Inject
+ VMInstanceDao _vmDao;
+ @Inject
+ AccountDao _accountDao;
+ @Inject
+ VpcDao _vpcDao;
+ @Inject
+ IPAddressDao _ipAddressDao;
+ @Inject
+ HostPodDao _podDao;
+ @Inject
+ NetworkService _netService;
+ @Inject
+ VlanDao _vlanDao;
+ @Inject
+ FirewallRulesDao _fwRulesDao;
+ @Inject
+ FirewallRulesCidrsDao _fwCidrsDao;
+ @Inject
+ NetworkACLItemDao _aclItemDao;
+ @Inject
+ NetworkACLItemCidrsDao _aclItemCidrsDao;
+
+ private BigSwitchBcfUtils _bcfUtils = null;
+
+ public BigSwitchBcfGuestNetworkGuru() {
+ super();
+ _isolationMethods = new IsolationMethod[] {IsolationMethod.BCF_SEGMENT};
+ }
+
+ @Override
+ protected boolean canHandle(NetworkOffering offering, NetworkType networkType, PhysicalNetwork physicalNetwork) {
+ if (networkType == NetworkType.Advanced && isMyTrafficType(offering.getTrafficType()) && offering.getGuestType() == Network.GuestType.Isolated &&
+ isMyIsolationMethod(physicalNetwork)) {
+ return true;
+ } else {
+ s_logger.trace("We only take care of Guest networks of type " + GuestType.Isolated + " in zone of type " + NetworkType.Advanced);
+ return false;
+ }
+ }
+
+ @Override
+ public Network design(NetworkOffering offering, DeploymentPlan plan, Network userSpecified, Account owner) {
+ // Check if the isolation type of the physical network is BCF_SEGMENT, then delegate GuestNetworkGuru to design
+ PhysicalNetworkVO physnet = _physicalNetworkDao.findById(plan.getPhysicalNetworkId());
+ if (physnet == null || physnet.getIsolationMethods() == null || !physnet.getIsolationMethods().contains("BCF_SEGMENT")) {
+ s_logger.debug("Refusing to design this network, the physical isolation type is not BCF_SEGMENT");
+ return null;
+ }
+
+ List<BigSwitchBcfDeviceVO> devices = _bigswitchBcfDao.listByPhysicalNetwork(physnet.getId());
+ if (devices.isEmpty()) {
+ s_logger.error("No BigSwitch Controller on physical network " + physnet.getName());
+ return null;
+ }
+ for (BigSwitchBcfDeviceVO d: devices){
+ s_logger.debug("BigSwitch Controller " + d.getUuid()
+ + " found on physical network " + physnet.getId());
+ }
+
+ s_logger.debug("Physical isolation type is BCF_SEGMENT, asking GuestNetworkGuru to design this network");
+ NetworkVO networkObject = (NetworkVO)super.design(offering, plan, userSpecified, owner);
+ if (networkObject == null) {
+ return null;
+ }
+ // Override the broadcast domain type
+ networkObject.setBroadcastDomainType(BroadcastDomainType.Vlan);
+
+ return networkObject;
+ }
+
+ @Override
+ public Network implement(Network network, NetworkOffering offering, DeployDestination dest, ReservationContext context)
+ throws InsufficientVirtualNetworkCapacityException {
+ assert (network.getState() == State.Implementing) : "Why are we implementing " + network;
+
+ bcfUtilsInit();
+
+ long dcId = dest.getDataCenter().getId();
+
+ long physicalNetworkId = _networkModel.findPhysicalNetworkId(dcId, offering.getTags(), offering.getTrafficType());
+
+ NetworkVO implemented =
+ new NetworkVO(network.getTrafficType(), network.getMode(), network.getBroadcastDomainType(), network.getNetworkOfferingId(), State.Allocated,
+ network.getDataCenterId(), physicalNetworkId, false);
+
+ if (network.getGateway() != null) {
+ implemented.setGateway(network.getGateway());
+ }
+
+ if (network.getCidr() != null) {
+ implemented.setCidr(network.getCidr());
+ }
+
+ String vnetId = "";
+ if (network.getBroadcastUri() == null) {
+ vnetId = _dcDao.allocateVnet(dcId, physicalNetworkId, network.getAccountId(), context.getReservationId(), UseSystemGuestVlans.valueIn(network.getAccountId()));
+ if (vnetId == null) {
+ throw new InsufficientVirtualNetworkCapacityException("Unable to allocate vnet as a " +
+ "part of network " + network + " implement ", DataCenter.class, dcId);
+ }
+ implemented.setBroadcastUri(BroadcastDomainType.Vlan.toUri(vnetId));
+ ActionEventUtils.onCompletedActionEvent(CallContext.current().getCallingUserId(), network.getAccountId(),
+ EventVO.LEVEL_INFO, EventTypes.EVENT_ZONE_VLAN_ASSIGN, "Assigned Zone Vlan: " + vnetId + " Network Id: " + network.getId(), 0);
+ } else {
+ implemented.setBroadcastUri(network.getBroadcastUri());
+ }
+
+ // Name is either the given name or the uuid
+ String name = network.getName();
+ if (name == null || name.isEmpty()) {
+ name = ((NetworkVO)network).getUuid();
+ }
+ if (name.length() > 64) {
+ name = name.substring(0, 63); // max length 64
+ }
+
+ // update fields in network object
+ NetworkVO networkObject = (NetworkVO) network;
+
+ // determine whether this is VPC network or stand-alone network
+ Vpc vpc = null;
+ if(network.getVpcId()!=null){
+ vpc = _vpcDao.acquireInLockTable(network.getVpcId());
+ }
+
+ // use uuid of networkVO as network id in BSN
+ String networkId = networkObject.getUuid();
+
+ String tenantId;
+ String tenantName;
+ if (vpc != null) {
+ tenantId = vpc.getUuid();
+ tenantName = vpc.getName();
+ _vpcDao.releaseFromLockTable(vpc.getId());
+ } else {
+ // use network in CS as tenant in BSN
+ // for non-VPC networks, use network name and id as tenant name and id
+ tenantId = networkId;
+ tenantName = name;
+ }
+
+ // store tenantId in networkObject for NetworkOrchestrator implementNetwork use
+ networkObject.setNetworkDomain(tenantId);
+ // store tenant Id in implemented object for future actions (e.g., shutdown)
+ implemented.setNetworkDomain(tenantId);
+ String vlanStr = BroadcastDomainType.getValue(implemented.getBroadcastUri());
+
+ // get public net info - needed to set up source nat gateway
+ NetworkVO pubNet = _bcfUtils.getPublicNetwork(physicalNetworkId);
+
+ // locate subnet info
+ SearchCriteria<VlanVO> sc = _vlanDao.createSearchCriteria();
+ sc.setParameters("network_id", pubNet.getId());
+ VlanVO pubVlanVO = _vlanDao.findOneBy(sc);
+ String pubVlanStr = pubVlanVO.getVlanTag();
+
+ Integer vlan;
+ if(StringUtils.isNumeric(vlanStr)){
+ vlan = Integer.valueOf(vlanStr);
+ } else {
+ vlan = 0;
+ }
+ CreateBcfSegmentCommand cmd1 = new CreateBcfSegmentCommand(tenantId, tenantName, networkId, name, vlan);
+
+ _bcfUtils.sendBcfCommandWithNetworkSyncCheck(cmd1, networkObject);
+
+ if(_bcfUtils.isNatEnabled()){
+ Integer pvlan;
+ if(StringUtils.isNumeric(pubVlanStr)){
+ pvlan = Integer.valueOf(pubVlanStr);
+ } else {
+ pvlan = 0;
+ }
+ CreateBcfSegmentCommand cmd2 = new CreateBcfSegmentCommand("external", "external", "external", "external", pvlan);
+
+ _bcfUtils.sendBcfCommandWithNetworkSyncCheck(cmd2, networkObject);
+
+ CreateBcfRouterCommand cmd3 = new CreateBcfRouterCommand(tenantId);
+
+ _bcfUtils.sendBcfCommandWithNetworkSyncCheck(cmd3, network);
+
+ CreateBcfRouterInterfaceCommand cmd5 = new CreateBcfRouterInterfaceCommand(tenantId, network.getUuid(), network.getCidr(),
+ network.getGateway(), network.getName());
+
+ _bcfUtils.sendBcfCommandWithNetworkSyncCheck(cmd5, network);
+ }
+ return implemented;
+ }
+
+
+
+ @Override
+ public void reserve(NicProfile nic, Network network, VirtualMachineProfile vm, DeployDestination dest, ReservationContext context)
+ throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException {
+ // Have GuestNetworkGuru reserve for us
+ super.reserve(nic, network, vm, dest, context);
+ }
+
+ @Override
+ public boolean release(NicProfile nic, VirtualMachineProfile vm, String reservationId) {
+ return super.release(nic, vm, reservationId);
+ }
+
+ @Override
+ public void shutdown(NetworkProfile profile, NetworkOffering offering) {
+ NetworkVO networkObject = _networkDao.findById(profile.getId());
+ if (networkObject.getBroadcastDomainType() != BroadcastDomainType.Vlan || networkObject.getBroadcastUri() == null) {
+ s_logger.warn("BroadcastUri is empty or incorrect for guestnetwork " + networkObject.getDisplayText());
+ return;
+ }
+
+ bcfUtilsInit();
+
+ // tenantId stored in network domain field at creation
+ String tenantId = networkObject.getNetworkDomain();
+
+ String networkId = networkObject.getUuid();
+
+ DeleteBcfSegmentCommand cmd = new DeleteBcfSegmentCommand(tenantId, networkId);
+
+ _bcfUtils.sendBcfCommandWithNetworkSyncCheck(cmd, networkObject);
+
+ super.shutdown(profile, offering);
+ }
+
+ @Override
+ public boolean trash(Network network, NetworkOffering offering) {
+ return super.trash(network, offering);
+ }
+
+ @Override
+ public boolean prepareMigration(NicProfile nic, Network network,
+ VirtualMachineProfile vm, DeployDestination dest,
+ ReservationContext context) {
+ bcfUtilsInit();
+
+ // get arguments for CreateBcfAttachmentCommand
+ // determine whether this is VPC network or stand-alone network
+ Vpc vpc = null;
+ if(network.getVpcId()!=null){
+ vpc = _vpcDao.acquireInLockTable(network.getVpcId());
+ }
+
+ String networkId = network.getUuid();
+
+ String tenantId;
+ String tenantName;
+ if (vpc != null) {
+ tenantId = vpc.getUuid();
+ tenantName = vpc.getName();
+ boolean released = _vpcDao.releaseFromLockTable(vpc.getId());
+ s_logger.debug("BCF guru release lock vpc id: " + vpc.getId()
+ + " released? " + released);
+ } else {
+ // use network id in CS as tenant in BSN
+ // use network uuid as tenant id for non-VPC networks
+ tenantId = networkId;
+ tenantName = network.getName();
+ }
+
+ String hostname = dest.getHost().getName();
+ long zoneId = network.getDataCenterId();
+ String vmwareVswitchLabel = _networkModel.getDefaultGuestTrafficLabel(zoneId, HypervisorType.VMware);
+ String[] labelArray = null;
+ String vswitchName = null;
+ if(vmwareVswitchLabel!=null){
+ labelArray=vmwareVswitchLabel.split(",");
+ vswitchName = labelArray[0];
+ }
+
+ // hypervisor type:
+ // kvm: ivs port name
+ // vmware: specific portgroup naming convention
+ String pgName = "";
+ if (dest.getHost().getHypervisorType() == HypervisorType.KVM){
+ pgName = hostname;
+ } else if (dest.getHost().getHypervisorType() == HypervisorType.VMware){
+ pgName = hostname + "-" + vswitchName;
+ }
+
+ String nicId = nic.getUuid();
+ Integer vlan = Integer.valueOf(BroadcastDomainType.getValue(nic.getIsolationUri()));
+ String ipv4 = nic.getIp4Address();
+ String mac = nic.getMacAddress();
+
+ CreateBcfAttachmentCommand cmd = new CreateBcfAttachmentCommand(tenantId,
+ tenantName, networkId, pgName, nicId, vlan, ipv4, mac);
+
+ _bcfUtils.sendBcfCommandWithNetworkSyncCheck(cmd, network);
+
+ return true;
+ }
+
+ @Override
+ public void rollbackMigration(NicProfile nic, Network network,
+ VirtualMachineProfile vm, ReservationContext src,
+ ReservationContext dst) {
+ s_logger.debug("BCF guru rollback migration");
+ }
+
+ @Override
+ public void commitMigration(NicProfile nic, Network network,
+ VirtualMachineProfile vm, ReservationContext src,
+ ReservationContext dst) {
+ s_logger.debug("BCF guru commit migration");
+ }
+
+ private void bcfUtilsInit(){
+ if (_bcfUtils == null) {
+ _bcfUtils = new BigSwitchBcfUtils(_networkDao, _nicDao,
+ _vmDao, _hostDao, _vpcDao, _bigswitchBcfDao,
+ _agentMgr, _vlanDao, _ipAddressDao, _fwRulesDao,
+ _fwCidrsDao, _aclItemDao, _aclItemCidrsDao, _networkModel);
+ }
+ }
+}