Update MRI projects for Aluminium
[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.FlowTableStatisticsData;
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(
85                                     FlowTableStatisticsData.class,
86                                     new FlowTableStatisticsDataBuilder().build())
87                             .build());
88         }
89     }
90
91     /**
92      * Retrieve ip address from connection.
93      *
94      * @param connectionContext  connection context
95      * @param instanceIdentifier instance identifier
96      * @return ip address
97      */
98     public static IpAddress getIpAddress(final ConnectionContext connectionContext,
99                                          final InstanceIdentifier<Node> instanceIdentifier) {
100         final String node = PathUtil.extractNodeId(instanceIdentifier).getValue();
101
102         return getRemoteAddress(connectionContext, instanceIdentifier)
103                 .map(inetSocketAddress -> {
104                     final IpAddress ipAddress = IetfInetUtil.INSTANCE.ipAddressFor(inetSocketAddress.getAddress());
105                     LOG.info("IP address of the node {} is: {}", node, ipAddress);
106                     return ipAddress;
107                 })
108                 .orElse(null);
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
122         return getRemoteAddress(connectionContext, instanceIdentifier)
123                 .map(inetSocketAddress -> {
124                     final int port = inetSocketAddress.getPort();
125                     LOG.info("Port number of the node {} is: {}", node, port);
126                     return new PortNumber(port);
127                 })
128                 .orElse(null);
129
130     }
131
132     /**
133      * Retrieve switch features from connection.
134      *
135      * @param connectionContext connection context
136      * @return switch features
137      */
138     public static SwitchFeatures getSwitchFeatures(final ConnectionContext connectionContext) {
139         return SwitchFeaturesUtil
140                 .getInstance()
141                 .buildSwitchFeatures(new GetFeaturesOutputBuilder(connectionContext
142                         .getFeatures())
143                         .build());
144     }
145
146     private static Optional<InetSocketAddress> getRemoteAddress(final ConnectionContext connectionContext,
147                                                                 final InstanceIdentifier<Node> instanceIdentifier) {
148         final Optional<InetSocketAddress> inetSocketAddress = Optional
149                 .ofNullable(connectionContext.getConnectionAdapter())
150                 .flatMap(connectionAdapter -> Optional.ofNullable(connectionAdapter.getRemoteAddress()));
151
152         if (!inetSocketAddress.isPresent()) {
153             LOG.warn("Remote address of the node {} cannot be obtained. No connection with switch.", PathUtil
154                     .extractNodeId(instanceIdentifier));
155         }
156
157         return inetSocketAddress;
158     }
159
160 }