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