2 * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.vpnservice.mdsalutil;
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.LinkedBlockingQueue;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.BucketId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.Buckets;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.BucketsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInputBuilder;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
55 import com.google.common.base.Joiner;
56 import com.google.common.primitives.Bytes;
57 import com.google.common.primitives.Ints;
59 public class MDSALUtil {
61 public static final String NODE_PREFIX = "openflow";
62 public static final String SEPARATOR = ":";
63 private static final Buckets EMPTY_Buckets = new BucketsBuilder().build();
64 private static final Instructions EMPTY_Instructions = new InstructionsBuilder().setInstruction(
65 new ArrayList<Instruction>()).build();
66 private static final Match EMPTY_Matches = new MatchBuilder().build();
68 public static FlowEntity buildFlowEntity(BigInteger dpnId, short tableId, String flowId, int priority, String flowName,
69 int idleTimeOut, int hardTimeOut, BigInteger cookie, List<MatchInfo> listMatchInfo,
70 List<InstructionInfo> listInstructionInfo) {
72 FlowEntity flowEntity = new FlowEntity(dpnId);
74 flowEntity.setTableId(tableId);
75 flowEntity.setFlowId(flowId);
76 flowEntity.setPriority(priority);
77 flowEntity.setFlowName(flowName);
78 flowEntity.setIdleTimeOut(idleTimeOut);
79 flowEntity.setHardTimeOut(hardTimeOut);
80 flowEntity.setCookie(cookie);
81 flowEntity.setMatchInfoList(listMatchInfo);
82 flowEntity.setInstructionInfoList(listInstructionInfo);
87 // TODO: CHECK IF THIS IS USED
88 public static Flow buildFlow(short tableId, String flowId, int priority, String flowName, int idleTimeOut,
89 int hardTimeOut, BigInteger cookie, List<MatchInfo> listMatchInfo, List<InstructionInfo> listInstructionInfo) {
90 return MDSALUtil.buildFlow(tableId, flowId, priority, flowName, idleTimeOut, hardTimeOut, cookie,
91 listMatchInfo, listInstructionInfo, true);
94 public static Flow buildFlow(short tableId, String flowId, int priority, String flowName, int idleTimeOut,
95 int hardTimeOut, BigInteger cookie, List<MatchInfo> listMatchInfo,
96 List<InstructionInfo> listInstructionInfo, boolean isStrict) {
97 FlowKey key = new FlowKey(new FlowId(flowId));
98 return new FlowBuilder().setMatch(buildMatches(listMatchInfo)).setKey(key)
99 .setPriority(Integer.valueOf(priority)).setInstructions(buildInstructions(listInstructionInfo))
100 .setBarrier(false).setInstallHw(true).setHardTimeout(hardTimeOut).setIdleTimeout(idleTimeOut)
101 .setFlowName(flowName).setTableId(Short.valueOf(tableId)).setStrict(isStrict)
102 .setCookie(new FlowCookie(cookie)).build();
105 public static GroupEntity buildGroupEntity(BigInteger dpnId, long groupId, String groupName, GroupTypes groupType,
106 List<BucketInfo> listBucketInfo) {
108 GroupEntity groupEntity = new GroupEntity(dpnId);
110 groupEntity.setGroupId(groupId);
111 groupEntity.setGroupName(groupName);
112 groupEntity.setGroupType(groupType);
113 groupEntity.setBucketInfoList(listBucketInfo);
118 public static TransmitPacketInput getPacketOutDefault(List<ActionInfo> actionInfos, byte[] payload, BigInteger dpnId) {
119 return new TransmitPacketInputBuilder()
120 .setAction(buildActions(actionInfos))
123 new NodeRef(InstanceIdentifier.builder(Nodes.class)
124 .child(Node.class, new NodeKey(new NodeId("openflow:" + dpnId))).toInstance()))
125 .setIngress(getDefaultNodeConnRef(dpnId)).setEgress(getDefaultNodeConnRef(dpnId)).build();
128 public static TransmitPacketInput getPacketOut(List<ActionInfo> actionInfos, byte[] payload, long dpnId,
129 NodeConnectorRef ingress) {
130 return new TransmitPacketInputBuilder()
131 .setAction(buildActions(actionInfos))
134 new NodeRef(InstanceIdentifier.builder(Nodes.class)
135 .child(Node.class, new NodeKey(new NodeId("openflow:" + dpnId))).toInstance()))
136 .setIngress(ingress).setEgress(ingress).build();
139 private static List<Action> buildActions(List<ActionInfo> actions) {
140 List<Action> actionsList = new ArrayList<Action>();
141 for (ActionInfo actionInfo : actions) {
142 actionsList.add(actionInfo.buildAction());
147 public static String longToIp(long ip, long mask) {
148 StringBuilder sb = new StringBuilder(15);
149 Joiner joiner = Joiner.on('.');
151 joiner.appendTo(sb, Bytes.asList(Ints.toByteArray((int) ip)));
153 sb.append("/" + mask);
155 return sb.toString();
158 protected static Buckets buildBuckets(List<BucketInfo> listBucketInfo) {
160 if (listBucketInfo != null) {
161 BucketsBuilder bucketsBuilder = new BucketsBuilder();
162 List<Bucket> bucketList = new ArrayList<Bucket>();
164 for (BucketInfo bucketInfo : listBucketInfo) {
165 BucketBuilder bucketBuilder = new BucketBuilder();
166 List<Action> actionsList = new ArrayList<Action>();
168 bucketInfo.buildAndAddActions(actionsList);
169 bucketBuilder.setAction(actionsList);
170 bucketBuilder.setWeight(bucketInfo.getWeight());
171 bucketBuilder.setBucketId(new BucketId(i++));
172 bucketBuilder.setWeight(bucketInfo.getWeight()).setWatchPort(bucketInfo.getWatchPort())
173 .setWatchGroup(bucketInfo.getWatchGroup());
174 bucketList.add(bucketBuilder.build());
177 bucketsBuilder.setBucket(bucketList);
178 return bucketsBuilder.build();
181 return EMPTY_Buckets;
184 protected static Instructions buildInstructions(List<InstructionInfo> listInstructionInfo) {
185 if (listInstructionInfo != null) {
186 List<Instruction> instructions = new ArrayList<Instruction>();
187 int instructionKey = 0;
189 for (InstructionInfo instructionInfo : listInstructionInfo) {
190 instructions.add(instructionInfo.buildInstruction(instructionKey));
194 return new InstructionsBuilder().setInstruction(instructions).build();
197 return EMPTY_Instructions;
200 protected static Match buildMatches(List<MatchInfo> listMatchInfo) {
201 if (listMatchInfo != null) {
202 MatchBuilder matchBuilder = new MatchBuilder();
203 Map<Class<?>, Object> mapMatchBuilder = new HashMap<Class<?>, Object>();
205 for (MatchInfo matchInfo : listMatchInfo) {
206 matchInfo.createInnerMatchBuilder(mapMatchBuilder);
209 for (MatchInfo matchInfo : listMatchInfo) {
210 matchInfo.setMatch(matchBuilder, mapMatchBuilder);
213 return matchBuilder.build();
216 return EMPTY_Matches;
219 // TODO: Check the port const
220 public static NodeConnectorRef getDefaultNodeConnRef(BigInteger nDpId) {
221 return getNodeConnRef(NODE_PREFIX + SEPARATOR + nDpId, "0xfffffffd");
224 public static NodeConnectorRef getNodeConnRef(BigInteger nDpId, String port) {
225 return getNodeConnRef(NODE_PREFIX + SEPARATOR + nDpId, port);
228 public static NodeConnectorRef getNodeConnRef(String sNodeId, String port) {
229 String sNodeConnectorKey;
233 NodeConnectorId nodeConnectorId;
234 NodeConnectorKey nodeConnectorKey;
235 InstanceIdentifierBuilder<Nodes> nodesInstanceIdentifierBuilder;
236 InstanceIdentifierBuilder<Node> nodeInstanceIdentifierBuilder;
237 InstanceIdentifierBuilder<NodeConnector> nodeConnectorInstanceIdentifierBuilder;
238 InstanceIdentifier<NodeConnector> nodeConnectorInstanceIdentifier;
239 NodeConnectorRef nodeConnectorRef;
241 sbTmp = new StringBuilder();
243 sbTmp.append(sNodeId);
244 sbTmp.append(SEPARATOR);
247 sNodeConnectorKey = sbTmp.toString();
248 nodeConnectorId = new NodeConnectorId(sNodeConnectorKey);
249 nodeConnectorKey = new NodeConnectorKey(nodeConnectorId);
251 nodeId = new NodeId(sNodeId);
252 nodeKey = new NodeKey(nodeId);
254 nodesInstanceIdentifierBuilder = InstanceIdentifier.<Nodes> builder(Nodes.class);
255 nodeInstanceIdentifierBuilder = nodesInstanceIdentifierBuilder.<Node, NodeKey> child(Node.class, nodeKey);
256 nodeConnectorInstanceIdentifierBuilder = nodeInstanceIdentifierBuilder.<NodeConnector, NodeConnectorKey> child(
257 NodeConnector.class, nodeConnectorKey);
258 nodeConnectorInstanceIdentifier = nodeConnectorInstanceIdentifierBuilder.toInstance();
259 nodeConnectorRef = new NodeConnectorRef(nodeConnectorInstanceIdentifier);
260 return nodeConnectorRef;
263 public static BigInteger getDpnIdFromNodeName(NodeId nodeId) {
264 return getDpnIdFromNodeName(nodeId.getValue());
267 public static BigInteger getDpnIdFromNodeName(String sMdsalNodeName) {
268 String sDpId = sMdsalNodeName.substring(sMdsalNodeName.lastIndexOf(":") + 1);
269 return new BigInteger(sDpId);
272 public static long getOfPortNumberFromPortName(NodeConnectorId nodeConnectorId) {
273 return getOfPortNumberFromPortName(nodeConnectorId.getValue());
276 public static long getOfPortNumberFromPortName(String sMdsalPortName) {
277 String sPortNumber = sMdsalPortName.substring(sMdsalPortName.lastIndexOf(":") + 1);
278 return Long.parseLong(sPortNumber);
281 public static TransmitPacketInput getPacketOut(List<ActionInfo> actionInfos, byte[] payload, BigInteger dpnId,
282 NodeConnectorRef nodeConnRef) {
283 // TODO Auto-generated method stub