2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowplugin.impl.util;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Preconditions;
13 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
14 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
15 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPort;
20 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
22 import javax.annotation.Nonnull;
23 import javax.annotation.Nullable;
24 import java.math.BigInteger;
25 import java.util.List;
26 import java.util.Objects;
29 * Created by Tomas Slusny on 23.3.2016.
31 public class NodeConnectorRefToPortTranslator {
33 * Converts {@link DeviceState} to {@link NodeConnectorRef}
34 * @param deviceState Device state to be converted
35 * @return Device state converted to node connector reference
38 public static NodeConnectorRef toNodeConnectorRef(@Nonnull DeviceState deviceState) {
39 Preconditions.checkNotNull(deviceState);
41 Long port = getPortNoFromDeviceState(deviceState);
42 OpenflowVersion version = OpenflowVersion.get(deviceState.getVersion());
43 BigInteger dataPathId = deviceState.getFeatures().getDatapathId();
45 return InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(dataPathId, port, version);
49 * Gets port number from {@link NodeConnectorRef}. If it is null, it will try to get the port from
51 * @param deviceState Device state fallback if there is any problem with node connector reference
52 * @param nodeConnectorRef Node connector reference
55 @SuppressWarnings("unchecked")
57 public static Long fromNodeConnectorRef(@Nonnull DeviceState deviceState, NodeConnectorRef nodeConnectorRef) {
58 Preconditions.checkNotNull(deviceState);
60 if (nodeConnectorRef != null && nodeConnectorRef.getValue() instanceof KeyedInstanceIdentifier) {
61 KeyedInstanceIdentifier<NodeConnector, NodeConnectorKey> identifier =
62 (KeyedInstanceIdentifier<NodeConnector, NodeConnectorKey>) nodeConnectorRef.getValue();
64 OpenflowVersion version = OpenflowVersion.get(deviceState.getVersion());
65 String nodeConnectorId = identifier.getKey().getId().getValue();
67 return InventoryDataServiceUtil.portNumberfromNodeConnectorId(version, nodeConnectorId);
69 return getPortNoFromDeviceState(deviceState);
75 static Long getPortNoFromDeviceState(@Nonnull DeviceState deviceState) {
76 Preconditions.checkNotNull(deviceState);
78 List<PhyPort> ports = deviceState.getFeatures().getPhyPort();
80 return ports != null ?
81 ports.stream().filter(Objects::nonNull).map(PhyPort::getPortNo).filter(Objects::nonNull).findFirst().orElse(null) :