Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 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; |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 20 | import org.onosproject.net.HostId; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 21 | import org.onosproject.net.intent.ConnectivityIntent; |
| 22 | import org.onosproject.net.intent.HostToHostIntent; |
| 23 | |
| 24 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 25 | |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 27 | import static org.onlab.util.Tools.nullIsIllegal; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Host to host intent codec. |
| 31 | */ |
Ray Milkey | 540b2ce | 2015-02-04 17:50:20 -0800 | [diff] [blame] | 32 | public final class HostToHostIntentCodec extends JsonCodec<HostToHostIntent> { |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 33 | |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 34 | private static final String ONE = "one"; |
| 35 | private static final String TWO = "two"; |
| 36 | |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 37 | @Override |
| 38 | public ObjectNode encode(HostToHostIntent intent, CodecContext context) { |
| 39 | checkNotNull(intent, "Host to host intent cannot be null"); |
| 40 | |
| 41 | final JsonCodec<ConnectivityIntent> connectivityIntentCodec = |
| 42 | context.codec(ConnectivityIntent.class); |
| 43 | final ObjectNode result = connectivityIntentCodec.encode(intent, context); |
| 44 | |
| 45 | final String one = intent.one().toString(); |
| 46 | final String two = intent.two().toString(); |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 47 | result.put(ONE, one); |
| 48 | result.put(TWO, two); |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 49 | |
| 50 | return result; |
| 51 | } |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 52 | |
| 53 | @Override |
| 54 | public HostToHostIntent decode(ObjectNode json, CodecContext context) { |
| 55 | HostToHostIntent.Builder builder = HostToHostIntent.builder(); |
| 56 | |
| 57 | IntentCodec.intentAttributes(json, context, builder); |
| 58 | ConnectivityIntentCodec.intentAttributes(json, context, builder); |
| 59 | |
| 60 | String one = nullIsIllegal(json.get(ONE), |
| 61 | ONE + IntentCodec.MISSING_MEMBER_MESSAGE).asText(); |
| 62 | builder.one(HostId.hostId(one)); |
| 63 | |
| 64 | String two = nullIsIllegal(json.get(TWO), |
| 65 | TWO + IntentCodec.MISSING_MEMBER_MESSAGE).asText(); |
| 66 | builder.two(HostId.hostId(two)); |
| 67 | |
| 68 | return builder.build(); |
| 69 | } |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 70 | } |