Search in sources :

Example 1 with Port

use of org.opentosca.toscana.model.datatype.Port in project TOSCAna by StuPro-TOSCAna.

the class DockerfileBuildingVisitor method visit.

@Override
public void visit(WebApplication node) {
    // If a Web Application does not define a port. add the defaults for HTTP and HTTPS
    if (!node.getAppEndpoint().getPort().isPresent()) {
        ports.add(80);
        ports.add(443);
        // Set the ports in the model
        node.getAppEndpoint().setPort(new Port(80));
    }
    handleDefault(node, new String[] {});
}
Also used : Port(org.opentosca.toscana.model.datatype.Port)

Example 2 with Port

use of org.opentosca.toscana.model.datatype.Port in project TOSCAna by StuPro-TOSCAna.

the class DataTypeTest method portTest.

@Test
public void portTest() {
    EffectiveModel model = new EffectiveModelFactory().create(TestTemplates.Datatypes.PORT, logMock());
    WebApplication app = (WebApplication) model.getNodes().iterator().next();
    EndpointCapability endpoint = app.getAppEndpoint();
    assertEquals(new Port(3000), endpoint.getPort().get());
    Port expected = new Port(4000);
    endpoint.setPort(expected);
    assertEquals(expected, endpoint.getPort().get());
}
Also used : Port(org.opentosca.toscana.model.datatype.Port) WebApplication(org.opentosca.toscana.model.node.WebApplication) EffectiveModel(org.opentosca.toscana.model.EffectiveModel) EffectiveModelFactory(org.opentosca.toscana.model.EffectiveModelFactory) EndpointCapability(org.opentosca.toscana.model.capability.EndpointCapability) BaseUnitTest(org.opentosca.toscana.core.BaseUnitTest) Test(org.junit.Test)

Example 3 with Port

use of org.opentosca.toscana.model.datatype.Port in project TOSCAna by StuPro-TOSCAna.

the class PrepareModelNodeVisitor method visit.

@Override
public void visit(MysqlDatabase node) {
    // if certain values aren't given, fill them
    if (node.getPassword().isPresent()) {
        // password needs to be at least 8 characters long
        String password = node.getPassword().get();
        if (password.length() < minPWLength) {
            logger.warn("Database password too short, creating new random password");
            node.setPassword(randomString(minPWLength));
        }
    } else {
        logger.warn("No database password given, creating new random password");
        node.setPassword(randomString(minPWLength));
    }
    if (!node.getUser().isPresent()) {
        logger.warn("User not set, setting to default");
        node.setUser(DEFAULT_DB_USER);
    }
    if (!node.getPort().isPresent()) {
        logger.warn("Database port not set, setting to default");
        node.setPort(DEFAULT_DB_PORT);
    }
    // check if Mysql is the only node hosted on his compute node
    Compute compute = getCompute(node);
    if (topology.incomingEdgesOf(compute).stream().filter(relation -> relation instanceof HostedOn).collect(Collectors.toSet()).size() == 1) {
        // means our dbms is the only one hosted on this compute
        // means we can set the private address as reference the database endpoint
        Fn databaseEndpointFn = Fn.fnGetAtt(toAlphanumerical(node.getEntityName()), AWS_ENDPOINT_REFERENCE);
        String databaseEndpoint = databaseEndpointFn.toString(true);
        cfnModule.putFn(databaseEndpoint, databaseEndpointFn);
        compute.setPrivateAddress(databaseEndpoint);
        compute.setPublicAddress(databaseEndpoint);
        logger.debug("Set private address and public address of '{}' to reference MysqlDatabase '{}'", compute.getEntityName(), node.getEntityName());
        // also the underlying compute should not get mapped to an ec2
        cfnModule.removeComputeToEc2(compute);
        logger.debug("Removing Compute '{}' to be transformed", compute.getEntityName());
    }
}
Also used : Compute(org.opentosca.toscana.model.node.Compute) HostedOn(org.opentosca.toscana.model.relation.HostedOn) NodeVisitor(org.opentosca.toscana.model.visitor.NodeVisitor) Collectors(java.util.stream.Collectors) SecureRandom(java.security.SecureRandom) Port(org.opentosca.toscana.model.datatype.Port) Fn(com.scaleset.cfbuilder.core.Fn) MysqlDatabase(org.opentosca.toscana.model.node.MysqlDatabase) CloudFormationLifecycle.toAlphanumerical(org.opentosca.toscana.plugins.cloudformation.CloudFormationLifecycle.toAlphanumerical) TransformationContext(org.opentosca.toscana.core.transformation.TransformationContext) WebApplication(org.opentosca.toscana.model.node.WebApplication) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) CloudFormationModule(org.opentosca.toscana.plugins.cloudformation.CloudFormationModule) HostedOn(org.opentosca.toscana.model.relation.HostedOn) Compute(org.opentosca.toscana.model.node.Compute) Fn(com.scaleset.cfbuilder.core.Fn)

Example 4 with Port

use of org.opentosca.toscana.model.datatype.Port in project TOSCAna by StuPro-TOSCAna.

the class ScalarTypeConverter method convertScalarEntity.

/**
 *     Converts the value attached to given {@code scalarEntity } to an instance of a type which is specified by given {@code key}.
 */
static <T> T convertScalarEntity(ScalarEntity scalarEntity, ToscaKey<T> key, Entity parent) {
    String value = scalarEntity.getValue();
    Class targetType = key.getType();
    if (String.class.isAssignableFrom(targetType)) {
        return (T) value;
    } else if (Integer.class.isAssignableFrom(targetType)) {
        Integer number;
        if (UNBOUNDED.equals(value)) {
            number = Integer.MAX_VALUE;
        } else {
            number = Integer.valueOf(value);
        }
        return (T) number;
    } else if (Boolean.class.isAssignableFrom(targetType)) {
        return (T) Boolean.valueOf(value);
    } else if (targetType.isEnum()) {
        Map<String, T> enumMap = EnumUtils.getEnumMap(targetType);
        Optional<T> result = enumMap.entrySet().stream().filter(entry -> value.equalsIgnoreCase(entry.getKey())).map(Map.Entry::getValue).findAny();
        return result.orElseThrow(() -> new NoSuchElementException(String.format("No value with name '%s' in enum '%s'", value, targetType.getSimpleName())));
    } else if (OperationVariable.class.isAssignableFrom(targetType)) {
        Connection c = scalarEntity.getGraph().getEdge(parent, scalarEntity);
        String name = null;
        OperationVariable var;
        if (c != null) {
            name = c.getKey();
            var = new OperationVariable(scalarEntity, name);
        } else {
            var = new OperationVariable(scalarEntity);
        }
        return (T) var;
    } else if (SizeUnit.class.isAssignableFrom(targetType)) {
        SizeUnit.Unit fromDefaultUnit = (SizeUnit.Unit) key.getDirectives().get(SizeUnit.FROM);
        SizeUnit.Unit toUnit = (SizeUnit.Unit) key.getDirectives().get(SizeUnit.TO);
        if (fromDefaultUnit == null || toUnit == null) {
            throw new IllegalStateException("ToscaKey defining a SizeUnit is illegal: No directive set for source and target units");
        }
        return (T) SizeUnit.convert(value, fromDefaultUnit, toUnit);
    } else if (Port.class.isAssignableFrom(targetType)) {
        return (T) new Port(Integer.valueOf(value));
    } else {
        throw new UnsupportedOperationException(String.format("Cannot convert value of type %s: currently unsupported", targetType.getSimpleName()));
    }
}
Also used : Port(org.opentosca.toscana.model.datatype.Port) ToscaKey(org.opentosca.toscana.model.util.ToscaKey) OperationVariable(org.opentosca.toscana.model.operation.OperationVariable) EnumUtils(org.apache.commons.lang3.EnumUtils) Map(java.util.Map) Connection(org.opentosca.toscana.core.parse.model.Connection) ScalarEntity(org.opentosca.toscana.core.parse.model.ScalarEntity) Optional(java.util.Optional) SizeUnit(org.opentosca.toscana.model.datatype.SizeUnit) NoSuchElementException(java.util.NoSuchElementException) Entity(org.opentosca.toscana.core.parse.model.Entity) OperationVariable(org.opentosca.toscana.model.operation.OperationVariable) Optional(java.util.Optional) Port(org.opentosca.toscana.model.datatype.Port) Connection(org.opentosca.toscana.core.parse.model.Connection) SizeUnit(org.opentosca.toscana.model.datatype.SizeUnit) SizeUnit(org.opentosca.toscana.model.datatype.SizeUnit) Map(java.util.Map) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

Port (org.opentosca.toscana.model.datatype.Port)4 WebApplication (org.opentosca.toscana.model.node.WebApplication)2 Fn (com.scaleset.cfbuilder.core.Fn)1 SecureRandom (java.security.SecureRandom)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 EnumUtils (org.apache.commons.lang3.EnumUtils)1 RandomStringUtils (org.apache.commons.lang3.RandomStringUtils)1 Test (org.junit.Test)1 BaseUnitTest (org.opentosca.toscana.core.BaseUnitTest)1 Connection (org.opentosca.toscana.core.parse.model.Connection)1 Entity (org.opentosca.toscana.core.parse.model.Entity)1 ScalarEntity (org.opentosca.toscana.core.parse.model.ScalarEntity)1 TransformationContext (org.opentosca.toscana.core.transformation.TransformationContext)1 EffectiveModel (org.opentosca.toscana.model.EffectiveModel)1 EffectiveModelFactory (org.opentosca.toscana.model.EffectiveModelFactory)1 EndpointCapability (org.opentosca.toscana.model.capability.EndpointCapability)1 SizeUnit (org.opentosca.toscana.model.datatype.SizeUnit)1