Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [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 | |
Jian Li | 7c322f4 | 2016-03-04 11:00:59 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 20 | import org.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.codec.JsonCodec; |
Jian Li | 2907ad2 | 2016-05-12 23:08:54 -0700 | [diff] [blame] | 22 | import org.onosproject.core.ApplicationId; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 23 | import org.onosproject.core.CoreService; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.flow.DefaultFlowRule; |
| 26 | import org.onosproject.net.flow.FlowRule; |
| 27 | import org.onosproject.net.flow.TrafficSelector; |
| 28 | import org.onosproject.net.flow.TrafficTreatment; |
| 29 | |
Jian Li | 2907ad2 | 2016-05-12 23:08:54 -0700 | [diff] [blame] | 30 | import static com.google.common.base.Preconditions.checkNotNull; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 31 | import static org.onlab.util.Tools.nullIsIllegal; |
| 32 | |
| 33 | /** |
| 34 | * Flow rule JSON codec. |
| 35 | */ |
| 36 | public final class FlowRuleCodec extends JsonCodec<FlowRule> { |
| 37 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 38 | private static final String PRIORITY = "priority"; |
| 39 | private static final String TIMEOUT = "timeout"; |
| 40 | private static final String IS_PERMANENT = "isPermanent"; |
Jian Li | 2907ad2 | 2016-05-12 23:08:54 -0700 | [diff] [blame] | 41 | private static final String APP_ID = "appId"; |
Jian Li | 7c322f4 | 2016-03-04 11:00:59 -0800 | [diff] [blame] | 42 | private static final String TABLE_ID = "tableId"; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 43 | private static final String DEVICE_ID = "deviceId"; |
| 44 | private static final String TREATMENT = "treatment"; |
| 45 | private static final String SELECTOR = "selector"; |
| 46 | private static final String MISSING_MEMBER_MESSAGE = |
Jian Li | 2907ad2 | 2016-05-12 23:08:54 -0700 | [diff] [blame] | 47 | " member is required in FlowRule"; |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 48 | public static final String REST_APP_ID = "org.onosproject.rest"; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 49 | |
Jian Li | 2907ad2 | 2016-05-12 23:08:54 -0700 | [diff] [blame] | 50 | @Override |
| 51 | public ObjectNode encode(FlowRule flowRule, CodecContext context) { |
| 52 | checkNotNull(flowRule, "Flow rule cannot be null"); |
| 53 | |
| 54 | CoreService service = context.getService(CoreService.class); |
| 55 | ApplicationId appId = service.getAppId(flowRule.appId()); |
| 56 | String strAppId = (appId == null) ? "<none>" : appId.name(); |
| 57 | |
| 58 | final ObjectNode result = context.mapper().createObjectNode() |
| 59 | .put("id", Long.toString(flowRule.id().value())) |
| 60 | .put("tableId", flowRule.tableId()) |
| 61 | .put("appId", strAppId) |
| 62 | .put("priority", flowRule.priority()) |
| 63 | .put("timeout", flowRule.timeout()) |
| 64 | .put("isPermanent", flowRule.isPermanent()) |
| 65 | .put("deviceId", flowRule.deviceId().toString()); |
| 66 | |
| 67 | if (flowRule.treatment() != null) { |
| 68 | final JsonCodec<TrafficTreatment> treatmentCodec = |
| 69 | context.codec(TrafficTreatment.class); |
| 70 | result.set("treatment", treatmentCodec.encode(flowRule.treatment(), context)); |
| 71 | } |
| 72 | |
| 73 | if (flowRule.selector() != null) { |
| 74 | final JsonCodec<TrafficSelector> selectorCodec = |
| 75 | context.codec(TrafficSelector.class); |
| 76 | result.set("selector", selectorCodec.encode(flowRule.selector(), context)); |
| 77 | } |
| 78 | |
| 79 | return result; |
| 80 | } |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 81 | |
| 82 | @Override |
| 83 | public FlowRule decode(ObjectNode json, CodecContext context) { |
| 84 | if (json == null || !json.isObject()) { |
| 85 | return null; |
| 86 | } |
| 87 | |
| 88 | FlowRule.Builder resultBuilder = new DefaultFlowRule.Builder(); |
| 89 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 90 | CoreService coreService = context.getService(CoreService.class); |
Jian Li | 2907ad2 | 2016-05-12 23:08:54 -0700 | [diff] [blame] | 91 | JsonNode appIdJson = json.get(APP_ID); |
| 92 | String appId = appIdJson != null ? appIdJson.asText() : REST_APP_ID; |
| 93 | resultBuilder.fromApp(coreService.registerApplication(appId)); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 94 | |
| 95 | int priority = nullIsIllegal(json.get(PRIORITY), |
| 96 | PRIORITY + MISSING_MEMBER_MESSAGE).asInt(); |
| 97 | resultBuilder.withPriority(priority); |
| 98 | |
| 99 | boolean isPermanent = nullIsIllegal(json.get(IS_PERMANENT), |
| 100 | IS_PERMANENT + MISSING_MEMBER_MESSAGE).asBoolean(); |
| 101 | if (isPermanent) { |
| 102 | resultBuilder.makePermanent(); |
| 103 | } else { |
| 104 | resultBuilder.makeTemporary(nullIsIllegal(json.get(TIMEOUT), |
| 105 | TIMEOUT |
| 106 | + MISSING_MEMBER_MESSAGE |
| 107 | + " if the flow is temporary").asInt()); |
| 108 | } |
| 109 | |
Jian Li | 7c322f4 | 2016-03-04 11:00:59 -0800 | [diff] [blame] | 110 | JsonNode tableIdJson = json.get(TABLE_ID); |
| 111 | if (tableIdJson != null) { |
| 112 | resultBuilder.forTable(tableIdJson.asInt()); |
| 113 | } |
| 114 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 115 | DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID), |
| 116 | DEVICE_ID + MISSING_MEMBER_MESSAGE).asText()); |
| 117 | resultBuilder.forDevice(deviceId); |
| 118 | |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 119 | ObjectNode treatmentJson = get(json, TREATMENT); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 120 | if (treatmentJson != null) { |
| 121 | JsonCodec<TrafficTreatment> treatmentCodec = |
| 122 | context.codec(TrafficTreatment.class); |
| 123 | resultBuilder.withTreatment(treatmentCodec.decode(treatmentJson, context)); |
| 124 | } |
| 125 | |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 126 | ObjectNode selectorJson = get(json, SELECTOR); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 127 | if (selectorJson != null) { |
| 128 | JsonCodec<TrafficSelector> selectorCodec = |
| 129 | context.codec(TrafficSelector.class); |
| 130 | resultBuilder.withSelector(selectorCodec.decode(selectorJson, context)); |
| 131 | } |
| 132 | |
| 133 | return resultBuilder.build(); |
| 134 | } |
| 135 | } |