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