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.TrafficTreatment; |
| 25 | import org.onosproject.net.flow.criteria.Criterion; |
| 26 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 27 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 28 | import org.slf4j.Logger; |
| 29 | |
| 30 | import java.util.stream.IntStream; |
| 31 | |
| 32 | import static com.google.common.base.Preconditions.checkNotNull; |
| 33 | import static org.onlab.util.Tools.nullIsIllegal; |
| 34 | import static org.slf4j.LoggerFactory.getLogger; |
| 35 | |
| 36 | /** |
| 37 | * Filtering Objective Codec. |
| 38 | */ |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 39 | public final class FilteringObjectiveCodec extends JsonCodec<FilteringObjective> { |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 40 | private final Logger log = getLogger(getClass()); |
| 41 | |
| 42 | // JSON field names |
| 43 | private static final String ID = "id"; |
| 44 | private static final String TYPE = "type"; |
| 45 | private static final String KEY = "key"; |
| 46 | private static final String META = "meta"; |
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 OPERATION = "operation"; |
| 49 | private static final String CONDITIONS = "conditions"; |
| 50 | |
| 51 | // messages to be printed out |
| 52 | private static final String MISSING_MEMBER_MESSAGE = |
| 53 | " member is required in FilteringObjective"; |
| 54 | private static final String NOT_NULL_MESSAGE = |
| 55 | "FilteringObjective cannot be null"; |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 56 | |
| 57 | public static final String REST_APP_ID = "org.onosproject.rest"; |
| 58 | |
| 59 | @Override |
| 60 | public ObjectNode encode(FilteringObjective filteringObjective, CodecContext context) { |
| 61 | |
| 62 | checkNotNull(filteringObjective, NOT_NULL_MESSAGE); |
| 63 | |
| 64 | final JsonCodec<Criterion> criterionCodec = context.codec(Criterion.class); |
| 65 | final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class); |
| 66 | |
| 67 | // encode common properties |
| 68 | ObjectiveCodecHelper och = new ObjectiveCodecHelper(); |
| 69 | ObjectNode result = och.encode(filteringObjective, context); |
| 70 | |
| 71 | // encode id |
| 72 | result.put(ID, filteringObjective.id()); |
| 73 | |
| 74 | // encode type |
| 75 | result.put(TYPE, filteringObjective.type().toString()); |
| 76 | |
| 77 | // encode key |
| 78 | if (filteringObjective.key() != null) { |
| 79 | ObjectNode criterionNode = criterionCodec.encode(filteringObjective.key(), context); |
| 80 | result.set(KEY, criterionNode); |
| 81 | } |
| 82 | |
| 83 | // encode meta |
| 84 | if (filteringObjective.meta() != null) { |
| 85 | ObjectNode trafficTreatmentNode = trafficTreatmentCodec.encode(filteringObjective.meta(), context); |
| 86 | result.set(META, trafficTreatmentNode); |
| 87 | } |
| 88 | |
| 89 | // encode conditions |
| 90 | ArrayNode conditions = context.mapper().createArrayNode(); |
| 91 | filteringObjective.conditions().forEach(c -> { |
| 92 | ObjectNode criterionJson = criterionCodec.encode(c, context); |
| 93 | conditions.add(criterionJson); |
| 94 | }); |
| 95 | result.set(CONDITIONS, conditions); |
| 96 | |
| 97 | return result; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public FilteringObjective decode(ObjectNode json, CodecContext context) { |
| 102 | if (json == null || !json.isObject()) { |
| 103 | return null; |
| 104 | } |
| 105 | |
| 106 | CoreService coreService = context.getService(CoreService.class); |
| 107 | |
| 108 | final JsonCodec<Criterion> criterionCodec = context.codec(Criterion.class); |
| 109 | final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class); |
| 110 | |
| 111 | ObjectiveCodecHelper och = new ObjectiveCodecHelper(); |
| 112 | |
| 113 | DefaultFilteringObjective.Builder baseBuilder = DefaultFilteringObjective.builder(); |
| 114 | final DefaultFilteringObjective.Builder builder = |
| 115 | (DefaultFilteringObjective.Builder) och.decode(json, baseBuilder, context); |
| 116 | |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 117 | |
| 118 | |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 119 | // application id |
Jian Li | a424a05 | 2016-05-30 21:02:33 +0900 | [diff] [blame] | 120 | JsonNode appIdJson = json.get(APP_ID); |
| 121 | String appId = appIdJson != null ? appIdJson.asText() : REST_APP_ID; |
| 122 | builder.fromApp(coreService.registerApplication(appId)); |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 123 | |
| 124 | // decode type |
| 125 | String typeStr = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asText(); |
| 126 | |
| 127 | switch (typeStr) { |
| 128 | case "PERMIT": |
| 129 | builder.permit(); |
| 130 | break; |
| 131 | case "DENY": |
| 132 | builder.deny(); |
| 133 | break; |
| 134 | default: |
Kavitha Alagesan | df352c6 | 2016-07-13 09:58:19 +0530 | [diff] [blame] | 135 | throw new IllegalArgumentException("The requested type " + typeStr + |
| 136 | " is not defined for FilteringObjective."); |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | // decode key |
| 140 | JsonNode keyJson = json.get(KEY); |
| 141 | if (keyJson != null) { |
| 142 | Criterion key = criterionCodec.decode((ObjectNode) keyJson, context); |
| 143 | builder.withKey(key); |
| 144 | } |
| 145 | |
| 146 | // decode conditions |
| 147 | JsonNode conditionsJson = json.get(CONDITIONS); |
| 148 | checkNotNull(conditionsJson); |
| 149 | if (conditionsJson != null) { |
| 150 | IntStream.range(0, conditionsJson.size()).forEach(i -> { |
| 151 | ObjectNode conditionJson = get(conditionsJson, i); |
| 152 | builder.addCondition(criterionCodec.decode(conditionJson, context)); |
| 153 | }); |
| 154 | } |
| 155 | |
| 156 | // decode meta |
| 157 | JsonNode metaJson = json.get(META); |
| 158 | if (metaJson != null) { |
| 159 | TrafficTreatment trafficTreatment = trafficTreatmentCodec.decode((ObjectNode) metaJson, context); |
| 160 | builder.withMeta(trafficTreatment); |
| 161 | } |
| 162 | |
| 163 | // decode operation |
| 164 | String opStr = nullIsIllegal(json.get(OPERATION), OPERATION + MISSING_MEMBER_MESSAGE).asText(); |
| 165 | FilteringObjective filteringObjective; |
| 166 | |
| 167 | switch (opStr) { |
| 168 | case "ADD": |
| 169 | filteringObjective = builder.add(); |
| 170 | break; |
| 171 | case "REMOVE": |
| 172 | filteringObjective = builder.remove(); |
| 173 | break; |
| 174 | default: |
Kavitha Alagesan | df352c6 | 2016-07-13 09:58:19 +0530 | [diff] [blame] | 175 | throw new IllegalArgumentException("The requested operation " + opStr + |
| 176 | " is not defined for FilteringObjective."); |
Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | return filteringObjective; |
| 180 | } |
| 181 | } |