- NeutronCRUDInterfaces interfaces = getNeutronInterfaces(true, true);
- INeutronPortCRUD portInterface = interfaces.getPortInterface();
- INeutronNetworkCRUD networkInterface = interfaces.getNetworkInterface();
- INeutronSubnetCRUD subnetInterface = interfaces.getSubnetInterface();
- if (input.isSingleton()) {
- NeutronPort singleton = input.getSingleton();
-
- /*
- * the port must be part of an existing network, must not already exist,
- * have a valid MAC and the MAC not be in use
- */
- if (singleton.getNetworkUUID() == null) {
- throw new BadRequestException("network UUID musy be specified");
- }
- if (portInterface.portExists(singleton.getID())) {
- throw new BadRequestException(UUID_EXISTS);
- }
- if (!networkInterface.networkExists(singleton.getNetworkUUID())) {
- throw new ResourceNotFoundException("network UUID does not exist.");
- }
- if (singleton.getMacAddress() == null ||
- !singleton.getMacAddress().matches(MAC_REGEX)) {
- throw new BadRequestException("MAC address not properly formatted");
- }
- if (portInterface.macInUse(singleton.getMacAddress())) {
- throw new ResourceConflictException("MAC Address is in use.");
- }
- /*
- * if fixed IPs are specified, each one has to have an existing subnet ID
- * that is in the same scoping network as the port. In addition, if an IP
- * address is specified it has to be a valid address for the subnet and not
- * already in use
- */
- List<Neutron_IPs> fixedIPs = singleton.getFixedIPs();
- if (fixedIPs != null && fixedIPs.size() > 0) {
- Iterator<Neutron_IPs> fixedIPIterator = fixedIPs.iterator();
- while (fixedIPIterator.hasNext()) {
- Neutron_IPs ip = fixedIPIterator.next();
- if (ip.getSubnetUUID() == null) {
- throw new BadRequestException("subnet UUID not specified");
- }
- NeutronSubnet subnet = subnetInterface.getSubnet(ip.getSubnetUUID());
- if (subnet == null) {
- throw new BadRequestException("subnet UUID must exist");
- }
- if (!singleton.getNetworkUUID().equalsIgnoreCase(subnet.getNetworkUUID())) {
- throw new BadRequestException(NET_UUID_MATCH);
- }
- if (ip.getIpAddress() != null) {
- if (!subnet.isValidIP(ip.getIpAddress())) {
- throw new BadRequestException("IP address is not valid");
- }
- if (subnet.isIPInUse(ip.getIpAddress())) {
- throw new ResourceConflictException("IP address is in use.");
- }
- }
- }
- }
-
- Object[] instances = NeutronUtil.getInstances(INeutronPortAware.class, this);
- if (instances != null) {
- if (instances.length > 0) {
- for (Object instance : instances) {
- INeutronPortAware service = (INeutronPortAware) instance;
- int status = service.canCreatePort(singleton);
- if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
- return Response.status(status).build();
- }
- }
- } else {
- throw new ServiceUnavailableException(NO_PROVIDERS);
- }
- } else {
- throw new ServiceUnavailableException(NO_PROVIDER_LIST);
- }
-
- // add the port to the cache
- portInterface.addPort(singleton);
- if (instances != null) {
- for (Object instance : instances) {
- INeutronPortAware service = (INeutronPortAware) instance;
- service.neutronPortCreated(singleton);
- }
- }
- } else {
- List<NeutronPort> bulk = input.getBulk();
- Iterator<NeutronPort> i = bulk.iterator();
- Map<String, NeutronPort> testMap = new HashMap<String, NeutronPort>();
- Object[] instances = NeutronUtil.getInstances(INeutronPortAware.class, this);
- while (i.hasNext()) {
- NeutronPort test = i.next();
-
- /*
- * the port must be part of an existing network, must not already exist,
- * have a valid MAC and the MAC not be in use. Further the bulk request
- * can't already contain a new port with the same UUID
- */
- if (portInterface.portExists(test.getID())) {
- throw new BadRequestException(UUID_EXISTS);
- }
- if (testMap.containsKey(test.getID())) {
- throw new BadRequestException(UUID_EXISTS);
- }
- for (NeutronPort check : testMap.values()) {
- if (test.getMacAddress().equalsIgnoreCase(check.getMacAddress())) {
- throw new ResourceConflictException("MAC address already allocated");
- }
- for (Neutron_IPs test_fixedIP : test.getFixedIPs()) {
- for (Neutron_IPs check_fixedIP : check.getFixedIPs()) {
- if (test_fixedIP.getSubnetUUID().equals(check_fixedIP.getSubnetUUID()) &&
- (test_fixedIP.getIpAddress().equals(check_fixedIP.getIpAddress()))) {
- throw new ResourceConflictException("IP address already allocated");
- }
- }
- }
- }
- testMap.put(test.getID(), test);
- if (!networkInterface.networkExists(test.getNetworkUUID())) {
- throw new ResourceNotFoundException("network UUID does not exist.");
- }
- if (!test.getMacAddress().matches(MAC_REGEX)) {
- throw new BadRequestException("MAC address not properly formatted");
- }
- if (portInterface.macInUse(test.getMacAddress())) {
- throw new ResourceConflictException("MAC address in use");
- }
-
- /*
- * if fixed IPs are specified, each one has to have an existing subnet ID
- * that is in the same scoping network as the port. In addition, if an IP
- * address is specified it has to be a valid address for the subnet and not
- * already in use (or be the gateway IP address of the subnet)
- */
- List<Neutron_IPs> fixedIPs = test.getFixedIPs();
- if (fixedIPs != null && fixedIPs.size() > 0) {
- Iterator<Neutron_IPs> fixedIPIterator = fixedIPs.iterator();
- while (fixedIPIterator.hasNext()) {
- Neutron_IPs ip = fixedIPIterator.next();
- if (ip.getSubnetUUID() == null) {
- throw new BadRequestException("subnet UUID must be specified");
- }
- if (!subnetInterface.subnetExists(ip.getSubnetUUID())) {
- throw new BadRequestException("subnet UUID doesn't exists");
- }
- NeutronSubnet subnet = subnetInterface.getSubnet(ip.getSubnetUUID());
- if (!test.getNetworkUUID().equalsIgnoreCase(subnet.getNetworkUUID())) {
- throw new BadRequestException(NET_UUID_MATCH);
- }
- if (ip.getIpAddress() != null) {
- if (!subnet.isValidIP(ip.getIpAddress())) {
- throw new BadRequestException("ip address not valid");
- }
- //TODO: need to add consideration for a fixed IP being assigned the same address as a allocated IP in the
- //same bulk create
- if (subnet.isIPInUse(ip.getIpAddress())) {
- throw new ResourceConflictException("IP address in use");
- }
- }
- }
- }
- if (instances != null) {
- if (instances.length > 0) {
- for (Object instance : instances) {
- INeutronPortAware service = (INeutronPortAware) instance;
- int status = service.canCreatePort(test);
- if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
- return Response.status(status).build();
- }
- }
- } else {
- throw new ServiceUnavailableException(NO_PROVIDERS);
- }
- } else {
- throw new ServiceUnavailableException(NO_PROVIDER_LIST);
- }
- }