Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.drivers.polatis.netconf; |
| 18 | |
Laszlo Papp | bd0779f | 2018-03-28 18:08:51 +0100 | [diff] [blame] | 19 | import org.onosproject.yang.gen.v1.opticalswitch.rev20180322.opticalswitch.CrossConnects; |
| 20 | import org.onosproject.yang.gen.v1.opticalswitch.rev20180322.opticalswitch.crossconnects.Pair; |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 21 | |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 22 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 23 | import org.onosproject.net.flow.FlowEntry; |
| 24 | import org.onosproject.net.flow.FlowRule; |
| 25 | import org.onosproject.net.flow.FlowRuleProgrammable; |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 26 | import org.slf4j.Logger; |
| 27 | |
| 28 | import java.util.Collection; |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 29 | import java.util.List; |
| 30 | import java.util.stream.Collectors; |
| 31 | |
Michael Enrico | 584eebd | 2021-07-22 08:04:13 +0000 | [diff] [blame^] | 32 | import static org.onosproject.drivers.polatis.netconf.PolatisUtility.parseKeyPairCompat; |
| 33 | import static org.onosproject.drivers.polatis.netconf.PolatisUtility.parseConnections; |
| 34 | import static org.onosproject.drivers.polatis.netconf.PolatisUtility.KEY_SRC; |
| 35 | import static org.onosproject.drivers.polatis.netconf.PolatisUtility.KEY_DST; |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 36 | |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 37 | import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.netconfEditConfig; |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 38 | import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.xmlOpen; |
| 39 | import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.xmlClose; |
| 40 | import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_CONNS; |
| 41 | import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_CONNS_XMLNS; |
Laszlo Papp | 8b87a19 | 2017-10-19 18:47:12 +0100 | [diff] [blame] | 42 | import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.CFG_MODE_MERGE; |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 43 | import static org.slf4j.LoggerFactory.getLogger; |
| 44 | |
| 45 | /** |
Michael Enrico | 584eebd | 2021-07-22 08:04:13 +0000 | [diff] [blame^] | 46 | * Flow rule programmable behaviour for Polatis optical netconf devices. |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 47 | */ |
| 48 | public class PolatisFlowRuleProgrammable |
| 49 | extends AbstractHandlerBehaviour implements FlowRuleProgrammable { |
| 50 | |
| 51 | public static final String KEY_CHID = "wavelength-id"; |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 52 | public static final String KEY_SRC_CHID = String.format("%s.%s", KEY_SRC, KEY_CHID); |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 53 | public static final String CFG_MODE_DELETE = "delete"; |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 54 | |
| 55 | private static final Logger log = getLogger(PolatisFlowRuleProgrammable.class); |
| 56 | |
| 57 | @Override |
| 58 | public Collection<FlowEntry> getFlowEntries() { |
Michael Enrico | 584eebd | 2021-07-22 08:04:13 +0000 | [diff] [blame^] | 59 | return parseConnections(this); |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) { |
| 64 | return applyConnections(rules); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) { |
| 69 | return removeConnections(rules); |
| 70 | } |
| 71 | |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 72 | private Collection<FlowRule> applyConnections(Collection<FlowRule> rules) { |
| 73 | return rules.stream() |
| 74 | .filter(c -> editConnection(c, CFG_MODE_MERGE)) |
| 75 | .collect(Collectors.toList()); |
| 76 | } |
| 77 | |
| 78 | private boolean editConnection(FlowRule rule, String mode) { |
| 79 | CrossConnects crossConnects = PolatisOpticalUtility.fromFlowRule(this, rule); |
| 80 | final StringBuilder cfg = new StringBuilder(xmlOpen(KEY_CONNS_XMLNS)); |
| 81 | List<Pair> pairs = crossConnects.pair(); |
Michael Enrico | 584eebd | 2021-07-22 08:04:13 +0000 | [diff] [blame^] | 82 | final String keyPairCompat = parseKeyPairCompat(this); |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 83 | final String keyPairMode = String.format("%s operation=\"%s\"", keyPairCompat, mode); |
| 84 | pairs.forEach(p -> { |
| 85 | cfg.append(xmlOpen(keyPairMode)) |
| 86 | .append(xmlOpen(KEY_SRC)) |
| 87 | .append(p.ingress()) |
| 88 | .append(xmlClose(KEY_SRC)) |
| 89 | .append(xmlOpen(KEY_DST)) |
| 90 | .append(p.egress()) |
| 91 | .append(xmlClose(KEY_DST)) |
| 92 | .append(xmlClose(keyPairCompat)); |
| 93 | }); |
| 94 | cfg.append(xmlClose(KEY_CONNS)); |
| 95 | return netconfEditConfig(handler(), null, cfg.toString()); |
| 96 | } |
| 97 | |
| 98 | private Collection<FlowRule> removeConnections(Collection<FlowRule> rules) { |
| 99 | return rules.stream() |
| 100 | .filter(c -> editConnection(c, CFG_MODE_DELETE)) |
| 101 | .collect(Collectors.toList()); |
| 102 | } |
| 103 | |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 104 | } |