aboutsummaryrefslogtreecommitdiffstats
path: root/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/model/inventory/BlueprintInput.java
blob: 596438b20ae961c594d0158fabe38a89fd0f680a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package org.onap.ccsdk.dashboard.model.inventory;

import java.util.Optional;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * Compliance with the schema spec'd here: http://docs.getcloudify.org/3.4.0/blueprints/spec-inputs/
 */
@JsonInclude(Include.NON_NULL)
public class BlueprintInput {

	private final BlueprintInput.Type type;
	private final Optional<Object> defaultValue;
	private final Optional<String> description;

	public static enum Type {
		@JsonProperty("any")
		ANY,

		@JsonProperty("string")
		STRING,

		@JsonProperty("integer")
		INTEGER,

		@JsonProperty("boolean")
		BOOLEAN
	}

	@JsonCreator
	public BlueprintInput(
			@JsonProperty("type") String type,
			@JsonProperty("default") Object defaultValue,
			@JsonProperty("description") String description) {

		// Case where there is no default and no type --> Type should be ANY
		if (defaultValue == null && type == null) {
			this.type = BlueprintInput.Type.ANY;
		}

		// Case where there is a default but no type --> Type should be ANY
		else if (defaultValue != null && type == null) {
			this.type = BlueprintInput.Type.ANY;
		}

		// Case where there is a type but no default --> Type should be the specified type.
		else if (defaultValue == null && type != null) {
			this.type = BlueprintInput.Type.valueOf(type.toString().toUpperCase());
		}

		// Cases where there is a default and a type
		else {
			switch (BlueprintInput.Type.valueOf(type.toString().toUpperCase())) {
			case ANY:
				throw new IllegalArgumentException("Cannot specify type ANY (leave blank instead to get ANY type)");
			case BOOLEAN:
				if (defaultValue != null && !(defaultValue instanceof Boolean)) throw new IllegalArgumentException("default value does not match specified type");
				this.type = BlueprintInput.Type.BOOLEAN;
			break;
			case INTEGER:
				if (defaultValue != null && !(defaultValue instanceof Integer)) throw new IllegalArgumentException("default value does not match specified type");
				this.type = BlueprintInput.Type.INTEGER;
			break;
			case STRING:
				if (defaultValue != null && !(defaultValue instanceof String)) throw new IllegalArgumentException("default value does not match specified type");

				this.type = BlueprintInput.Type.STRING;
			break;
			default:
				this.type = Type.ANY;
			break;
			}
		}

		this.defaultValue = Optional.ofNullable(defaultValue);
		this.description = Optional.ofNullable(description);
	}

	public BlueprintInput.Type getType() { return type; }

	@JsonIgnore
	public Optional<Object> getDefault() { return defaultValue; }

	@JsonProperty("defaultValue")
	public Object getDefaultValue() { return defaultValue.orElse(null); }

	@JsonIgnore
	public Optional<String> getDescription() { return description; }

	@JsonProperty("description")
	public String getDescriptionValue() { return description.orElse(null); }

	@Override
	public boolean equals(Object o) {
		if (o instanceof BlueprintInput) {
			final BlueprintInput obj = (BlueprintInput) o;

			return obj.getDefaultValue().equals(getDefaultValue()) &&
					obj.getDescriptionValue().equals(getDescriptionValue()) &&
					obj.getType().equals(getType());
		}

		return false;
	}

	@Override
	public String toString() {
		return "{" +
				"type: " + getType() +
				",default: " + getDefault() +
				",description: " + getDescription() +
				"}";
	}
}