package com.microsoft.azure.management.network.implementation;
import com.microsoft.azure.management.apigeneration.LangDefinition;
import com.microsoft.azure.management.network.IPConfiguration;
import com.microsoft.azure.management.network.Network;
import com.microsoft.azure.management.network.NetworkInterface;
import com.microsoft.azure.management.network.NetworkSecurityGroup;
import com.microsoft.azure.management.network.NicIPConfiguration;
import com.microsoft.azure.management.network.RouteTable;
import com.microsoft.azure.management.network.ServiceEndpointPropertiesFormat;
import com.microsoft.azure.management.network.ServiceEndpointType;
import com.microsoft.azure.management.network.Subnet;
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
import com.microsoft.azure.management.resources.fluentcore.arm.ResourceUtils;
import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.ChildResourceImpl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
@LangDefinition
class SubnetImpl
extends ChildResourceImpl<SubnetInner, NetworkImpl, Network>
implements
Subnet,
Subnet.Definition<Network.DefinitionStages.WithCreateAndSubnet>,
Subnet.UpdateDefinition<Network.Update>,
Subnet.Update {
SubnetImpl(SubnetInner inner, NetworkImpl parent) {
super(inner, parent);
}
@Override
public int networkInterfaceIPConfigurationCount() {
List<IPConfiguration> ipConfigRefs = this.inner().ipConfigurations();
if (ipConfigRefs != null) {
return ipConfigRefs.size();
} else {
return 0;
}
}
@Override
public String addressPrefix() {
return this.inner().addressPrefix();
}
@Override
public String name() {
return this.inner().name();
}
@Override
public String networkSecurityGroupId() {
return (this.inner().networkSecurityGroup() != null) ? this.inner().networkSecurityGroup().id() : null;
}
@Override
public String routeTableId() {
return (this.inner().routeTable() != null) ? this.inner().routeTable().id() : null;
}
@Override
public Map<ServiceEndpointType, List<Region>> servicesWithAccess() {
Map<ServiceEndpointType, List<Region>> services = new HashMap<>();
if (this.inner().serviceEndpoints() != null) {
for (ServiceEndpointPropertiesFormat endpoint : this.inner().serviceEndpoints()) {
ServiceEndpointType serviceEndpointType = ServiceEndpointType.fromString(endpoint.service());
if (!services.containsKey(serviceEndpointType)) {
services.put(serviceEndpointType, new ArrayList<Region>());
}
if (endpoint.locations() != null) {
List<Region> regions = new ArrayList<>();
for (String location : endpoint.locations()) {
regions.add(Region.fromName(location));
}
services.get(serviceEndpointType).addAll(regions);
}
}
}
return services;
}
@Override
public SubnetImpl withoutNetworkSecurityGroup() {
this.inner().withNetworkSecurityGroup(null);
return this;
}
@Override
public SubnetImpl withExistingNetworkSecurityGroup(NetworkSecurityGroup nsg) {
return withExistingNetworkSecurityGroup(nsg.id());
}
@Override
public SubnetImpl withExistingNetworkSecurityGroup(String resourceId) {
NetworkSecurityGroupInner reference = new NetworkSecurityGroupInner().withId(resourceId);
this.inner().withNetworkSecurityGroup(reference);
return this;
}
@Override
public SubnetImpl withExistingRouteTable(String resourceId) {
RouteTableInner reference = new RouteTableInner().withId(resourceId);
this.inner().withRouteTable(reference);
return this;
}
@Override
public SubnetImpl withExistingRouteTable(RouteTable routeTable) {
return this.withExistingRouteTable(routeTable.id());
}
@Override
public Update withoutRouteTable() {
this.inner().withRouteTable(null);
return this;
}
@Override
public SubnetImpl withAddressPrefix(String cidr) {
this.inner().withAddressPrefix(cidr);
return this;
}
@Override
public SubnetImpl withAccessFromService(ServiceEndpointType service) {
if (this.inner().serviceEndpoints() == null) {
this.inner().withServiceEndpoints(new ArrayList<ServiceEndpointPropertiesFormat>());
}
boolean found = false;
for (ServiceEndpointPropertiesFormat endpoint : this.inner().serviceEndpoints()) {
if (endpoint.service().equalsIgnoreCase(service.toString())) {
found = true;
break;
}
}
if (!found) {
this.inner()
.serviceEndpoints()
.add(new ServiceEndpointPropertiesFormat()
.withService(service.toString())
.withLocations(new ArrayList<String>()));
}
return this;
}
@Override
public Update withoutAccessFromService(ServiceEndpointType service) {
if (this.inner().serviceEndpoints() != null) {
int foundIndex = -1;
int i = 0;
for (ServiceEndpointPropertiesFormat endpoint : this.inner().serviceEndpoints()) {
if (endpoint.service().equalsIgnoreCase(service.toString())) {
foundIndex = i;
break;
}
i++;
}
if (foundIndex != -1) {
this.inner().serviceEndpoints().remove(foundIndex);
}
}
return this;
}
@Override
public NetworkImpl attach() {
return this.parent().withSubnet(this);
}
@Override
public RouteTable getRouteTable() {
return (this.routeTableId() != null)
? this.parent().manager().routeTables().getById(this.routeTableId())
: null;
}
@Override
public NetworkSecurityGroup getNetworkSecurityGroup() {
String nsgId = this.networkSecurityGroupId();
return (nsgId != null)
? this.parent().manager().networkSecurityGroups().getById(nsgId)
: null;
}
@Override
public Set<NicIPConfiguration> getNetworkInterfaceIPConfigurations() {
return Collections.unmodifiableSet(new TreeSet<NicIPConfiguration>(listNetworkInterfaceIPConfigurations()));
}
@Override
public Collection<NicIPConfiguration> listNetworkInterfaceIPConfigurations() {
Collection<NicIPConfiguration> ipConfigs = new ArrayList<>();
Map<String, NetworkInterface> nics = new TreeMap<>();
List<IPConfiguration> ipConfigRefs = this.inner().ipConfigurations();
if (ipConfigRefs == null) {
return ipConfigs;
}
for (IPConfiguration ipConfigRef : ipConfigRefs) {
String nicID = ResourceUtils.parentResourceIdFromResourceId(ipConfigRef.id());
String ipConfigName = ResourceUtils.nameFromResourceId(ipConfigRef.id());
NetworkInterface nic = nics.get(nicID.toLowerCase());
if (nic == null) {
nic = this.parent().manager().networkInterfaces().getById(nicID);
}
if (nic == null) {
continue;
}
nics.put(nic.id().toLowerCase(), nic);
NicIPConfiguration ipConfig = nic.ipConfigurations().get(ipConfigName);
if (ipConfig == null) {
continue;
}
ipConfigs.add(ipConfig);
}
return Collections.unmodifiableCollection(ipConfigs);
}
@Override
public Set<String> listAvailablePrivateIPAddresses() {
Set<String> ipAddresses = new TreeSet<>();
String cidr = this.addressPrefix();
if (cidr == null) {
return ipAddresses;
}
String takenIPAddress = cidr.split("/")[0];
IPAddressAvailabilityResultInner result = this.parent().manager().networks().inner().checkIPAddressAvailability(
this.parent().resourceGroupName(),
this.parent().name(),
takenIPAddress);
if (result == null) {
return ipAddresses;
}
ipAddresses.addAll(result.availableIPAddresses());
return ipAddresses;
}
}