m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.workflow.api; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import com.google.common.base.MoreObjects; |
| 23 | import org.slf4j.Logger; |
| 24 | |
| 25 | import static org.slf4j.LoggerFactory.getLogger; |
| 26 | |
| 27 | /** |
| 28 | * Class for default worklet description. |
| 29 | */ |
| 30 | public final class DefaultWorkletDescription implements WorkletDescription { |
| 31 | |
| 32 | protected static final Logger log = getLogger(DefaultWorkletDescription.class); |
| 33 | |
| 34 | /** |
| 35 | * worklet Name. |
| 36 | */ |
| 37 | private String tag; |
| 38 | |
| 39 | /** |
| 40 | * worklet data model. |
| 41 | */ |
| 42 | private JsonDataModelTree data; |
| 43 | |
| 44 | /** |
| 45 | * Constructor of worklet description. |
| 46 | * |
| 47 | * @param builder worklet description builder |
| 48 | */ |
| 49 | public DefaultWorkletDescription(DefaultWorkletDescription.Builder builder) { |
| 50 | this.tag = builder.tag; |
| 51 | this.data = builder.data; |
| 52 | } |
| 53 | |
| 54 | public DefaultWorkletDescription(String tag) { |
| 55 | this.tag = tag; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public String tag() { |
| 60 | return this.tag; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public JsonDataModelTree data() { |
| 65 | return this.data; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public String toString() { |
| 70 | return MoreObjects.toStringHelper(getClass()) |
| 71 | .add("tag", tag()) |
| 72 | .add("data", data()) |
| 73 | .toString(); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Gets builder instance. |
| 78 | * |
| 79 | * @return builder instance |
| 80 | */ |
| 81 | public static DefaultWorkletDescription.Builder builder() { |
| 82 | return new DefaultWorkletDescription.Builder(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Builder for worklet description. |
| 87 | */ |
| 88 | public static class Builder { |
| 89 | |
| 90 | /** |
| 91 | * worklet name. |
| 92 | */ |
| 93 | private String tag; |
| 94 | |
| 95 | /** |
| 96 | * static data model tree. |
| 97 | */ |
| 98 | JsonDataModelTree data = new JsonDataModelTree(); |
| 99 | |
| 100 | /** |
| 101 | * Sets worklet name. |
| 102 | * |
| 103 | * @param tag worklet name |
| 104 | * @return builder |
| 105 | */ |
| 106 | public DefaultWorkletDescription.Builder name(String tag) { |
| 107 | this.tag = tag; |
| 108 | return this; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | public DefaultWorkletDescription.Builder staticDataModel(String path, String value) throws WorkflowException { |
| 113 | |
| 114 | data.setAt(path, value); |
| 115 | |
| 116 | return this; |
| 117 | } |
| 118 | |
| 119 | public DefaultWorkletDescription.Builder staticDataModel(String path, Integer value) throws WorkflowException { |
| 120 | |
| 121 | data.setAt(path, value); |
| 122 | |
| 123 | return this; |
| 124 | } |
| 125 | |
| 126 | public DefaultWorkletDescription.Builder staticDataModel(String path, Boolean value) throws WorkflowException { |
| 127 | |
| 128 | data.setAt(path, value); |
| 129 | |
| 130 | return this; |
| 131 | } |
| 132 | |
| 133 | public DefaultWorkletDescription.Builder staticDataModel(String path, JsonNode value) throws WorkflowException { |
| 134 | |
| 135 | data.setAt(path, value); |
| 136 | |
| 137 | return this; |
| 138 | } |
| 139 | |
| 140 | public DefaultWorkletDescription.Builder staticDataModel(String path, ArrayNode value) |
| 141 | throws WorkflowException { |
| 142 | |
| 143 | data.setAt(path, value); |
| 144 | |
| 145 | return this; |
| 146 | } |
| 147 | |
| 148 | public DefaultWorkletDescription.Builder staticDataModel(String path, ObjectNode value) |
| 149 | throws WorkflowException { |
| 150 | |
| 151 | data.setAt(path, value); |
| 152 | |
| 153 | return this; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /** |
| 158 | * Builds worklet description from builder. |
| 159 | * |
| 160 | * @return instance of worklet description |
| 161 | * @throws WorkflowException workflow exception |
| 162 | */ |
| 163 | public DefaultWorkletDescription build() { |
| 164 | |
| 165 | return new DefaultWorkletDescription(this); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | } |
| 170 | } |