Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 org.onosproject.codec.CodecContext; |
| 19 | import org.onosproject.codec.JsonCodec; |
| 20 | import org.onosproject.core.CoreService; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.flow.DefaultFlowRule; |
| 23 | import org.onosproject.net.flow.FlowRule; |
| 24 | import org.onosproject.net.flow.TrafficSelector; |
| 25 | import org.onosproject.net.flow.TrafficTreatment; |
| 26 | |
| 27 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 28 | |
| 29 | import static org.onlab.util.Tools.nullIsIllegal; |
| 30 | |
| 31 | /** |
| 32 | * Flow rule JSON codec. |
| 33 | */ |
| 34 | public final class FlowRuleCodec extends JsonCodec<FlowRule> { |
| 35 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 36 | private static final String PRIORITY = "priority"; |
| 37 | private static final String TIMEOUT = "timeout"; |
| 38 | private static final String IS_PERMANENT = "isPermanent"; |
| 39 | private static final String DEVICE_ID = "deviceId"; |
| 40 | private static final String TREATMENT = "treatment"; |
| 41 | private static final String SELECTOR = "selector"; |
| 42 | private static final String MISSING_MEMBER_MESSAGE = |
| 43 | " member is required in FlowRule"; |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 44 | public static final String REST_APP_ID = "org.onosproject.rest"; |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 45 | |
| 46 | |
| 47 | @Override |
| 48 | public FlowRule decode(ObjectNode json, CodecContext context) { |
| 49 | if (json == null || !json.isObject()) { |
| 50 | return null; |
| 51 | } |
| 52 | |
| 53 | FlowRule.Builder resultBuilder = new DefaultFlowRule.Builder(); |
| 54 | |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 55 | CoreService coreService = context.getService(CoreService.class); |
Ray Milkey | eb5c717 | 2015-06-23 14:59:27 -0700 | [diff] [blame] | 56 | resultBuilder.fromApp(coreService |
| 57 | .registerApplication(REST_APP_ID)); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 58 | |
| 59 | int priority = nullIsIllegal(json.get(PRIORITY), |
| 60 | PRIORITY + MISSING_MEMBER_MESSAGE).asInt(); |
| 61 | resultBuilder.withPriority(priority); |
| 62 | |
| 63 | boolean isPermanent = nullIsIllegal(json.get(IS_PERMANENT), |
| 64 | IS_PERMANENT + MISSING_MEMBER_MESSAGE).asBoolean(); |
| 65 | if (isPermanent) { |
| 66 | resultBuilder.makePermanent(); |
| 67 | } else { |
| 68 | resultBuilder.makeTemporary(nullIsIllegal(json.get(TIMEOUT), |
| 69 | TIMEOUT |
| 70 | + MISSING_MEMBER_MESSAGE |
| 71 | + " if the flow is temporary").asInt()); |
| 72 | } |
| 73 | |
| 74 | DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID), |
| 75 | DEVICE_ID + MISSING_MEMBER_MESSAGE).asText()); |
| 76 | resultBuilder.forDevice(deviceId); |
| 77 | |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 78 | ObjectNode treatmentJson = get(json, TREATMENT); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 79 | if (treatmentJson != null) { |
| 80 | JsonCodec<TrafficTreatment> treatmentCodec = |
| 81 | context.codec(TrafficTreatment.class); |
| 82 | resultBuilder.withTreatment(treatmentCodec.decode(treatmentJson, context)); |
| 83 | } |
| 84 | |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 85 | ObjectNode selectorJson = get(json, SELECTOR); |
Ray Milkey | d43fe45 | 2015-05-29 09:35:12 -0700 | [diff] [blame] | 86 | if (selectorJson != null) { |
| 87 | JsonCodec<TrafficSelector> selectorCodec = |
| 88 | context.codec(TrafficSelector.class); |
| 89 | resultBuilder.withSelector(selectorCodec.decode(selectorJson, context)); |
| 90 | } |
| 91 | |
| 92 | return resultBuilder.build(); |
| 93 | } |
| 94 | } |