blob: 9470a48a3d020acb74ac05cd312b862b237472b4 [file] [log] [blame]
Thomas Vachuska5c6ed222016-06-29 11:02:22 +02001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
Thomas Vachuska5c6ed222016-06-29 11:02:22 +02003 *
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
17package org.onosproject.provider.nil;
18
19import com.google.common.collect.Maps;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020020import org.onlab.packet.IpAddress;
Thomas Vachuska5c6ed222016-06-29 11:02:22 +020021import org.onlab.packet.MacAddress;
22import org.onlab.packet.VlanId;
23import org.onosproject.net.Device;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.HostId;
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020026import org.onosproject.net.HostLocation;
27import org.onosproject.net.host.DefaultHostDescription;
Thomas Vachuska5c6ed222016-06-29 11:02:22 +020028
29import java.util.Map;
30
31import static org.onlab.util.Tools.toHex;
32import static org.onosproject.provider.nil.NullProviders.SCHEME;
33
34/**
35 * Custom topology defined by a concise language.
36 */
37public class CustomTopologySimulator extends TopologySimulator {
38
39 private int nextDeviceId = 0;
40 private int nextHostId = 0;
41
42 private Map<String, DeviceId> nameToId = Maps.newConcurrentMap();
43
44 /**
45 * Returns the next device id.
46 *
47 * @return the next device id
48 */
49 public DeviceId nextDeviceId() {
50 return DeviceId.deviceId(SCHEME + ":" + toHex(++nextDeviceId));
51 }
52
53 /**
54 * Returns the next host id.
55 *
56 * @return the next host id
57 */
58 public HostId nextHostId() {
Thomas Vachuska83da9b72016-07-02 12:52:23 +020059 return HostId.hostId(MacAddress.valueOf(++nextHostId), VlanId.NONE);
Thomas Vachuska5c6ed222016-06-29 11:02:22 +020060 }
61
62 /**
63 * Returns the identifier of the device with the specified alias.
64 *
65 * @param name device name
66 * @return device identifier
67 */
68 public DeviceId deviceId(String name) {
69 return nameToId.get(name);
70 }
71
72 /**
73 * Creates simulated device.
74 *
75 * @param id device identifier
76 * @param name device name
77 * @param type device type
78 * @param portCount number of device ports
79 */
80 public void createDevice(DeviceId id, String name, Device.Type type, int portCount) {
Andreas Pantelopoulosf28c0562016-11-12 14:12:05 +010081 int chassisId = Integer.parseInt(id.uri().getSchemeSpecificPart(), 16);
Thomas Vachuska5c6ed222016-06-29 11:02:22 +020082 createDevice(id, chassisId, type, portCount);
83 nameToId.put(name, id);
84 }
85
Thomas Vachuska4407a7e2016-07-02 12:38:01 +020086 /**
87 * Creates a simulated host.
88 *
89 * @param hostId host identifier
90 * @param location host location
91 * @param hostIp host IP address
92 */
93 public void createHost(HostId hostId, HostLocation location, IpAddress hostIp) {
94 DefaultHostDescription description =
95 new DefaultHostDescription(hostId.mac(), hostId.vlanId(), location, hostIp);
96 hostProviderService.hostDetected(hostId, description, false);
97 }
98
Thomas Vachuska5c6ed222016-06-29 11:02:22 +020099 @Override
100 protected void createDevices() {
101 }
102
103 @Override
104 protected void createLinks() {
105 }
106
107 @Override
108 protected void createHosts() {
109 }
110}