blob: 1845458735dcc7ad69597d290f3afd9e0d5326ad [file] [log] [blame]
Brian O'Connor7cbbbb72016-04-09 02:13:23 -07001/*
2 * Copyright 2016-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 */
Jonathan Hart07eb0412016-02-08 16:42:29 -080016package org.onosproject.codec.impl;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onlab.packet.IpAddress;
20import org.onosproject.codec.CodecContext;
21import org.onosproject.codec.JsonCodec;
22import org.onosproject.net.mcast.McastRoute;
23
24import static com.google.common.base.Preconditions.checkNotNull;
25
26/**
27 * Codec to encode and decode a multicast route to and from JSON.
28 */
29public class McastRouteCodec extends JsonCodec<McastRoute> {
30
31 private static final String SOURCE = "source";
32 private static final String GROUP = "group";
33 private static final String TYPE = "type";
34
35 @Override
36 public ObjectNode encode(McastRoute route, CodecContext context) {
37 checkNotNull(route);
38 ObjectNode root = context.mapper().createObjectNode()
39 .put(TYPE, route.type().toString())
40 .put(SOURCE, route.source().toString())
41 .put(GROUP, route.group().toString());
42
43 return root;
44 }
45
46 @Override
47 public McastRoute decode(ObjectNode json, CodecContext context) {
48 if (json == null || !json.isObject()) {
49 return null;
50 }
51
52 IpAddress source = IpAddress.valueOf(json.path(SOURCE).asText());
53 IpAddress group = IpAddress.valueOf(json.path(GROUP).asText());
54
55 McastRoute route = new McastRoute(source, group, McastRoute.Type.STATIC);
56
57 return route;
58 }
59}