Merge "Bug 8408 - Deserialization exception in logs when NAT flows are added."
[openflowplugin.git] / applications / bulk-o-matic / src / main / java / org / opendaylight / openflowplugin / applications / bulk / o / matic / BulkOMaticUtils.java
1 /*
2  * Copyright (c) 2016 Ericsson 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.applications.bulk.o.matic;
9
10 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetTypeBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30
31 public final class BulkOMaticUtils {
32
33     public static final int DEFUALT_STATUS = FlowCounter.OperationStatus.INIT.status();
34     public static final int DEFAULT_FLOW_COUNT = 0;
35     public static final int DEFAULT_TABLE_COUNT = 0;
36     public static final long DEFAULT_COMPLETION_TIME = 0;
37     public static final String DEFAULT_UNITS = "ns";
38     public static final String DEVICE_TYPE_PREFIX = "openflow:";
39
40     private BulkOMaticUtils() {
41     }
42
43     public static String ipIntToStr (int k) {
44         return new StringBuilder().append(k >> 24 & 0xFF).append(".")
45                 .append(k >> 16 & 0xFF).append(".")
46                 .append(k >> 8 & 0xFF).append(".")
47                 .append(k & 0xFF).append("/32").toString();
48     }
49
50     public static Match getMatch(final Integer sourceIp){
51         Ipv4Match ipv4Match = new Ipv4MatchBuilder().setIpv4Source(
52                 new Ipv4Prefix(ipIntToStr(sourceIp))).build();
53         MatchBuilder matchBuilder = new MatchBuilder();
54         matchBuilder.setLayer3Match(ipv4Match);
55         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
56         EthernetMatchBuilder ethMatchBuilder = new EthernetMatchBuilder();
57         ethTypeBuilder.setType(new EtherType(2048L));
58         ethMatchBuilder.setEthernetType(ethTypeBuilder.build());
59         matchBuilder.setEthernetMatch(ethMatchBuilder.build());
60         return matchBuilder.build();
61     }
62
63     public static Flow buildFlow(Short tableId, String flowId, Match match){
64         FlowBuilder flowBuilder = new FlowBuilder();
65         flowBuilder.setKey(new FlowKey(new FlowId(flowId)));
66         flowBuilder.setTableId(tableId);
67         flowBuilder.setMatch(match);
68         return flowBuilder.build();
69     }
70
71     public static InstanceIdentifier<Flow> getFlowInstanceIdentifier(Short tableId, String flowId, String dpId) {
72         return InstanceIdentifier.create(Nodes.class).child(Node.class,
73                 new NodeKey(new NodeId(dpId)))
74                 .augmentation(FlowCapableNode.class)
75                 .child(Table.class, new TableKey(tableId))
76                 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow.class,
77                         new FlowKey(new FlowId(flowId)));
78     }
79
80     public static InstanceIdentifier<Node> getFlowCapableNodeId(String dpId){
81         return InstanceIdentifier.builder(Nodes.class)
82                 .child(Node.class, new NodeKey(new NodeId(dpId)))
83                 .build();
84     }
85
86     public static InstanceIdentifier<Table> getTableId(Short tableId, String dpId) {
87         return InstanceIdentifier.builder(Nodes.class)
88                 .child(Node.class, new NodeKey(new NodeId(dpId)))
89                 .augmentation(FlowCapableNode.class)
90                 .child(Table.class, new TableKey(tableId))
91                 .build();
92     }
93
94     public static InstanceIdentifier<Flow> getFlowId(final InstanceIdentifier<Table> tablePath, final String flowId) {
95         return tablePath.child(Flow.class, new FlowKey(new FlowId(flowId)));
96     }
97 }