Add single layer deserialization support
[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 org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
13 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
14 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
15 import org.opendaylight.openflowplugin.api.openflow.device.TxFacade;
16 import org.opendaylight.openflowplugin.openflow.md.core.sal.SwitchFeaturesUtil;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.flow.node.SwitchFeatures;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsDataBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class DeviceInitializationUtil {
33
34     private static final Logger LOG = LoggerFactory.getLogger(DeviceInitializationUtil.class);
35
36     private DeviceInitializationUtil() {
37         // Hiding implicit constructor
38     }
39
40     /**
41      * Create specified number of empty tables on device
42      * FIXME: remove after ovs table features fix
43      * @param txFacade transaction facade
44      * @param deviceInfo device info
45      * @param nrOfTables number of tables
46      */
47     public static void makeEmptyTables(final TxFacade txFacade, final DeviceInfo deviceInfo, final Short nrOfTables) {
48         if (LOG.isDebugEnabled()) {
49             LOG.debug("About to create {} empty tables for node {}.", nrOfTables, deviceInfo.getLOGValue());
50         }
51
52         for (int i = 0; i < nrOfTables; i++) {
53             try {
54                 txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL,
55                     deviceInfo
56                         .getNodeInstanceIdentifier()
57                         .augmentation(FlowCapableNode.class)
58                         .child(Table.class, new TableKey((short) i)),
59                     new TableBuilder()
60                         .setId((short) i)
61                         .addAugmentation(
62                             FlowTableStatisticsData.class,
63                             new FlowTableStatisticsDataBuilder().build())
64                         .build());
65             } catch (final Exception e) {
66                 LOG.debug("makeEmptyTables: Failed to write node {} to DS ", deviceInfo.getLOGValue(), e);
67             }
68         }
69     }
70
71     /**
72      * Retrieve ip address from connection
73      * @param connectionContext connection context
74      * @param instanceIdentifier instance identifier
75      * @return ip adress
76      */
77     public static IpAddress getIpAddress(final ConnectionContext connectionContext,
78                                          final InstanceIdentifier<Node> instanceIdentifier) {
79         final InetSocketAddress remoteAddress = connectionContext
80             .getConnectionAdapter()
81             .getRemoteAddress();
82
83         if (remoteAddress == null) {
84             LOG.warn("IP address of the node {} cannot be obtained. No connection with switch.", instanceIdentifier);
85             return null;
86         }
87
88         LOG.info("IP address of the node {} is: {}", instanceIdentifier, remoteAddress);
89         return IetfInetUtil.INSTANCE.ipAddressFor(remoteAddress.getAddress());
90     }
91
92     /**
93      * Retrieve switch features from connection
94      * @param connectionContext connection context
95      * @return switch features
96      */
97     public static SwitchFeatures getSwitchFeatures(final ConnectionContext connectionContext) {
98         return SwitchFeaturesUtil
99             .getInstance()
100             .buildSwitchFeatures(new GetFeaturesOutputBuilder(connectionContext
101                 .getFeatures())
102                 .build());
103     }
104
105 }