Open Kilda Java Documentation
OFFlowStatsConverter.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.floodlight.converter;
17 
23 import org.projectfloodlight.openflow.protocol.OFActionType;
24 import org.projectfloodlight.openflow.protocol.OFFlowModFlags;
25 import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry;
26 import org.projectfloodlight.openflow.protocol.OFInstructionType;
27 import org.projectfloodlight.openflow.protocol.action.OFAction;
28 import org.projectfloodlight.openflow.protocol.action.OFActionMeter;
29 import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
30 import org.projectfloodlight.openflow.protocol.action.OFActionPushVlan;
31 import org.projectfloodlight.openflow.protocol.action.OFActionSetField;
32 import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
33 import org.projectfloodlight.openflow.protocol.instruction.OFInstructionApplyActions;
34 import org.projectfloodlight.openflow.protocol.instruction.OFInstructionMeter;
35 import org.projectfloodlight.openflow.protocol.match.Match;
36 import org.projectfloodlight.openflow.protocol.match.MatchField;
37 import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
38 
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Objects;
42 import java.util.Optional;
43 import java.util.stream.Collectors;
44 
48 public final class OFFlowStatsConverter {
49 
53  private static final int VLAN_MASK = 0xFFF;
54 
60  public static FlowEntry toFlowEntry(final OFFlowStatsEntry entry) {
61  return FlowEntry.builder()
62  .version(entry.getVersion().toString())
63  .durationSeconds(entry.getDurationSec())
64  .durationNanoSeconds(entry.getDurationNsec())
65  .hardTimeout(entry.getHardTimeout())
66  .idleTimeout(entry.getIdleTimeout())
67  .priority(entry.getPriority())
68  .byteCount(entry.getByteCount().getValue())
69  .packetCount(entry.getPacketCount().getValue())
70  .flags(entry.getFlags().stream()
72  .toArray(String[]::new))
73  .cookie(entry.getCookie().getValue())
74  .tableId(entry.getTableId().getValue())
75  .match(buildFlowMatch(entry.getMatch()))
76  .instructions(buildFlowInstructions(entry.getInstructions()))
77  .build();
78  }
79 
80  private static FlowMatchField buildFlowMatch(final Match match) {
81  return FlowMatchField.builder()
82  .vlanVid(Optional.ofNullable(match.get(MatchField.VLAN_VID))
83  .map(value -> String.valueOf(value.getVlan() & VLAN_MASK))
84  .orElse(null))
85  .ethType(Optional.ofNullable(match.get(MatchField.ETH_TYPE))
86  .map(Objects::toString).orElse(null))
87  .ethDst(Optional.ofNullable(match.get(MatchField.ETH_DST))
88  .map(Objects::toString).orElse(null))
89  .inPort(Optional.ofNullable(match.get(MatchField.IN_PORT))
90  .map(Objects::toString).orElse(null))
91  .ipProto(Optional.ofNullable(match.get(MatchField.IP_PROTO))
92  .map(Objects::toString).orElse(null))
93  .udpDst(Optional.ofNullable(match.get(MatchField.UDP_DST))
94  .map(Objects::toString).orElse(null))
95  .udpSrc(Optional.ofNullable(match.get(MatchField.UDP_SRC))
96  .map(Objects::toString).orElse(null))
97  .build();
98  }
99 
100  private static FlowInstructions buildFlowInstructions(final List<OFInstruction> instructions) {
101  Map<OFInstructionType, OFInstruction> instructionMap = instructions
102  .stream()
103  .collect(Collectors.toMap(OFInstruction::getType, instruction -> instruction));
104 
105  FlowApplyActions applyActions = Optional.ofNullable(instructionMap.get(OFInstructionType.APPLY_ACTIONS))
106  .map(OFFlowStatsConverter::buildApplyActions)
107  .orElse(null);
108 
109  Long meter = Optional.ofNullable(instructionMap.get(OFInstructionType.METER))
110  .map(instruction -> ((OFInstructionMeter) instruction).getMeterId())
111  .orElse(null);
112 
113  return FlowInstructions.builder()
114  .applyActions(applyActions)
115  .goToMeter(meter)
116  .build();
117  }
118 
119  //TODO(Nikita C): make possible to have multiple actions with s
120  private static FlowApplyActions buildApplyActions(OFInstruction instruction) {
121  Map<OFActionType, OFAction> actions = ((OFInstructionApplyActions) instruction).getActions()
122  .stream()
123  .collect(Collectors.toMap(OFAction::getType, action -> action));
124  return FlowApplyActions.builder()
125  .meter(Optional.ofNullable(actions.get(OFActionType.METER))
126  .map(action -> String.valueOf(((OFActionMeter) action).getMeterId()))
127  .orElse(null))
128  .pushVlan(Optional.ofNullable(actions.get(OFActionType.PUSH_VLAN))
129  .map(action ->
130  String.valueOf(((OFActionPushVlan) action).getEthertype().toString()))
131  .orElse(null))
132  .flowOutput(Optional.ofNullable(actions.get(OFActionType.OUTPUT))
133  .map(action -> String.valueOf(((OFActionOutput) action).getPort().toString()))
134  .orElse(null))
135  .fieldAction(Optional.ofNullable(actions.get(OFActionType.SET_FIELD))
136  .map(OFFlowStatsConverter::buildSetField)
137  .orElse(null))
138  .build();
139  }
140 
141  private static FlowSetFieldAction buildSetField(OFAction action) {
142  OFOxm<?> setFieldAction = ((OFActionSetField) action).getField();
143  String value = setFieldAction.getValue().toString();
144 
145  if (MatchField.VLAN_VID.getName().equals(setFieldAction.getMatchField().getName())) {
146  value = String.valueOf(Long.decode(value) & VLAN_MASK);
147  }
148  return FlowSetFieldAction.builder()
149  .fieldName(setFieldAction.getMatchField().getName())
150  .fieldValue(value)
151  .build();
152  }
153 
154  private OFFlowStatsConverter() {
155  }
156 }
static FlowEntry toFlowEntry(final OFFlowStatsEntry entry)
value
Definition: nodes.py:62
Definition: FlowEntry.java:29
name
Definition: setup.py:24
def build()
Definition: plan-e.py:73