blob: affebe59a5654c8983b6ed10576a8b1b92442e04 [file] [log] [blame]
Andrea Campanellad8d92db2016-01-14 16:24:41 -08001/*
2 *
3 * * Copyright 2016 Open Networking Laboratory
4 * *
5 * * Licensed under the Apache License, Version 2.0 (the "License");
6 * * you may not use this file except in compliance with the License.
7 * * You may obtain a copy of the License at
8 * *
9 * * http://www.apache.org/licenses/LICENSE-2.0
10 * *
11 * * Unless required by applicable law or agreed to in writing, software
12 * * distributed under the License is distributed on an "AS IS" BASIS,
13 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * * See the License for the specific language governing permissions and
15 * * limitations under the License.
16 *
17 */
18
Andrea Campanella238d96e2016-01-20 11:52:02 -080019package org.onosproject.drivers.ciena;
Andrea Campanellad8d92db2016-01-14 16:24:41 -080020
21import com.google.common.collect.Lists;
22import org.apache.commons.configuration.HierarchicalConfiguration;
Andrea Campanella238d96e2016-01-20 11:52:02 -080023import org.onosproject.drivers.utilities.XmlConfigParser;
Andrea Campanellad8d92db2016-01-14 16:24:41 -080024import org.onosproject.net.AnnotationKeys;
25import org.onosproject.net.CltSignalType;
26import org.onosproject.net.DefaultAnnotations;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.PortNumber;
29import org.onosproject.net.SparseAnnotations;
30import org.onosproject.net.behaviour.PortDiscovery;
31import org.onosproject.net.device.OduCltPortDescription;
32import org.onosproject.net.device.PortDescription;
33import org.onosproject.net.driver.AbstractHandlerBehaviour;
34import org.onosproject.net.driver.DriverHandler;
35import org.onosproject.protocol.rest.RestSBController;
36
37import java.util.ArrayList;
38import java.util.List;
39
40import static com.google.common.base.Preconditions.checkNotNull;
41
42/**
43 * Discovers the ports from a Ciena WaveServer Rest device.
44 */
45public class PortDiscoveryCienaWaveserverImpl extends AbstractHandlerBehaviour
46 implements PortDiscovery {
47
48 private static final String SPEED = "speed";
49 private static final String GBPS = "Gbps";
50 private static final String PORT_ID = "port-id";
51 private static final String XML = "xml";
52 private static final String ENABLED = "enabled";
53 private static final String EMPTY_STRING = "";
54 private static final String NAME = "name";
55 private static final String ADMIN_STATE = "admin-state";
56
57 private static final ArrayList<String> LINESIDE = Lists.newArrayList(
58 "1.1", "1.2", "12.1", "12.2");
59
60 private static final String GENERAL_PORT_REQUEST =
61 "yang-api/datastore/ws-ports?config=true&format=xml&depth=unbounded";
62 private static final String SPECIFIC_PORT_PATH = "yang-api/datastore/ws-ptps/ptp/";
63 private static final String SPECIFIC_PORT_CONFIG =
64 "/ptp-config?config=true&format=xml&depth=unbounded";
65
66
67 @Override
68 public List<PortDescription> getPorts() {
69 List<PortDescription> ports = Lists.newArrayList();
70 DriverHandler handler = handler();
71 RestSBController controller = checkNotNull(handler.get(RestSBController.class));
72 DeviceId deviceId = handler.data().deviceId();
73
74
75 HierarchicalConfiguration config = XmlConfigParser.
76 loadXml(controller.get(deviceId, GENERAL_PORT_REQUEST, XML));
77 List<HierarchicalConfiguration> portsConfig =
78 XmlConfigParser.parseWaveServerCienaPorts(config);
79
80 portsConfig.stream().forEach(sub -> {
81 String name = sub.getString(NAME);
82 SparseAnnotations annotations = DefaultAnnotations.builder()
83 .set(AnnotationKeys.NAME, String.valueOf(name)).build();
84 if (LINESIDE.contains(name)) {
85 String wsportInfoRequest = SPECIFIC_PORT_PATH + sub.getLong(PORT_ID) +
86 SPECIFIC_PORT_CONFIG;
87 ports.add(XmlConfigParser.parseWaveServerCienaOCHPorts(
88 sub.getLong(PORT_ID),
89 toGbps(Long.parseLong(sub.getString(SPEED).replace(GBPS, EMPTY_STRING))),
90 XmlConfigParser.loadXml(controller.get(deviceId, wsportInfoRequest, XML)),
91 annotations));
92 } else {
93 //FIXME change when all optical types have two way information methods, see jira tickets
94 final int speed100GbpsinMbps = 100000;
95 CltSignalType cltType = toGbps(Long.parseLong(
96 sub.getString(SPEED).replace(GBPS, EMPTY_STRING))) == speed100GbpsinMbps ?
97 CltSignalType.CLT_100GBE : null;
98 ports.add(new OduCltPortDescription(PortNumber.portNumber(sub.getLong(PORT_ID)),
99 sub.getString(ADMIN_STATE).equals(ENABLED),
100 cltType, annotations));
101 }
102 });
103 return ports;
104 }
105
106 //FIXME remove when all optical types have two way information methods, see jira tickets
107 private long toGbps(long speed) {
108 return speed * 1000;
109 }
110}
111