Jovana Vuleta | fe32db7d | 2017-05-01 12:18:00 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present 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.ofagent.rest; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.packet.TpPort; |
| 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.codec.JsonCodec; |
| 23 | import org.onosproject.ofagent.api.OFController; |
| 24 | import org.onosproject.ofagent.impl.DefaultOFController; |
| 25 | |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | import static org.onlab.util.Tools.nullIsIllegal; |
| 28 | |
| 29 | /** |
| 30 | * OFController JSON codec. |
| 31 | */ |
| 32 | public 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 | } |