edcaa4decb601e8c0911d423a9c18e8915f60506
[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.rev100924.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 class BulkOMaticUtils {
32
33     public final static int DEFUALT_STATUS = FlowCounter.OperationStatus.INIT.status();
34     public final static int DEFAULT_FLOW_COUNT = 0;
35     public final static long DEFAULT_COMPLETION_TIME = 0;
36     public final static String DEFAULT_UNITS = "ns";
37     public final static String DEVICE_TYPE_PREFIX = "openflow:";
38
39     public static String ipIntToStr (int k) {
40         return new StringBuilder().append(((k >> 24) & 0xFF)).append(".")
41                 .append(((k >> 16) & 0xFF)).append(".")
42                 .append(((k >> 8) & 0xFF)).append(".")
43                 .append((k & 0xFF)).append("/32").toString();
44     }
45
46     public static Match getMatch(final Integer sourceIp){
47         Ipv4Match ipv4Match = new Ipv4MatchBuilder().setIpv4Source(
48                 new Ipv4Prefix(ipIntToStr(sourceIp))).build();
49         MatchBuilder matchBuilder = new MatchBuilder();
50         matchBuilder.setLayer3Match(ipv4Match);
51         EthernetTypeBuilder ethTypeBuilder = new EthernetTypeBuilder();
52         EthernetMatchBuilder ethMatchBuilder = new EthernetMatchBuilder();
53         ethTypeBuilder.setType(new EtherType(2048L));
54         ethMatchBuilder.setEthernetType(ethTypeBuilder.build());
55         matchBuilder.setEthernetMatch(ethMatchBuilder.build());
56         return matchBuilder.build();
57     }
58
59     public static Flow buildFlow(Short tableId, String flowId, Match match){
60         FlowBuilder flowBuilder = new FlowBuilder();
61         flowBuilder.setKey(new FlowKey(new FlowId(flowId)));
62         flowBuilder.setTableId(tableId);
63         flowBuilder.setMatch(match);
64         return flowBuilder.build();
65     }
66
67     public static InstanceIdentifier<Flow> getFlowInstanceIdentifier(Short tableId, String flowId, String dpId) {
68         return InstanceIdentifier.create(Nodes.class).child(Node.class,
69                 new NodeKey(new NodeId(dpId)))
70                 .augmentation(FlowCapableNode.class)
71                 .child(Table.class, new TableKey(tableId))
72                 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow.class,
73                         new FlowKey(new FlowId(flowId)));
74     }
75
76     public static InstanceIdentifier<Node> getFlowCapableNodeId(String dpId){
77         return InstanceIdentifier.builder(Nodes.class)
78                 .child(Node.class, new NodeKey(new NodeId(dpId)))
79                 .build();
80     }
81
82     public static InstanceIdentifier<Table> getTableId(Short tableId, String dpId) {
83         return InstanceIdentifier.builder(Nodes.class)
84                 .child(Node.class, new NodeKey(new NodeId(dpId)))
85                 .augmentation(FlowCapableNode.class)
86                 .child(Table.class, new TableKey(tableId))
87                 .build();
88
89     }
90
91     public static InstanceIdentifier<Flow> getFlowId(final InstanceIdentifier<Table> tablePath, final String flowId) {
92         return tablePath.child(Flow.class, new FlowKey(new FlowId(flowId)));
93     }
94 }