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
9 package org.opendaylight.openflowplugin.impl.util;
11 import java.net.InetSocketAddress;
12 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
13 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
14 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
15 import org.opendaylight.openflowplugin.api.openflow.device.TxFacade;
16 import org.opendaylight.openflowplugin.openflow.md.core.sal.SwitchFeaturesUtil;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.flow.node.SwitchFeatures;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsDataBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class DeviceInitializationUtil {
34 private static final Logger LOG = LoggerFactory.getLogger(DeviceInitializationUtil.class);
36 private DeviceInitializationUtil() {
37 // Hiding implicit constructor
41 * Create specified number of empty tables on device
42 * FIXME: remove after ovs table features fix
43 * @param txFacade transaction facade
44 * @param deviceInfo device info
45 * @param nrOfTables number of tables
47 public static void makeEmptyTables(final TxFacade txFacade, final DeviceInfo deviceInfo, final Short nrOfTables) {
48 if (LOG.isDebugEnabled()) {
49 LOG.debug("About to create {} empty tables for node {}.", nrOfTables, deviceInfo.getLOGValue());
52 for (int i = 0; i < nrOfTables; i++) {
54 txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL,
56 .getNodeInstanceIdentifier()
57 .augmentation(FlowCapableNode.class)
58 .child(Table.class, new TableKey((short) i)),
62 FlowTableStatisticsData.class,
63 new FlowTableStatisticsDataBuilder().build())
65 } catch (final Exception e) {
66 LOG.debug("makeEmptyTables: Failed to write node {} to DS ", deviceInfo.getLOGValue(), e);
72 * Retrieve ip address from connection
73 * @param connectionContext connection context
74 * @param instanceIdentifier instance identifier
77 public static IpAddress getIpAddress(final ConnectionContext connectionContext,
78 final InstanceIdentifier<Node> instanceIdentifier) {
79 final InetSocketAddress remoteAddress = connectionContext
80 .getConnectionAdapter()
83 if (remoteAddress == null) {
84 LOG.warn("IP address of the node {} cannot be obtained. No connection with switch.", instanceIdentifier);
88 LOG.info("IP address of the node {} is: {}", instanceIdentifier, remoteAddress);
89 return IetfInetUtil.INSTANCE.ipAddressFor(remoteAddress.getAddress());
93 * Retrieve switch features from connection
94 * @param connectionContext connection context
95 * @return switch features
97 public static SwitchFeatures getSwitchFeatures(final ConnectionContext connectionContext) {
98 return SwitchFeaturesUtil
100 .buildSwitchFeatures(new GetFeaturesOutputBuilder(connectionContext