blob: cb41e34a7fe6e706764b57aa7be0c8db6d54b422 [file] [log] [blame]
Andrea Campanella644a8a62018-03-21 19:08:21 -07001/*
2 * Copyright 2016-present Open Networking Foundation
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 */
16package org.onosproject.mcast.web;
17
18import com.fasterxml.jackson.databind.node.ArrayNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onlab.packet.IpAddress;
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.mcast.api.McastRoute;
24import org.onosproject.mcast.api.MulticastRouteService;
25import org.slf4j.Logger;
26
27import java.util.Optional;
28
29import static com.google.common.base.Preconditions.checkNotNull;
30import static org.slf4j.LoggerFactory.getLogger;
31
32/**
33 * Codec to encode and decode a multicast route to and from JSON.
34 */
35public class McastHostRouteCodec extends JsonCodec<McastRoute> {
36
37 private Logger log = getLogger(getClass());
38
39 private static final String SOURCE = "source";
40 private static final String GROUP = "group";
41 private static final String TYPE = "type";
42 private static final String SOURCES = "sources";
43 private static final String SINKS = "sinks";
44
45 @Override
46 public ObjectNode encode(McastRoute route, CodecContext context) {
47 checkNotNull(route);
48
49 ObjectNode root = context.mapper().createObjectNode()
50 .put(TYPE, route.type().toString())
51 .put(GROUP, route.group().toString());
52 Optional<IpAddress> sourceIp = route.source();
53 if (sourceIp.isPresent()) {
54 root.put(SOURCE, sourceIp.get().toString());
55 } else {
56 root.put(SOURCE, "*");
57 }
58
59 ArrayNode sources = context.mapper().createArrayNode();
60 context.getService(MulticastRouteService.class).sources(route).forEach(source -> {
61 sources.add(source.toString());
62 });
63 root.putPOJO(SOURCES, sources);
64
65 ObjectNode sinks = context.mapper().createObjectNode();
66 context.getService(MulticastRouteService.class).routeData(route).sinks().forEach((k, v) -> {
67 ArrayNode node = context.mapper().createArrayNode();
68 v.forEach(sink -> {
69 node.add(sink.toString());
70 });
71 sinks.putPOJO(k.toString(), node);
72 });
73 root.putPOJO(SINKS, sinks);
74
75 return root;
76 }
77
78 @Override
79 public McastRoute decode(ObjectNode json, CodecContext context) {
80 if (json == null || !json.isObject()) {
81 return null;
82 }
83
84 String source = json.path(SOURCE).asText();
85
86 IpAddress sourceIp = null;
87
88 if (!source.equals("*")) {
89 sourceIp = IpAddress.valueOf(source);
90 }
91
92 IpAddress group = IpAddress.valueOf(json.path(GROUP).asText());
93
94 McastRoute route = new McastRoute(sourceIp, group, McastRoute.Type.STATIC);
95
96 return route;
97 }
98}