fd5b1ee4140b819145b28d9b05ecbf1e3729366e
[onosfw.git] /
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.vtnrsc.web;
17
18 import static com.google.common.base.Preconditions.checkNotNull;
19 import static org.onlab.util.Tools.nullIsIllegal;
20
21 import java.util.UUID;
22
23 import org.onlab.packet.IpPrefix;
24 import org.onosproject.codec.CodecContext;
25 import org.onosproject.codec.JsonCodec;
26 import org.onosproject.vtnrsc.DefaultFlowClassifier;
27 import org.onosproject.vtnrsc.FlowClassifier;
28 import org.onosproject.vtnrsc.FlowClassifierId;
29 import org.onosproject.vtnrsc.VirtualPortId;
30 import org.onosproject.vtnrsc.TenantId;
31
32 import com.fasterxml.jackson.databind.node.ObjectNode;
33
34 /**
35  * Flow Classifier JSON codec.
36  */
37 public final class FlowClassifierCodec extends JsonCodec<FlowClassifier> {
38
39     private static final String FLOW_CLASSIFIER_ID = "id";
40     private static final String TENANT_ID = "tenant_id";
41     private static final String NAME = "name";
42     private static final String DESCRIPTION = "description";
43     private static final String ETHER_TYPE = "etherType";
44     private static final String PROTOCOL = "protocol";
45     private static final String MIN_SRC_PORT_RANGE = "source_port_range_min";
46     private static final String MAX_SRC_PORT_RANGE = "source_port_range_max";
47     private static final String MIN_DST_PORT_RANGE = "destination_port_range_min";
48     private static final String MAX_DST_PORT_RANGE = "destination_port_range_max";
49     private static final String SRC_IP_PREFIX = "source_ip_prefix";
50     private static final String DST_IP_PREFIX = "destination_ip_prefix";
51     private static final String SRC_PORT = "logical_source_port";
52     private static final String DST_PORT = "logical_destination_port";
53     private static final String MISSING_MEMBER_MESSAGE = " member is required in Flow Classifier.";
54
55     @Override
56     public FlowClassifier decode(ObjectNode json, CodecContext context) {
57         if (json == null || !json.isObject()) {
58             return null;
59         }
60
61         FlowClassifier.Builder resultBuilder = new DefaultFlowClassifier.Builder();
62
63         String flowClassifierId = nullIsIllegal(json.get(FLOW_CLASSIFIER_ID),
64                 FLOW_CLASSIFIER_ID + MISSING_MEMBER_MESSAGE).asText();
65         resultBuilder.setFlowClassifierId(FlowClassifierId.flowClassifierId(UUID.fromString(flowClassifierId)));
66
67         String tenantId = nullIsIllegal(json.get(TENANT_ID), TENANT_ID + MISSING_MEMBER_MESSAGE).asText();
68         resultBuilder.setTenantId(TenantId.tenantId(tenantId));
69
70         String flowClassiferName = nullIsIllegal(json.get(NAME), NAME + MISSING_MEMBER_MESSAGE).asText();
71         resultBuilder.setName(flowClassiferName);
72
73         String flowClassiferDescription = nullIsIllegal(json.get(DESCRIPTION), DESCRIPTION + MISSING_MEMBER_MESSAGE)
74                 .asText();
75         resultBuilder.setDescription(flowClassiferDescription);
76
77         String etherType = nullIsIllegal(json.get(ETHER_TYPE), ETHER_TYPE + MISSING_MEMBER_MESSAGE).asText();
78         resultBuilder.setEtherType(etherType);
79
80         String protocol = nullIsIllegal(json.get(PROTOCOL), PROTOCOL + MISSING_MEMBER_MESSAGE).asText();
81         resultBuilder.setProtocol(protocol);
82
83         int minSrcPortRange = nullIsIllegal(json.get(MIN_SRC_PORT_RANGE), MIN_SRC_PORT_RANGE + MISSING_MEMBER_MESSAGE)
84                 .asInt();
85         resultBuilder.setMinSrcPortRange(minSrcPortRange);
86
87         int maxSrcPortRange = nullIsIllegal(json.get(MAX_SRC_PORT_RANGE), MAX_SRC_PORT_RANGE + MISSING_MEMBER_MESSAGE)
88                 .asInt();
89         resultBuilder.setMaxSrcPortRange(maxSrcPortRange);
90
91         int minDstPortRange = nullIsIllegal(json.get(MIN_DST_PORT_RANGE), MIN_DST_PORT_RANGE + MISSING_MEMBER_MESSAGE)
92                 .asInt();
93         resultBuilder.setMinDstPortRange(minDstPortRange);
94
95         int maxDstPortRange = nullIsIllegal(json.get(MAX_DST_PORT_RANGE), MAX_DST_PORT_RANGE + MISSING_MEMBER_MESSAGE)
96                 .asInt();
97         resultBuilder.setMaxDstPortRange(maxDstPortRange);
98
99         String srcIpPrefix = nullIsIllegal(json.get(SRC_IP_PREFIX), SRC_IP_PREFIX + MISSING_MEMBER_MESSAGE).asText();
100         resultBuilder.setSrcIpPrefix(IpPrefix.valueOf(srcIpPrefix));
101
102         String dstIpPrefix = nullIsIllegal(json.get(DST_IP_PREFIX), DST_IP_PREFIX + MISSING_MEMBER_MESSAGE).asText();
103         resultBuilder.setDstIpPrefix(IpPrefix.valueOf(dstIpPrefix));
104
105         String srcPort = nullIsIllegal(json.get(SRC_PORT), SRC_PORT + MISSING_MEMBER_MESSAGE).asText();
106         resultBuilder.setSrcPort(VirtualPortId.portId(srcPort));
107
108         String dstPort = nullIsIllegal(json.get(DST_PORT), DST_PORT + MISSING_MEMBER_MESSAGE).asText();
109         resultBuilder.setDstPort(VirtualPortId.portId(dstPort));
110
111         return resultBuilder.build();
112     }
113
114     @Override
115     public ObjectNode encode(FlowClassifier flowClassifier, CodecContext context) {
116         checkNotNull(flowClassifier, "flowClassifier cannot be null");
117         ObjectNode result = context.mapper().createObjectNode()
118                 .put("FLOW_CLASSIFIER_ID", flowClassifier.flowClassifierId().toString())
119                 .put("TENANT_ID", flowClassifier.tenantId().toString())
120                 .put("NAME", flowClassifier.name())
121                 .put("DESCRIPTION", flowClassifier.description())
122                 .put("ETHER_TYPE", flowClassifier.etherType())
123                 .put("PROTOCOL", flowClassifier.protocol())
124                 .put("MIN_SRC_PORT_RANGE", flowClassifier.minSrcPortRange())
125                 .put("MAX_SRC_PORT_RANGE", flowClassifier.maxSrcPortRange())
126                 .put("MIN_DST_PORT_RANGE", flowClassifier.minDstPortRange())
127                 .put("MAX_DST_PORT_RANGE", flowClassifier.maxDstPortRange())
128                 .put("SRC_IP_PREFIX", flowClassifier.srcIpPrefix().toString())
129                 .put("DST_IP_PREFIX", flowClassifier.dstIpPrefix().toString())
130                 .put("SRC_PORT", flowClassifier.srcPort().toString())
131                 .put("DST_PORT", flowClassifier.dstPort().toString());
132         return result;
133     }
134 }