Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | package org.onosproject.drivers.microsemi; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
| 21 | import java.time.OffsetDateTime; |
| 22 | import java.time.ZoneId; |
| 23 | import java.time.format.DateTimeFormatter; |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.List; |
| 26 | |
| 27 | import org.onlab.packet.ChassisId; |
| 28 | import org.onosproject.drivers.microsemi.yang.IetfSystemNetconfService; |
| 29 | import org.onosproject.net.AnnotationKeys; |
| 30 | import org.onosproject.net.DefaultAnnotations; |
| 31 | import org.onosproject.net.Device; |
| 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.Port; |
| 34 | import org.onosproject.net.PortNumber; |
| 35 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 36 | import org.onosproject.net.device.DefaultPortDescription; |
| 37 | import org.onosproject.net.device.DeviceDescription; |
| 38 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 39 | import org.onosproject.net.device.DeviceService; |
| 40 | import org.onosproject.net.device.PortDescription; |
| 41 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 42 | import org.onosproject.netconf.NetconfController; |
| 43 | import org.onosproject.netconf.NetconfException; |
| 44 | import org.onosproject.netconf.NetconfSession; |
| 45 | import org.onosproject.yang.gen.v1.http.www.microsemi.com.microsemi.edge.assure.msea.system.rev20160505.ietfsystemmicrosemi.system.AugmentedSysSystem; |
| 46 | import org.onosproject.yang.gen.v1.http.www.microsemi.com.microsemi.edge.assure.msea.system.rev20160505.ietfsystemmicrosemi.systemstate.platform.AugmentedSysPlatform; |
| 47 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.system.rev20140806.IetfSystem; |
| 48 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev20130715.ietfyangtypes.DateAndTime; |
| 49 | import org.slf4j.Logger; |
| 50 | |
| 51 | public class Ea1000DeviceDescription extends AbstractHandlerBehaviour implements DeviceDescriptionDiscovery { |
| 52 | |
| 53 | private String serialNumber = "unavailable"; |
| 54 | private String swVersion = "unavailable"; |
| 55 | private String longitudeStr = null; |
| 56 | private String latitudeStr = null; |
| 57 | private final Logger log = getLogger(getClass()); |
| 58 | |
| 59 | public Ea1000DeviceDescription() { |
| 60 | log.info("Loaded handler behaviour Ea1000DeviceDescription."); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public DeviceDescription discoverDeviceDetails() { |
| 65 | log.info("Adding description for EA1000 device"); |
| 66 | |
| 67 | NetconfController controller = checkNotNull(handler().get(NetconfController.class)); |
| 68 | NetconfSession session = controller.getDevicesMap().get(handler().data().deviceId()).getSession(); |
| 69 | IetfSystemNetconfService ietfSystemService = |
| 70 | (IetfSystemNetconfService) checkNotNull(handler().get(IetfSystemNetconfService.class)); |
| 71 | |
| 72 | try { |
| 73 | IetfSystem system = ietfSystemService.getIetfSystemInit(session); |
| 74 | if (system != null && system.systemState() != null) { |
| 75 | swVersion = system.systemState().platform().osRelease(); |
| 76 | AugmentedSysPlatform augmentedSysPlatform = |
| 77 | (AugmentedSysPlatform) system.systemState() |
| 78 | .platform().yangAugmentedInfo(AugmentedSysPlatform.class); |
| 79 | serialNumber = augmentedSysPlatform.deviceIdentification().serialNumber(); |
| 80 | DateAndTime deviceDateAndTime = system.systemState().clock().currentDatetime(); |
| 81 | OffsetDateTime odt = |
| 82 | OffsetDateTime.parse(deviceDateAndTime.string(), DateTimeFormatter.ISO_OFFSET_DATE_TIME); |
| 83 | if (odt.getYear() < OffsetDateTime.now(ZoneId.of("UTC")).getYear()) { |
| 84 | OffsetDateTime nowUtc = OffsetDateTime.now(ZoneId.of("UTC")); |
| 85 | log.warn("Date on device is in the past: {}. Setting it to {}", odt.toString(), nowUtc); |
| 86 | ietfSystemService.setCurrentDatetime(nowUtc, session); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if (system != null && system.system() != null) { |
| 91 | AugmentedSysSystem augmentedSystem = |
| 92 | (AugmentedSysSystem) system.system().yangAugmentedInfo(AugmentedSysSystem.class); |
| 93 | longitudeStr = augmentedSystem.longitude().toPlainString(); |
| 94 | latitudeStr = augmentedSystem.latitude().toPlainString(); |
| 95 | } |
| 96 | } catch (NetconfException e) { |
| 97 | log.error("Unable to retrieve init data from device: " + handler().data().deviceId().toString() |
| 98 | + " Error: " + e.getMessage()); |
| 99 | e.printStackTrace(); |
| 100 | } |
| 101 | |
| 102 | DeviceService deviceService = checkNotNull(handler().get(DeviceService.class)); |
| 103 | DeviceId deviceId = handler().data().deviceId(); |
| 104 | Device device = deviceService.getDevice(deviceId); |
| 105 | DefaultAnnotations.Builder annotationsBuilder = DefaultAnnotations.builder(); |
| 106 | if (longitudeStr != null && latitudeStr != null) { |
| 107 | annotationsBuilder.set(AnnotationKeys.LONGITUDE, longitudeStr) |
| 108 | .set(AnnotationKeys.LATITUDE, latitudeStr).build(); |
| 109 | } else { |
| 110 | log.warn("Longitude and latitude could not be retrieved from device " + deviceId); |
| 111 | } |
| 112 | |
| 113 | return new DefaultDeviceDescription(device.id().uri(), Device.Type.OTHER, "Microsemi", "EA1000", swVersion, |
| 114 | serialNumber, new ChassisId(), annotationsBuilder.build()); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public List<PortDescription> discoverPortDetails() { |
| 119 | |
| 120 | List<PortDescription> ports = new ArrayList<PortDescription>(); |
| 121 | |
| 122 | DefaultAnnotations annotationOptics = DefaultAnnotations.builder().set(AnnotationKeys.PORT_NAME, "Optics") |
| 123 | .build(); |
| 124 | PortDescription optics = new DefaultPortDescription(PortNumber.portNumber(0), true, Port.Type.FIBER, 1000, |
| 125 | annotationOptics); |
| 126 | ports.add(optics); |
| 127 | |
| 128 | DefaultAnnotations annotationHost = DefaultAnnotations.builder().set(AnnotationKeys.PORT_NAME, "Host").build(); |
| 129 | PortDescription host = new DefaultPortDescription(PortNumber.portNumber(1), true, Port.Type.COPPER, 1000, |
| 130 | annotationHost); |
| 131 | ports.add(host); |
| 132 | |
| 133 | return ports; |
| 134 | } |
| 135 | } |