blob: bac9f635fabc70ef784346be3b34dae2605a3f80 [file] [log] [blame]
Laszlo Papp8b3a5f62017-10-05 13:32:00 +01001/*
2 * Copyright 2017 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.netconf;
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.onosproject.yang.gen.v1.opticalswitch.rev20170804.opticalswitch.CrossConnects;
37import org.onosproject.yang.gen.v1.opticalswitch.rev20170804.opticalswitch.crossconnects.Pair;
38import org.onosproject.yang.gen.v1.opticalswitch.rev20170804.opticalswitch.crossconnects.DefaultPair;
39import org.onosproject.yang.gen.v1.opticalswitch.rev20170804.opticalswitch.DefaultCrossConnects;
40import org.onosproject.yang.gen.v1.opticalswitch.rev20170804.opticalswitch.PortFormat;
41
42import java.util.List;
43import java.util.Set;
44
45/**
46 * Polatis optical utilities.
47 */
48public final class PolatisOpticalUtility {
49
50 private static final int DEFAULT_PRIORITY = 88;
51 private static final String DEFAULT_APP = "org.onosproject.drivers.polatis.netconf";
52 public static final int POWER_MULTIPLIER = 100;
53 public static final Range<Long> POWER_RANGE = Range.closed(-6000L, 2800L);
54
55 private PolatisOpticalUtility() {
56 }
57
58 /**
59 * Transforms a flow FlowRule object to a cross-connect object.
60 * @param behaviour the parent driver handler
61 * @param rule FlowRule object
62 * @return cross connect object
63 */
64 public static CrossConnects fromFlowRule(HandlerBehaviour behaviour, FlowRule rule) {
65 // TrafficSelector
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 // TrafficTreatment
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 DefaultCrossConnects crossConnects = new DefaultCrossConnects();
80 DefaultPair p = new DefaultPair();
81 p.ingress(new PortFormat(inPort.toLong()));
82 p.egress(new PortFormat(outPort.toLong()));
83 crossConnects.addToPair(p);
84 return crossConnects;
85 }
86
87 /**
88 * Finds the FlowRule from flow rule store by the given cross connect information.
89 * Returns an extra flow to remove the flow by ONOS if not found.
90 * @param behaviour the parent driver handler
91 * @param cfg cross connect information
92 * @return the flow rule
93 */
94 public static FlowRule toFlowRule(HandlerBehaviour behaviour, CrossConnects cfg) {
95 // Note: do we need to handle more than one pair? In any case, this
96 // looks strange.
97 Pair p = cfg.pair().get(0);
98 long i = p.ingress().uint32();
99 long o = p.egress().uint32();
100 PortNumber iPortNumber = PortNumber.portNumber(i);
101 PortNumber oPortNumber = PortNumber.portNumber(o);
102 return toFlowRule(behaviour, iPortNumber, oPortNumber);
103 }
104
105 /**
106 * Finds the FlowRule from flow rule store by the given ports and channel.
107 * Returns an extra flow to remove the flow by ONOS if not found.
108 * @param behaviour the parent driver handler
109 * @param inPort the input port
110 * @param outPort the output port
111 * @return the flow rule
112 */
113 public static FlowRule toFlowRule(HandlerBehaviour behaviour, PortNumber inPort,
114 PortNumber outPort) {
115 FlowRuleService service = behaviour.handler().get(FlowRuleService.class);
116 Iterable<FlowEntry> entries = service.getFlowEntries(behaviour.data().deviceId());
117 // Try to Find the flow from flow rule store.
118 for (FlowEntry entry : entries) {
119 Set<Criterion> criterions = entry.selector().criteria();
120 // input port
121 PortNumber ip = criterions.stream()
122 .filter(c -> c instanceof PortCriterion)
123 .map(c -> ((PortCriterion) c).port())
124 .findAny()
125 .orElse(null);
126 // output port
127 PortNumber op = entry.treatment().immediate().stream()
128 .filter(c -> c instanceof Instructions.OutputInstruction)
129 .map(c -> ((Instructions.OutputInstruction) c).port())
130 .findAny()
131 .orElse(null);
132 if (inPort.equals(ip) && outPort.equals(op)) {
133 // Find the flow.
134 return entry;
135 }
136 }
137 // Cannot find the flow from store. So report an extra flow to remove the flow by ONOS.
138 TrafficSelector selector = DefaultTrafficSelector.builder()
139 .matchInPort(inPort)
140 .build();
141 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
142 .setOutput(outPort)
143 .build();
144 return DefaultFlowRule.builder()
145 .forDevice(behaviour.data().deviceId())
146 .withSelector(selector)
147 .withTreatment(treatment)
148 .makePermanent()
149 .withPriority(DEFAULT_PRIORITY)
150 .fromApp(behaviour.handler().get(CoreService.class).getAppId(DEFAULT_APP))
151 .build();
152
153 }
154}