- # Virtual network operations
-
- def _create_virtual_network(self, controller_url, name, vni):
- routetarget = '{}:{}'.format(self.asn,vni)
- vnet_dict = {
- "virtual-network": {
- "virtual_network_properties": {
- "vxlan_network_identifier": vni,
- },
- "parent_type": "project",
- "fq_name": [
- "default-domain",
- "admin",
- name
- ],
- "route_target_list": {
- "route_target": [
- "target:" + routetarget
- ]
- }
- }
- }
- self._get_token()
- endpoint = controller_url + 'virtual-networks'
- http_code, resp = self.http.post_cmd(url = endpoint,
- headers = self.http_header,
- postfields_dict = vnet_dict)
- if http_code not in (200, 201, 202, 204) or not resp:
- raise SdnConnectorError('Unexpected http status code, or empty response')
- vnet_info = json.loads(resp)
- self.logger.debug("vnet_info: {}".format(vnet_info))
- return vnet_info.get("virtual-network").get('uuid'), vnet_info.get("virtual-network")
-
-
- def _get_virtual_networks(self, controller_url):
- self._get_token()
- endpoint = controller_url + 'virtual-networks'
- print(endpoint)
- http_code, resp = self.http.get_cmd(url=endpoint, headers=self.http_header)
- if http_code not in (200, 201, 202, 204) or not resp:
- raise SdnConnectorError('Unexpected http status code, or empty response')
- vnets_info = json.loads(resp)
- self.logger.debug("vnets_info: {}".format(vnets_info))
- return vnets_info.get('virtual-networks')
-
-
- def _get_virtual_network(self, controller_url, network_id):
- self._get_token()
- endpoint = controller_url + 'virtual-network/{}'.format(network_id)
- http_code, resp = self.http.get_cmd(url=endpoint, headers=self.http_header)
- if http_code not in (200, 201, 202, 204) or not resp:
- if http_code == 404:
- return None
- raise SdnConnectorError('Unexpected http status code, or empty response')
- vnet_info = json.loads(resp)
- self.logger.debug("vnet_info: {}".format(vnet_info))
- return vnet_info.get("virtual-network")
-
-
- def _delete_virtual_network(self, controller_url, network_id):
- self._get_token()
- endpoint = controller_url + 'virtual-network/{}'.format(network_id)
- http_code, _ = self.http.delete_cmd(url=endpoint, headers=self.http_header)
- if http_code not in (200, 201, 202, 204):
- raise SdnConnectorError('Unexpected http status code')
- return
-
-
- # Virtual port group operations
-
- def _create_vpg(self, controller_url, switch_id, switch_port, network, vlan):
- vpg_dict = {
- "virtual-port-group": {
- }
- }
- self._get_token()
- endpoint = controller_url + 'virtual-port-groups'
- http_code, resp = self.http.post_cmd(url = endpoint,
- headers = self.http_header,
- postfields_dict = vpg_dict)
- if http_code not in (200, 201, 202, 204) or not resp:
- raise SdnConnectorError('Unexpected http status code, or empty response')
- vpg_info = json.loads(resp)
- self.logger.debug("vpg_info: {}".format(vpg_info))
- return vpg_info.get("virtual-port-group").get('uuid'), vpg_info.get("virtual-port-group")
-
-
- def _get_vpgs(self, controller_url):
- self._get_token()
- endpoint = controller_url + 'virtual-port-groups'
- http_code, resp = self.http.get_cmd(url=endpoint, headers=self.http_header)
- if http_code not in (200, 201, 202, 204) or not resp:
- raise SdnConnectorError('Unexpected http status code, or empty response')
- vpgs_info = json.loads(resp)
- self.logger.debug("vpgs_info: {}".format(vpgs_info))
- return vpgs_info.get('virtual-port-groups')
-
-
- def _get_vpg(self, controller_url, vpg_id):
- self._get_token()
- endpoint = controller_url + 'virtual-port-group/{}'.format(vpg_id)
- http_code, resp = self.http.get_cmd(url=endpoint, headers=self.http_header)
- if http_code not in (200, 201, 202, 204) or not resp:
- if http_code == 404:
- return None
- raise SdnConnectorError('Unexpected http status code, or empty response')
- vpg_info = json.loads(resp)
- self.logger.debug("vpg_info: {}".format(vpg_info))
- return vpg_info.get("virtual-port-group")
-
-
- def _delete_vpg(self, controller_url, vpg_id):
- self._get_token()
- endpoint = controller_url + 'virtual-port-group/{}'.format(vpg_id)
- http_code, resp = self.http.delete_cmd(url=endpoint, headers=self.http_header)
- if http_code not in (200, 201, 202, 204):
- raise SdnConnectorError('Unexpected http status code')
- return
+ def _create_port(self, switch_id, switch_port, network, vlan):
+ """
+ 1 - Look for virtual port groups for provided switch_id, switch_port using name
+ 2 - It the virtual port group does not exist, create it
+ 3 - Create virtual machine interface for the indicated network and vlan
+ """
+ self.logger.debug("create_port: switch_id: {}, switch_port: {}, network: {}, vlan: {}".format(
+ switch_id, switch_port, network, vlan))
+
+ # 1 - Check if the vpg exists
+ vpg_name = self.underlay_api.get_vpg_name(switch_id, switch_port)
+ vpg = self.underlay_api.get_vpg_by_name(vpg_name)
+ if not vpg:
+ # 2 - If it does not exist create it
+ vpg_id, _ = self.underlay_api.create_vpg(switch_id, switch_port)
+ else:
+ # Assign vpg_id from vpg
+ vpg_id = vpg.get("uuid")
+
+ # 3 - Check if the vmi alreaady exists
+ vmi_id, _ = self.underlay_api.create_vmi(switch_id, switch_port, network, vlan)
+ self.logger.debug("port created")