Andrea Campanella | 644a8a6 | 2018-03-21 19:08:21 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.mcast.web; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.codec.JsonCodec; |
| 23 | import org.onosproject.mcast.api.McastRoute; |
| 24 | import org.onosproject.mcast.api.MulticastRouteService; |
| 25 | import org.slf4j.Logger; |
| 26 | |
| 27 | import java.util.Optional; |
| 28 | |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | import static org.slf4j.LoggerFactory.getLogger; |
| 31 | |
| 32 | /** |
| 33 | * Codec to encode and decode a multicast route to and from JSON. |
| 34 | */ |
| 35 | public 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 | |
Andrea Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame^] | 59 | ObjectNode sources = context.mapper().createObjectNode(); |
| 60 | context.getService(MulticastRouteService.class).routeData(route).sources().forEach((k, v) -> { |
| 61 | ArrayNode node = context.mapper().createArrayNode(); |
| 62 | v.forEach(source -> { |
| 63 | node.add(source.toString()); |
| 64 | }); |
| 65 | sources.putPOJO(k.toString(), node); |
Andrea Campanella | 644a8a6 | 2018-03-21 19:08:21 -0700 | [diff] [blame] | 66 | }); |
Andrea Campanella | 0ddf9b8 | 2018-04-27 15:54:42 +0200 | [diff] [blame^] | 67 | |
Andrea Campanella | 644a8a6 | 2018-03-21 19:08:21 -0700 | [diff] [blame] | 68 | root.putPOJO(SOURCES, sources); |
| 69 | |
| 70 | ObjectNode sinks = context.mapper().createObjectNode(); |
| 71 | context.getService(MulticastRouteService.class).routeData(route).sinks().forEach((k, v) -> { |
| 72 | ArrayNode node = context.mapper().createArrayNode(); |
| 73 | v.forEach(sink -> { |
| 74 | node.add(sink.toString()); |
| 75 | }); |
| 76 | sinks.putPOJO(k.toString(), node); |
| 77 | }); |
| 78 | root.putPOJO(SINKS, sinks); |
| 79 | |
| 80 | return root; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public McastRoute decode(ObjectNode json, CodecContext context) { |
| 85 | if (json == null || !json.isObject()) { |
| 86 | return null; |
| 87 | } |
| 88 | |
| 89 | String source = json.path(SOURCE).asText(); |
| 90 | |
| 91 | IpAddress sourceIp = null; |
| 92 | |
| 93 | if (!source.equals("*")) { |
| 94 | sourceIp = IpAddress.valueOf(source); |
| 95 | } |
| 96 | |
| 97 | IpAddress group = IpAddress.valueOf(json.path(GROUP).asText()); |
| 98 | |
| 99 | McastRoute route = new McastRoute(sourceIp, group, McastRoute.Type.STATIC); |
| 100 | |
| 101 | return route; |
| 102 | } |
| 103 | } |