Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 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 | package org.onosproject.codec.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.codec.JsonCodec; |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 22 | import org.onosproject.core.CoreService; |
| 23 | import org.onosproject.net.flow.TrafficSelector; |
| 24 | import org.onosproject.net.flow.TrafficTreatment; |
| 25 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 26 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 27 | import org.slf4j.Logger; |
| 28 | |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | import static org.onlab.util.Tools.nullIsIllegal; |
| 31 | import static org.slf4j.LoggerFactory.getLogger; |
| 32 | |
| 33 | /** |
| 34 | * Forwarding Objective Codec. |
| 35 | */ |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 36 | public final class ForwardingObjectiveCodec extends JsonCodec<ForwardingObjective> { |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 37 | private final Logger log = getLogger(getClass()); |
| 38 | |
| 39 | // JSON field names |
| 40 | private static final String ID = "id"; |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 41 | private static final String APP_ID = "appId"; |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 42 | private static final String SELECTOR = "selector"; |
| 43 | private static final String FLAG = "flag"; |
| 44 | private static final String OPERATION = "operation"; |
| 45 | private static final String NEXT_ID = "nextId"; |
| 46 | private static final String TREATMENT = "treatment"; |
| 47 | |
| 48 | // messages to be printed out |
| 49 | private static final String MISSING_MEMBER_MESSAGE = |
| 50 | " member is required in ForwardingObjective"; |
| 51 | private static final String NOT_NULL_MESSAGE = |
| 52 | "ForwardingObjective cannot be null"; |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 53 | |
| 54 | public static final String REST_APP_ID = "org.onosproject.rest"; |
| 55 | |
| 56 | @Override |
| 57 | public ObjectNode encode(ForwardingObjective forwardingObjective, CodecContext context) { |
| 58 | |
| 59 | checkNotNull(forwardingObjective, NOT_NULL_MESSAGE); |
| 60 | |
| 61 | final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class); |
| 62 | final JsonCodec<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class); |
| 63 | |
| 64 | // encode common properties |
| 65 | ObjectiveCodecHelper och = new ObjectiveCodecHelper(); |
| 66 | ObjectNode result = och.encode(forwardingObjective, context); |
| 67 | |
| 68 | // encode id |
| 69 | result.put(ID, forwardingObjective.id()); |
| 70 | |
| 71 | // encode flag |
| 72 | result.put(FLAG, forwardingObjective.flag().toString()); |
| 73 | |
| 74 | // encode op |
| 75 | result.put(OPERATION, forwardingObjective.op().toString()); |
| 76 | |
| 77 | // encode selector |
| 78 | ObjectNode trafficSelectorNode = |
| 79 | trafficSelectorCodec.encode(forwardingObjective.selector(), context); |
| 80 | result.set(SELECTOR, trafficSelectorNode); |
| 81 | |
| 82 | // encode nextId |
| 83 | if (forwardingObjective.nextId() != null) { |
| 84 | result.put(NEXT_ID, forwardingObjective.nextId()); |
| 85 | } |
| 86 | |
| 87 | // encode treatment |
| 88 | if (forwardingObjective.treatment() != null) { |
| 89 | ObjectNode trafficTreatmentNode = |
| 90 | trafficTreatmentCodec.encode(forwardingObjective.treatment(), context); |
| 91 | result.set(TREATMENT, trafficTreatmentNode); |
| 92 | } |
| 93 | |
| 94 | return result; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public ForwardingObjective decode(ObjectNode json, CodecContext context) { |
| 99 | if (json == null || !json.isObject()) { |
| 100 | return null; |
| 101 | } |
| 102 | |
| 103 | CoreService coreService = context.getService(CoreService.class); |
| 104 | |
| 105 | final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class); |
| 106 | final JsonCodec<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class); |
| 107 | |
| 108 | ObjectiveCodecHelper och = new ObjectiveCodecHelper(); |
| 109 | |
| 110 | DefaultForwardingObjective.Builder baseBuilder = DefaultForwardingObjective.builder(); |
| 111 | final DefaultForwardingObjective.Builder builder = |
| 112 | (DefaultForwardingObjective.Builder) och.decode(json, baseBuilder, context); |
| 113 | |
| 114 | // application id |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 115 | JsonNode appIdJson = json.get(APP_ID); |
| 116 | String appId = appIdJson != null ? appIdJson.asText() : REST_APP_ID; |
| 117 | builder.fromApp(coreService.registerApplication(appId)); |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 118 | |
| 119 | // decode flag |
| 120 | String flagStr = nullIsIllegal(json.get(FLAG), FLAG + MISSING_MEMBER_MESSAGE).asText(); |
| 121 | switch (flagStr) { |
| 122 | case "SPECIFIC": |
| 123 | builder.withFlag(ForwardingObjective.Flag.SPECIFIC); |
| 124 | break; |
| 125 | case "VERSATILE": |
| 126 | builder.withFlag(ForwardingObjective.Flag.VERSATILE); |
| 127 | break; |
| 128 | default: |
Kavitha Alagesan | df352c6 | 2016-07-13 09:58:19 +0530 | [diff] [blame] | 129 | throw new IllegalArgumentException("The requested flag " + flagStr + |
| 130 | " is not defined for FilteringObjective."); |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // decode selector |
| 134 | JsonNode selectorJson = json.get(SELECTOR); |
| 135 | if (selectorJson != null) { |
| 136 | TrafficSelector trafficSelector = trafficSelectorCodec.decode((ObjectNode) selectorJson, context); |
| 137 | builder.withSelector(trafficSelector); |
| 138 | } |
| 139 | |
| 140 | // decode treatment |
| 141 | JsonNode treatmentJson = json.get(TREATMENT); |
| 142 | if (treatmentJson != null) { |
| 143 | TrafficTreatment trafficTreatment = trafficTreatmentCodec.decode((ObjectNode) treatmentJson, context); |
| 144 | builder.withTreatment(trafficTreatment); |
| 145 | } |
| 146 | |
| 147 | // decode nextId |
| 148 | JsonNode nextIdJson = json.get(NEXT_ID); |
| 149 | if (nextIdJson != null) { |
| 150 | builder.nextStep(nextIdJson.asInt()); |
| 151 | } |
| 152 | |
| 153 | // decode operation |
| 154 | String opStr = nullIsIllegal(json.get(OPERATION), OPERATION + MISSING_MEMBER_MESSAGE).asText(); |
Kavitha Alagesan | df352c6 | 2016-07-13 09:58:19 +0530 | [diff] [blame] | 155 | ForwardingObjective forwardingObjective = null; |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 156 | |
| 157 | switch (opStr) { |
| 158 | case "ADD": |
| 159 | forwardingObjective = builder.add(); |
| 160 | break; |
| 161 | case "REMOVE": |
| 162 | forwardingObjective = builder.remove(); |
| 163 | break; |
| 164 | default: |
Kavitha Alagesan | df352c6 | 2016-07-13 09:58:19 +0530 | [diff] [blame] | 165 | throw new IllegalArgumentException("The requested operation " + opStr + |
| 166 | " is not defined for FilteringObjective."); |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | return forwardingObjective; |
| 170 | } |
| 171 | } |