Merge "Fix uint warnings in sample-bundles"
[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.Optional;
12 import java.util.concurrent.ExecutionException;
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.openflowplugin.api.openflow.connection.ConnectionContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
18 import org.opendaylight.openflowplugin.api.openflow.device.TxFacade;
19 import org.opendaylight.openflowplugin.impl.device.SwitchFeaturesUtil;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.flow.node.SwitchFeatures;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsDataBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodesBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public final class DeviceInitializationUtil {
38     private static final Logger LOG = LoggerFactory.getLogger(DeviceInitializationUtil.class);
39
40     private DeviceInitializationUtil() {
41         // Hiding implicit constructor
42     }
43
44     /**
45      * Merge empty nodes to operational DS to predict any problems with missing parent for node.
46      *
47      * @param dataBroker the data broker
48      */
49     public static void makeEmptyNodes(final DataBroker dataBroker) {
50         final WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
51
52         try {
53             tx.merge(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class), new NodesBuilder()
54                     .build());
55             tx.commit().get();
56         } catch (ExecutionException | InterruptedException e) {
57             LOG.error("Creation of node failed.", e);
58             throw new IllegalStateException(e);
59         }
60     }
61
62     /**
63      * Create specified number of empty tables on device.
64      * FIXME: remove after ovs table features fix
65      *
66      * @param txFacade   transaction facade
67      * @param deviceInfo device info
68      * @param nrOfTables number of tables
69      */
70     public static void makeEmptyTables(final TxFacade txFacade, final DeviceInfo deviceInfo, final short nrOfTables) {
71         if (LOG.isDebugEnabled()) {
72             LOG.debug("About to create {} empty tables for node {}.", nrOfTables, deviceInfo);
73         }
74
75         for (int i = 0; i < nrOfTables; i++) {
76             txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL,
77                     deviceInfo
78                             .getNodeInstanceIdentifier()
79                             .augmentation(FlowCapableNode.class)
80                             .child(Table.class, new TableKey((short) i)),
81                     new TableBuilder()
82                             .setId((short) i)
83                             .addAugmentation(new FlowTableStatisticsDataBuilder().build())
84                             .build());
85         }
86     }
87
88     /**
89      * Retrieve ip address from connection.
90      *
91      * @param connectionContext  connection context
92      * @param instanceIdentifier instance identifier
93      * @return ip address
94      */
95     public static IpAddress getIpAddress(final ConnectionContext connectionContext,
96                                          final InstanceIdentifier<Node> instanceIdentifier) {
97         final String node = PathUtil.extractNodeId(instanceIdentifier).getValue();
98
99         return getRemoteAddress(connectionContext, instanceIdentifier)
100                 .map(inetSocketAddress -> {
101                     final IpAddress ipAddress = IetfInetUtil.INSTANCE.ipAddressFor(inetSocketAddress.getAddress());
102                     LOG.info("IP address of the node {} is: {}", node, ipAddress);
103                     return ipAddress;
104                 })
105                 .orElse(null);
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
119         return getRemoteAddress(connectionContext, instanceIdentifier)
120                 .map(inetSocketAddress -> {
121                     final int port = inetSocketAddress.getPort();
122                     LOG.info("Port number of the node {} is: {}", node, port);
123                     return new PortNumber(port);
124                 })
125                 .orElse(null);
126
127     }
128
129     /**
130      * Retrieve switch features from connection.
131      *
132      * @param connectionContext connection context
133      * @return switch features
134      */
135     public static SwitchFeatures getSwitchFeatures(final ConnectionContext connectionContext) {
136         return SwitchFeaturesUtil
137                 .getInstance()
138                 .buildSwitchFeatures(new GetFeaturesOutputBuilder(connectionContext
139                         .getFeatures())
140                         .build());
141     }
142
143     private static Optional<InetSocketAddress> getRemoteAddress(final ConnectionContext connectionContext,
144                                                                 final InstanceIdentifier<Node> instanceIdentifier) {
145         final Optional<InetSocketAddress> inetSocketAddress = Optional
146                 .ofNullable(connectionContext.getConnectionAdapter())
147                 .flatMap(connectionAdapter -> Optional.ofNullable(connectionAdapter.getRemoteAddress()));
148
149         if (!inetSocketAddress.isPresent()) {
150             LOG.warn("Remote address of the node {} cannot be obtained. No connection with switch.", PathUtil
151                     .extractNodeId(instanceIdentifier));
152         }
153
154         return inetSocketAddress;
155     }
156
157 }