Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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.segmentrouting.xconnect.api; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.collect.Sets; |
| 22 | import org.onlab.packet.VlanId; |
| 23 | import org.onosproject.codec.CodecContext; |
| 24 | import org.onosproject.codec.JsonCodec; |
| 25 | import org.onosproject.net.DeviceId; |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | import java.util.Set; |
| 30 | |
| 31 | public class XconnectCodec extends JsonCodec<XconnectDesc> { |
| 32 | private static final String DEVICE_ID = "deviceId"; |
| 33 | private static final String VLAN_ID = "vlanId"; |
| 34 | private static final String PORTS = "ports"; |
| 35 | |
| 36 | private static Logger log = LoggerFactory.getLogger(XconnectCodec.class); |
| 37 | |
| 38 | @Override |
| 39 | public ObjectNode encode(XconnectDesc desc, CodecContext context) { |
| 40 | final ObjectNode result = context.mapper().createObjectNode(); |
| 41 | result.put(DEVICE_ID, desc.key().deviceId().toString()); |
| 42 | result.put(VLAN_ID, desc.key().vlanId().toString()); |
| 43 | final ArrayNode portNode = result.putArray(PORTS); |
| 44 | desc.ports().forEach(port -> portNode.add(port.toString())); |
| 45 | |
| 46 | return result; |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public XconnectDesc decode(ObjectNode json, CodecContext context) { |
| 51 | DeviceId deviceId = DeviceId.deviceId(json.path(DEVICE_ID).asText()); |
| 52 | VlanId vlanId = VlanId.vlanId(json.path(VLAN_ID).asText()); |
| 53 | |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 54 | Set<String> ports = Sets.newHashSet(); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 55 | JsonNode portNodes = json.get(PORTS); |
| 56 | if (portNodes != null) { |
Charles Chan | 48df9ad | 2018-10-30 18:08:59 -0700 | [diff] [blame] | 57 | portNodes.forEach(portNode -> ports.add(portNode.asText())); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | XconnectKey key = new XconnectKey(deviceId, vlanId); |
| 61 | return new XconnectDesc(key, ports); |
| 62 | } |
| 63 | } |