blob: a65247f7537cf366ec2c567d8ccf5922cb0849a6 [file] [log] [blame]
Aaron Kruglikov17b4c852016-01-15 16:37:04 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Aaron Kruglikov17b4c852016-01-15 16:37:04 -08003 *
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.fujitsu;
18
HIGUCHI Yutadbb631f2016-05-16 16:18:10 -070019import org.apache.commons.configuration.HierarchicalConfiguration;
Aaron Kruglikov17b4c852016-01-15 16:37:04 -080020import org.onosproject.drivers.utilities.XmlConfigParser;
HIGUCHI Yutadbb631f2016-05-16 16:18:10 -070021import org.onosproject.net.AnnotationKeys;
22import org.onosproject.net.ChannelSpacing;
23import org.onosproject.net.CltSignalType;
24import org.onosproject.net.DefaultAnnotations;
25import org.onosproject.net.GridType;
26import org.onosproject.net.OchSignal;
27import org.onosproject.net.OduSignalType;
28import org.onosproject.net.PortNumber;
Aaron Kruglikov17b4c852016-01-15 16:37:04 -080029import org.onosproject.net.behaviour.PortDiscovery;
30import org.onosproject.net.device.PortDescription;
31import org.onosproject.net.driver.AbstractHandlerBehaviour;
32import org.onosproject.netconf.NetconfController;
33import org.onosproject.netconf.NetconfException;
34import org.onosproject.netconf.NetconfSession;
35import org.slf4j.Logger;
36
HIGUCHI Yutadbb631f2016-05-16 16:18:10 -070037import com.google.common.collect.Lists;
38
Aaron Kruglikov17b4c852016-01-15 16:37:04 -080039import java.io.ByteArrayInputStream;
40import java.io.IOException;
41import java.util.List;
HIGUCHI Yutadbb631f2016-05-16 16:18:10 -070042import java.util.concurrent.atomic.AtomicInteger;
Aaron Kruglikov17b4c852016-01-15 16:37:04 -080043
44import static com.google.common.base.Preconditions.checkNotNull;
HIGUCHI Yutadbb631f2016-05-16 16:18:10 -070045import static org.onosproject.net.optical.device.OchPortHelper.ochPortDescription;
46import static org.onosproject.net.optical.device.OduCltPortHelper.oduCltPortDescription;
Aaron Kruglikov17b4c852016-01-15 16:37:04 -080047import static org.slf4j.LoggerFactory.getLogger;
48
49/**
50 * Retrieves the ports from a Fujitsu T100 device via netconf.
51 */
52public class PortGetterFujitsuImpl extends AbstractHandlerBehaviour
53 implements PortDiscovery {
54
55 private final Logger log = getLogger(getClass());
56
57 @Override
58 public List<PortDescription> getPorts() {
59 NetconfController controller = checkNotNull(handler().get(NetconfController.class));
60 NetconfSession session = controller.getDevicesMap().get(handler().data().deviceId()).getSession();
61 String reply;
62 try {
63 reply = session.get(requestBuilder());
64 } catch (IOException e) {
65 throw new RuntimeException(new NetconfException("Failed to retrieve configuration.", e));
66 }
HIGUCHI Yutadbb631f2016-05-16 16:18:10 -070067 List<PortDescription> descriptions =
Aaron Kruglikov17b4c852016-01-15 16:37:04 -080068 parseFujitsuT100Ports(XmlConfigParser.
69 loadXml(new ByteArrayInputStream(reply.getBytes())));
70 return descriptions;
71 }
72
73 /**
74 * Builds a request crafted to get the configuration required to create port
75 * descriptions for the device.
76 * @return The request string.
77 */
78 private String requestBuilder() {
79 StringBuilder rpc = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
80 //Message ID is injected later.
81 rpc.append("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">");
82 rpc.append("<get>");
83 rpc.append("<filter type=\"subtree\">");
84 rpc.append("<interfaces xmlns=\"urn:ietf:params:xml:ns:yang:ietf-interfaces\">");
85 rpc.append("</interfaces>");
86 rpc.append("</filter>");
87 rpc.append("</get>");
88 rpc.append("</rpc>");
89 return rpc.toString();
90 }
HIGUCHI Yutadbb631f2016-05-16 16:18:10 -070091
92 /**
93 * Parses a configuration and returns a set of ports for the fujitsu T100.
94 * @param cfg a hierarchical configuration
95 * @return a list of port descriptions
96 */
97 private static List<PortDescription> parseFujitsuT100Ports(HierarchicalConfiguration cfg) {
98 AtomicInteger counter = new AtomicInteger(1);
99 List<PortDescription> portDescriptions = Lists.newArrayList();
100 List<HierarchicalConfiguration> subtrees =
101 cfg.configurationsAt("data.interfaces.interface");
102 for (HierarchicalConfiguration portConfig : subtrees) {
103 if (!portConfig.getString("name").contains("LCN") &&
104 !portConfig.getString("name").contains("LMP") &&
105 portConfig.getString("type").equals("ianaift:ethernetCsmacd")) {
106 portDescriptions.add(parseT100OduPort(portConfig, counter.getAndIncrement()));
107 } else if (portConfig.getString("type").equals("ianaift:otnOtu")) {
108 portDescriptions.add(parseT100OchPort(portConfig, counter.getAndIncrement()));
109 }
110 }
111 return portDescriptions;
112 }
113
114 private static PortDescription parseT100OchPort(HierarchicalConfiguration cfg, long count) {
115 PortNumber portNumber = PortNumber.portNumber(count);
116 HierarchicalConfiguration otuConfig = cfg.configurationAt("otu");
117 boolean enabled = otuConfig.getString("administrative-state").equals("up");
118 OduSignalType signalType = otuConfig.getString("rate").equals("OTU4") ? OduSignalType.ODU4 : null;
119 //Unsure how to retreive, outside knowledge it is tunable.
120 boolean isTunable = true;
121 OchSignal lambda = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_50GHZ, 0, 4);
122 DefaultAnnotations annotations = DefaultAnnotations.builder().
123 set(AnnotationKeys.PORT_NAME, cfg.getString("name")).
124 build();
125 return ochPortDescription(portNumber, enabled, signalType, isTunable, lambda, annotations);
126 }
127
128 private static PortDescription parseT100OduPort(HierarchicalConfiguration cfg, long count) {
129 PortNumber portNumber = PortNumber.portNumber(count);
130 HierarchicalConfiguration ethernetConfig = cfg.configurationAt("ethernet");
131 boolean enabled = ethernetConfig.getString("administrative-state").equals("up");
132 //Rate is in kbps
133 CltSignalType signalType = ethernetConfig.getString("rate").equals("100000000") ?
134 CltSignalType.CLT_100GBE : null;
135 DefaultAnnotations annotations = DefaultAnnotations.builder().
136 set(AnnotationKeys.PORT_NAME, cfg.getString("name")).
137 build();
138 return oduCltPortDescription(portNumber, enabled, signalType, annotations);
139 }
140
Aaron Kruglikov17b4c852016-01-15 16:37:04 -0800141}