23b7b619ea430a87bf6b7d9c84c55b156c9fb140
[vpnservice.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / vpnservice / mdsalutil / MDSALUtil.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. 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.vpnservice.mdsalutil;
10
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.LinkedBlockingQueue;
18
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopVlanActionCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.pop.vlan.action._case.PopVlanActionBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActions;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.metadata._case.WriteMetadataBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.BucketId;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.Buckets;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.BucketsBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInputBuilder;
64 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
65 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
66
67 import com.google.common.base.Joiner;
68 import com.google.common.primitives.Bytes;
69 import com.google.common.primitives.Ints;
70
71 public class MDSALUtil {
72
73     public static final String NODE_PREFIX = "openflow";
74     public static final String SEPARATOR = ":";
75     private static final Buckets EMPTY_Buckets = new BucketsBuilder().build();
76     private static final Instructions EMPTY_Instructions = new InstructionsBuilder().setInstruction(
77             new ArrayList<Instruction>()).build();
78     private static final Match EMPTY_Matches = new MatchBuilder().build();
79
80     public static FlowEntity buildFlowEntity(BigInteger dpnId, short tableId, String flowId, int priority, String flowName,
81             int idleTimeOut, int hardTimeOut, BigInteger cookie, List<MatchInfo> listMatchInfo,
82             List<InstructionInfo> listInstructionInfo) {
83
84         FlowEntity flowEntity = new FlowEntity(dpnId);
85
86         flowEntity.setTableId(tableId);
87         flowEntity.setFlowId(flowId);
88         flowEntity.setPriority(priority);
89         flowEntity.setFlowName(flowName);
90         flowEntity.setIdleTimeOut(idleTimeOut);
91         flowEntity.setHardTimeOut(hardTimeOut);
92         flowEntity.setCookie(cookie);
93         flowEntity.setMatchInfoList(listMatchInfo);
94         flowEntity.setInstructionInfoList(listInstructionInfo);
95
96         return flowEntity;
97     }
98
99     // TODO: CHECK IF THIS IS USED
100     public static Flow buildFlow(short tableId, String flowId, int priority, String flowName, int idleTimeOut,
101             int hardTimeOut, BigInteger cookie, List<MatchInfo> listMatchInfo, List<InstructionInfo> listInstructionInfo) {
102         return MDSALUtil.buildFlow(tableId, flowId, priority, flowName, idleTimeOut, hardTimeOut, cookie,
103                 listMatchInfo, listInstructionInfo, true);
104     }
105
106     public static Flow buildFlow(short tableId, String flowId, int priority, String flowName, int idleTimeOut,
107             int hardTimeOut, BigInteger cookie, List<MatchInfo> listMatchInfo,
108             List<InstructionInfo> listInstructionInfo, boolean isStrict) {
109         FlowKey key = new FlowKey(new FlowId(flowId));
110         return new FlowBuilder().setMatch(buildMatches(listMatchInfo)).setKey(key)
111                 .setPriority(Integer.valueOf(priority)).setInstructions(buildInstructions(listInstructionInfo))
112                 .setBarrier(false).setInstallHw(true).setHardTimeout(hardTimeOut).setIdleTimeout(idleTimeOut)
113                 .setFlowName(flowName).setTableId(Short.valueOf(tableId)).setStrict(isStrict)
114                 .setCookie(new FlowCookie(cookie)).build();
115     }
116
117     public static Flow buildFlowNew(short tableId, String flowId, int priority, String flowName, int idleTimeOut,
118                                  int hardTimeOut, BigInteger cookie, List<MatchInfo> listMatchInfo, List<Instruction> listInstructionInfo) {
119         return MDSALUtil.buildFlowNew(tableId, flowId, priority, flowName, idleTimeOut, hardTimeOut, cookie,
120                 listMatchInfo, listInstructionInfo, true);
121     }
122
123     private static Flow buildFlowNew(short tableId, String flowId, int priority, String flowName, int idleTimeOut,
124                                   int hardTimeOut, BigInteger cookie, List<MatchInfo> listMatchInfo,
125                                   List<Instruction> listInstructionInfo, boolean isStrict) {
126         FlowKey key = new FlowKey(new FlowId(flowId));
127         return new FlowBuilder().setMatch(buildMatches(listMatchInfo)).setKey(key)
128                 .setPriority(Integer.valueOf(priority)).setInstructions(new InstructionsBuilder().setInstruction(listInstructionInfo).build())
129                 .setBarrier(false).setInstallHw(true).setHardTimeout(hardTimeOut).setIdleTimeout(idleTimeOut)
130                 .setFlowName(flowName).setTableId(Short.valueOf(tableId)).setStrict(isStrict)
131                 .setCookie(new FlowCookie(cookie)).build();
132     }
133
134     public static GroupEntity buildGroupEntity(BigInteger dpnId, long groupId, String groupName, GroupTypes groupType,
135             List<BucketInfo> listBucketInfo) {
136
137         GroupEntity groupEntity = new GroupEntity(dpnId);
138
139         groupEntity.setGroupId(groupId);
140         groupEntity.setGroupName(groupName);
141         groupEntity.setGroupType(groupType);
142         groupEntity.setBucketInfoList(listBucketInfo);
143
144         return groupEntity;
145     }
146
147     public static TransmitPacketInput getPacketOutDefault(List<ActionInfo> actionInfos, byte[] payload, BigInteger dpnId) {
148         return new TransmitPacketInputBuilder()
149                 .setAction(buildActions(actionInfos))
150                 .setPayload(payload)
151                 .setNode(
152                         new NodeRef(InstanceIdentifier.builder(Nodes.class)
153                                 .child(Node.class, new NodeKey(new NodeId("openflow:" + dpnId))).toInstance()))
154                 .setIngress(getDefaultNodeConnRef(dpnId)).setEgress(getDefaultNodeConnRef(dpnId)).build();
155     }
156
157     public static TransmitPacketInput getPacketOut(List<ActionInfo> actionInfos, byte[] payload, long dpnId,
158             NodeConnectorRef ingress) {
159         return new TransmitPacketInputBuilder()
160                 .setAction(buildActions(actionInfos))
161                 .setPayload(payload)
162                 .setNode(
163                         new NodeRef(InstanceIdentifier.builder(Nodes.class)
164                                 .child(Node.class, new NodeKey(new NodeId("openflow:" + dpnId))).toInstance()))
165                 .setIngress(ingress).setEgress(ingress).build();
166     }
167
168     private static List<Action> buildActions(List<ActionInfo> actions) {
169         List<Action> actionsList = new ArrayList<Action>();
170         for (ActionInfo actionInfo : actions) {
171             actionsList.add(actionInfo.buildAction());
172         }
173         return actionsList;
174     }
175
176     public static String longToIp(long ip, long mask) {
177         StringBuilder sb = new StringBuilder(15);
178         Joiner joiner = Joiner.on('.');
179
180         joiner.appendTo(sb, Bytes.asList(Ints.toByteArray((int) ip)));
181
182         sb.append("/" + mask);
183
184         return sb.toString();
185     }
186
187     protected static Buckets buildBuckets(List<BucketInfo> listBucketInfo) {
188         long i = 0;
189         if (listBucketInfo != null) {
190             BucketsBuilder bucketsBuilder = new BucketsBuilder();
191             List<Bucket> bucketList = new ArrayList<Bucket>();
192
193             for (BucketInfo bucketInfo : listBucketInfo) {
194                 BucketBuilder bucketBuilder = new BucketBuilder();
195                 List<Action> actionsList = new ArrayList<Action>();
196
197                 bucketInfo.buildAndAddActions(actionsList);
198                 bucketBuilder.setAction(actionsList);
199                 bucketBuilder.setWeight(bucketInfo.getWeight());
200                 bucketBuilder.setBucketId(new BucketId(i++));
201                 bucketBuilder.setWeight(bucketInfo.getWeight()).setWatchPort(bucketInfo.getWatchPort())
202                         .setWatchGroup(bucketInfo.getWatchGroup());
203                 bucketList.add(bucketBuilder.build());
204             }
205
206             bucketsBuilder.setBucket(bucketList);
207             return bucketsBuilder.build();
208         }
209
210         return EMPTY_Buckets;
211     }
212
213     protected static Instructions buildInstructions(List<InstructionInfo> listInstructionInfo) {
214         if (listInstructionInfo != null) {
215             List<Instruction> instructions = new ArrayList<Instruction>();
216             int instructionKey = 0;
217
218             for (InstructionInfo instructionInfo : listInstructionInfo) {
219                 instructions.add(instructionInfo.buildInstruction(instructionKey));
220                 instructionKey++;
221             }
222
223             return new InstructionsBuilder().setInstruction(instructions).build();
224         }
225
226         return EMPTY_Instructions;
227     }
228
229     protected static Match buildMatches(List<MatchInfo> listMatchInfo) {
230         if (listMatchInfo != null) {
231             MatchBuilder matchBuilder = new MatchBuilder();
232             Map<Class<?>, Object> mapMatchBuilder = new HashMap<Class<?>, Object>();
233
234             for (MatchInfo matchInfo : listMatchInfo) {
235                 matchInfo.createInnerMatchBuilder(mapMatchBuilder);
236             }
237
238             for (MatchInfo matchInfo : listMatchInfo) {
239                 matchInfo.setMatch(matchBuilder, mapMatchBuilder);
240             }
241
242             return matchBuilder.build();
243         }
244
245         return EMPTY_Matches;
246     }
247
248     // TODO: Check the port const
249     public static NodeConnectorRef getDefaultNodeConnRef(BigInteger nDpId) {
250         return getNodeConnRef(NODE_PREFIX + SEPARATOR + nDpId, "0xfffffffd");
251     }
252
253     public static NodeConnectorRef getNodeConnRef(BigInteger nDpId, String port) {
254         return getNodeConnRef(NODE_PREFIX + SEPARATOR + nDpId, port);
255     }
256
257     public static NodeConnectorRef getNodeConnRef(String sNodeId, String port) {
258         String sNodeConnectorKey;
259         StringBuilder sbTmp;
260         NodeId nodeId;
261         NodeKey nodeKey;
262         NodeConnectorId nodeConnectorId;
263         NodeConnectorKey nodeConnectorKey;
264         InstanceIdentifierBuilder<Nodes> nodesInstanceIdentifierBuilder;
265         InstanceIdentifierBuilder<Node> nodeInstanceIdentifierBuilder;
266         InstanceIdentifierBuilder<NodeConnector> nodeConnectorInstanceIdentifierBuilder;
267         InstanceIdentifier<NodeConnector> nodeConnectorInstanceIdentifier;
268         NodeConnectorRef nodeConnectorRef;
269
270         sbTmp = new StringBuilder();
271
272         sbTmp.append(sNodeId);
273         sbTmp.append(SEPARATOR);
274         sbTmp.append(port);
275
276         sNodeConnectorKey = sbTmp.toString();
277         nodeConnectorId = new NodeConnectorId(sNodeConnectorKey);
278         nodeConnectorKey = new NodeConnectorKey(nodeConnectorId);
279
280         nodeId = new NodeId(sNodeId);
281         nodeKey = new NodeKey(nodeId);
282
283         nodesInstanceIdentifierBuilder = InstanceIdentifier.<Nodes> builder(Nodes.class);
284         nodeInstanceIdentifierBuilder = nodesInstanceIdentifierBuilder.<Node, NodeKey> child(Node.class, nodeKey);
285         nodeConnectorInstanceIdentifierBuilder = nodeInstanceIdentifierBuilder.<NodeConnector, NodeConnectorKey> child(
286                 NodeConnector.class, nodeConnectorKey);
287         nodeConnectorInstanceIdentifier = nodeConnectorInstanceIdentifierBuilder.toInstance();
288         nodeConnectorRef = new NodeConnectorRef(nodeConnectorInstanceIdentifier);
289         return nodeConnectorRef;
290     }
291
292     public static BigInteger getDpnIdFromNodeName(NodeId nodeId) {
293         return getDpnIdFromNodeName(nodeId.getValue());
294     }
295
296     public static BigInteger getDpnIdFromNodeName(String sMdsalNodeName) {
297         String sDpId = sMdsalNodeName.substring(sMdsalNodeName.lastIndexOf(":") + 1);
298         return new BigInteger(sDpId);
299     }
300
301     public static long getOfPortNumberFromPortName(NodeConnectorId nodeConnectorId) {
302         return getOfPortNumberFromPortName(nodeConnectorId.getValue());
303     }
304
305     public static long getOfPortNumberFromPortName(String sMdsalPortName) {
306         String sPortNumber = sMdsalPortName.substring(sMdsalPortName.lastIndexOf(":") + 1);
307         return Long.parseLong(sPortNumber);
308     }
309
310     public static TransmitPacketInput getPacketOut(List<ActionInfo> actionInfos, byte[] payload, BigInteger dpnId,
311                     NodeConnectorRef nodeConnRef) {
312         // TODO Auto-generated method stub
313         return null;
314     }
315
316     public static Instruction buildAndGetPopVlanActionInstruction(int actionKey, int instructionKey) {
317         Action popVlanAction = new ActionBuilder().setAction(
318                 new PopVlanActionCaseBuilder().setPopVlanAction(new PopVlanActionBuilder().build()).build())
319                 .setKey(new ActionKey(actionKey)).build();
320         List<Action> listAction = new ArrayList<Action> ();
321         listAction.add(popVlanAction);
322         ApplyActions applyActions = new ApplyActionsBuilder().setAction(listAction).build();
323         ApplyActionsCase applyActionsCase = new ApplyActionsCaseBuilder().setApplyActions(applyActions).build();
324         InstructionBuilder instructionBuilder = new InstructionBuilder();
325
326         instructionBuilder.setInstruction(applyActionsCase);
327         instructionBuilder.setKey(new InstructionKey(instructionKey));
328         return instructionBuilder.build();
329     }
330
331     public static Instruction buildAndGetWriteMetadaInstruction(BigInteger metadata,
332                                                                 BigInteger mask, int instructionKey) {
333         return new InstructionBuilder()
334                 .setInstruction(
335                         new WriteMetadataCaseBuilder().setWriteMetadata(
336                                 new WriteMetadataBuilder().setMetadata(metadata).setMetadataMask(mask).build())
337                                 .build()).setKey(new InstructionKey(instructionKey)).build();
338     }
339 }