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