kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.opencord.kafka.integrations; |
| 18 | |
| 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import org.onlab.packet.ChassisId; |
| 22 | import org.onosproject.net.AnnotationKeys; |
| 23 | import org.onosproject.net.Annotations; |
| 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.DefaultAnnotations; |
| 26 | import org.onosproject.net.DefaultDevice; |
| 27 | import org.onosproject.net.Device; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.Element; |
| 30 | import org.onosproject.net.ElementId; |
| 31 | import org.onosproject.net.Port; |
| 32 | import org.onosproject.net.PortNumber; |
| 33 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 34 | import org.onosproject.net.device.PortStatistics; |
| 35 | import org.onosproject.net.driver.Behaviour; |
| 36 | import org.onosproject.net.provider.ProviderId; |
| 37 | |
| 38 | import java.util.ArrayList; |
| 39 | import java.util.List; |
| 40 | import java.util.Set; |
| 41 | |
| 42 | /** |
| 43 | * DeviceServiceAdapter mocker class. |
| 44 | */ |
| 45 | public class MockDeviceService extends DeviceServiceAdapter { |
| 46 | |
| 47 | public static final String OLT_DEV_ID = "of:0000c6b1cd40dc93"; |
| 48 | public static final DeviceId DEVICE_ID_1 = DeviceId.deviceId(OLT_DEV_ID); |
| 49 | private static final String SCHEME_NAME = "kafka-onos"; |
| 50 | private static final DefaultAnnotations DEVICE_ANNOTATIONS = DefaultAnnotations.builder() |
| 51 | .set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase()).build(); |
| 52 | private final ProviderId providerId = new ProviderId("of", "foo"); |
| 53 | private final Device device1 = new MockDevice(providerId, DEVICE_ID_1, Device.Type.SWITCH, |
| 54 | "foo.inc", "0", "0", OLT_DEV_ID, new ChassisId(), |
| 55 | DEVICE_ANNOTATIONS); |
| 56 | |
| 57 | |
| 58 | @Override |
| 59 | public Device getDevice(DeviceId devId) { |
| 60 | return device1; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public Iterable<Device> getDevices() { |
| 65 | List<Device> devices = new ArrayList<>(); |
| 66 | devices.add(device1); |
| 67 | return devices; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public Port getPort(ConnectPoint cp) { |
| 72 | return new MockPort(); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public List<PortStatistics> getPortStatistics(DeviceId deviceId) { |
| 77 | PortStatistics ps = new PortStatistics() { |
| 78 | @Override |
| 79 | public PortNumber portNumber() { |
| 80 | return PortNumber.portNumber(1); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public long packetsReceived() { |
| 85 | return 100; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public long packetsSent() { |
| 90 | return 10; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public long bytesReceived() { |
| 95 | return 100; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public long bytesSent() { |
| 100 | return 10; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public long packetsRxDropped() { |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public long packetsTxDropped() { |
| 110 | return 1; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public long packetsRxErrors() { |
| 115 | return 1; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public long packetsTxErrors() { |
| 120 | return 1; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public long durationSec() { |
| 125 | return 100; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public long durationNano() { |
| 130 | return 100 * 1000; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public boolean isZero() { |
| 135 | return false; |
| 136 | } |
| 137 | }; |
| 138 | return Lists.newArrayList(ps); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Port object mock. |
| 143 | */ |
| 144 | public static class MockPort implements Port { |
| 145 | |
| 146 | @Override |
| 147 | public boolean isEnabled() { |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | public long portSpeed() { |
| 152 | return 1000; |
| 153 | } |
| 154 | |
| 155 | public Element element() { |
| 156 | return new Element() { |
| 157 | @Override |
| 158 | public ElementId id() { |
| 159 | return DEVICE_ID_1; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public Annotations annotations() { |
| 164 | return null; |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public ProviderId providerId() { |
| 169 | return null; |
| 170 | } |
| 171 | |
| 172 | @Override |
Matteo Scandolo | 1298ffc | 2020-12-10 12:46:55 -0800 | [diff] [blame] | 173 | @SuppressWarnings({"TypeParameterUnusedInFormals"}) |
kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [diff] [blame] | 174 | public <B extends Behaviour> B as(Class<B> projectionClass) { |
| 175 | return null; |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public <B extends Behaviour> boolean is(Class<B> projectionClass) { |
| 180 | return false; |
| 181 | } |
| 182 | }; |
| 183 | } |
| 184 | |
| 185 | public PortNumber number() { |
| 186 | return PortNumber.portNumber(1); |
| 187 | } |
| 188 | |
| 189 | public Annotations annotations() { |
| 190 | return new MockAnnotations(); |
| 191 | } |
| 192 | |
| 193 | public Type type() { |
| 194 | return Port.Type.FIBER; |
| 195 | } |
| 196 | |
| 197 | private static class MockAnnotations implements Annotations { |
| 198 | |
| 199 | @Override |
| 200 | public String value(String val) { |
| 201 | return "nni-"; |
| 202 | } |
| 203 | |
| 204 | public Set<String> keys() { |
| 205 | return Sets.newHashSet("portName"); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Device mock. |
| 212 | */ |
| 213 | protected static class MockDevice extends DefaultDevice { |
| 214 | |
| 215 | /* |
| 216 | Mocks OLT device. |
| 217 | */ |
| 218 | public MockDevice(ProviderId providerId, DeviceId id, Type type, |
| 219 | String manufacturer, String hwVersion, String swVersion, |
| 220 | String serialNumber, ChassisId chassisId, Annotations... annotations) { |
| 221 | super(providerId, id, type, manufacturer, hwVersion, swVersion, serialNumber, |
| 222 | chassisId, annotations); |
| 223 | } |
| 224 | } |
| 225 | } |