Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
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; |
Jonghwan Hyun | a76bf03 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 128 | case "EGRESS": |
| 129 | builder.withFlag(ForwardingObjective.Flag.EGRESS); |
| 130 | break; |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 131 | default: |
Kavitha Alagesan | df352c6 | 2016-07-13 09:58:19 +0530 | [diff] [blame] | 132 | throw new IllegalArgumentException("The requested flag " + flagStr + |
Jonghwan Hyun | a76bf03 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 133 | " is not defined for ForwardingObjective."); |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // decode selector |
| 137 | JsonNode selectorJson = json.get(SELECTOR); |
| 138 | if (selectorJson != null) { |
| 139 | TrafficSelector trafficSelector = trafficSelectorCodec.decode((ObjectNode) selectorJson, context); |
| 140 | builder.withSelector(trafficSelector); |
| 141 | } |
| 142 | |
| 143 | // decode treatment |
| 144 | JsonNode treatmentJson = json.get(TREATMENT); |
| 145 | if (treatmentJson != null) { |
| 146 | TrafficTreatment trafficTreatment = trafficTreatmentCodec.decode((ObjectNode) treatmentJson, context); |
| 147 | builder.withTreatment(trafficTreatment); |
| 148 | } |
| 149 | |
| 150 | // decode nextId |
| 151 | JsonNode nextIdJson = json.get(NEXT_ID); |
| 152 | if (nextIdJson != null) { |
| 153 | builder.nextStep(nextIdJson.asInt()); |
| 154 | } |
| 155 | |
| 156 | // decode operation |
| 157 | String opStr = nullIsIllegal(json.get(OPERATION), OPERATION + MISSING_MEMBER_MESSAGE).asText(); |
Kavitha Alagesan | df352c6 | 2016-07-13 09:58:19 +0530 | [diff] [blame] | 158 | ForwardingObjective forwardingObjective = null; |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 159 | |
| 160 | switch (opStr) { |
| 161 | case "ADD": |
| 162 | forwardingObjective = builder.add(); |
| 163 | break; |
| 164 | case "REMOVE": |
| 165 | forwardingObjective = builder.remove(); |
| 166 | break; |
| 167 | default: |
Kavitha Alagesan | df352c6 | 2016-07-13 09:58:19 +0530 | [diff] [blame] | 168 | throw new IllegalArgumentException("The requested operation " + opStr + |
| 169 | " is not defined for FilteringObjective."); |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | return forwardingObjective; |
| 173 | } |
| 174 | } |