blob: 34274e24cd2a863cd8dd37f830115ee23f104289 [file] [log] [blame]
Sean Condonfae8e662016-12-15 10:25:13 +00001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Sean Condonfae8e662016-12-15 10:25:13 +00003 *
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 */
16package org.onosproject.drivers.microsemi;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19import static org.slf4j.LoggerFactory.getLogger;
20
21import java.time.OffsetDateTime;
22import java.time.ZoneId;
23import java.time.format.DateTimeFormatter;
24import java.util.ArrayList;
25import java.util.List;
26
27import org.onlab.packet.ChassisId;
28import org.onosproject.drivers.microsemi.yang.IetfSystemNetconfService;
29import org.onosproject.net.AnnotationKeys;
30import org.onosproject.net.DefaultAnnotations;
31import org.onosproject.net.Device;
32import org.onosproject.net.DeviceId;
33import org.onosproject.net.Port;
34import org.onosproject.net.PortNumber;
35import org.onosproject.net.device.DefaultDeviceDescription;
36import org.onosproject.net.device.DefaultPortDescription;
37import org.onosproject.net.device.DeviceDescription;
38import org.onosproject.net.device.DeviceDescriptionDiscovery;
39import org.onosproject.net.device.DeviceService;
40import org.onosproject.net.device.PortDescription;
41import org.onosproject.net.driver.AbstractHandlerBehaviour;
42import org.onosproject.netconf.NetconfController;
Sean Condon06613e92017-06-09 15:14:01 +010043import org.onosproject.netconf.NetconfDevice;
Sean Condonfae8e662016-12-15 10:25:13 +000044import org.onosproject.netconf.NetconfException;
45import org.onosproject.netconf.NetconfSession;
Sean Condon06613e92017-06-09 15:14:01 +010046import org.onosproject.yang.gen.v1.ietfsystemmicrosemi.rev20160505.ietfsystemmicrosemi.system.AugmentedSysSystem;
47import org.onosproject.yang.gen.v1.ietfsystemmicrosemi.rev20160505.ietfsystemmicrosemi.system.DefaultAugmentedSysSystem;
48import org.onosproject.yang.gen.v1.ietfsystemmicrosemi.rev20160505.ietfsystemmicrosemi.systemstate.platform.AugmentedSysPlatform;
49import org.onosproject.yang.gen.v1.ietfsystem.rev20140806.IetfSystem;
50import org.onosproject.yang.gen.v1.ietfsystemmicrosemi.rev20160505.ietfsystemmicrosemi.systemstate.platform.DefaultAugmentedSysPlatform;
51import org.onosproject.yang.gen.v1.ietfyangtypes.rev20130715.ietfyangtypes.DateAndTime;
Sean Condonfae8e662016-12-15 10:25:13 +000052import org.slf4j.Logger;
53
54public class Ea1000DeviceDescription extends AbstractHandlerBehaviour implements DeviceDescriptionDiscovery {
55
56 private String serialNumber = "unavailable";
57 private String swVersion = "unavailable";
58 private String longitudeStr = null;
59 private String latitudeStr = null;
60 private final Logger log = getLogger(getClass());
61
62 public Ea1000DeviceDescription() {
63 log.info("Loaded handler behaviour Ea1000DeviceDescription.");
64 }
65
66 @Override
67 public DeviceDescription discoverDeviceDetails() {
68 log.info("Adding description for EA1000 device");
69
70 NetconfController controller = checkNotNull(handler().get(NetconfController.class));
Sean Condon06613e92017-06-09 15:14:01 +010071 NetconfDevice ncDevice = controller.getDevicesMap().get(handler().data().deviceId());
72 if (ncDevice == null) {
73 log.error("Internal ONOS Error. Device has been marked as reachable, " +
74 "but deviceID {} is not in Devices Map. Continuing with empty description",
75 handler().data().deviceId());
76 return null;
77 }
78 NetconfSession session = ncDevice.getSession();
Sean Condonfae8e662016-12-15 10:25:13 +000079 IetfSystemNetconfService ietfSystemService =
80 (IetfSystemNetconfService) checkNotNull(handler().get(IetfSystemNetconfService.class));
81
82 try {
83 IetfSystem system = ietfSystemService.getIetfSystemInit(session);
84 if (system != null && system.systemState() != null) {
85 swVersion = system.systemState().platform().osRelease();
Sean Condon1dbcd712017-10-19 12:09:21 +010086 AugmentedSysPlatform augmentedSysStatePlatform =
Sean Condonfae8e662016-12-15 10:25:13 +000087 (AugmentedSysPlatform) system.systemState()
Sean Condon06613e92017-06-09 15:14:01 +010088 .platform().augmentation(DefaultAugmentedSysPlatform.class);
Sean Condon1dbcd712017-10-19 12:09:21 +010089 if (augmentedSysStatePlatform != null && augmentedSysStatePlatform.deviceIdentification() != null) {
90 serialNumber = augmentedSysStatePlatform.deviceIdentification().serialNumber();
91 } else {
92 log.warn("Serial Number of device not available: {}", handler().data().deviceId());
93 }
Sean Condonfae8e662016-12-15 10:25:13 +000094 DateAndTime deviceDateAndTime = system.systemState().clock().currentDatetime();
95 OffsetDateTime odt =
96 OffsetDateTime.parse(deviceDateAndTime.string(), DateTimeFormatter.ISO_OFFSET_DATE_TIME);
97 if (odt.getYear() < OffsetDateTime.now(ZoneId.of("UTC")).getYear()) {
98 OffsetDateTime nowUtc = OffsetDateTime.now(ZoneId.of("UTC"));
Sean Condon1dbcd712017-10-19 12:09:21 +010099 log.warn("Date on device {} is in the past: {}. Setting it to {}",
100 handler().data().deviceId(), odt.toString(), nowUtc);
Sean Condonfae8e662016-12-15 10:25:13 +0000101 ietfSystemService.setCurrentDatetime(nowUtc, session);
102 }
103 }
104
105 if (system != null && system.system() != null) {
106 AugmentedSysSystem augmentedSystem =
Sean Condon06613e92017-06-09 15:14:01 +0100107 (AugmentedSysSystem) system.system().augmentation(DefaultAugmentedSysSystem.class);
Sean Condonfae8e662016-12-15 10:25:13 +0000108 longitudeStr = augmentedSystem.longitude().toPlainString();
109 latitudeStr = augmentedSystem.latitude().toPlainString();
110 }
111 } catch (NetconfException e) {
112 log.error("Unable to retrieve init data from device: " + handler().data().deviceId().toString()
113 + " Error: " + e.getMessage());
114 e.printStackTrace();
115 }
116
117 DeviceService deviceService = checkNotNull(handler().get(DeviceService.class));
118 DeviceId deviceId = handler().data().deviceId();
119 Device device = deviceService.getDevice(deviceId);
120 DefaultAnnotations.Builder annotationsBuilder = DefaultAnnotations.builder();
121 if (longitudeStr != null && latitudeStr != null) {
122 annotationsBuilder.set(AnnotationKeys.LONGITUDE, longitudeStr)
123 .set(AnnotationKeys.LATITUDE, latitudeStr).build();
124 } else {
125 log.warn("Longitude and latitude could not be retrieved from device " + deviceId);
126 }
127
128 return new DefaultDeviceDescription(device.id().uri(), Device.Type.OTHER, "Microsemi", "EA1000", swVersion,
129 serialNumber, new ChassisId(), annotationsBuilder.build());
130 }
131
132 @Override
133 public List<PortDescription> discoverPortDetails() {
134
135 List<PortDescription> ports = new ArrayList<PortDescription>();
136
137 DefaultAnnotations annotationOptics = DefaultAnnotations.builder().set(AnnotationKeys.PORT_NAME, "Optics")
138 .build();
139 PortDescription optics = new DefaultPortDescription(PortNumber.portNumber(0), true, Port.Type.FIBER, 1000,
140 annotationOptics);
141 ports.add(optics);
142
143 DefaultAnnotations annotationHost = DefaultAnnotations.builder().set(AnnotationKeys.PORT_NAME, "Host").build();
144 PortDescription host = new DefaultPortDescription(PortNumber.portNumber(1), true, Port.Type.COPPER, 1000,
145 annotationHost);
146 ports.add(host);
147
148 return ports;
149 }
150}