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 | |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 31 | /** |
| 32 | * Codec for Xconnect. |
| 33 | */ |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 34 | public class XconnectCodec extends JsonCodec<XconnectDesc> { |
| 35 | private static final String DEVICE_ID = "deviceId"; |
| 36 | private static final String VLAN_ID = "vlanId"; |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 37 | private static final String ENDPOINTS = "endpoints"; |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 38 | |
| 39 | private static Logger log = LoggerFactory.getLogger(XconnectCodec.class); |
| 40 | |
| 41 | @Override |
| 42 | public ObjectNode encode(XconnectDesc desc, CodecContext context) { |
| 43 | final ObjectNode result = context.mapper().createObjectNode(); |
| 44 | result.put(DEVICE_ID, desc.key().deviceId().toString()); |
| 45 | result.put(VLAN_ID, desc.key().vlanId().toString()); |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 46 | final ArrayNode portNode = result.putArray(ENDPOINTS); |
| 47 | desc.endpoints().forEach(endpoint -> portNode.add(endpoint.toString())); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 48 | |
| 49 | return result; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public XconnectDesc decode(ObjectNode json, CodecContext context) { |
| 54 | DeviceId deviceId = DeviceId.deviceId(json.path(DEVICE_ID).asText()); |
| 55 | VlanId vlanId = VlanId.vlanId(json.path(VLAN_ID).asText()); |
| 56 | |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 57 | Set<XconnectEndpoint> endpoints = Sets.newHashSet(); |
| 58 | JsonNode endpointNodes = json.get(ENDPOINTS); |
| 59 | if (endpointNodes != null) { |
| 60 | XconnectEndpoint endpoint = XconnectEndpoint.fromString(endpointNodes.asText()); |
| 61 | endpointNodes.forEach(endpointNode -> endpoints.add(endpoint)); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | XconnectKey key = new XconnectKey(deviceId, vlanId); |
Charles Chan | 445659f | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 65 | return new XconnectDesc(key, endpoints); |
Charles Chan | 8d31633 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 66 | } |
| 67 | } |