2 * Copyright (c) 2016 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
8 package org.opendaylight.openflowplugin.impl.util;
10 import java.net.InetSocketAddress;
11 import java.util.concurrent.ExecutionException;
12 import org.eclipse.jdt.annotation.Nullable;
13 import org.opendaylight.mdsal.binding.api.DataBroker;
14 import org.opendaylight.mdsal.binding.api.WriteTransaction;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
17 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
19 import org.opendaylight.openflowplugin.api.openflow.device.TxFacade;
20 import org.opendaylight.openflowplugin.impl.device.SwitchFeaturesUtil;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.flow.node.SwitchFeatures;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsDataBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodesBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.common.Uint16;
36 import org.opendaylight.yangtools.yang.common.Uint8;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public final class DeviceInitializationUtil {
41 private static final Logger LOG = LoggerFactory.getLogger(DeviceInitializationUtil.class);
43 private DeviceInitializationUtil() {
44 // Hiding implicit constructor
48 * Merge empty nodes to operational DS to predict any problems with missing parent for node.
50 * @param dataBroker the data broker
52 public static void makeEmptyNodes(final DataBroker dataBroker) {
53 final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
56 tx.merge(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class), new NodesBuilder()
59 } catch (ExecutionException | InterruptedException e) {
60 LOG.error("Creation of node failed.", e);
61 throw new IllegalStateException(e);
66 * Create specified number of empty tables on device.
67 * FIXME: remove after ovs table features fix
69 * @param txFacade transaction facade
70 * @param deviceInfo device info
71 * @param nrOfTables number of tables
73 public static void makeEmptyTables(final TxFacade txFacade, final DeviceInfo deviceInfo, final short nrOfTables) {
74 if (LOG.isDebugEnabled()) {
75 LOG.debug("About to create {} empty tables for node {}.", nrOfTables, deviceInfo);
78 for (int i = 0; i < nrOfTables; i++) {
79 final Uint8 tableId = Uint8.valueOf(i);
80 txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL,
82 .getNodeInstanceIdentifier()
83 .augmentation(FlowCapableNode.class)
84 .child(Table.class, new TableKey(tableId)),
87 .addAugmentation(new FlowTableStatisticsDataBuilder().build())
93 * Retrieve ip address from connection.
95 * @param connectionContext connection context
96 * @param instanceIdentifier instance identifier
99 public static IpAddress getIpAddress(final ConnectionContext connectionContext,
100 final InstanceIdentifier<Node> instanceIdentifier) {
101 final String node = PathUtil.extractNodeId(instanceIdentifier).getValue();
102 final InetSocketAddress address = getRemoteAddress(connectionContext, instanceIdentifier);
103 if (address == null) {
106 final IpAddress ipAddress = IetfInetUtil.INSTANCE.ipAddressFor(address.getAddress());
107 LOG.info("IP address of the node {} is: {}", node, ipAddress);
112 * Retrieve port number from connection.
114 * @param connectionContext connection context
115 * @param instanceIdentifier instance identifier
116 * @return port number
118 public static PortNumber getPortNumber(final ConnectionContext connectionContext,
119 final InstanceIdentifier<Node> instanceIdentifier) {
120 final String node = PathUtil.extractNodeId(instanceIdentifier).getValue();
121 final InetSocketAddress address = getRemoteAddress(connectionContext, instanceIdentifier);
122 if (address == null) {
125 final int port = address.getPort();
126 LOG.info("Port number of the node {} is: {}", node, port);
127 return new PortNumber(Uint16.valueOf(port));
131 * Retrieve switch features from connection.
133 * @param connectionContext connection context
134 * @return switch features
136 public static SwitchFeatures getSwitchFeatures(final ConnectionContext connectionContext) {
137 return SwitchFeaturesUtil.buildSwitchFeatures(new GetFeaturesOutputBuilder(connectionContext.getFeatures())
141 private static @Nullable InetSocketAddress getRemoteAddress(final ConnectionContext connectionContext,
142 final InstanceIdentifier<Node> instanceIdentifier) {
143 final ConnectionAdapter adapter = connectionContext.getConnectionAdapter();
144 final InetSocketAddress remoteAddress = adapter == null ? null : adapter.getRemoteAddress();
145 if (remoteAddress == null) {
146 LOG.warn("Remote address of the node {} cannot be obtained. No connection with switch.",
147 PathUtil.extractNodeId(instanceIdentifier));
149 return remoteAddress;