class EnvironmentBaseNetworkConverter implements EnvironmentNetworkConverter {

    @Override
    public Network convertToLegacyNetwork(EnvironmentNetworkResponse source, String availabilityZone) {
        Network result = new Network();
        result.setName(missingResourceNameGenerator.generateName(APIResourceType.NETWORK));
        result.setSubnetCIDR(null);
        result.setOutboundInternetTraffic(source.getOutboundInternetTraffic());
        result.setNetworkCidrs(source.getNetworkCidrs());
        Map<String, Object> attributes = new HashMap<>();
        Optional<CloudSubnet> cloudSubnet;
        if (StringUtils.isNotBlank(source.getPreferedSubnetId())) {
            LOGGER.debug("Choosing subnet by prefered subnet Id {}", source.getPreferedSubnetId());
            CloudSubnet cloudSubnetById = source.getSubnetMetas().get(source.getPreferedSubnetId());
            if (cloudSubnetById == null) {
                cloudSubnetById = source.getSubnetMetas().values()
                        .stream()
                        .filter(e -> e.getId().equals(source.getPreferedSubnetId()))
                        .findFirst().orElse(null);
            }
            cloudSubnet = Optional.of(cloudSubnetById);
        } else if (StringUtils.isNotEmpty(availabilityZone)) {
            LOGGER.debug("Choosing subnet by availability zone {}", availabilityZone);
            cloudSubnet = source.getSubnetMetas().values().stream()
                    .filter(s -> StringUtils.isNotEmpty(s.getAvailabilityZone()) &&
                            s.getAvailabilityZone().equals(availabilityZone))
                    .findFirst();
        } else {
            LOGGER.debug("Fallback to choose random subnet");
            cloudSubnet = source.getSubnetMetas().values().stream().findFirst();
        }
        if (cloudSubnet.isEmpty()) {
            throw new BadRequestException("No subnet for the given availability zone: " + availabilityZone);
        }
        LOGGER.debug("Chosen subnet: {}", cloudSubnet.get());
        attributes.put("subnetId", cloudSubnet.get().getId());
        attributes.put("cloudPlatform", getCloudPlatform().name());
        attributes.putAll(getAttributesForLegacyNetwork(source));
        try {
            result.setAttributes(new Json(attributes));
        } catch (IllegalArgumentException e) {
            LOGGER.debug("Environment's network could not be converted to network.", e);
        }
        return result;
    }

}