Skip to content

Commit

Permalink
Allow for definition of custom tags.
Browse files Browse the repository at this point in the history
Change-Id: I4811d45216753a526acf68f687b23692da6caa6a
Depends-On: I18cfd4b0117cde62f855e48f475c2bc70ab57cac
Partial-Bug: #1772594
  • Loading branch information
danieljasinski committed Jun 5, 2018
1 parent 84f4434 commit 63f7c89
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 51 deletions.
Expand Up @@ -40,6 +40,7 @@ val ipamHasAllocationMode = "ipamHasAllocationMode"
val ipamHasNotAllocationMode = "ipamHasNotAllocationMode"
val networkHasNotAllcationMode = "networkHasNotAllocationMode"
val propertyOfObjectRule = "propertyOfObjectRule"
val listTagTypes = "listTagTypes"

val portOfVCVirtualMachine = "portOfVCVirtualMachine"
val networkOfVCPortGroup = "networkOfVCPortGroup"
Expand Up @@ -74,7 +74,8 @@ val customCreateWorkflows = setOf(
the<ServiceTemplate>(),
the<ServiceInstance>(),
the<PortTuple>(),
the<Tag>()
the<Tag>(),
the<TagType>()
)

val customEditWorkflows = setOf(
Expand All @@ -88,7 +89,8 @@ val customEditWorkflows = setOf(

val customDeleteWorkflows = setOf(
the<VirtualMachineInterface>(),
the<PortTuple>()
the<PortTuple>(),
the<TagType>()
)

val directChildren = setOf(
Expand Down
Expand Up @@ -19,6 +19,7 @@ val Connection = "Connection"
val parent = "parent"
val child = "child"
val item = "item"
val name = "name"
val id = "id"
val ingress = "ingress"
val egress = "egress"
Expand Down
Expand Up @@ -10,6 +10,7 @@ import net.juniper.contrail.api.types.InstanceIp
import net.juniper.contrail.api.types.VirtualMachineInterface
import net.juniper.contrail.api.types.ServiceTemplate
import net.juniper.contrail.api.types.ServiceInstance
import net.juniper.contrail.api.types.TagType

// To reduce complexity of one class, the functionality has been split between multiple simpler classes.
// They are combined using inheritance so that their functions become Executor's methods.
Expand Down Expand Up @@ -44,4 +45,7 @@ NetworkPolicyRuleProperties by NetworkPolicyRulePropertyExecutor(connection) {
if (properties.interfaceList.size <= index || index < 0) return null
return connection.findByFQN(properties.interfaceList[index].virtualNetwork)
}

fun Connection.listTagTypes(): List<String> =
list<TagType>()?.asSequence()?.map { it.name }?.sorted()?.toList() ?: emptyList()
}
Expand Up @@ -39,7 +39,6 @@ private val executorTypes = Executor::class.java.declaredMethods.asSequence()
.groupBy { it.parameterTypes[0] }

private fun ManagedType.createExecutorMethods(): List<ManagedMethod> {
if (!modelClass.isApiObjectClass) return emptyList()
val methods = executorTypes[modelClass] ?: emptyList()
return methods.map { it.toExecutorMethod() }
}
Expand Down
Expand Up @@ -157,6 +157,42 @@ public class ${className}
<@singletonMethod />
</#if>

<#if objectClass || connectionClass>
public Connection_Wrapper getConnection() {
<#if connectionClass>
<#-- This special case is here to provide consistent API for all findable objects. -->
return this;
<#else>
return (Connection_Wrapper) util.findConnectionWrapper(getInternalId());
</#if>
}
</#if>

<#list executorMethods as m>
<@compress single_line=true>public ${m.returns.typeName} ${m.name}(<@params m />)<@thrown m /> {</@compress>
<@locals m />

${m.returns.fullClassName} _res$ = util.executor(getInternalId()).${m.name}(__getTarget()<#if m.params?has_content>,</#if><@localNames m />);

<#if m.returns.fullClassName != 'boolean'>
if(_res$ == null) return null;
</#if>

${m.returns.typeName} _res$pl = _ctx.createPluginObject(_res$, ${m.returns.convertFriendlyClassName});
<#if m.returns.componentTypeName??>
<#if m.returns.componentClassName??>
for(int i = 0; i < _res$.size(); i++) {
_res$pl.get(i).setInternalId(getInternalId().with("${m.returns.componentTypeName}",_res$.get(i).getUuid()));
}
<#else>
_res$pl.setInternalId(getInternalId().with("${m.returns.componentTypeName}",_res$.getUuid()));
</#if>
</#if>
return _res$pl;
}

</#list>

<#if objectClass >
<#list parents as parent>
public void setParent${parent.simpleName}(${packageName}.${parent.simpleName}_Wrapper _parent) {
Expand Down Expand Up @@ -192,35 +228,6 @@ public class ${className}
util.delete(getInternalId(), __getTarget());
}

public Connection_Wrapper getConnection() {
return (Connection_Wrapper) util.findConnectionWrapper(getInternalId());
}

<#list executorMethods as m>
<@compress single_line=true>public ${m.returns.typeName} ${m.name}(<@params m />)<@thrown m /> {</@compress>
<@locals m />

${m.returns.fullClassName} _res$ = util.executor(getInternalId()).${m.name}(__getTarget()<#if m.params?has_content>,</#if><@localNames m />);

<#if m.returns.fullClassName != 'boolean'>
if(_res$ == null) return null;
</#if>

${m.returns.typeName} _res$pl = _ctx.createPluginObject(_res$, ${m.returns.convertFriendlyClassName});
<#if m.returns.componentTypeName??>
<#if m.returns.componentClassName??>
for(int i = 0; i < _res$.size(); i++) {
_res$pl.get(i).setInternalId(getInternalId().with("${m.returns.componentTypeName}",_res$.get(i).getUuid()));
}
<#else>
_res$pl.setInternalId(getInternalId().with("${m.returns.componentTypeName}",_res$.getUuid()));
</#if>
</#if>
return _res$pl;
}

</#list>

<#list references as ref>
//List returned by this method is read-only. Changes to the list will not be reflected in the state of the object.
public java.util.List<${ref.className}_Wrapper> ${ref.pluginMethodName}() {
Expand Down
@@ -0,0 +1,3 @@
if (!item) return ["application", "deployment", "label", "site", "tier"];
var connection = item.getConnection();
return connection.listTagTypes();
@@ -1,8 +1,7 @@
var type = typeName.toLowerCase();
item = new ContrailTag();
item.setName(type+"="+value);
item.setName(typeName+"="+value);
item.setParentConfigRoot(new ContrailConfigRoot());
item.setParentConnection(parent);
item.setValue(value);
item.setTypeName(type);
item.setTypeName(typeName);
item.create();
@@ -1,7 +1,6 @@
var type = typeName.toLowerCase();
item = new ContrailTag();
item.setName(type+"="+value);
item.setName(typeName+"="+value);
item.setParentProject(parent);
item.setValue(value);
item.setTypeName(type);
item.setTypeName(typeName);
item.create();
@@ -0,0 +1,4 @@
item = new ContrailTagType();
item.setName(name);
item.setParentConnection(parent);
item.create();
@@ -0,0 +1 @@
item.delete();
Expand Up @@ -5,12 +5,14 @@
package net.juniper.contrail.vro.workflows.custom

import net.juniper.contrail.vro.config.constants.item
import net.juniper.contrail.vro.config.listTagTypes
import net.juniper.contrail.vro.config.propertyNotNull
import net.juniper.contrail.vro.config.propertyOfObjectRule
import net.juniper.contrail.vro.config.propertyValue
import net.juniper.contrail.vro.config.readSubnet
import net.juniper.contrail.vro.workflows.dsl.ofType
import net.juniper.contrail.vro.workflows.model.any
import net.juniper.contrail.vro.workflows.model.array
import net.juniper.contrail.vro.workflows.model.boolean
import net.juniper.contrail.vro.workflows.model.string

Expand Down Expand Up @@ -51,4 +53,12 @@ val propertyOfObjectRule = ActionDefinition(
"rule" ofType string,
"propertyName" ofType string
)
)

val listTagTypesAction = ActionDefinition (
name = listTagTypes,
resultType = string.array,
parameters = listOf(
item ofType any
)
)
Expand Up @@ -10,21 +10,15 @@ import net.juniper.contrail.api.types.Tag
import net.juniper.contrail.vro.config.allCapitalized
import net.juniper.contrail.vro.config.constants.Connection
import net.juniper.contrail.vro.config.constants.parent
import net.juniper.contrail.vro.config.listTagTypes
import net.juniper.contrail.vro.schema.Schema
import net.juniper.contrail.vro.workflows.dsl.WorkflowDefinition
import net.juniper.contrail.vro.workflows.dsl.actionCallTo
import net.juniper.contrail.vro.workflows.model.reference
import net.juniper.contrail.vro.workflows.model.string
import net.juniper.contrail.vro.workflows.util.propertyDescription
import net.juniper.contrail.vro.workflows.util.relationDescription

private val tagTypes = listOf(
"Application",
"Tier",
"Deployment",
"Site",
"Label"
)

internal fun createGlobalTag(schema: Schema) =
createTag(schema, global = true)

Expand All @@ -39,18 +33,18 @@ private fun createTag(schema: Schema, global: Boolean): WorkflowDefinition {

return customWorkflow<Tag>(workflowName).withScriptFile(scriptName) {
description = relationDescription<ConfigRoot, Tag>(schema)
parameter(parent, parentName.reference) {
description = "Parent ${parentName.allCapitalized}"
mandatory = true
}
parameter("typeName", string) {
description = propertyDescription<Tag>(schema)
predefinedAnswers = tagTypes
predefinedAnswersFrom = actionCallTo(listTagTypes).parameter(parent)
mandatory = true
}
parameter("value", string) {
description = propertyDescription<Tag>(schema)
mandatory = true
}
parameter(parent, parentName.reference) {
description = "Parent ${parentName.allCapitalized}"
mandatory = true
}
}
}
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2018 Juniper Networks, Inc. All rights reserved.
*/

package net.juniper.contrail.vro.workflows.custom

import net.juniper.contrail.api.types.TagType
import net.juniper.contrail.vro.config.constants.Connection
import net.juniper.contrail.vro.config.constants.name
import net.juniper.contrail.vro.config.constants.item
import net.juniper.contrail.vro.config.constants.parent
import net.juniper.contrail.vro.workflows.dsl.WorkflowDefinition
import net.juniper.contrail.vro.workflows.model.reference
import net.juniper.contrail.vro.workflows.model.string

internal fun createTagType(): WorkflowDefinition {

val workflowName = "Create tag type"

return customWorkflow<TagType>(workflowName).withScriptFile("createTagType") {
parameter(name, string) {
description = "Name of the tag type"
mandatory = true
}
parameter(parent, Connection.reference) {
description = "Parent connection"
mandatory = true
}
output(item, reference<TagType>()) {
description = "Tag Type created in this workflow"
}
}
}

internal fun deleteTagType(): WorkflowDefinition {

val workflowName = "Delete tag type"

return customWorkflow<TagType>(workflowName).withScriptFile("deleteTagType") {
parameter(item, reference<TagType>()) {
description = "Tag Type to delete"
mandatory = true
}
}
}

0 comments on commit 63f7c89

Please sign in to comment.