Added copyright and updated appropriate log levels
[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.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;
54
55 import com.google.common.base.Joiner;
56 import com.google.common.primitives.Bytes;
57 import com.google.common.primitives.Ints;
58
59 public class MDSALUtil {
60
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();
67
68     public static FlowEntity buildFlowEntity(long dpnId, short tableId, String flowId, int priority, String flowName,
69             int idleTimeOut, int hardTimeOut, BigInteger cookie, List<MatchInfo> listMatchInfo,
70             List<InstructionInfo> listInstructionInfo) {
71
72         FlowEntity flowEntity = new FlowEntity(dpnId);
73
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);
83
84         return flowEntity;
85     }
86
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);
92     }
93
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();
103     }
104
105     public static GroupEntity buildGroupEntity(long dpnId, long groupId, String groupName, GroupTypes groupType,
106             List<BucketInfo> listBucketInfo) {
107
108         GroupEntity groupEntity = new GroupEntity(dpnId);
109
110         groupEntity.setGroupId(groupId);
111         groupEntity.setGroupName(groupName);
112         groupEntity.setGroupType(groupType);
113         groupEntity.setBucketInfoList(listBucketInfo);
114
115         return groupEntity;
116     }
117
118     public static TransmitPacketInput getPacketOutDefault(List<ActionInfo> actionInfos, byte[] payload, long dpnId) {
119         return new TransmitPacketInputBuilder()
120                 .setAction(buildActions(actionInfos))
121                 .setPayload(payload)
122                 .setNode(
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();
126     }
127
128     public static TransmitPacketInput getPacketOut(List<ActionInfo> actionInfos, byte[] payload, long dpnId,
129             NodeConnectorRef ingress) {
130         return new TransmitPacketInputBuilder()
131                 .setAction(buildActions(actionInfos))
132                 .setPayload(payload)
133                 .setNode(
134                         new NodeRef(InstanceIdentifier.builder(Nodes.class)
135                                 .child(Node.class, new NodeKey(new NodeId("openflow:" + dpnId))).toInstance()))
136                 .setIngress(ingress).setEgress(ingress).build();
137     }
138
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());
143         }
144         return actionsList;
145     }
146
147     public static String longToIp(long ip, long mask) {
148         StringBuilder sb = new StringBuilder(15);
149         Joiner joiner = Joiner.on('.');
150
151         joiner.appendTo(sb, Bytes.asList(Ints.toByteArray((int) ip)));
152
153         sb.append("/" + mask);
154
155         return sb.toString();
156     }
157
158     protected static Buckets buildBuckets(List<BucketInfo> listBucketInfo) {
159         long i = 0;
160         if (listBucketInfo != null) {
161             BucketsBuilder bucketsBuilder = new BucketsBuilder();
162             List<Bucket> bucketList = new ArrayList<Bucket>();
163
164             for (BucketInfo bucketInfo : listBucketInfo) {
165                 BucketBuilder bucketBuilder = new BucketBuilder();
166                 List<Action> actionsList = new ArrayList<Action>();
167
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());
175             }
176
177             bucketsBuilder.setBucket(bucketList);
178             return bucketsBuilder.build();
179         }
180
181         return EMPTY_Buckets;
182     }
183
184     protected static Instructions buildInstructions(List<InstructionInfo> listInstructionInfo) {
185         if (listInstructionInfo != null) {
186             List<Instruction> instructions = new ArrayList<Instruction>();
187             int instructionKey = 0;
188
189             for (InstructionInfo instructionInfo : listInstructionInfo) {
190                 instructions.add(instructionInfo.buildInstruction(instructionKey));
191                 instructionKey++;
192             }
193
194             return new InstructionsBuilder().setInstruction(instructions).build();
195         }
196
197         return EMPTY_Instructions;
198     }
199
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>();
204
205             for (MatchInfo matchInfo : listMatchInfo) {
206                 matchInfo.createInnerMatchBuilder(mapMatchBuilder);
207             }
208
209             for (MatchInfo matchInfo : listMatchInfo) {
210                 matchInfo.setMatch(matchBuilder, mapMatchBuilder);
211             }
212
213             return matchBuilder.build();
214         }
215
216         return EMPTY_Matches;
217     }
218
219     // TODO: Check the port const
220     public static NodeConnectorRef getDefaultNodeConnRef(long nDpId) {
221         return getNodeConnRef(NODE_PREFIX + SEPARATOR + nDpId, "0xfffffffd");
222     }
223
224     public static NodeConnectorRef getNodeConnRef(long nDpId, String port) {
225         return getNodeConnRef(NODE_PREFIX + SEPARATOR + nDpId, port);
226     }
227
228     public static NodeConnectorRef getNodeConnRef(String sNodeId, String port) {
229         String sNodeConnectorKey;
230         StringBuilder sbTmp;
231         NodeId nodeId;
232         NodeKey nodeKey;
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;
240
241         sbTmp = new StringBuilder();
242
243         sbTmp.append(sNodeId);
244         sbTmp.append(SEPARATOR);
245         sbTmp.append(port);
246
247         sNodeConnectorKey = sbTmp.toString();
248         nodeConnectorId = new NodeConnectorId(sNodeConnectorKey);
249         nodeConnectorKey = new NodeConnectorKey(nodeConnectorId);
250
251         nodeId = new NodeId(sNodeId);
252         nodeKey = new NodeKey(nodeId);
253
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;
261     }
262
263     public static long getDpnIdFromNodeName(NodeId nodeId) {
264         return getDpnIdFromNodeName(nodeId.getValue());
265     }
266
267     public static long getDpnIdFromNodeName(String sMdsalNodeName) {
268         String sDpId = sMdsalNodeName.substring(sMdsalNodeName.lastIndexOf(":") + 1);
269         return Long.parseLong(sDpId);
270     }
271
272     public static long getOfPortNumberFromPortName(NodeConnectorId nodeConnectorId) {
273         return getOfPortNumberFromPortName(nodeConnectorId.getValue());
274     }
275
276     public static long getOfPortNumberFromPortName(String sMdsalPortName) {
277         String sPortNumber = sMdsalPortName.substring(sMdsalPortName.lastIndexOf(":") + 1);
278         return Long.parseLong(sPortNumber);
279     }
280 }