blob: 18295bdbb23e4c274dbbc10a5eaae31eb702d561 [file] [log] [blame]
Ray Milkeyc95bb9d2015-01-06 10:28:24 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkeyc95bb9d2015-01-06 10:28:24 -08003 *
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;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070020import org.onosproject.net.HostId;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080021import org.onosproject.net.intent.ConnectivityIntent;
22import org.onosproject.net.intent.HostToHostIntent;
23
24import com.fasterxml.jackson.databind.node.ObjectNode;
25
26import static com.google.common.base.Preconditions.checkNotNull;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070027import static org.onlab.util.Tools.nullIsIllegal;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080028
29/**
30 * Host to host intent codec.
31 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080032public final class HostToHostIntentCodec extends JsonCodec<HostToHostIntent> {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080033
Ray Milkeyb82c42b2015-06-30 09:42:20 -070034 private static final String ONE = "one";
35 private static final String TWO = "two";
36
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080037 @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 Milkeyb82c42b2015-06-30 09:42:20 -070047 result.put(ONE, one);
48 result.put(TWO, two);
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080049
50 return result;
51 }
Ray Milkeyb82c42b2015-06-30 09:42:20 -070052
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 Milkeyc95bb9d2015-01-06 10:28:24 -080070}