Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Claudine Chiu | fb8b816 | 2016-04-01 23:50:51 +0000 | [diff] [blame] | 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.codec.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import org.onosproject.codec.CodecContext; |
| 20 | import org.onosproject.codec.JsonCodec; |
| 21 | import org.onosproject.incubator.net.virtual.DefaultVirtualPort; |
| 22 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 23 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
| 24 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
| 25 | import org.onosproject.incubator.net.virtual.VirtualPort; |
| 26 | import org.onosproject.net.DefaultAnnotations; |
| 27 | import org.onosproject.net.DefaultDevice; |
| 28 | import org.onosproject.net.DefaultPort; |
| 29 | import org.onosproject.net.Device; |
| 30 | import org.onosproject.net.DeviceId; |
| 31 | import org.onosproject.net.Port; |
| 32 | import org.onosproject.net.PortNumber; |
| 33 | |
| 34 | import java.util.Set; |
| 35 | |
| 36 | import static com.google.common.base.Preconditions.checkNotNull; |
| 37 | import static org.onlab.util.Tools.nullIsIllegal; |
| 38 | |
| 39 | /** |
| 40 | * Codec for the VirtualPort class. |
| 41 | */ |
| 42 | public class VirtualPortCodec extends JsonCodec<VirtualPort> { |
| 43 | |
| 44 | // JSON field names |
| 45 | private static final String NETWORK_ID = "networkId"; |
| 46 | private static final String DEVICE_ID = "deviceId"; |
| 47 | private static final String PORT_NUM = "portNum"; |
| 48 | private static final String PHYS_DEVICE_ID = "physDeviceId"; |
| 49 | private static final String PHYS_PORT_NUM = "physPortNum"; |
| 50 | |
| 51 | private static final String NULL_OBJECT_MSG = "VirtualPort cannot be null"; |
| 52 | private static final String MISSING_MEMBER_MSG = " member is required in VirtualPort"; |
| 53 | private static final String INVALID_VIRTUAL_DEVICE = " is not a valid VirtualDevice"; |
| 54 | |
| 55 | @Override |
| 56 | public ObjectNode encode(VirtualPort vPort, CodecContext context) { |
| 57 | checkNotNull(vPort, NULL_OBJECT_MSG); |
| 58 | |
| 59 | ObjectNode result = context.mapper().createObjectNode() |
| 60 | .put(NETWORK_ID, vPort.networkId().toString()) |
| 61 | .put(DEVICE_ID, vPort.element().id().toString()) |
| 62 | .put(PORT_NUM, vPort.number().toString()) |
| 63 | .put(PHYS_DEVICE_ID, vPort.realizedBy().element().id().toString()) |
| 64 | .put(PHYS_PORT_NUM, vPort.realizedBy().number().toString()); |
| 65 | |
| 66 | return result; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public VirtualPort decode(ObjectNode json, CodecContext context) { |
| 71 | if (json == null || !json.isObject()) { |
| 72 | return null; |
| 73 | } |
| 74 | |
| 75 | NetworkId nId = NetworkId.networkId(Long.parseLong(extractMember(NETWORK_ID, json))); |
| 76 | DeviceId dId = DeviceId.deviceId(extractMember(DEVICE_ID, json)); |
| 77 | |
| 78 | VirtualNetworkService vnetService = context.getService(VirtualNetworkService.class); |
| 79 | Set<VirtualDevice> vDevs = vnetService.getVirtualDevices(nId); |
| 80 | VirtualDevice vDev = vDevs.stream() |
| 81 | .filter(virtualDevice -> virtualDevice.id().equals(dId)) |
| 82 | .findFirst().orElse(null); |
| 83 | nullIsIllegal(vDev, dId.toString() + INVALID_VIRTUAL_DEVICE); |
| 84 | |
| 85 | PortNumber portNum = PortNumber.portNumber(extractMember(PORT_NUM, json)); |
| 86 | DeviceId physDId = DeviceId.deviceId(extractMember(PHYS_DEVICE_ID, json)); |
| 87 | PortNumber physPortNum = PortNumber.portNumber(extractMember(PHYS_PORT_NUM, json)); |
| 88 | |
| 89 | DefaultAnnotations annotations = DefaultAnnotations.builder().build(); |
| 90 | Device physDevice = new DefaultDevice(null, physDId, |
| 91 | null, null, null, null, null, null, annotations); |
| 92 | Port realizedBy = new DefaultPort(physDevice, physPortNum, true); |
| 93 | return new DefaultVirtualPort(nId, vDev, portNum, realizedBy); |
| 94 | } |
| 95 | |
| 96 | private String extractMember(String key, ObjectNode json) { |
| 97 | return nullIsIllegal(json.get(key), key + MISSING_MEMBER_MSG).asText(); |
| 98 | } |
| 99 | } |