MaoLu | 819fde2 | 2017-04-20 17:17:49 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 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 com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.Lists; |
| 21 | import org.apache.commons.configuration.HierarchicalConfiguration; |
| 22 | import org.onosproject.drivers.utilities.XmlConfigParser; |
| 23 | import org.onosproject.net.AnnotationKeys; |
| 24 | import org.onosproject.net.DefaultAnnotations; |
| 25 | import org.onosproject.net.PortNumber; |
| 26 | import org.onosproject.net.device.DeviceDescription; |
| 27 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 28 | import org.onosproject.net.device.PortDescription; |
| 29 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 30 | import org.slf4j.Logger; |
| 31 | |
| 32 | import java.util.List; |
| 33 | |
| 34 | import static org.onosproject.drivers.oplink.OplinkOpticalUtility.CHANNEL_SPACING; |
| 35 | import static org.onosproject.drivers.oplink.OplinkOpticalUtility.START_CENTER_FREQ; |
| 36 | import static org.onosproject.drivers.oplink.OplinkOpticalUtility.STOP_CENTER_FREQ; |
| 37 | import static org.onosproject.net.optical.device.OmsPortHelper.omsPortDescription; |
| 38 | import static org.slf4j.LoggerFactory.getLogger; |
| 39 | import static org.onosproject.drivers.oplink.OplinkNetconfUtility.*; |
| 40 | |
| 41 | /** |
| 42 | * Retrieves the ports from an Oplink optical netconf device. |
| 43 | */ |
| 44 | public class OplinkOpticalDeviceDescription extends AbstractHandlerBehaviour |
| 45 | implements DeviceDescriptionDiscovery { |
| 46 | |
| 47 | private static final String KEY_PORTNAME = "port-name"; |
| 48 | private static final String PORT_DIRECTION = "direction"; |
| 49 | // log |
| 50 | private static final Logger log = getLogger(OplinkOpticalDeviceDescription.class); |
| 51 | |
| 52 | @Override |
| 53 | public DeviceDescription discoverDeviceDetails() { |
| 54 | log.debug("No description to be added for device"); |
| 55 | //TODO to be implemented if needed. |
| 56 | return null; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public List<PortDescription> discoverPortDetails() { |
| 61 | log.debug("Port description to be added for device {}", data().deviceId()); |
| 62 | String reply = netconfGetConfig(handler(), getPortsFilter()); |
| 63 | List<PortDescription> descriptions = parsePorts(reply); |
| 64 | return ImmutableList.copyOf(descriptions); |
| 65 | } |
| 66 | |
| 67 | private String getPortsFilter() { |
| 68 | return new StringBuilder(xmlOpen(KEY_OPENOPTICALDEV_XMLNS)) |
| 69 | .append(xmlEmpty(KEY_PORTS)) |
| 70 | .append(xmlClose(KEY_OPENOPTICALDEV)) |
| 71 | .toString(); |
| 72 | } |
| 73 | |
| 74 | private List<PortDescription> parsePorts(String content) { |
| 75 | HierarchicalConfiguration cfg = XmlConfigParser.loadXmlString(content); |
| 76 | List<PortDescription> portDescriptions = Lists.newArrayList(); |
| 77 | List<HierarchicalConfiguration> subtrees = cfg.configurationsAt(KEY_DATA_PORTS); |
| 78 | for (HierarchicalConfiguration portConfig : subtrees) { |
| 79 | portDescriptions.add(parsePort(portConfig)); |
| 80 | } |
| 81 | return portDescriptions; |
| 82 | } |
| 83 | |
| 84 | private PortDescription parsePort(HierarchicalConfiguration cfg) { |
| 85 | PortNumber portNumber = PortNumber.portNumber(cfg.getLong(KEY_PORTID)); |
| 86 | HierarchicalConfiguration portInfo = cfg.configurationAt(KEY_PORT); |
| 87 | DefaultAnnotations annotations = DefaultAnnotations.builder() |
| 88 | .set(AnnotationKeys.PORT_NAME, portInfo.getString(KEY_PORTNAME)) |
| 89 | .set(PORT_DIRECTION, portInfo.getString(KEY_PORTDIRECT)) |
| 90 | .build(); |
| 91 | return omsPortDescription(portNumber, |
| 92 | true, |
| 93 | START_CENTER_FREQ, |
| 94 | STOP_CENTER_FREQ, |
| 95 | CHANNEL_SPACING.frequency(), |
| 96 | annotations); |
| 97 | } |
| 98 | } |