Merge "Cleanup MeterMessageSerializer"
[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.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public final class DeviceInitializationUtil {
39     private static final Logger LOG = LoggerFactory.getLogger(DeviceInitializationUtil.class);
40
41     private DeviceInitializationUtil() {
42         // Hiding implicit constructor
43     }
44
45     /**
46      * Merge empty nodes to operational DS to predict any problems with missing parent for node.
47      *
48      * @param dataBroker the data broker
49      */
50     public static void makeEmptyNodes(final DataBroker dataBroker) {
51         final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
52
53         try {
54             tx.merge(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class), new NodesBuilder()
55                     .build());
56             tx.commit().get();
57         } catch (ExecutionException | InterruptedException e) {
58             LOG.error("Creation of node failed.", e);
59             throw new IllegalStateException(e);
60         }
61     }
62
63     /**
64      * Create specified number of empty tables on device.
65      * FIXME: remove after ovs table features fix
66      *
67      * @param txFacade   transaction facade
68      * @param deviceInfo device info
69      * @param nrOfTables number of tables
70      */
71     public static void makeEmptyTables(final TxFacade txFacade, final DeviceInfo deviceInfo, final short nrOfTables) {
72         if (LOG.isDebugEnabled()) {
73             LOG.debug("About to create {} empty tables for node {}.", nrOfTables, deviceInfo);
74         }
75
76         for (int i = 0; i < nrOfTables; i++) {
77             txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL,
78                     deviceInfo
79                             .getNodeInstanceIdentifier()
80                             .augmentation(FlowCapableNode.class)
81                             .child(Table.class, new TableKey((short) i)),
82                     new TableBuilder()
83                             .setId((short) i)
84                             .addAugmentation(new FlowTableStatisticsDataBuilder().build())
85                             .build());
86         }
87     }
88
89     /**
90      * Retrieve ip address from connection.
91      *
92      * @param connectionContext  connection context
93      * @param instanceIdentifier instance identifier
94      * @return ip address
95      */
96     public static IpAddress getIpAddress(final ConnectionContext connectionContext,
97                                          final InstanceIdentifier<Node> instanceIdentifier) {
98         final String node = PathUtil.extractNodeId(instanceIdentifier).getValue();
99         final InetSocketAddress address = getRemoteAddress(connectionContext, instanceIdentifier);
100         if (address == null) {
101             return null;
102         }
103         final IpAddress ipAddress = IetfInetUtil.INSTANCE.ipAddressFor(address.getAddress());
104         LOG.info("IP address of the node {} is: {}", node, ipAddress);
105         return ipAddress;
106     }
107
108     /**
109      * Retrieve port number from connection.
110      *
111      * @param connectionContext  connection context
112      * @param instanceIdentifier instance identifier
113      * @return port number
114      */
115     public static PortNumber getPortNumber(final ConnectionContext connectionContext,
116                                            final InstanceIdentifier<Node> instanceIdentifier) {
117         final String node = PathUtil.extractNodeId(instanceIdentifier).getValue();
118         final InetSocketAddress address = getRemoteAddress(connectionContext, instanceIdentifier);
119         if (address == null) {
120             return null;
121         }
122         final int port = address.getPort();
123         LOG.info("Port number of the node {} is: {}", node, port);
124         return new PortNumber(port);
125     }
126
127     /**
128      * Retrieve switch features from connection.
129      *
130      * @param connectionContext connection context
131      * @return switch features
132      */
133     public static SwitchFeatures getSwitchFeatures(final ConnectionContext connectionContext) {
134         return SwitchFeaturesUtil
135                 .getInstance()
136                 .buildSwitchFeatures(new GetFeaturesOutputBuilder(connectionContext
137                         .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 }