Fix "Bug 5589 - Deprecate PortNumberCache"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / util / NodeConnectorRefToPortTranslator.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.impl.util;
10
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;
21
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;
27
28 /**
29  * Created by Tomas Slusny on 23.3.2016.
30  */
31 public class NodeConnectorRefToPortTranslator {
32     /**
33      * Converts {@link DeviceState} to {@link NodeConnectorRef}
34      * @param deviceState Device state to be converted
35      * @return Device state converted to node connector reference
36      */
37     @Nullable
38     public static NodeConnectorRef toNodeConnectorRef(@Nonnull DeviceState deviceState) {
39         Preconditions.checkNotNull(deviceState);
40
41         Long port = getPortNoFromDeviceState(deviceState);
42         OpenflowVersion version = OpenflowVersion.get(deviceState.getVersion());
43         BigInteger dataPathId = deviceState.getFeatures().getDatapathId();
44
45         return InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(dataPathId, port, version);
46     }
47
48     /**
49      * Gets port number from {@link NodeConnectorRef}. If it is null, it will try to get the port from
50      * {@link DeviceState}
51      * @param deviceState Device state fallback if there is any problem with node connector reference
52      * @param nodeConnectorRef Node connector reference
53      * @return port number
54      */
55     @SuppressWarnings("unchecked")
56     @Nullable
57     public static Long fromNodeConnectorRef(@Nonnull DeviceState deviceState, NodeConnectorRef nodeConnectorRef) {
58         Preconditions.checkNotNull(deviceState);
59
60         if (nodeConnectorRef != null && nodeConnectorRef.getValue() instanceof KeyedInstanceIdentifier) {
61             KeyedInstanceIdentifier<NodeConnector, NodeConnectorKey> identifier =
62                     (KeyedInstanceIdentifier<NodeConnector, NodeConnectorKey>) nodeConnectorRef.getValue();
63
64             OpenflowVersion version = OpenflowVersion.get(deviceState.getVersion());
65             String nodeConnectorId = identifier.getKey().getId().getValue();
66
67             return InventoryDataServiceUtil.portNumberfromNodeConnectorId(version, nodeConnectorId);
68         } else {
69             return getPortNoFromDeviceState(deviceState);
70         }
71     }
72
73     @VisibleForTesting
74     @Nullable
75     static Long getPortNoFromDeviceState(@Nonnull DeviceState deviceState) {
76         Preconditions.checkNotNull(deviceState);
77
78         List<PhyPort> ports = deviceState.getFeatures().getPhyPort();
79
80         return ports != null ?
81                 ports.stream().filter(Objects::nonNull).map(PhyPort::getPortNo).filter(Objects::nonNull).findFirst().orElse(null) :
82                 null;
83     }
84 }