Bump upstreams
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / util / DeviceInitializationUtil.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.openflowplugin.impl.util;
9
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;
39
40 public final class DeviceInitializationUtil {
41     private static final Logger LOG = LoggerFactory.getLogger(DeviceInitializationUtil.class);
42
43     private DeviceInitializationUtil() {
44         // Hiding implicit constructor
45     }
46
47     /**
48      * Merge empty nodes to operational DS to predict any problems with missing parent for node.
49      *
50      * @param dataBroker the data broker
51      */
52     public static void makeEmptyNodes(final DataBroker dataBroker) {
53         final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
54
55         try {
56             tx.merge(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class), new NodesBuilder()
57                     .build());
58             tx.commit().get();
59         } catch (ExecutionException | InterruptedException e) {
60             LOG.error("Creation of node failed.", e);
61             throw new IllegalStateException(e);
62         }
63     }
64
65     /**
66      * Create specified number of empty tables on device.
67      * FIXME: remove after ovs table features fix
68      *
69      * @param txFacade   transaction facade
70      * @param deviceInfo device info
71      * @param nrOfTables number of tables
72      */
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);
76         }
77
78         for (int i = 0; i < nrOfTables; i++) {
79             final Uint8 tableId = Uint8.valueOf(i);
80             txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL,
81                     deviceInfo
82                             .getNodeInstanceIdentifier()
83                             .augmentation(FlowCapableNode.class)
84                             .child(Table.class, new TableKey(tableId)),
85                     new TableBuilder()
86                             .setId(tableId)
87                             .addAugmentation(new FlowTableStatisticsDataBuilder().build())
88                             .build());
89         }
90     }
91
92     /**
93      * Retrieve ip address from connection.
94      *
95      * @param connectionContext  connection context
96      * @param instanceIdentifier instance identifier
97      * @return ip address
98      */
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) {
104             return null;
105         }
106         final IpAddress ipAddress = IetfInetUtil.ipAddressFor(address.getAddress());
107         LOG.info("IP address of the node {} is: {}", node, ipAddress);
108         return ipAddress;
109     }
110
111     /**
112      * Retrieve port number from connection.
113      *
114      * @param connectionContext  connection context
115      * @param instanceIdentifier instance identifier
116      * @return port number
117      */
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) {
123             return null;
124         }
125         final int port = address.getPort();
126         LOG.info("Port number of the node {} is: {}", node, port);
127         return new PortNumber(Uint16.valueOf(port));
128     }
129
130     /**
131      * Retrieve switch features from connection.
132      *
133      * @param connectionContext connection context
134      * @return switch features
135      */
136     public static SwitchFeatures getSwitchFeatures(final ConnectionContext connectionContext) {
137         return SwitchFeaturesUtil.buildSwitchFeatures(new GetFeaturesOutputBuilder(connectionContext.getFeatures())
138             .build());
139     }
140
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));
148         }
149         return remoteAddress;
150     }
151 }