blob: ed063476bd1a8da5c188123805afc63c4cbe3689 [file] [log] [blame]
Jian Li8bcef8b2016-01-06 11:35:53 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Jian Li8bcef8b2016-01-06 11:35:53 -08003 *
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 */
16package org.onosproject.codec.impl;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onosproject.codec.CodecContext;
20import org.onosproject.net.flowobjective.Objective;
21
22import static com.google.common.base.Preconditions.checkNotNull;
23
24/**
25 * Objective Codec Helper.
26 */
27public class ObjectiveCodecHelper {
28
29 // JSON field names
30 private static final String ID = "id";
31 private static final String APP_ID = "appId";
32 private static final String OPERATION = "operation";
33 private static final String PERMANENT = "isPermanent";
34 private static final String PRIORITY = "priority";
35 private static final String TIMEOUT = "timeout";
36 public static final String REST_APP_ID = "org.onosproject.rest";
37
38 public ObjectNode encode(Objective objective, CodecContext context) {
39 checkNotNull(objective, "Objective cannot be null");
40
41 ObjectNode result = context.mapper().createObjectNode()
42 .put(ID, objective.id())
43 .put(OPERATION, objective.op().toString())
44 .put(PERMANENT, String.valueOf(objective.permanent()))
45 .put(PRIORITY, objective.priority())
46 .put(TIMEOUT, objective.timeout());
47
48 if (objective.appId() != null) {
49 result.put(APP_ID, objective.appId().toString());
50 }
51
52 return result;
53 }
54
55 public Objective.Builder decode(ObjectNode json, Objective.Builder builder, CodecContext context) {
56 if (json == null || !json.isObject()) {
57 return null;
58 }
59
60 // permanent
61 boolean permanent = false;
62 if (json.get(PERMANENT) != null) {
63 permanent = json.get(PERMANENT).asBoolean();
64 }
65
66 // timeout
67 int timeoutInt = 0;
68 if (json.get(TIMEOUT) != null) {
69 timeoutInt = json.get(TIMEOUT).asInt();
70 }
71
72 // priority
73 int priorityInt = 0;
74 if (json.get(PRIORITY) != null) {
75 priorityInt = json.get(PRIORITY).asInt();
76 }
77
78 if (permanent) {
79 builder.makePermanent()
80 .makeTemporary(timeoutInt)
81 .withPriority(priorityInt);
82 } else {
83 builder.makeTemporary(timeoutInt)
84 .withPriority(priorityInt);
85 }
86 return builder;
87 }
88}