[ONOS-2225] Add codecs with unit test for FlowObjective REST API
* Add codec for FilteringObjective
* Add codec for ForwardingObjective
* Add codec for NextObjective
Change-Id: I715aa7f1969697468692459052fd27cc65ca2363
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java b/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java
index 633a356..7c19202 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java
@@ -43,6 +43,9 @@
import org.onosproject.net.flow.TrafficTreatment;
import org.onosproject.net.flow.criteria.Criterion;
import org.onosproject.net.flow.instructions.Instruction;
+import org.onosproject.net.flowobjective.FilteringObjective;
+import org.onosproject.net.flowobjective.ForwardingObjective;
+import org.onosproject.net.flowobjective.NextObjective;
import org.onosproject.net.group.Group;
import org.onosproject.net.group.GroupBucket;
import org.onosproject.net.intent.ConnectivityIntent;
@@ -109,6 +112,9 @@
registerCodec(TableStatisticsEntry.class, new TableStatisticsEntryCodec());
registerCodec(PortStatistics.class, new PortStatisticsCodec());
registerCodec(Metric.class, new MetricCodec());
+ registerCodec(FilteringObjective.class, new FilteringObjectiveCodec());
+ registerCodec(ForwardingObjective.class, new ForwardingObjectiveCodec());
+ registerCodec(NextObjective.class, new NextObjectiveCodec());
log.info("Started");
}
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/FilteringObjectiveCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/FilteringObjectiveCodec.java
new file mode 100644
index 0000000..4cbfffa
--- /dev/null
+++ b/core/common/src/main/java/org/onosproject/codec/impl/FilteringObjectiveCodec.java
@@ -0,0 +1,182 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.codec.impl;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.flow.criteria.Criterion;
+import org.onosproject.net.flowobjective.DefaultFilteringObjective;
+import org.onosproject.net.flowobjective.FilteringObjective;
+import org.slf4j.Logger;
+
+import java.util.stream.IntStream;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onlab.util.Tools.nullIsIllegal;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Filtering Objective Codec.
+ */
+public class FilteringObjectiveCodec extends JsonCodec<FilteringObjective> {
+ private final Logger log = getLogger(getClass());
+
+ // JSON field names
+ private static final String ID = "id";
+ private static final String TYPE = "type";
+ private static final String KEY = "key";
+ private static final String META = "meta";
+ private static final String OPERATION = "operation";
+ private static final String CONDITIONS = "conditions";
+
+ // messages to be printed out
+ private static final String MISSING_MEMBER_MESSAGE =
+ " member is required in FilteringObjective";
+ private static final String NOT_NULL_MESSAGE =
+ "FilteringObjective cannot be null";
+ private static final String INVALID_TYPE_MESSAGE =
+ "The requested type {} is not defined in FilteringObjective.";
+ private static final String INVALID_OP_MESSAGE =
+ "The requested operation {} is not defined for FilteringObjective.";
+
+ public static final String REST_APP_ID = "org.onosproject.rest";
+
+ @Override
+ public ObjectNode encode(FilteringObjective filteringObjective, CodecContext context) {
+
+ checkNotNull(filteringObjective, NOT_NULL_MESSAGE);
+
+ final JsonCodec<Criterion> criterionCodec = context.codec(Criterion.class);
+ final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class);
+
+ // encode common properties
+ ObjectiveCodecHelper och = new ObjectiveCodecHelper();
+ ObjectNode result = och.encode(filteringObjective, context);
+
+ // encode id
+ result.put(ID, filteringObjective.id());
+
+ // encode type
+ result.put(TYPE, filteringObjective.type().toString());
+
+ // encode key
+ if (filteringObjective.key() != null) {
+ ObjectNode criterionNode = criterionCodec.encode(filteringObjective.key(), context);
+ result.set(KEY, criterionNode);
+ }
+
+ // encode meta
+ if (filteringObjective.meta() != null) {
+ ObjectNode trafficTreatmentNode = trafficTreatmentCodec.encode(filteringObjective.meta(), context);
+ result.set(META, trafficTreatmentNode);
+ }
+
+ // encode conditions
+ ArrayNode conditions = context.mapper().createArrayNode();
+ filteringObjective.conditions().forEach(c -> {
+ ObjectNode criterionJson = criterionCodec.encode(c, context);
+ conditions.add(criterionJson);
+ });
+ result.set(CONDITIONS, conditions);
+
+ return result;
+ }
+
+ @Override
+ public FilteringObjective decode(ObjectNode json, CodecContext context) {
+ if (json == null || !json.isObject()) {
+ return null;
+ }
+
+ CoreService coreService = context.getService(CoreService.class);
+
+ final JsonCodec<Criterion> criterionCodec = context.codec(Criterion.class);
+ final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class);
+
+ ObjectiveCodecHelper och = new ObjectiveCodecHelper();
+
+ DefaultFilteringObjective.Builder baseBuilder = DefaultFilteringObjective.builder();
+ final DefaultFilteringObjective.Builder builder =
+ (DefaultFilteringObjective.Builder) och.decode(json, baseBuilder, context);
+
+ // application id
+ ApplicationId appId = coreService.registerApplication(REST_APP_ID);
+ builder.fromApp(appId);
+
+ // decode type
+ String typeStr = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asText();
+
+ switch (typeStr) {
+ case "PERMIT":
+ builder.permit();
+ break;
+ case "DENY":
+ builder.deny();
+ break;
+ default:
+ log.warn(INVALID_TYPE_MESSAGE, typeStr);
+ return null;
+ }
+
+ // decode key
+ JsonNode keyJson = json.get(KEY);
+ if (keyJson != null) {
+ Criterion key = criterionCodec.decode((ObjectNode) keyJson, context);
+ builder.withKey(key);
+ }
+
+ // decode conditions
+ JsonNode conditionsJson = json.get(CONDITIONS);
+ checkNotNull(conditionsJson);
+ if (conditionsJson != null) {
+ IntStream.range(0, conditionsJson.size()).forEach(i -> {
+ ObjectNode conditionJson = get(conditionsJson, i);
+ builder.addCondition(criterionCodec.decode(conditionJson, context));
+ });
+ }
+
+ // decode meta
+ JsonNode metaJson = json.get(META);
+ if (metaJson != null) {
+ TrafficTreatment trafficTreatment = trafficTreatmentCodec.decode((ObjectNode) metaJson, context);
+ builder.withMeta(trafficTreatment);
+ }
+
+ // decode operation
+ String opStr = nullIsIllegal(json.get(OPERATION), OPERATION + MISSING_MEMBER_MESSAGE).asText();
+ FilteringObjective filteringObjective;
+
+ switch (opStr) {
+ case "ADD":
+ filteringObjective = builder.add();
+ break;
+ case "REMOVE":
+ filteringObjective = builder.remove();
+ break;
+ default:
+ log.warn(INVALID_OP_MESSAGE, opStr);
+ return null;
+ }
+
+ return filteringObjective;
+ }
+}
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/ForwardingObjectiveCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/ForwardingObjectiveCodec.java
new file mode 100644
index 0000000..d053790
--- /dev/null
+++ b/core/common/src/main/java/org/onosproject/codec/impl/ForwardingObjectiveCodec.java
@@ -0,0 +1,174 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.codec.impl;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.flowobjective.DefaultForwardingObjective;
+import org.onosproject.net.flowobjective.ForwardingObjective;
+import org.slf4j.Logger;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onlab.util.Tools.nullIsIllegal;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Forwarding Objective Codec.
+ */
+public class ForwardingObjectiveCodec extends JsonCodec<ForwardingObjective> {
+ private final Logger log = getLogger(getClass());
+
+ // JSON field names
+ private static final String ID = "id";
+ private static final String SELECTOR = "selector";
+ private static final String FLAG = "flag";
+ private static final String OPERATION = "operation";
+ private static final String NEXT_ID = "nextId";
+ private static final String TREATMENT = "treatment";
+
+ // messages to be printed out
+ private static final String MISSING_MEMBER_MESSAGE =
+ " member is required in ForwardingObjective";
+ private static final String NOT_NULL_MESSAGE =
+ "ForwardingObjective cannot be null";
+ private static final String INVALID_FLAG_MESSAGE =
+ "The requested flag {} is not defined in ForwardingObjective.";
+ private static final String INVALID_OP_MESSAGE =
+ "The requested operation {} is not defined for FilteringObjective.";
+
+ public static final String REST_APP_ID = "org.onosproject.rest";
+
+ @Override
+ public ObjectNode encode(ForwardingObjective forwardingObjective, CodecContext context) {
+
+ checkNotNull(forwardingObjective, NOT_NULL_MESSAGE);
+
+ final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class);
+ final JsonCodec<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class);
+
+ // encode common properties
+ ObjectiveCodecHelper och = new ObjectiveCodecHelper();
+ ObjectNode result = och.encode(forwardingObjective, context);
+
+ // encode id
+ result.put(ID, forwardingObjective.id());
+
+ // encode flag
+ result.put(FLAG, forwardingObjective.flag().toString());
+
+ // encode op
+ result.put(OPERATION, forwardingObjective.op().toString());
+
+ // encode selector
+ ObjectNode trafficSelectorNode =
+ trafficSelectorCodec.encode(forwardingObjective.selector(), context);
+ result.set(SELECTOR, trafficSelectorNode);
+
+ // encode nextId
+ if (forwardingObjective.nextId() != null) {
+ result.put(NEXT_ID, forwardingObjective.nextId());
+ }
+
+ // encode treatment
+ if (forwardingObjective.treatment() != null) {
+ ObjectNode trafficTreatmentNode =
+ trafficTreatmentCodec.encode(forwardingObjective.treatment(), context);
+ result.set(TREATMENT, trafficTreatmentNode);
+ }
+
+ return result;
+ }
+
+ @Override
+ public ForwardingObjective decode(ObjectNode json, CodecContext context) {
+ if (json == null || !json.isObject()) {
+ return null;
+ }
+
+ CoreService coreService = context.getService(CoreService.class);
+
+ final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class);
+ final JsonCodec<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class);
+
+ ObjectiveCodecHelper och = new ObjectiveCodecHelper();
+
+ DefaultForwardingObjective.Builder baseBuilder = DefaultForwardingObjective.builder();
+ final DefaultForwardingObjective.Builder builder =
+ (DefaultForwardingObjective.Builder) och.decode(json, baseBuilder, context);
+
+ // application id
+ ApplicationId appId = coreService.registerApplication(REST_APP_ID);
+ builder.fromApp(appId);
+
+ // decode flag
+ String flagStr = nullIsIllegal(json.get(FLAG), FLAG + MISSING_MEMBER_MESSAGE).asText();
+ switch (flagStr) {
+ case "SPECIFIC":
+ builder.withFlag(ForwardingObjective.Flag.SPECIFIC);
+ break;
+ case "VERSATILE":
+ builder.withFlag(ForwardingObjective.Flag.VERSATILE);
+ break;
+ default:
+ log.warn(INVALID_FLAG_MESSAGE, flagStr);
+ return null;
+ }
+
+ // decode selector
+ JsonNode selectorJson = json.get(SELECTOR);
+ if (selectorJson != null) {
+ TrafficSelector trafficSelector = trafficSelectorCodec.decode((ObjectNode) selectorJson, context);
+ builder.withSelector(trafficSelector);
+ }
+
+ // decode treatment
+ JsonNode treatmentJson = json.get(TREATMENT);
+ if (treatmentJson != null) {
+ TrafficTreatment trafficTreatment = trafficTreatmentCodec.decode((ObjectNode) treatmentJson, context);
+ builder.withTreatment(trafficTreatment);
+ }
+
+ // decode nextId
+ JsonNode nextIdJson = json.get(NEXT_ID);
+ if (nextIdJson != null) {
+ builder.nextStep(nextIdJson.asInt());
+ }
+
+ // decode operation
+ String opStr = nullIsIllegal(json.get(OPERATION), OPERATION + MISSING_MEMBER_MESSAGE).asText();
+ ForwardingObjective forwardingObjective;
+
+ switch (opStr) {
+ case "ADD":
+ forwardingObjective = builder.add();
+ break;
+ case "REMOVE":
+ forwardingObjective = builder.remove();
+ break;
+ default:
+ log.warn(INVALID_OP_MESSAGE, opStr);
+ return null;
+ }
+
+ return forwardingObjective;
+ }
+}
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/NextObjectiveCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/NextObjectiveCodec.java
new file mode 100644
index 0000000..cd75ae7
--- /dev/null
+++ b/core/common/src/main/java/org/onosproject/codec/impl/NextObjectiveCodec.java
@@ -0,0 +1,183 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.codec.impl;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.flowobjective.DefaultNextObjective;
+import org.onosproject.net.flowobjective.NextObjective;
+import org.slf4j.Logger;
+
+import java.util.stream.IntStream;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onlab.util.Tools.nullIsIllegal;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Next Objective Codec.
+ */
+public class NextObjectiveCodec extends JsonCodec<NextObjective> {
+
+ private final Logger log = getLogger(getClass());
+
+ // JSON field names
+ private static final String ID = "id";
+ private static final String TYPE = "type";
+ private static final String OPERATION = "operation";
+ private static final String TREATMENTS = "treatments";
+ private static final String META = "meta";
+
+ // messages to be printed out
+ private static final String MISSING_MEMBER_MESSAGE =
+ " member is required in NextObjective";
+ private static final String NOT_NULL_MESSAGE =
+ "NextObjective cannot be null";
+ private static final String INVALID_TYPE_MESSAGE =
+ "The requested flag {} is not defined in NextObjective.";
+ private static final String INVALID_OP_MESSAGE =
+ "The requested operation {} is not defined for NextObjective.";
+
+ public static final String REST_APP_ID = "org.onosproject.rest";
+
+ @Override
+ public ObjectNode encode(NextObjective nextObjective, CodecContext context) {
+
+ checkNotNull(nextObjective, NOT_NULL_MESSAGE);
+
+ final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class);
+ final JsonCodec<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class);
+
+ // encode common properties
+ ObjectiveCodecHelper och = new ObjectiveCodecHelper();
+ ObjectNode result = och.encode(nextObjective, context);
+
+ // encode id
+ result.put(ID, nextObjective.id());
+
+ // encode type
+ result.put(TYPE, nextObjective.type().toString());
+
+ // encode operation
+ result.put(OPERATION, nextObjective.op().toString());
+
+ // encode treatments
+ ArrayNode treatments = context.mapper().createArrayNode();
+ nextObjective.next().forEach(t -> {
+ ObjectNode treatmentJson = trafficTreatmentCodec.encode(t, context);
+ treatments.add(treatmentJson);
+ });
+ result.set(TREATMENTS, treatments);
+
+ // encode meta
+ if (nextObjective.meta() != null) {
+ ObjectNode trafficSelectorNode = trafficSelectorCodec.encode(nextObjective.meta(), context);
+ result.set(META, trafficSelectorNode);
+ }
+
+ return result;
+ }
+
+ @Override
+ public NextObjective decode(ObjectNode json, CodecContext context) {
+ if (json == null || !json.isObject()) {
+ return null;
+ }
+
+ CoreService coreService = context.getService(CoreService.class);
+
+ final JsonCodec<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class);
+ final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class);
+
+ ObjectiveCodecHelper och = new ObjectiveCodecHelper();
+
+ DefaultNextObjective.Builder baseBuilder = DefaultNextObjective.builder();
+ final DefaultNextObjective.Builder builder =
+ (DefaultNextObjective.Builder) och.decode(json, baseBuilder, context);
+
+ // decode id
+ JsonNode idJson = json.get(ID);
+ checkNotNull(idJson);
+ builder.withId(idJson.asInt());
+
+ // decode application id
+ ApplicationId appId = coreService.registerApplication(REST_APP_ID);
+ builder.fromApp(appId);
+
+ // decode type
+ String typeStr = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asText();
+
+ switch (typeStr) {
+ case "HASHED":
+ builder.withType(NextObjective.Type.HASHED);
+ break;
+ case "BROADCAST":
+ builder.withType(NextObjective.Type.BROADCAST);
+ break;
+ case "FAILOVER":
+ builder.withType(NextObjective.Type.FAILOVER);
+ break;
+ case "SIMPLE":
+ builder.withType(NextObjective.Type.SIMPLE);
+ break;
+ default:
+ log.warn(INVALID_TYPE_MESSAGE, typeStr);
+ return null;
+ }
+
+ // decode treatments
+ JsonNode treatmentsJson = json.get(TREATMENTS);
+ checkNotNull(treatmentsJson);
+ if (treatmentsJson != null) {
+ IntStream.range(0, treatmentsJson.size()).forEach(i -> {
+ ObjectNode treatmentJson = get(treatmentsJson, i);
+ builder.addTreatment(trafficTreatmentCodec.decode(treatmentJson, context));
+ });
+ }
+
+ // decode meta
+ JsonNode metaJson = json.get(META);
+ if (metaJson != null) {
+ TrafficSelector trafficSelector = trafficSelectorCodec.decode((ObjectNode) metaJson, context);
+ builder.withMeta(trafficSelector);
+ }
+
+ // decode operation
+ String opStr = nullIsIllegal(json.get(OPERATION), OPERATION + MISSING_MEMBER_MESSAGE).asText();
+ NextObjective nextObjective;
+
+ switch (opStr) {
+ case "ADD":
+ nextObjective = builder.add();
+ break;
+ case "REMOVE":
+ nextObjective = builder.remove();
+ break;
+ default:
+ log.warn(INVALID_OP_MESSAGE, opStr);
+ return null;
+ }
+
+ return nextObjective;
+ }
+}
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/ObjectiveCodecHelper.java b/core/common/src/main/java/org/onosproject/codec/impl/ObjectiveCodecHelper.java
new file mode 100644
index 0000000..4434ae0
--- /dev/null
+++ b/core/common/src/main/java/org/onosproject/codec/impl/ObjectiveCodecHelper.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.codec.impl;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.net.flowobjective.Objective;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Objective Codec Helper.
+ */
+public class ObjectiveCodecHelper {
+
+ // JSON field names
+ private static final String ID = "id";
+ private static final String APP_ID = "appId";
+ private static final String OPERATION = "operation";
+ private static final String PERMANENT = "isPermanent";
+ private static final String PRIORITY = "priority";
+ private static final String TIMEOUT = "timeout";
+ public static final String REST_APP_ID = "org.onosproject.rest";
+
+ public ObjectNode encode(Objective objective, CodecContext context) {
+ checkNotNull(objective, "Objective cannot be null");
+
+ ObjectNode result = context.mapper().createObjectNode()
+ .put(ID, objective.id())
+ .put(OPERATION, objective.op().toString())
+ .put(PERMANENT, String.valueOf(objective.permanent()))
+ .put(PRIORITY, objective.priority())
+ .put(TIMEOUT, objective.timeout());
+
+ if (objective.appId() != null) {
+ result.put(APP_ID, objective.appId().toString());
+ }
+
+ return result;
+ }
+
+ public Objective.Builder decode(ObjectNode json, Objective.Builder builder, CodecContext context) {
+ if (json == null || !json.isObject()) {
+ return null;
+ }
+
+ // permanent
+ boolean permanent = false;
+ if (json.get(PERMANENT) != null) {
+ permanent = json.get(PERMANENT).asBoolean();
+ }
+
+ // timeout
+ int timeoutInt = 0;
+ if (json.get(TIMEOUT) != null) {
+ timeoutInt = json.get(TIMEOUT).asInt();
+ }
+
+ // priority
+ int priorityInt = 0;
+ if (json.get(PRIORITY) != null) {
+ priorityInt = json.get(PRIORITY).asInt();
+ }
+
+ if (permanent) {
+ builder.makePermanent()
+ .makeTemporary(timeoutInt)
+ .withPriority(priorityInt);
+ } else {
+ builder.makeTemporary(timeoutInt)
+ .withPriority(priorityInt);
+ }
+ return builder;
+ }
+}