Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [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.net.flow.criteria.Criteria; |
| 21 | import org.onosproject.net.flow.criteria.Criterion; |
| 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; |
| 24 | |
| 25 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 26 | |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | |
| 29 | /** |
| 30 | * Criterion codec. |
| 31 | */ |
| 32 | public class CriterionCodec extends JsonCodec<Criterion> { |
| 33 | |
| 34 | protected static final Logger log = LoggerFactory.getLogger(CriterionCodec.class); |
| 35 | |
| 36 | @Override |
| 37 | public ObjectNode encode(Criterion criterion, CodecContext context) { |
| 38 | checkNotNull(criterion, "Criterion cannot be null"); |
| 39 | |
| 40 | final ObjectNode result = context.mapper().createObjectNode() |
| 41 | .put("type", criterion.type().toString()); |
| 42 | |
| 43 | switch (criterion.type()) { |
| 44 | |
| 45 | case IN_PORT: |
| 46 | final Criteria.PortCriterion portCriterion = (Criteria.PortCriterion) criterion; |
| 47 | result.put("tcpPort", portCriterion.port().toLong()); |
| 48 | break; |
| 49 | |
| 50 | case ETH_SRC: |
| 51 | case ETH_DST: |
| 52 | final Criteria.EthCriterion ethCriterion = (Criteria.EthCriterion) criterion; |
| 53 | break; |
| 54 | |
| 55 | case ETH_TYPE: |
| 56 | final Criteria.EthTypeCriterion ethTypeCriterion = |
| 57 | (Criteria.EthTypeCriterion) criterion; |
| 58 | result.put("ethType", ethTypeCriterion.ethType()); |
| 59 | break; |
| 60 | |
| 61 | case IPV4_SRC: |
| 62 | case IPV6_SRC: |
| 63 | final Criteria.IPCriterion iPCriterion = (Criteria.IPCriterion) criterion; |
| 64 | result.put("ip", iPCriterion.ip().toString()); |
| 65 | break; |
| 66 | |
| 67 | case IP_PROTO: |
| 68 | final Criteria.IPProtocolCriterion iPProtocolCriterion = |
| 69 | (Criteria.IPProtocolCriterion) criterion; |
| 70 | result.put("protocol", iPProtocolCriterion.protocol()); |
| 71 | break; |
| 72 | |
| 73 | case VLAN_PCP: |
| 74 | final Criteria.VlanPcpCriterion vlanPcpCriterion = |
| 75 | (Criteria.VlanPcpCriterion) criterion; |
| 76 | result.put("priority", vlanPcpCriterion.priority()); |
| 77 | break; |
| 78 | |
| 79 | case VLAN_VID: |
| 80 | final Criteria.VlanIdCriterion vlanIdCriterion = |
| 81 | (Criteria.VlanIdCriterion) criterion; |
| 82 | result.put("vlanId", vlanIdCriterion.vlanId().toShort()); |
| 83 | break; |
| 84 | |
| 85 | case TCP_SRC: |
| 86 | case TCP_DST: |
| 87 | final Criteria.TcpPortCriterion tcpPortCriterion = |
| 88 | (Criteria.TcpPortCriterion) criterion; |
| 89 | result.put("tcpPort", tcpPortCriterion.tcpPort().byteValue()); |
| 90 | break; |
| 91 | |
| 92 | case MPLS_LABEL: |
| 93 | final Criteria.MplsCriterion mplsCriterion = |
| 94 | (Criteria.MplsCriterion) criterion; |
| 95 | result.put("label", mplsCriterion.label()); |
| 96 | break; |
| 97 | |
| 98 | case OCH_SIGID: |
| 99 | final Criteria.LambdaCriterion lambdaCriterion = |
| 100 | (Criteria.LambdaCriterion) criterion; |
| 101 | result.put("lambda", lambdaCriterion.lambda()); |
| 102 | break; |
| 103 | |
| 104 | case OCH_SIGTYPE: |
| 105 | final Criteria.OpticalSignalTypeCriterion opticalSignalTypeCriterion = |
| 106 | (Criteria.OpticalSignalTypeCriterion) criterion; |
| 107 | result.put("signalType", opticalSignalTypeCriterion.signalType()); |
| 108 | break; |
| 109 | |
| 110 | default: |
| 111 | // Don't know how to format this type |
| 112 | log.info("Cannot convert criterion of type {} to JSON", |
| 113 | criterion.type()); |
| 114 | break; |
| 115 | } |
| 116 | |
| 117 | return result; |
| 118 | } |
| 119 | } |