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.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.codec.JsonCodec; |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 23 | import org.onosproject.core.CoreService; |
| 24 | import org.onosproject.net.flow.TrafficSelector; |
| 25 | import org.onosproject.net.flow.TrafficTreatment; |
| 26 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
| 27 | import org.onosproject.net.flowobjective.NextObjective; |
Niloofar Toorchi | 8bbe9ca | 2021-07-30 14:08:04 -0700 | [diff] [blame] | 28 | import org.onosproject.net.flowobjective.NextTreatment; |
| 29 | import org.onosproject.net.flowobjective.DefaultNextTreatment; |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 30 | import org.slf4j.Logger; |
| 31 | |
| 32 | import java.util.stream.IntStream; |
| 33 | |
| 34 | import static com.google.common.base.Preconditions.checkNotNull; |
| 35 | import static org.onlab.util.Tools.nullIsIllegal; |
| 36 | import static org.slf4j.LoggerFactory.getLogger; |
| 37 | |
| 38 | /** |
| 39 | * Next Objective Codec. |
| 40 | */ |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 41 | public final class NextObjectiveCodec extends JsonCodec<NextObjective> { |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 42 | |
| 43 | private final Logger log = getLogger(getClass()); |
| 44 | |
| 45 | // JSON field names |
| 46 | private static final String ID = "id"; |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 47 | private static final String APP_ID = "appId"; |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 48 | private static final String TYPE = "type"; |
| 49 | private static final String OPERATION = "operation"; |
| 50 | private static final String TREATMENTS = "treatments"; |
| 51 | private static final String META = "meta"; |
Niloofar Toorchi | 8bbe9ca | 2021-07-30 14:08:04 -0700 | [diff] [blame] | 52 | private static final String WEIGHT = "weight"; |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 53 | |
| 54 | // messages to be printed out |
| 55 | private static final String MISSING_MEMBER_MESSAGE = |
| 56 | " member is required in NextObjective"; |
| 57 | private static final String NOT_NULL_MESSAGE = |
| 58 | "NextObjective cannot be null"; |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 59 | |
| 60 | public static final String REST_APP_ID = "org.onosproject.rest"; |
| 61 | |
| 62 | @Override |
| 63 | public ObjectNode encode(NextObjective nextObjective, CodecContext context) { |
| 64 | |
| 65 | checkNotNull(nextObjective, NOT_NULL_MESSAGE); |
| 66 | |
| 67 | final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class); |
| 68 | final JsonCodec<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class); |
| 69 | |
| 70 | // encode common properties |
| 71 | ObjectiveCodecHelper och = new ObjectiveCodecHelper(); |
| 72 | ObjectNode result = och.encode(nextObjective, context); |
| 73 | |
| 74 | // encode id |
| 75 | result.put(ID, nextObjective.id()); |
| 76 | |
| 77 | // encode type |
| 78 | result.put(TYPE, nextObjective.type().toString()); |
| 79 | |
| 80 | // encode operation |
| 81 | result.put(OPERATION, nextObjective.op().toString()); |
| 82 | |
| 83 | // encode treatments |
| 84 | ArrayNode treatments = context.mapper().createArrayNode(); |
Niloofar Toorchi | 8bbe9ca | 2021-07-30 14:08:04 -0700 | [diff] [blame] | 85 | nextObjective.nextTreatments().forEach(nt -> { |
| 86 | if (nt.type().equals(NextTreatment.Type.TREATMENT)) { |
| 87 | TrafficTreatment tt = ((DefaultNextTreatment) nt).treatment(); |
| 88 | ObjectNode treatmentJson = trafficTreatmentCodec.encode(tt, context); |
| 89 | treatmentJson.put(WEIGHT, nt.weight()); |
| 90 | treatments.add(treatmentJson); |
| 91 | } |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 92 | }); |
| 93 | result.set(TREATMENTS, treatments); |
| 94 | |
| 95 | // encode meta |
| 96 | if (nextObjective.meta() != null) { |
| 97 | ObjectNode trafficSelectorNode = trafficSelectorCodec.encode(nextObjective.meta(), context); |
| 98 | result.set(META, trafficSelectorNode); |
| 99 | } |
| 100 | |
| 101 | return result; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public NextObjective decode(ObjectNode json, CodecContext context) { |
| 106 | if (json == null || !json.isObject()) { |
| 107 | return null; |
| 108 | } |
| 109 | |
| 110 | CoreService coreService = context.getService(CoreService.class); |
| 111 | |
| 112 | final JsonCodec<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class); |
| 113 | final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class); |
| 114 | |
| 115 | ObjectiveCodecHelper och = new ObjectiveCodecHelper(); |
| 116 | |
| 117 | DefaultNextObjective.Builder baseBuilder = DefaultNextObjective.builder(); |
| 118 | final DefaultNextObjective.Builder builder = |
| 119 | (DefaultNextObjective.Builder) och.decode(json, baseBuilder, context); |
| 120 | |
| 121 | // decode id |
| 122 | JsonNode idJson = json.get(ID); |
| 123 | checkNotNull(idJson); |
| 124 | builder.withId(idJson.asInt()); |
| 125 | |
| 126 | // decode application id |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 127 | JsonNode appIdJson = json.get(APP_ID); |
| 128 | String appId = appIdJson != null ? appIdJson.asText() : REST_APP_ID; |
| 129 | builder.fromApp(coreService.registerApplication(appId)); |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 130 | |
| 131 | // decode type |
| 132 | String typeStr = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asText(); |
| 133 | |
| 134 | switch (typeStr) { |
| 135 | case "HASHED": |
| 136 | builder.withType(NextObjective.Type.HASHED); |
| 137 | break; |
| 138 | case "BROADCAST": |
| 139 | builder.withType(NextObjective.Type.BROADCAST); |
| 140 | break; |
| 141 | case "FAILOVER": |
| 142 | builder.withType(NextObjective.Type.FAILOVER); |
| 143 | break; |
| 144 | case "SIMPLE": |
| 145 | builder.withType(NextObjective.Type.SIMPLE); |
| 146 | break; |
| 147 | default: |
Kavitha Alagesan | df352c6 | 2016-07-13 09:58:19 +0530 | [diff] [blame] | 148 | throw new IllegalArgumentException("The requested type " + typeStr + |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 149 | " is not defined for NextObjective."); |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | // decode treatments |
| 153 | JsonNode treatmentsJson = json.get(TREATMENTS); |
| 154 | checkNotNull(treatmentsJson); |
| 155 | if (treatmentsJson != null) { |
| 156 | IntStream.range(0, treatmentsJson.size()).forEach(i -> { |
| 157 | ObjectNode treatmentJson = get(treatmentsJson, i); |
Niloofar Toorchi | 8bbe9ca | 2021-07-30 14:08:04 -0700 | [diff] [blame] | 158 | JsonNode weightJson = treatmentJson.get(WEIGHT); |
| 159 | int weight = (weightJson != null) ? weightJson.asInt() : NextTreatment.DEFAULT_WEIGHT; |
| 160 | builder.addTreatment(DefaultNextTreatment.of( |
| 161 | trafficTreatmentCodec.decode(treatmentJson, context), weight)); |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 162 | }); |
| 163 | } |
| 164 | |
| 165 | // decode meta |
| 166 | JsonNode metaJson = json.get(META); |
| 167 | if (metaJson != null) { |
| 168 | TrafficSelector trafficSelector = trafficSelectorCodec.decode((ObjectNode) metaJson, context); |
| 169 | builder.withMeta(trafficSelector); |
| 170 | } |
| 171 | |
| 172 | // decode operation |
| 173 | String opStr = nullIsIllegal(json.get(OPERATION), OPERATION + MISSING_MEMBER_MESSAGE).asText(); |
| 174 | NextObjective nextObjective; |
| 175 | |
| 176 | switch (opStr) { |
| 177 | case "ADD": |
| 178 | nextObjective = builder.add(); |
| 179 | break; |
| 180 | case "REMOVE": |
| 181 | nextObjective = builder.remove(); |
| 182 | break; |
| 183 | default: |
Kavitha Alagesan | df352c6 | 2016-07-13 09:58:19 +0530 | [diff] [blame] | 184 | throw new IllegalArgumentException("The requested operation " + opStr + |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 185 | " is not defined for NextObjective."); |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | return nextObjective; |
| 189 | } |
| 190 | } |