blob: cc5195bc4819f7203381f3ca8b224d20c9259914 [file] [log] [blame]
MaoLu819fde22017-04-20 17:17:49 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016 Open Networking Foundation
MaoLu819fde22017-04-20 17:17:49 -07003 *
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.oplink;
18
19import org.apache.commons.configuration.HierarchicalConfiguration;
MaoLu819fde22017-04-20 17:17:49 -070020import org.onosproject.net.PortNumber;
21import org.onosproject.net.driver.AbstractHandlerBehaviour;
22import org.onosproject.net.flow.DefaultFlowEntry;
23import org.onosproject.net.flow.FlowEntry;
24import org.onosproject.net.flow.FlowRule;
25import org.onosproject.net.flow.FlowRuleProgrammable;
26import org.slf4j.Logger;
27
28import java.util.ArrayList;
29import java.util.Collection;
30import java.util.List;
31import java.util.stream.Collectors;
32
33import static org.onosproject.drivers.oplink.OplinkNetconfUtility.*;
34import static org.slf4j.LoggerFactory.getLogger;
35
36/**
37 * Flow rule programmable behaviour for oplink optical netconf devices.
38 */
39public 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...");
MaoLue00b7422017-05-11 20:27:35 -070077 String reply = netconfGet(handler(), getConnectionsFilter());
78 List<HierarchicalConfiguration> subtrees = configsAt(reply, KEY_DATA_CONNS);
MaoLu819fde22017-04-20 17:17:49 -070079 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 wangd271fbc2017-12-04 15:36:31 -0800134 .append(xmlOpen(String.format("%s %s", KEY_CONNS, CFG_OPT_DELETE)))
MaoLu819fde22017-04-20 17:17:49 -0700135 .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 wangd271fbc2017-12-04 15:36:31 -0800143 return netconfEditConfig(handler(), CFG_MODE_NONE, cfg);
MaoLu819fde22017-04-20 17:17:49 -0700144 }
145}