blob: cffb7f85ed857eec06d38154eefdb3db5c5b9727 [file] [log] [blame]
Laszlo Papp18efc972018-06-07 17:47:58 +01001/*
2 * Copyright 2018 Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.drivers.polatis.snmp;
18
19import com.google.common.collect.Range;
20import org.onosproject.core.CoreService;
21import org.onosproject.net.PortNumber;
22import org.onosproject.net.driver.HandlerBehaviour;
23import org.onosproject.net.flow.DefaultFlowRule;
24import org.onosproject.net.flow.DefaultTrafficSelector;
25import org.onosproject.net.flow.DefaultTrafficTreatment;
26import org.onosproject.net.flow.FlowEntry;
27import org.onosproject.net.flow.FlowRule;
28import org.onosproject.net.flow.FlowRuleService;
29import org.onosproject.net.flow.TrafficSelector;
30import org.onosproject.net.flow.TrafficTreatment;
31import org.onosproject.net.flow.criteria.Criterion;
32import org.onosproject.net.flow.criteria.PortCriterion;
33import org.onosproject.net.flow.instructions.Instruction;
34import org.onosproject.net.flow.instructions.Instructions;
35
36import org.snmp4j.smi.OID;
37import org.snmp4j.smi.UnsignedInteger32;
38import org.snmp4j.smi.Variable;
39import org.snmp4j.smi.VariableBinding;
40
41import java.util.List;
42import java.util.Set;
43
44/**
45 * Polatis optical utilities.
46 */
47public final class PolatisOpticalUtility {
48
49 private static final int DEFAULT_PRIORITY = 88;
50 private static final String DEFAULT_APP = "org.onosproject.drivers.polatis.snmp";
Laszlo Papp18efc972018-06-07 17:47:58 +010051 public static final Range<Long> POWER_RANGE = Range.closed(-6000L, 2800L);
52
53 private static final String PORT_ENTRY_OID = ".1.3.6.1.4.1.26592.2.2.2.1.2";
54 private static final String PORT_PATCH_OID = PORT_ENTRY_OID + ".1.2";
55
56 private PolatisOpticalUtility() {
57 }
58
59 /**
60 * Transforms a flow FlowRule object to a variable binding.
61 * @param rule FlowRule object
62 * @param delete whether it is a delete or edit request
63 * @return variable binding
64 */
65 public static VariableBinding fromFlowRule(FlowRule rule, boolean delete) {
66 Set<Criterion> criterions = rule.selector().criteria();
67 PortNumber inPort = criterions.stream()
68 .filter(c -> c instanceof PortCriterion)
69 .map(c -> ((PortCriterion) c).port())
70 .findAny()
71 .orElse(null);
72 long input = inPort.toLong();
73 List<Instruction> instructions = rule.treatment().immediate();
74 PortNumber outPort = instructions.stream()
75 .filter(c -> c instanceof Instructions.OutputInstruction)
76 .map(c -> ((Instructions.OutputInstruction) c).port())
77 .findAny()
78 .orElse(null);
79 long output = outPort.toLong();
80 OID oid = new OID(PORT_PATCH_OID + "." + input);
81 Variable var = new UnsignedInteger32(delete ? 0 : output);
82 return new VariableBinding(oid, var);
83 }
84
85 /**
86 * Finds the FlowRule from flow rule store by the given ports and channel.
87 * Returns an extra flow to remove the flow by ONOS if not found.
88 * @param behaviour the parent driver handler
89 * @param inPort the input port
90 * @param outPort the output port
91 * @return the flow rule
92 */
93 public static FlowRule toFlowRule(HandlerBehaviour behaviour, PortNumber inPort,
94 PortNumber outPort) {
95 FlowRuleService service = behaviour.handler().get(FlowRuleService.class);
96 Iterable<FlowEntry> entries = service.getFlowEntries(behaviour.data().deviceId());
97 // Try to Find the flow from flow rule store.
98 for (FlowEntry entry : entries) {
99 Set<Criterion> criterions = entry.selector().criteria();
100 // input port
101 PortNumber ip = criterions.stream()
102 .filter(c -> c instanceof PortCriterion)
103 .map(c -> ((PortCriterion) c).port())
104 .findAny()
105 .orElse(null);
106 // output port
107 PortNumber op = entry.treatment().immediate().stream()
108 .filter(c -> c instanceof Instructions.OutputInstruction)
109 .map(c -> ((Instructions.OutputInstruction) c).port())
110 .findAny()
111 .orElse(null);
112 if (inPort.equals(ip) && outPort.equals(op)) {
113 // Find the flow.
114 return entry;
115 }
116 }
117 // Cannot find the flow from store. So report an extra flow to remove the flow by ONOS.
118 TrafficSelector selector = DefaultTrafficSelector.builder()
119 .matchInPort(inPort)
120 .build();
121 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
122 .setOutput(outPort)
123 .build();
124 return DefaultFlowRule.builder()
125 .forDevice(behaviour.data().deviceId())
126 .withSelector(selector)
127 .withTreatment(treatment)
128 .makePermanent()
129 .withPriority(DEFAULT_PRIORITY)
130 .fromApp(behaviour.handler().get(CoreService.class).getAppId(DEFAULT_APP))
131 .build();
132
133 }
134}