80303a02d79135802a5c297991d2a286fe2d028f
[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
9 package org.opendaylight.openflowplugin.impl.util;
10
11 import java.net.InetSocketAddress;
12 import java.util.Collections;
13 import java.util.Optional;
14 import java.util.concurrent.ExecutionException;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
20 import org.opendaylight.openflowplugin.api.openflow.device.TxFacade;
21 import org.opendaylight.openflowplugin.openflow.md.core.sal.SwitchFeaturesUtil;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.flow.node.SwitchFeatures;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsDataBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public 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                     .setNode(Collections.emptyList())
58                     .build());
59             tx.submit().get();
60         } catch (ExecutionException | InterruptedException e) {
61             LOG.error("Creation of node failed.", e);
62             throw new IllegalStateException(e);
63         }
64     }
65
66     /**
67      * Create specified number of empty tables on device.
68      * FIXME: remove after ovs table features fix
69      *
70      * @param txFacade   transaction facade
71      * @param deviceInfo device info
72      * @param nrOfTables number of tables
73      */
74     public static void makeEmptyTables(final TxFacade txFacade, final DeviceInfo deviceInfo, final short nrOfTables) {
75         if (LOG.isDebugEnabled()) {
76             LOG.debug("About to create {} empty tables for node {}.", nrOfTables, deviceInfo.getLOGValue());
77         }
78
79         for (int i = 0; i < nrOfTables; i++) {
80             txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL,
81                     deviceInfo
82                             .getNodeInstanceIdentifier()
83                             .augmentation(FlowCapableNode.class)
84                             .child(Table.class, new TableKey((short) i)),
85                     new TableBuilder()
86                             .setId((short) i)
87                             .addAugmentation(
88                                     FlowTableStatisticsData.class,
89                                     new FlowTableStatisticsDataBuilder().build())
90                             .build());
91         }
92     }
93
94     /**
95      * Retrieve ip address from connection.
96      *
97      * @param connectionContext  connection context
98      * @param instanceIdentifier instance identifier
99      * @return ip address
100      */
101     public static IpAddress getIpAddress(final ConnectionContext connectionContext,
102                                          final InstanceIdentifier<Node> instanceIdentifier) {
103         final String node = PathUtil.extractNodeId(instanceIdentifier).getValue();
104
105         return getRemoteAddress(connectionContext, instanceIdentifier)
106                 .map(inetSocketAddress -> {
107                     final IpAddress ipAddress = IetfInetUtil.INSTANCE.ipAddressFor(inetSocketAddress.getAddress());
108                     LOG.info("IP address of the node {} is: {}", node, ipAddress);
109                     return ipAddress;
110                 })
111                 .orElse(null);
112     }
113
114     /**
115      * Retrieve port number from connection.
116      *
117      * @param connectionContext  connection context
118      * @param instanceIdentifier instance identifier
119      * @return port number
120      */
121     public static PortNumber getPortNumber(final ConnectionContext connectionContext,
122                                            final InstanceIdentifier<Node> instanceIdentifier) {
123         final String node = PathUtil.extractNodeId(instanceIdentifier).getValue();
124
125         return getRemoteAddress(connectionContext, instanceIdentifier)
126                 .map(inetSocketAddress -> {
127                     final int port = inetSocketAddress.getPort();
128                     LOG.info("Port number of the node {} is: {}", node, port);
129                     return new PortNumber(port);
130                 })
131                 .orElse(null);
132
133     }
134
135     /**
136      * Retrieve switch features from connection.
137      *
138      * @param connectionContext connection context
139      * @return switch features
140      */
141     public static SwitchFeatures getSwitchFeatures(final ConnectionContext connectionContext) {
142         return SwitchFeaturesUtil
143                 .getInstance()
144                 .buildSwitchFeatures(new GetFeaturesOutputBuilder(connectionContext
145                         .getFeatures())
146                         .build());
147     }
148
149     private static Optional<InetSocketAddress> getRemoteAddress(final ConnectionContext connectionContext,
150                                                                 final InstanceIdentifier<Node> instanceIdentifier) {
151         final Optional<InetSocketAddress> inetSocketAddress = Optional
152                 .ofNullable(connectionContext.getConnectionAdapter())
153                 .flatMap(connectionAdapter -> Optional.ofNullable(connectionAdapter.getRemoteAddress()));
154
155         if (!inetSocketAddress.isPresent()) {
156             LOG.warn("Remote address of the node {} cannot be obtained. No connection with switch.", PathUtil
157                     .extractNodeId(instanceIdentifier));
158         }
159
160         return inetSocketAddress;
161     }
162
163 }