blob: 0f27ddb01f80417b588e4940864f7a8adf5d8cf8 [file] [log] [blame]
Jovana Vuletafe32db7d2017-05-01 12:18:00 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jovana Vuletafe32db7d2017-05-01 12:18:00 +02003 *
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.ofagent.rest;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onlab.packet.IpAddress;
20import org.onlab.packet.TpPort;
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.ofagent.api.OFController;
24import org.onosproject.ofagent.impl.DefaultOFController;
25
26import static com.google.common.base.Preconditions.checkNotNull;
27import static org.onlab.util.Tools.nullIsIllegal;
28
29/**
30 * OFController JSON codec.
31 */
32public final class OFControllerCodec extends JsonCodec<OFController> {
33
34 private static final String IP = "ip";
35 private static final String PORT = "port";
36
37 private static final String MISSING_MEMBER_MESSAGE = " member is required in OFController";
38
39 @Override
40 public ObjectNode encode(OFController ofController, CodecContext context) {
41 checkNotNull(ofController, "OFController cannot be null");
42
43 return context.mapper().createObjectNode()
44 .put(IP, String.valueOf(ofController.ip()))
45 .put(PORT, String.valueOf(ofController.port()));
46
47 }
48
49 @Override
50 public OFController decode(ObjectNode json, CodecContext context) {
51 if (json == null || !json.isObject()) {
52 return null;
53 }
54
55 // parse ip address
56 int id = nullIsIllegal(json.get(IP), IP + MISSING_MEMBER_MESSAGE).asInt();
57
58 // parse port
59 String name = nullIsIllegal(json.get(PORT), PORT + MISSING_MEMBER_MESSAGE).asText();
60
61 return DefaultOFController.of(IpAddress.valueOf(id),
62 TpPort.tpPort(Integer.valueOf(name)));
63 }
64}