MaoLu | 819fde2 | 2017-04-20 17:17:49 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016 Open Networking Foundation |
MaoLu | 819fde2 | 2017-04-20 17:17:49 -0700 | [diff] [blame] | 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.oplink; |
| 18 | |
| 19 | import org.apache.commons.configuration.HierarchicalConfiguration; |
MaoLu | 819fde2 | 2017-04-20 17:17:49 -0700 | [diff] [blame] | 20 | import org.onosproject.net.PortNumber; |
| 21 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 22 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 23 | import org.onosproject.net.flow.FlowEntry; |
| 24 | import org.onosproject.net.flow.FlowRule; |
| 25 | import org.onosproject.net.flow.FlowRuleProgrammable; |
| 26 | import org.slf4j.Logger; |
| 27 | |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.Collection; |
| 30 | import java.util.List; |
| 31 | import java.util.stream.Collectors; |
| 32 | |
| 33 | import static org.onosproject.drivers.oplink.OplinkNetconfUtility.*; |
| 34 | import static org.slf4j.LoggerFactory.getLogger; |
| 35 | |
| 36 | /** |
| 37 | * Flow rule programmable behaviour for oplink optical netconf devices. |
| 38 | */ |
| 39 | public class OplinkOpticalFlowRuleProgrammable |
| 40 | extends AbstractHandlerBehaviour implements FlowRuleProgrammable { |
| 41 | |
| 42 | // key |
| 43 | public static final String KEY_CHID = "wavelength-id"; |
| 44 | public static final String KEY_SRC = "source"; |
| 45 | public static final String KEY_DST = "destination"; |
| 46 | public static final String KEY_SRC_PORTID = String.format("%s.%s", KEY_SRC, KEY_PORTID); |
| 47 | public static final String KEY_SRC_CHID = String.format("%s.%s", KEY_SRC, KEY_CHID); |
| 48 | public static final String KEY_DST_PORTID = String.format("%s.%s", KEY_DST, KEY_PORTID); |
| 49 | |
| 50 | // log |
| 51 | private static final Logger log = getLogger(OplinkOpticalFlowRuleProgrammable.class); |
| 52 | |
| 53 | @Override |
| 54 | public Collection<FlowEntry> getFlowEntries() { |
| 55 | return parseConnections(); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public Collection<FlowRule> applyFlowRules(Collection<FlowRule> rules) { |
| 60 | return applyConnections(rules); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public Collection<FlowRule> removeFlowRules(Collection<FlowRule> rules) { |
| 65 | return removeConnections(rules); |
| 66 | } |
| 67 | |
| 68 | private String getConnectionsFilter() { |
| 69 | return new StringBuilder(xmlOpen(KEY_OPENOPTICALDEV_XMLNS)) |
| 70 | .append(xmlEmpty(KEY_CONNS)) |
| 71 | .append(xmlClose(KEY_OPENOPTICALDEV)) |
| 72 | .toString(); |
| 73 | } |
| 74 | |
| 75 | private Collection<FlowEntry> parseConnections() { |
| 76 | log.debug("Fetch connections..."); |
MaoLu | e00b742 | 2017-05-11 20:27:35 -0700 | [diff] [blame] | 77 | String reply = netconfGet(handler(), getConnectionsFilter()); |
| 78 | List<HierarchicalConfiguration> subtrees = configsAt(reply, KEY_DATA_CONNS); |
MaoLu | 819fde2 | 2017-04-20 17:17:49 -0700 | [diff] [blame] | 79 | Collection<FlowEntry> list = new ArrayList<>(); |
| 80 | for (HierarchicalConfiguration connection : subtrees) { |
| 81 | list.add(new DefaultFlowEntry(parseConnection(connection), FlowEntry.FlowEntryState.ADDED)); |
| 82 | } |
| 83 | return list; |
| 84 | } |
| 85 | |
| 86 | private FlowRule parseConnection(HierarchicalConfiguration cfg) { |
| 87 | return OplinkOpticalUtility.toFlowRule(this, |
| 88 | PortNumber.portNumber(cfg.getString(KEY_SRC_PORTID)), |
| 89 | PortNumber.portNumber(cfg.getString(KEY_DST_PORTID)), |
| 90 | cfg.getInt(KEY_SRC_CHID)); |
| 91 | |
| 92 | } |
| 93 | |
| 94 | private Collection<FlowRule> applyConnections(Collection<FlowRule> rules) { |
| 95 | return rules.stream() |
| 96 | .filter(c -> applyConnection(c)) |
| 97 | .collect(Collectors.toList()); |
| 98 | } |
| 99 | |
| 100 | private boolean applyConnection(FlowRule rule) { |
| 101 | log.debug("Applying connection {}", rule); |
| 102 | OplinkCrossConnect crossConnect = OplinkOpticalUtility.fromFlowRule(this, rule); |
| 103 | // Build xml |
| 104 | String connID = Integer.toString(crossConnect.getChannel()); |
| 105 | String cfg = new StringBuilder(xmlOpen(KEY_OPENOPTICALDEV_XMLNS)) |
| 106 | .append(xmlOpen(KEY_CONNS)) |
| 107 | .append(xml(KEY_CONNID, connID)) |
| 108 | .append(xmlOpen(KEY_SRC)) |
| 109 | .append(xml(KEY_PORTID, crossConnect.getInPort().name())) |
| 110 | .append(xml(KEY_CHID, connID)) |
| 111 | .append(xmlClose(KEY_SRC)) |
| 112 | .append(xmlOpen(KEY_DST)) |
| 113 | .append(xml(KEY_PORTID, crossConnect.getOutPort().name())) |
| 114 | .append(xmlClose(KEY_DST)) |
| 115 | .append(xml(KEY_CHATT, Integer.toString(crossConnect.getAttenuation()))) |
| 116 | .append(xmlClose(KEY_CONNS)) |
| 117 | .append(xmlClose(KEY_OPENOPTICALDEV)) |
| 118 | .toString(); |
| 119 | return netconfEditConfig(handler(), CFG_MODE_MERGE, cfg); |
| 120 | } |
| 121 | |
| 122 | private Collection<FlowRule> removeConnections(Collection<FlowRule> rules) { |
| 123 | return rules.stream() |
| 124 | .filter(c -> removeConnection(c)) |
| 125 | .collect(Collectors.toList()); |
| 126 | } |
| 127 | |
| 128 | private boolean removeConnection(FlowRule rule) { |
| 129 | log.debug("Removing connection {}", rule); |
| 130 | OplinkCrossConnect crossConnect = OplinkOpticalUtility.fromFlowRule(this, rule); |
| 131 | // Build xml |
| 132 | String connID = Integer.toString(crossConnect.getChannel()); |
| 133 | String cfg = new StringBuilder(xmlOpen(KEY_OPENOPTICALDEV_XMLNS)) |
wei wang | d271fbc | 2017-12-04 15:36:31 -0800 | [diff] [blame] | 134 | .append(xmlOpen(String.format("%s %s", KEY_CONNS, CFG_OPT_DELETE))) |
MaoLu | 819fde2 | 2017-04-20 17:17:49 -0700 | [diff] [blame] | 135 | .append(xml(KEY_CONNID, connID)) |
| 136 | .append(xmlOpen(KEY_SRC)) |
| 137 | .append(xml(KEY_PORTID, crossConnect.getInPort().name())) |
| 138 | .append(xml(KEY_CHID, connID)) |
| 139 | .append(xmlClose(KEY_SRC)) |
| 140 | .append(xmlClose(KEY_CONNS)) |
| 141 | .append(xmlClose(KEY_OPENOPTICALDEV)) |
| 142 | .toString(); |
wei wang | d271fbc | 2017-12-04 15:36:31 -0800 | [diff] [blame] | 143 | return netconfEditConfig(handler(), CFG_MODE_NONE, cfg); |
MaoLu | 819fde2 | 2017-04-20 17:17:49 -0700 | [diff] [blame] | 144 | } |
| 145 | } |