blob: e6fbea886e42fb39288cc1107dd2fea405ed7565 [file] [log] [blame]
Ray Milkeyc95bb9d2015-01-06 10:28:24 -08001/*
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 */
16package org.onosproject.codec.impl;
17
18import org.onosproject.codec.CodecContext;
19import org.onosproject.codec.JsonCodec;
20import org.onosproject.net.flow.criteria.Criteria;
21import org.onosproject.net.flow.criteria.Criterion;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25import com.fasterxml.jackson.databind.node.ObjectNode;
26
27import static com.google.common.base.Preconditions.checkNotNull;
28
29/**
30 * Criterion codec.
31 */
32public 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;
Ray Milkeydb358082015-01-13 16:34:38 -080053 result.put("mac", ethCriterion.mac().toString());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080054 break;
55
56 case ETH_TYPE:
57 final Criteria.EthTypeCriterion ethTypeCriterion =
58 (Criteria.EthTypeCriterion) criterion;
59 result.put("ethType", ethTypeCriterion.ethType());
60 break;
61
62 case IPV4_SRC:
63 case IPV6_SRC:
Ray Milkeydb358082015-01-13 16:34:38 -080064 case IPV4_DST:
65 case IPV6_DST:
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080066 final Criteria.IPCriterion iPCriterion = (Criteria.IPCriterion) criterion;
67 result.put("ip", iPCriterion.ip().toString());
68 break;
69
70 case IP_PROTO:
71 final Criteria.IPProtocolCriterion iPProtocolCriterion =
72 (Criteria.IPProtocolCriterion) criterion;
73 result.put("protocol", iPProtocolCriterion.protocol());
74 break;
75
76 case VLAN_PCP:
77 final Criteria.VlanPcpCriterion vlanPcpCriterion =
78 (Criteria.VlanPcpCriterion) criterion;
79 result.put("priority", vlanPcpCriterion.priority());
80 break;
81
82 case VLAN_VID:
83 final Criteria.VlanIdCriterion vlanIdCriterion =
84 (Criteria.VlanIdCriterion) criterion;
85 result.put("vlanId", vlanIdCriterion.vlanId().toShort());
86 break;
87
88 case TCP_SRC:
89 case TCP_DST:
90 final Criteria.TcpPortCriterion tcpPortCriterion =
91 (Criteria.TcpPortCriterion) criterion;
92 result.put("tcpPort", tcpPortCriterion.tcpPort().byteValue());
93 break;
94
95 case MPLS_LABEL:
96 final Criteria.MplsCriterion mplsCriterion =
97 (Criteria.MplsCriterion) criterion;
98 result.put("label", mplsCriterion.label());
99 break;
100
101 case OCH_SIGID:
102 final Criteria.LambdaCriterion lambdaCriterion =
103 (Criteria.LambdaCriterion) criterion;
104 result.put("lambda", lambdaCriterion.lambda());
105 break;
106
107 case OCH_SIGTYPE:
108 final Criteria.OpticalSignalTypeCriterion opticalSignalTypeCriterion =
109 (Criteria.OpticalSignalTypeCriterion) criterion;
110 result.put("signalType", opticalSignalTypeCriterion.signalType());
111 break;
112
113 default:
114 // Don't know how to format this type
115 log.info("Cannot convert criterion of type {} to JSON",
116 criterion.type());
117 break;
118 }
119
120 return result;
121 }
122}