blob: 6f379601075d31414419452634eb6f1ca9a0e406 [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
Laszlo Pappbd0779f2018-03-28 18:08:51 +010019import org.onosproject.yang.gen.v1.opticalswitch.rev20180322.opticalswitch.CrossConnects;
20import org.onosproject.yang.gen.v1.opticalswitch.rev20180322.opticalswitch.crossconnects.Pair;
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010021
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010022import org.onosproject.net.driver.AbstractHandlerBehaviour;
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010023import org.onosproject.net.flow.FlowEntry;
24import org.onosproject.net.flow.FlowRule;
25import org.onosproject.net.flow.FlowRuleProgrammable;
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010026import org.slf4j.Logger;
27
28import java.util.Collection;
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010029import java.util.List;
30import java.util.stream.Collectors;
31
Michael Enrico584eebd2021-07-22 08:04:13 +000032import static org.onosproject.drivers.polatis.netconf.PolatisUtility.parseKeyPairCompat;
33import static org.onosproject.drivers.polatis.netconf.PolatisUtility.parseConnections;
34import static org.onosproject.drivers.polatis.netconf.PolatisUtility.KEY_SRC;
35import static org.onosproject.drivers.polatis.netconf.PolatisUtility.KEY_DST;
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010036
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010037import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.netconfEditConfig;
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010038import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.xmlOpen;
39import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.xmlClose;
40import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_CONNS;
41import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_CONNS_XMLNS;
Laszlo Papp8b87a192017-10-19 18:47:12 +010042import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.CFG_MODE_MERGE;
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010043import static org.slf4j.LoggerFactory.getLogger;
44
45/**
Michael Enrico584eebd2021-07-22 08:04:13 +000046 * Flow rule programmable behaviour for Polatis optical netconf devices.
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010047 */
48public class PolatisFlowRuleProgrammable
49 extends AbstractHandlerBehaviour implements FlowRuleProgrammable {
50
51 public static final String KEY_CHID = "wavelength-id";
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010052 public static final String KEY_SRC_CHID = String.format("%s.%s", KEY_SRC, KEY_CHID);
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010053 public static final String CFG_MODE_DELETE = "delete";
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010054
55 private static final Logger log = getLogger(PolatisFlowRuleProgrammable.class);
56
57 @Override
58 public Collection<FlowEntry> getFlowEntries() {
Michael Enrico584eebd2021-07-22 08:04:13 +000059 return parseConnections(this);
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010060 }
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 Papp8b3a5f62017-10-05 13:32:00 +010072 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 Enrico584eebd2021-07-22 08:04:13 +000082 final String keyPairCompat = parseKeyPairCompat(this);
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010083 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 Papp8b3a5f62017-10-05 13:32:00 +0100104}