Brian Stanke
Committed by Gerrit Code Review

ONOS-4076 - Virtual Network CLI commands and bug fixes in

DistributedVirtualNetworkStore.

Change-Id: I7bf67527bccb598385f15ad70091e3f8458d35d3
Showing 20 changed files with 1133 additions and 49 deletions
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.cli.net.vnet;
18 +
19 +import org.apache.karaf.shell.commands.Argument;
20 +import org.apache.karaf.shell.commands.Command;
21 +import org.onosproject.cli.AbstractShellCommand;
22 +import org.onosproject.incubator.net.virtual.TenantId;
23 +import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
24 +
25 +/**
26 + * Creates a new virtual network tenant.
27 + */
28 +@Command(scope = "onos", name = "vnet-add-tenant",
29 + description = "Creates a new virtual network tenant.")
30 +
31 +public class TenantAddCommand extends AbstractShellCommand {
32 +
33 + @Argument(index = 0, name = "id", description = "Tenant ID",
34 + required = true, multiValued = false)
35 + String id = null;
36 +
37 + @Override
38 + protected void execute() {
39 + VirtualNetworkAdminService service = get(VirtualNetworkAdminService.class);
40 + service.registerTenantId(TenantId.tenantId(id));
41 + print("Tenant successfully added.");
42 + }
43 +}
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.cli.net.vnet;
18 +
19 +import org.apache.karaf.shell.commands.Command;
20 +import org.onosproject.cli.AbstractShellCommand;
21 +import org.onosproject.incubator.net.virtual.TenantId;
22 +import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
23 +import org.onosproject.utils.Comparators;
24 +
25 +import java.util.ArrayList;
26 +import java.util.Collections;
27 +import java.util.List;
28 +
29 +/**
30 + * Lists all tenants.
31 + */
32 +@Command(scope = "onos", name = "vnet-tenants",
33 + description = "Lists all virtual network tenants.")
34 +public class TenantListCommand extends AbstractShellCommand {
35 +
36 + private static final String FMT_TENANT = "tenantId=%s";
37 +
38 + @Override
39 + protected void execute() {
40 + VirtualNetworkAdminService service = get(VirtualNetworkAdminService.class);
41 + List<TenantId> tenants = new ArrayList<>();
42 + tenants.addAll(service.getTenantIds());
43 + Collections.sort(tenants, Comparators.TENANT_ID_COMPARATOR);
44 +
45 + tenants.forEach(this::printTenant);
46 + }
47 +
48 + private void printTenant(TenantId tenantId) {
49 + print(FMT_TENANT, tenantId.id());
50 + }
51 +}
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.cli.net.vnet;
18 +
19 +import org.apache.karaf.shell.commands.Argument;
20 +import org.apache.karaf.shell.commands.Command;
21 +import org.onosproject.cli.AbstractShellCommand;
22 +import org.onosproject.incubator.net.virtual.TenantId;
23 +import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
24 +
25 +/**
26 + * Creates a new virtual network tenant.
27 + */
28 +@Command(scope = "onos", name = "vnet-remove-tenant",
29 + description = "Removes a virtual network tenant.")
30 +
31 +public class TenantRemoveCommand extends AbstractShellCommand {
32 +
33 + @Argument(index = 0, name = "id", description = "Tenant ID",
34 + required = true, multiValued = false)
35 + String id = null;
36 +
37 + @Override
38 + protected void execute() {
39 + VirtualNetworkAdminService service = get(VirtualNetworkAdminService.class);
40 + service.unregisterTenantId(TenantId.tenantId(id));
41 + print("Tenant successfully removed.");
42 + }
43 +}
...\ No newline at end of file ...\ No newline at end of file
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.cli.net.vnet;
18 +
19 +import org.apache.karaf.shell.commands.Argument;
20 +import org.apache.karaf.shell.commands.Command;
21 +import org.onosproject.cli.AbstractShellCommand;
22 +import org.onosproject.incubator.net.virtual.NetworkId;
23 +import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
24 +import org.onosproject.net.DeviceId;
25 +
26 +/**
27 + * Creates a new virtual device.
28 + */
29 +@Command(scope = "onos", name = "vnet-create-device",
30 + description = "Creates a new virtual device in a network.")
31 +public class VirtualDeviceCreateCommand extends AbstractShellCommand {
32 +
33 + @Argument(index = 0, name = "networkId", description = "Network ID",
34 + required = true, multiValued = false)
35 + Long networkId = null;
36 +
37 + @Argument(index = 1, name = "deviceId", description = "Device ID",
38 + required = true, multiValued = false)
39 + String deviceId = null;
40 +
41 + @Override
42 + protected void execute() {
43 + VirtualNetworkAdminService service = get(VirtualNetworkAdminService.class);
44 + service.createVirtualDevice(NetworkId.networkId(networkId), DeviceId.deviceId(deviceId));
45 + print("Virtual device successfully created.");
46 + }
47 +}
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.cli.net.vnet;
18 +
19 +import org.apache.karaf.shell.commands.Argument;
20 +import org.apache.karaf.shell.commands.Command;
21 +import org.onosproject.cli.AbstractShellCommand;
22 +import org.onosproject.incubator.net.virtual.NetworkId;
23 +import org.onosproject.incubator.net.virtual.VirtualDevice;
24 +import org.onosproject.incubator.net.virtual.VirtualNetworkService;
25 +import org.onosproject.utils.Comparators;
26 +
27 +import java.util.ArrayList;
28 +import java.util.Collections;
29 +import java.util.List;
30 +
31 +/**
32 + * Lists all virtual devices for the network ID.
33 + */
34 +@Command(scope = "onos", name = "vnet-devices",
35 + description = "Lists all virtual devices in a virtual network.")
36 +public class VirtualDeviceListCommand extends AbstractShellCommand {
37 +
38 + private static final String FMT_VIRTUAL_DEVICE =
39 + "deviceId=%s";
40 +
41 + @Argument(index = 0, name = "networkId", description = "Network ID",
42 + required = true, multiValued = false)
43 + Long networkId = null;
44 +
45 + @Override
46 + protected void execute() {
47 +
48 + getSortedVirtualDevices().forEach(this::printVirtualDevice);
49 + }
50 +
51 + /**
52 + * Returns the list of virtual devices sorted using the device identifier.
53 + *
54 + * @return sorted virtual device list
55 + */
56 + private List<VirtualDevice> getSortedVirtualDevices() {
57 + VirtualNetworkService service = get(VirtualNetworkService.class);
58 +
59 + List<VirtualDevice> virtualDevices = new ArrayList<>();
60 + virtualDevices.addAll(service.getVirtualDevices(NetworkId.networkId(networkId)));
61 + Collections.sort(virtualDevices, Comparators.VIRTUAL_DEVICE_COMPARATOR);
62 + return virtualDevices;
63 + }
64 +
65 + /**
66 + * Prints out each virtual device.
67 + *
68 + * @param virtualDevice virtual device
69 + */
70 + private void printVirtualDevice(VirtualDevice virtualDevice) {
71 + print(FMT_VIRTUAL_DEVICE, virtualDevice.id());
72 + }
73 +}
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.cli.net.vnet;
18 +
19 +import org.apache.karaf.shell.commands.Argument;
20 +import org.apache.karaf.shell.commands.Command;
21 +import org.onosproject.cli.AbstractShellCommand;
22 +import org.onosproject.incubator.net.virtual.NetworkId;
23 +import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
24 +import org.onosproject.net.DeviceId;
25 +
26 +/**
27 + * Removes a virtual device.
28 + */
29 +@Command(scope = "onos", name = "vnet-remove-device",
30 + description = "Removes a virtual device.")
31 +public class VirtualDeviceRemoveCommand extends AbstractShellCommand {
32 +
33 + @Argument(index = 0, name = "networkId", description = "Network ID",
34 + required = true, multiValued = false)
35 + Long networkId = null;
36 +
37 + @Argument(index = 1, name = "deviceId", description = "Device ID",
38 + required = true, multiValued = false)
39 + String deviceId = null;
40 +
41 + @Override
42 + protected void execute() {
43 + VirtualNetworkAdminService service = get(VirtualNetworkAdminService.class);
44 + service.removeVirtualDevice(NetworkId.networkId(networkId), DeviceId.deviceId(deviceId));
45 + print("Virtual device successfully removed.");
46 + }
47 +}
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.cli.net.vnet;
18 +
19 +import org.apache.karaf.shell.commands.Argument;
20 +import org.apache.karaf.shell.commands.Command;
21 +import org.apache.karaf.shell.commands.Option;
22 +import org.onosproject.cli.AbstractShellCommand;
23 +import org.onosproject.incubator.net.tunnel.TunnelId;
24 +import org.onosproject.incubator.net.virtual.NetworkId;
25 +import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
26 +import org.onosproject.net.ConnectPoint;
27 +import org.onosproject.net.DeviceId;
28 +import org.onosproject.net.PortNumber;
29 +
30 +/**
31 + * Creates a new virtual link.
32 + */
33 +@Command(scope = "onos", name = "vnet-create-link",
34 + description = "Creates a new virtual link in a network.")
35 +public class VirtualLinkCreateCommand extends AbstractShellCommand {
36 +
37 + @Argument(index = 0, name = "networkId", description = "Network ID",
38 + required = true, multiValued = false)
39 + Long networkId = null;
40 +
41 + @Argument(index = 1, name = "srcDeviceId", description = "Source device ID",
42 + required = true, multiValued = false)
43 + String srcDeviceId = null;
44 +
45 + @Argument(index = 2, name = "srcPortNum", description = "Source port number",
46 + required = true, multiValued = false)
47 + Integer srcPortNum = null;
48 +
49 + @Argument(index = 3, name = "dstDeviceId", description = "Destination device ID",
50 + required = true, multiValued = false)
51 + String dstDeviceId = null;
52 +
53 + @Argument(index = 4, name = "dstPortNum", description = "Destination port number",
54 + required = true, multiValued = false)
55 + Integer dstPortNum = null;
56 +
57 + @Option(name = "-b", aliases = "--bidirectional",
58 + description = "If this argument is passed in then the virtual link created will be bidirectional, " +
59 + "otherwise the link will be unidirectional.",
60 + required = false, multiValued = false)
61 + boolean bidirectional = false;
62 +
63 + @Override
64 + protected void execute() {
65 + VirtualNetworkAdminService service = get(VirtualNetworkAdminService.class);
66 + ConnectPoint src = new ConnectPoint(DeviceId.deviceId(srcDeviceId), PortNumber.portNumber(srcPortNum));
67 + ConnectPoint dst = new ConnectPoint(DeviceId.deviceId(dstDeviceId), PortNumber.portNumber(dstPortNum));
68 + //TODO use a real/valid tunnel ID
69 + TunnelId tunnelId = TunnelId.valueOf(0);
70 +
71 + service.createVirtualLink(NetworkId.networkId(networkId), src, dst, tunnelId);
72 + if (bidirectional) {
73 + service.createVirtualLink(NetworkId.networkId(networkId), dst, src, tunnelId);
74 + }
75 + print("Virtual link successfully created.");
76 + }
77 +}
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.cli.net.vnet;
18 +
19 +import org.apache.karaf.shell.commands.Argument;
20 +import org.apache.karaf.shell.commands.Command;
21 +import org.onosproject.cli.AbstractShellCommand;
22 +import org.onosproject.incubator.net.virtual.NetworkId;
23 +import org.onosproject.incubator.net.virtual.VirtualLink;
24 +import org.onosproject.incubator.net.virtual.VirtualNetworkService;
25 +
26 +import java.util.ArrayList;
27 +import java.util.List;
28 +
29 +/**
30 + * Lists all virtual links for the network ID.
31 + */
32 +@Command(scope = "onos", name = "vnet-links",
33 + description = "Lists all virtual links in a virtual network.")
34 +public class VirtualLinkListCommand extends AbstractShellCommand {
35 +
36 + private static final String FMT_VIRTUAL_LINK =
37 + "src=%s, dst=%s";
38 +
39 + @Argument(index = 0, name = "networkId", description = "Network ID",
40 + required = true, multiValued = false)
41 + Long networkId = null;
42 +
43 + @Override
44 + protected void execute() {
45 +
46 + getSortedVirtualLinks().forEach(this::printVirtualLink);
47 + }
48 +
49 + /**
50 + * Returns the list of virtual links sorted using the device identifier.
51 + *
52 + * @return virtual link list
53 + */
54 + private List<VirtualLink> getSortedVirtualLinks() {
55 + VirtualNetworkService service = get(VirtualNetworkService.class);
56 +
57 + List<VirtualLink> virtualLinks = new ArrayList<>();
58 + virtualLinks.addAll(service.getVirtualLinks(NetworkId.networkId(networkId)));
59 + return virtualLinks;
60 + }
61 +
62 + /**
63 + * Prints out each virtual link.
64 + *
65 + * @param virtualLink virtual link
66 + */
67 + private void printVirtualLink(VirtualLink virtualLink) {
68 + print(FMT_VIRTUAL_LINK, virtualLink.src().toString(), virtualLink.dst().toString());
69 + }
70 +}
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.cli.net.vnet;
18 +
19 +import org.apache.karaf.shell.commands.Argument;
20 +import org.apache.karaf.shell.commands.Command;
21 +import org.apache.karaf.shell.commands.Option;
22 +import org.onosproject.cli.AbstractShellCommand;
23 +import org.onosproject.incubator.net.virtual.NetworkId;
24 +import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
25 +import org.onosproject.net.ConnectPoint;
26 +import org.onosproject.net.DeviceId;
27 +import org.onosproject.net.PortNumber;
28 +
29 +/**
30 + * Removes a virtual link.
31 + */
32 +@Command(scope = "onos", name = "vnet-remove-link",
33 + description = "Removes a virtual link.")
34 +public class VirtualLinkRemoveCommand extends AbstractShellCommand {
35 +
36 + @Argument(index = 0, name = "networkId", description = "Network ID",
37 + required = true, multiValued = false)
38 + Long networkId = null;
39 +
40 + @Argument(index = 1, name = "srcDeviceId", description = "Source device ID",
41 + required = true, multiValued = false)
42 + String srcDeviceId = null;
43 +
44 + @Argument(index = 2, name = "srcPortNum", description = "Source port number",
45 + required = true, multiValued = false)
46 + Integer srcPortNum = null;
47 +
48 + @Argument(index = 3, name = "dstDeviceId", description = "Destination device ID",
49 + required = true, multiValued = false)
50 + String dstDeviceId = null;
51 +
52 + @Argument(index = 4, name = "dstPortNum", description = "Destination port number",
53 + required = true, multiValued = false)
54 + Integer dstPortNum = null;
55 +
56 + @Option(name = "-b", aliases = "--bidirectional",
57 + description = "If this argument is passed in then then bidirectional virtual link will be removed, " +
58 + "otherwise the unidirectional link will be removed.",
59 + required = false, multiValued = false)
60 + boolean bidirectional = false;
61 +
62 + @Override
63 + protected void execute() {
64 + VirtualNetworkAdminService service = get(VirtualNetworkAdminService.class);
65 + ConnectPoint src = new ConnectPoint(DeviceId.deviceId(srcDeviceId), PortNumber.portNumber(srcPortNum));
66 + ConnectPoint dst = new ConnectPoint(DeviceId.deviceId(dstDeviceId), PortNumber.portNumber(dstPortNum));
67 +
68 + service.removeVirtualLink(NetworkId.networkId(networkId), src, dst);
69 + if (bidirectional) {
70 + service.removeVirtualLink(NetworkId.networkId(networkId), dst, src);
71 + }
72 + print("Virtual link successfully removed.");
73 + }
74 +}
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.cli.net.vnet;
18 +
19 +import org.apache.karaf.shell.commands.Argument;
20 +import org.apache.karaf.shell.commands.Command;
21 +import org.onosproject.cli.AbstractShellCommand;
22 +import org.onosproject.incubator.net.virtual.TenantId;
23 +import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
24 +
25 +/**
26 + * Creates a new virtual network.
27 + */
28 +@Command(scope = "onos", name = "vnet-create",
29 + description = "Creates a new virtual network.")
30 +public class VirtualNetworkCreateCommand extends AbstractShellCommand {
31 +
32 + @Argument(index = 0, name = "id", description = "Tenant ID",
33 + required = true, multiValued = false)
34 + String id = null;
35 +
36 + @Override
37 + protected void execute() {
38 + VirtualNetworkAdminService service = get(VirtualNetworkAdminService.class);
39 + service.createVirtualNetwork(TenantId.tenantId(id));
40 + print("Virtual network successfully created.");
41 + }
42 +}
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.cli.net.vnet;
18 +
19 +import org.apache.karaf.shell.commands.Command;
20 +import org.onosproject.cli.AbstractShellCommand;
21 +import org.onosproject.incubator.net.virtual.TenantId;
22 +import org.onosproject.incubator.net.virtual.VirtualNetwork;
23 +import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
24 +import org.onosproject.incubator.net.virtual.VirtualNetworkService;
25 +import org.onosproject.utils.Comparators;
26 +
27 +import java.util.ArrayList;
28 +import java.util.Collections;
29 +import java.util.List;
30 +import java.util.Set;
31 +
32 +/**
33 + * Lists all virtual networks for the tenant ID.
34 + */
35 +@Command(scope = "onos", name = "vnets",
36 + description = "Lists all virtual networks.")
37 +public class VirtualNetworkListCommand extends AbstractShellCommand {
38 +
39 + private static final String FMT_VIRTUAL_NETWORK =
40 + "tenantId=%s, networkId=%s";
41 +
42 + @Override
43 + protected void execute() {
44 +
45 + getSortedVirtualNetworks().forEach(this::printVirtualNetwork);
46 + }
47 +
48 + /**
49 + * Returns the list of virtual networks sorted using the tenant identifier.
50 + *
51 + * @return sorted virtual network list
52 + */
53 + private List<VirtualNetwork> getSortedVirtualNetworks() {
54 + VirtualNetworkService service = get(VirtualNetworkService.class);
55 + VirtualNetworkAdminService adminService = get(VirtualNetworkAdminService.class);
56 +
57 + List<VirtualNetwork> virtualNetworks = new ArrayList<>();
58 +
59 + Set<TenantId> tenantSet = adminService.getTenantIds();
60 + tenantSet.forEach(tenantId -> virtualNetworks.addAll(service.getVirtualNetworks(tenantId)));
61 +
62 + Collections.sort(virtualNetworks, Comparators.VIRTUAL_NETWORK_COMPARATOR);
63 + return virtualNetworks;
64 + }
65 +
66 + /**
67 + * Prints out each virtual network.
68 + *
69 + * @param virtualNetwork virtual network
70 + */
71 + private void printVirtualNetwork(VirtualNetwork virtualNetwork) {
72 + print(FMT_VIRTUAL_NETWORK, virtualNetwork.tenantId(), virtualNetwork.id());
73 + }
74 +}
75 +
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.cli.net.vnet;
18 +
19 +import org.apache.karaf.shell.commands.Argument;
20 +import org.apache.karaf.shell.commands.Command;
21 +import org.onosproject.cli.AbstractShellCommand;
22 +import org.onosproject.incubator.net.virtual.NetworkId;
23 +import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
24 +
25 +/**
26 + * Removes a virtual network.
27 + */
28 +@Command(scope = "onos", name = "vnet-remove",
29 + description = "Removes a virtual network.")
30 +public class VirtualNetworkRemoveCommand extends AbstractShellCommand {
31 +
32 + @Argument(index = 0, name = "id", description = "Network ID",
33 + required = true, multiValued = false)
34 + Long id;
35 +
36 + @Override
37 + protected void execute() {
38 + VirtualNetworkAdminService service = get(VirtualNetworkAdminService.class);
39 + service.removeVirtualNetwork(NetworkId.networkId(id));
40 + print("Virtual network successfully removed.");
41 + }
42 +}
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.cli.net.vnet;
18 +
19 +import org.apache.karaf.shell.commands.Argument;
20 +import org.apache.karaf.shell.commands.Command;
21 +import org.onosproject.cli.AbstractShellCommand;
22 +import org.onosproject.incubator.net.virtual.NetworkId;
23 +import org.onosproject.incubator.net.virtual.VirtualDevice;
24 +import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
25 +import org.onosproject.incubator.net.virtual.VirtualNetworkService;
26 +import org.onosproject.net.DefaultAnnotations;
27 +import org.onosproject.net.DefaultDevice;
28 +import org.onosproject.net.DefaultPort;
29 +import org.onosproject.net.Device;
30 +import org.onosproject.net.DeviceId;
31 +import org.onosproject.net.Port;
32 +import org.onosproject.net.PortNumber;
33 +import org.onosproject.net.device.DeviceService;
34 +
35 +import java.util.Set;
36 +
37 +import static com.google.common.base.Preconditions.checkNotNull;
38 +
39 +/**
40 + * Creates a new virtual port.
41 + */
42 +@Command(scope = "onos", name = "vnet-create-port",
43 + description = "Creates a new virtual port in a network.")
44 +public class VirtualPortCreateCommand extends AbstractShellCommand {
45 +
46 + @Argument(index = 0, name = "networkId", description = "Network ID",
47 + required = true, multiValued = false)
48 + Long networkId = null;
49 +
50 + @Argument(index = 1, name = "deviceId", description = "Virtual Device ID",
51 + required = true, multiValued = false)
52 + String deviceId = null;
53 +
54 + @Argument(index = 2, name = "portNum", description = "Virtual device port number",
55 + required = true, multiValued = false)
56 + Integer portNum = null;
57 +
58 + @Argument(index = 3, name = "physDeviceId", description = "Physical Device ID",
59 + required = true, multiValued = false)
60 + String physDeviceId = null;
61 +
62 + @Argument(index = 4, name = "physPortNum", description = "Physical device port number",
63 + required = true, multiValued = false)
64 + Integer physPortNum = null;
65 +
66 + @Override
67 + protected void execute() {
68 + VirtualNetworkAdminService service = get(VirtualNetworkAdminService.class);
69 + DeviceService deviceService = get(DeviceService.class);
70 + VirtualDevice virtualDevice = getVirtualDevice(DeviceId.deviceId(deviceId));
71 + checkNotNull(virtualDevice, "The virtual device does not exist.");
72 +
73 + DefaultAnnotations annotations = DefaultAnnotations.builder().build();
74 + Device physDevice = new DefaultDevice(null, DeviceId.deviceId(physDeviceId),
75 + null, null, null, null, null, null, annotations);
76 + Port realizedBy = new DefaultPort(physDevice, PortNumber.portNumber(physPortNum), true);
77 + service.createVirtualPort(NetworkId.networkId(networkId), DeviceId.deviceId(deviceId),
78 + PortNumber.portNumber(portNum), realizedBy);
79 + print("Virtual port successfully created.");
80 + }
81 +
82 + /**
83 + * Returns the virtual device matching the device identifier.
84 + *
85 + * @param aDeviceId device identifier
86 + * @return matching virtual device, or null.
87 + */
88 + private VirtualDevice getVirtualDevice(DeviceId aDeviceId) {
89 + VirtualNetworkService service = get(VirtualNetworkService.class);
90 +
91 + Set<VirtualDevice> virtualDevices = service.getVirtualDevices(NetworkId.networkId(networkId));
92 +
93 + for (VirtualDevice virtualDevice : virtualDevices) {
94 + if (virtualDevice.id().equals(aDeviceId)) {
95 + return virtualDevice;
96 + }
97 + }
98 + return null;
99 + }
100 +}
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.cli.net.vnet;
18 +
19 +import org.apache.karaf.shell.commands.Argument;
20 +import org.apache.karaf.shell.commands.Command;
21 +import org.onosproject.cli.AbstractShellCommand;
22 +import org.onosproject.incubator.net.virtual.NetworkId;
23 +import org.onosproject.incubator.net.virtual.VirtualNetworkService;
24 +import org.onosproject.incubator.net.virtual.VirtualPort;
25 +import org.onosproject.net.DeviceId;
26 +import org.onosproject.utils.Comparators;
27 +
28 +import java.util.ArrayList;
29 +import java.util.Collections;
30 +import java.util.List;
31 +
32 +/**
33 + * Lists all virtual ports for the network ID.
34 + */
35 +@Command(scope = "onos", name = "vnet-ports",
36 + description = "Lists all virtual ports in a virtual network.")
37 +public class VirtualPortListCommand extends AbstractShellCommand {
38 +
39 + private static final String FMT_VIRTUAL_PORT =
40 + "virtual portNumber=%s, physical deviceId=%s, portNumber=%s";
41 +
42 + @Argument(index = 0, name = "networkId", description = "Network ID",
43 + required = true, multiValued = false)
44 + Long networkId = null;
45 +
46 + @Argument(index = 1, name = "deviceId", description = "Virtual Device ID",
47 + required = true, multiValued = false)
48 + String deviceId = null;
49 +
50 + @Override
51 + protected void execute() {
52 +
53 + getSortedVirtualPorts().forEach(this::printVirtualPort);
54 + }
55 +
56 + /**
57 + * Returns the list of virtual ports sorted using the network identifier.
58 + *
59 + * @return sorted virtual port list
60 + */
61 + private List<VirtualPort> getSortedVirtualPorts() {
62 + VirtualNetworkService service = get(VirtualNetworkService.class);
63 +
64 + List<VirtualPort> virtualPorts = new ArrayList<>();
65 + virtualPorts.addAll(service.getVirtualPorts(NetworkId.networkId(networkId), DeviceId.deviceId(deviceId)));
66 + Collections.sort(virtualPorts, Comparators.VIRTUAL_PORT_COMPARATOR);
67 + return virtualPorts;
68 + }
69 +
70 + /**
71 + * Prints out each virtual port.
72 + *
73 + * @param virtualPort virtual port
74 + */
75 + private void printVirtualPort(VirtualPort virtualPort) {
76 + print(FMT_VIRTUAL_PORT, virtualPort.number(),
77 + virtualPort.realizedBy().element().id(), virtualPort.realizedBy().number());
78 + }
79 +}
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.cli.net.vnet;
18 +
19 +import org.apache.karaf.shell.commands.Argument;
20 +import org.apache.karaf.shell.commands.Command;
21 +import org.onosproject.cli.AbstractShellCommand;
22 +import org.onosproject.incubator.net.virtual.NetworkId;
23 +import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
24 +import org.onosproject.net.DeviceId;
25 +import org.onosproject.net.PortNumber;
26 +
27 +/**
28 + * Removes a virtual port.
29 + */
30 +@Command(scope = "onos", name = "vnet-remove-port",
31 + description = "Removes a virtual port.")
32 +public class VirtualPortRemoveCommand extends AbstractShellCommand {
33 +
34 + @Argument(index = 0, name = "networkId", description = "Network ID",
35 + required = true, multiValued = false)
36 + Long networkId = null;
37 +
38 + @Argument(index = 1, name = "deviceId", description = "Device ID",
39 + required = true, multiValued = false)
40 + String deviceId = null;
41 +
42 + @Argument(index = 2, name = "portNum", description = "Device port number",
43 + required = true, multiValued = false)
44 + Integer portNum = null;
45 +
46 + @Override
47 + protected void execute() {
48 + VirtualNetworkAdminService service = get(VirtualNetworkAdminService.class);
49 + service.removeVirtualPort(NetworkId.networkId(networkId), DeviceId.deviceId(deviceId),
50 + PortNumber.portNumber(portNum));
51 + print("Virtual port successfully removed.");
52 + }
53 +}
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 +/**
18 + * CLI commands for querying and administering virtual networks.
19 + */
20 +package org.onosproject.cli.net.vnet;
...\ No newline at end of file ...\ No newline at end of file
1 <!-- 1 <!--
2 - ~ Copyright 2014 Open Networking Laboratory 2 + ~ Copyright 2016 Open Networking Laboratory
3 ~ 3 ~
4 ~ Licensed under the Apache License, Version 2.0 (the "License"); 4 ~ Licensed under the Apache License, Version 2.0 (the "License");
5 ~ you may not use this file except in compliance with the License. 5 ~ you may not use this file except in compliance with the License.
...@@ -613,6 +613,53 @@ ...@@ -613,6 +613,53 @@
613 <ref component-id="deviceIdCompleter"/> 613 <ref component-id="deviceIdCompleter"/>
614 </completers> 614 </completers>
615 </command> 615 </command>
616 +
617 + <!--virtual network commands -->
618 + <command>
619 + <action class="org.onosproject.cli.net.vnet.TenantListCommand"/>
620 + </command>
621 + <command>
622 + <action class="org.onosproject.cli.net.vnet.TenantAddCommand"/>
623 + </command>
624 + <command>
625 + <action class="org.onosproject.cli.net.vnet.TenantRemoveCommand"/>
626 + </command>
627 + <command>
628 + <action class="org.onosproject.cli.net.vnet.VirtualNetworkListCommand"/>
629 + </command>
630 + <command>
631 + <action class="org.onosproject.cli.net.vnet.VirtualNetworkCreateCommand"/>
632 + </command>
633 + <command>
634 + <action class="org.onosproject.cli.net.vnet.VirtualNetworkRemoveCommand"/>
635 + </command>
636 + <command>
637 + <action class="org.onosproject.cli.net.vnet.VirtualDeviceListCommand"/>
638 + </command>
639 + <command>
640 + <action class="org.onosproject.cli.net.vnet.VirtualDeviceCreateCommand"/>
641 + </command>
642 + <command>
643 + <action class="org.onosproject.cli.net.vnet.VirtualDeviceRemoveCommand"/>
644 + </command>
645 + <command>
646 + <action class="org.onosproject.cli.net.vnet.VirtualLinkListCommand"/>
647 + </command>
648 + <command>
649 + <action class="org.onosproject.cli.net.vnet.VirtualLinkCreateCommand"/>
650 + </command>
651 + <command>
652 + <action class="org.onosproject.cli.net.vnet.VirtualLinkRemoveCommand"/>
653 + </command>
654 + <command>
655 + <action class="org.onosproject.cli.net.vnet.VirtualPortListCommand"/>
656 + </command>
657 + <command>
658 + <action class="org.onosproject.cli.net.vnet.VirtualPortCreateCommand"/>
659 + </command>
660 + <command>
661 + <action class="org.onosproject.cli.net.vnet.VirtualPortRemoveCommand"/>
662 + </command>
616 </command-bundle> 663 </command-bundle>
617 664
618 <bean id="reviewAppNameCompleter" class="org.onosproject.cli.security.ReviewApplicationNameCompleter"/> 665 <bean id="reviewAppNameCompleter" class="org.onosproject.cli.security.ReviewApplicationNameCompleter"/>
......
...@@ -19,6 +19,10 @@ import org.onosproject.cluster.ControllerNode; ...@@ -19,6 +19,10 @@ import org.onosproject.cluster.ControllerNode;
19 import org.onosproject.core.Application; 19 import org.onosproject.core.Application;
20 import org.onosproject.core.ApplicationId; 20 import org.onosproject.core.ApplicationId;
21 import org.onosproject.incubator.net.intf.Interface; 21 import org.onosproject.incubator.net.intf.Interface;
22 +import org.onosproject.incubator.net.virtual.TenantId;
23 +import org.onosproject.incubator.net.virtual.VirtualDevice;
24 +import org.onosproject.incubator.net.virtual.VirtualNetwork;
25 +import org.onosproject.incubator.net.virtual.VirtualPort;
22 import org.onosproject.net.key.DeviceKey; 26 import org.onosproject.net.key.DeviceKey;
23 import org.onosproject.net.ConnectPoint; 27 import org.onosproject.net.ConnectPoint;
24 import org.onosproject.net.Element; 28 import org.onosproject.net.Element;
...@@ -149,4 +153,32 @@ public final class Comparators { ...@@ -149,4 +153,32 @@ public final class Comparators {
149 return region1.id().toString().compareTo(region2.id().toString()); 153 return region1.id().toString().compareTo(region2.id().toString());
150 } 154 }
151 }; 155 };
156 +
157 + public static final Comparator<TenantId> TENANT_ID_COMPARATOR = new Comparator<TenantId>() {
158 + @Override
159 + public int compare(TenantId tenant1, TenantId tenant2) {
160 + return tenant1.id().toString().compareTo(tenant2.id().toString());
161 + }
162 + };
163 +
164 + public static final Comparator<VirtualNetwork> VIRTUAL_NETWORK_COMPARATOR = new Comparator<VirtualNetwork>() {
165 + @Override
166 + public int compare(VirtualNetwork virtualNetwork1, VirtualNetwork virtualNetwork2) {
167 + return virtualNetwork1.tenantId().toString().compareTo(virtualNetwork2.tenantId().toString());
168 + }
169 + };
170 +
171 + public static final Comparator<VirtualDevice> VIRTUAL_DEVICE_COMPARATOR = new Comparator<VirtualDevice>() {
172 + @Override
173 + public int compare(VirtualDevice virtualDevice1, VirtualDevice virtualDevice2) {
174 + return virtualDevice1.id().toString().compareTo(virtualDevice2.id().toString());
175 + }
176 + };
177 +
178 + public static final Comparator<VirtualPort> VIRTUAL_PORT_COMPARATOR = new Comparator<VirtualPort>() {
179 + @Override
180 + public int compare(VirtualPort virtualPort1, VirtualPort virtualPort2) {
181 + return virtualPort1.number().toString().compareTo(virtualPort2.number().toString());
182 + }
183 + };
152 } 184 }
......
...@@ -146,17 +146,30 @@ public class VirtualNetworkManagerTest { ...@@ -146,17 +146,30 @@ public class VirtualNetworkManagerTest {
146 assertNotNull("The virtual network set should not be null", virtualNetworks); 146 assertNotNull("The virtual network set should not be null", virtualNetworks);
147 assertEquals("The virtual network set size did not match.", 2, virtualNetworks.size()); 147 assertEquals("The virtual network set size did not match.", 2, virtualNetworks.size());
148 148
149 + int remaining = virtualNetworks.size();
149 for (VirtualNetwork virtualNetwork : virtualNetworks) { 150 for (VirtualNetwork virtualNetwork : virtualNetworks) {
150 manager.removeVirtualNetwork(virtualNetwork.id()); 151 manager.removeVirtualNetwork(virtualNetwork.id());
152 + assertEquals("The expected virtual network size does not match",
153 + --remaining, manager.getVirtualNetworks(TenantId.tenantId(tenantIdValue1)).size());
151 // attempt to remove the same virtual network again. 154 // attempt to remove the same virtual network again.
152 manager.removeVirtualNetwork(virtualNetwork.id()); 155 manager.removeVirtualNetwork(virtualNetwork.id());
156 + assertEquals("The expected virtual network size does not match",
157 + remaining, manager.getVirtualNetworks(TenantId.tenantId(tenantIdValue1)).size());
153 } 158 }
154 virtualNetworks = manager.getVirtualNetworks(TenantId.tenantId(tenantIdValue1)); 159 virtualNetworks = manager.getVirtualNetworks(TenantId.tenantId(tenantIdValue1));
155 assertTrue("The virtual network set should be empty.", virtualNetworks.isEmpty()); 160 assertTrue("The virtual network set should be empty.", virtualNetworks.isEmpty());
156 161
162 + // Create/remove a virtual network.
163 + VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
164 + manager.removeVirtualNetwork(virtualNetwork.id());
165 +
166 + virtualNetworks = manager.getVirtualNetworks(TenantId.tenantId(tenantIdValue1));
167 + assertTrue("The virtual network set should be empty.", virtualNetworks.isEmpty());
168 +
157 // Validate that the events were all received in the correct order. 169 // Validate that the events were all received in the correct order.
158 validateEvents(VirtualNetworkEvent.Type.TENANT_REGISTERED, VirtualNetworkEvent.Type.NETWORK_ADDED, 170 validateEvents(VirtualNetworkEvent.Type.TENANT_REGISTERED, VirtualNetworkEvent.Type.NETWORK_ADDED,
159 VirtualNetworkEvent.Type.NETWORK_ADDED, VirtualNetworkEvent.Type.NETWORK_REMOVED, 171 VirtualNetworkEvent.Type.NETWORK_ADDED, VirtualNetworkEvent.Type.NETWORK_REMOVED,
172 + VirtualNetworkEvent.Type.NETWORK_REMOVED, VirtualNetworkEvent.Type.NETWORK_ADDED,
160 VirtualNetworkEvent.Type.NETWORK_REMOVED); 173 VirtualNetworkEvent.Type.NETWORK_REMOVED);
161 } 174 }
162 175
...@@ -209,6 +222,13 @@ public class VirtualNetworkManagerTest { ...@@ -209,6 +222,13 @@ public class VirtualNetworkManagerTest {
209 virtualDevices1 = manager.getVirtualDevices(virtualNetwork1.id()); 222 virtualDevices1 = manager.getVirtualDevices(virtualNetwork1.id());
210 assertTrue("The virtual device set should be empty.", virtualDevices1.isEmpty()); 223 assertTrue("The virtual device set should be empty.", virtualDevices1.isEmpty());
211 224
225 + // Add/remove the virtual device again.
226 + VirtualDevice virtualDevice = manager.createVirtualDevice(virtualNetwork1.id(),
227 + DeviceId.deviceId(deviceIdValue1));
228 + manager.removeVirtualDevice(virtualDevice.networkId(), virtualDevice.id());
229 + virtualDevices1 = manager.getVirtualDevices(virtualNetwork1.id());
230 + assertTrue("The virtual device set should be empty.", virtualDevices1.isEmpty());
231 +
212 // Validate that the events were all received in the correct order. 232 // Validate that the events were all received in the correct order.
213 validateEvents(VirtualNetworkEvent.Type.TENANT_REGISTERED, VirtualNetworkEvent.Type.NETWORK_ADDED, 233 validateEvents(VirtualNetworkEvent.Type.TENANT_REGISTERED, VirtualNetworkEvent.Type.NETWORK_ADDED,
214 VirtualNetworkEvent.Type.NETWORK_ADDED); 234 VirtualNetworkEvent.Type.NETWORK_ADDED);
...@@ -241,6 +261,12 @@ public class VirtualNetworkManagerTest { ...@@ -241,6 +261,12 @@ public class VirtualNetworkManagerTest {
241 } 261 }
242 virtualLinks = manager.getVirtualLinks(virtualNetwork1.id()); 262 virtualLinks = manager.getVirtualLinks(virtualNetwork1.id());
243 assertTrue("The virtual link set should be empty.", virtualLinks.isEmpty()); 263 assertTrue("The virtual link set should be empty.", virtualLinks.isEmpty());
264 +
265 + // Add/remove the virtual link again.
266 + VirtualLink virtualLink = manager.createVirtualLink(virtualNetwork1.id(), src, dst, TunnelId.valueOf(0));
267 + manager.removeVirtualLink(virtualLink.networkId(), virtualLink.src(), virtualLink.dst());
268 + virtualLinks = manager.getVirtualLinks(virtualNetwork1.id());
269 + assertTrue("The virtual link set should be empty.", virtualLinks.isEmpty());
244 } 270 }
245 271
246 /** 272 /**
...@@ -250,19 +276,18 @@ public class VirtualNetworkManagerTest { ...@@ -250,19 +276,18 @@ public class VirtualNetworkManagerTest {
250 public void testAddRemoveVirtualPort() { 276 public void testAddRemoveVirtualPort() {
251 manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); 277 manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
252 VirtualNetwork virtualNetwork1 = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); 278 VirtualNetwork virtualNetwork1 = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
253 - VirtualDevice srcVirtualDevice = 279 + VirtualDevice virtualDevice =
254 manager.createVirtualDevice(virtualNetwork1.id(), DeviceId.deviceId(deviceIdValue1)); 280 manager.createVirtualDevice(virtualNetwork1.id(), DeviceId.deviceId(deviceIdValue1));
255 - VirtualDevice dstVirtualDevice = 281 + Port port = new DefaultPort(virtualDevice, PortNumber.portNumber(1), true);
256 - manager.createVirtualDevice(virtualNetwork1.id(), DeviceId.deviceId(deviceIdValue2));
257 - Port port = new DefaultPort(srcVirtualDevice, PortNumber.portNumber(1), true);
258 282
259 - manager.createVirtualPort(virtualNetwork1.id(), srcVirtualDevice.id(), PortNumber.portNumber(1), port); 283 + manager.createVirtualPort(virtualNetwork1.id(), virtualDevice.id(), PortNumber.portNumber(1), port);
260 - manager.createVirtualPort(virtualNetwork1.id(), dstVirtualDevice.id(), PortNumber.portNumber(1), port); 284 + manager.createVirtualPort(virtualNetwork1.id(), virtualDevice.id(), PortNumber.portNumber(2), port);
261 285
262 - Set<VirtualPort> virtualPorts = manager.getVirtualPorts(virtualNetwork1.id(), srcVirtualDevice.id()); 286 + Set<VirtualPort> virtualPorts = manager.getVirtualPorts(virtualNetwork1.id(), virtualDevice.id());
263 assertNotNull("The virtual port set should not be null", virtualPorts); 287 assertNotNull("The virtual port set should not be null", virtualPorts);
264 assertEquals("The virtual port set size did not match.", 2, virtualPorts.size()); 288 assertEquals("The virtual port set size did not match.", 2, virtualPorts.size());
265 289
290 +
266 for (VirtualPort virtualPort : virtualPorts) { 291 for (VirtualPort virtualPort : virtualPorts) {
267 manager.removeVirtualPort(virtualNetwork1.id(), 292 manager.removeVirtualPort(virtualNetwork1.id(),
268 (DeviceId) virtualPort.element().id(), virtualPort.number()); 293 (DeviceId) virtualPort.element().id(), virtualPort.number());
...@@ -270,7 +295,14 @@ public class VirtualNetworkManagerTest { ...@@ -270,7 +295,14 @@ public class VirtualNetworkManagerTest {
270 manager.removeVirtualPort(virtualNetwork1.id(), 295 manager.removeVirtualPort(virtualNetwork1.id(),
271 (DeviceId) virtualPort.element().id(), virtualPort.number()); 296 (DeviceId) virtualPort.element().id(), virtualPort.number());
272 } 297 }
273 - virtualPorts = manager.getVirtualPorts(virtualNetwork1.id(), srcVirtualDevice.id()); 298 + virtualPorts = manager.getVirtualPorts(virtualNetwork1.id(), virtualDevice.id());
299 + assertTrue("The virtual port set should be empty.", virtualPorts.isEmpty());
300 +
301 + // Add/remove the virtual port again.
302 + VirtualPort virtualPort = manager.createVirtualPort(virtualNetwork1.id(), virtualDevice.id(),
303 + PortNumber.portNumber(1), port);
304 + manager.removeVirtualPort(virtualNetwork1.id(), (DeviceId) virtualPort.element().id(), virtualPort.number());
305 + virtualPorts = manager.getVirtualPorts(virtualNetwork1.id(), virtualDevice.id());
274 assertTrue("The virtual port set should be empty.", virtualPorts.isEmpty()); 306 assertTrue("The virtual port set should be empty.", virtualPorts.isEmpty());
275 } 307 }
276 308
...@@ -306,6 +338,9 @@ public class VirtualNetworkManagerTest { ...@@ -306,6 +338,9 @@ public class VirtualNetworkManagerTest {
306 338
307 } 339 }
308 340
341 + /**
342 + * Core service test class.
343 + */
309 private class TestCoreService extends CoreServiceAdapter { 344 private class TestCoreService extends CoreServiceAdapter {
310 345
311 @Override 346 @Override
......
...@@ -42,6 +42,8 @@ import org.onosproject.incubator.net.virtual.VirtualNetworkStore; ...@@ -42,6 +42,8 @@ import org.onosproject.incubator.net.virtual.VirtualNetworkStore;
42 import org.onosproject.incubator.net.virtual.VirtualNetworkStoreDelegate; 42 import org.onosproject.incubator.net.virtual.VirtualNetworkStoreDelegate;
43 import org.onosproject.incubator.net.virtual.VirtualPort; 43 import org.onosproject.incubator.net.virtual.VirtualPort;
44 import org.onosproject.net.ConnectPoint; 44 import org.onosproject.net.ConnectPoint;
45 +import org.onosproject.net.DefaultDevice;
46 +import org.onosproject.net.DefaultPort;
45 import org.onosproject.net.Device; 47 import org.onosproject.net.Device;
46 import org.onosproject.net.DeviceId; 48 import org.onosproject.net.DeviceId;
47 import org.onosproject.net.Port; 49 import org.onosproject.net.Port;
...@@ -123,11 +125,18 @@ public class DistributedVirtualNetworkStore ...@@ -123,11 +125,18 @@ public class DistributedVirtualNetworkStore
123 .register(TenantId.class) 125 .register(TenantId.class)
124 .register(NetworkId.class).register(DeviceId.class) 126 .register(NetworkId.class).register(DeviceId.class)
125 .register(VirtualNetwork.class) 127 .register(VirtualNetwork.class)
128 + .register(DefaultVirtualNetwork.class)
126 .register(VirtualDevice.class) 129 .register(VirtualDevice.class)
130 + .register(DefaultVirtualDevice.class)
127 .register(VirtualLink.class) 131 .register(VirtualLink.class)
132 + .register(DefaultVirtualLink.class)
128 .register(VirtualPort.class) 133 .register(VirtualPort.class)
134 + .register(DefaultVirtualPort.class)
129 .register(DeviceId.class) 135 .register(DeviceId.class)
130 .register(Device.class) 136 .register(Device.class)
137 + .register(TunnelId.class)
138 + .register(DefaultDevice.class)
139 + .register(DefaultPort.class)
131 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID).build()); 140 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID).build());
132 141
133 /** 142 /**
...@@ -233,12 +242,14 @@ public class DistributedVirtualNetworkStore ...@@ -233,12 +242,14 @@ public class DistributedVirtualNetworkStore
233 VirtualNetwork virtualNetwork = new DefaultVirtualNetwork(genNetworkId(), tenantId); 242 VirtualNetwork virtualNetwork = new DefaultVirtualNetwork(genNetworkId(), tenantId);
234 //TODO update both maps in one transaction. 243 //TODO update both maps in one transaction.
235 networkIdVirtualNetworkMap.put(virtualNetwork.id(), virtualNetwork); 244 networkIdVirtualNetworkMap.put(virtualNetwork.id(), virtualNetwork);
236 - Set<NetworkId> virtualNetworkSet = tenantIdNetworkIdSetMap.get(tenantId); 245 +
237 - if (virtualNetworkSet == null) { 246 + Set<NetworkId> networkIdSet = tenantIdNetworkIdSetMap.get(tenantId);
238 - virtualNetworkSet = new HashSet<>(); 247 + if (networkIdSet == null) {
248 + networkIdSet = new HashSet<>();
239 } 249 }
240 - virtualNetworkSet.add(virtualNetwork.id()); 250 + networkIdSet.add(virtualNetwork.id());
241 - tenantIdNetworkIdSetMap.put(tenantId, virtualNetworkSet); 251 + tenantIdNetworkIdSetMap.put(tenantId, networkIdSet);
252 +
242 return virtualNetwork; 253 return virtualNetwork;
243 } 254 }
244 255
...@@ -256,24 +267,26 @@ public class DistributedVirtualNetworkStore ...@@ -256,24 +267,26 @@ public class DistributedVirtualNetworkStore
256 public void removeNetwork(NetworkId networkId) { 267 public void removeNetwork(NetworkId networkId) {
257 // Make sure that the virtual network exists before attempting to remove it. 268 // Make sure that the virtual network exists before attempting to remove it.
258 if (networkExists(networkId)) { 269 if (networkExists(networkId)) {
259 - VirtualNetwork virtualNetwork = networkIdVirtualNetworkMap.get(networkId); 270 + //TODO update both maps in one transaction.
271 +
272 + VirtualNetwork virtualNetwork = networkIdVirtualNetworkMap.remove(networkId);
260 if (virtualNetwork == null) { 273 if (virtualNetwork == null) {
261 return; 274 return;
262 } 275 }
263 - //TODO update both maps in one transaction.
264 TenantId tenantId = virtualNetwork.tenantId(); 276 TenantId tenantId = virtualNetwork.tenantId();
265 - networkIdVirtualNetworkMap.compute(networkId, (id, existingVirtualNetwork) -> null);
266 277
278 + Set<NetworkId> networkIdSet = new HashSet<>();
279 + tenantIdNetworkIdSetMap.get(tenantId).forEach(networkId1 -> {
280 + if (networkId1.id().equals(networkId.id())) {
281 + networkIdSet.add(networkId1);
282 + }
283 + });
267 284
268 - Set<NetworkId> virtualNetworkSet = tenantIdNetworkIdSetMap.get(tenantId);
269 tenantIdNetworkIdSetMap.compute(virtualNetwork.tenantId(), (id, existingNetworkIds) -> { 285 tenantIdNetworkIdSetMap.compute(virtualNetwork.tenantId(), (id, existingNetworkIds) -> {
270 if (existingNetworkIds == null || existingNetworkIds.isEmpty()) { 286 if (existingNetworkIds == null || existingNetworkIds.isEmpty()) {
271 - return ImmutableSet.of(); 287 + return new HashSet<NetworkId>();
272 } else { 288 } else {
273 - return ImmutableSet.<NetworkId>builder() 289 + return new HashSet<NetworkId>(Sets.difference(existingNetworkIds, networkIdSet));
274 - .addAll(Sets.difference(existingNetworkIds,
275 - ImmutableSet.copyOf(virtualNetworkSet)))
276 - .build();
277 } 290 }
278 }); 291 });
279 } 292 }
...@@ -308,22 +321,24 @@ public class DistributedVirtualNetworkStore ...@@ -308,22 +321,24 @@ public class DistributedVirtualNetworkStore
308 public void removeDevice(NetworkId networkId, DeviceId deviceId) { 321 public void removeDevice(NetworkId networkId, DeviceId deviceId) {
309 checkState(networkExists(networkId), "The network has not been added."); 322 checkState(networkExists(networkId), "The network has not been added.");
310 //TODO update both maps in one transaction. 323 //TODO update both maps in one transaction.
311 - Set<DeviceId> deviceIdSet = networkIdDeviceIdSetMap.get(networkId); 324 +
325 + Set<DeviceId> deviceIdSet = new HashSet<>();
326 + networkIdDeviceIdSetMap.get(networkId).forEach(deviceId1 -> {
327 + if (deviceId1.equals(deviceId)) {
328 + deviceIdSet.add(deviceId1);
329 + }
330 + });
331 +
312 if (deviceIdSet != null) { 332 if (deviceIdSet != null) {
313 networkIdDeviceIdSetMap.compute(networkId, (id, existingDeviceIds) -> { 333 networkIdDeviceIdSetMap.compute(networkId, (id, existingDeviceIds) -> {
314 if (existingDeviceIds == null || existingDeviceIds.isEmpty()) { 334 if (existingDeviceIds == null || existingDeviceIds.isEmpty()) {
315 - return ImmutableSet.of(); 335 + return new HashSet<DeviceId>();
316 } else { 336 } else {
317 - return ImmutableSet.<DeviceId>builder() 337 + return new HashSet<DeviceId>(Sets.difference(existingDeviceIds, deviceIdSet));
318 - .addAll(Sets.difference(existingDeviceIds,
319 - ImmutableSet.copyOf(deviceIdSet)))
320 - .build();
321 } 338 }
322 }); 339 });
323 340
324 - deviceIdVirtualDeviceMap.compute(deviceId, (id, existingVirtualDevice) -> null); 341 + deviceIdVirtualDeviceMap.remove(deviceId);
325 -
326 - log.info("The deviceIdVirtualDeviceMap size is: " + getDevices(networkId));
327 } 342 }
328 } 343 }
329 344
...@@ -343,16 +358,20 @@ public class DistributedVirtualNetworkStore ...@@ -343,16 +358,20 @@ public class DistributedVirtualNetworkStore
343 @Override 358 @Override
344 public void removeLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst) { 359 public void removeLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst) {
345 checkState(networkExists(networkId), "The network has not been added."); 360 checkState(networkExists(networkId), "The network has not been added.");
346 - Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId); 361 +
362 + Set<VirtualLink> virtualLinkSet = new HashSet<>();
363 + networkIdVirtualLinkSetMap.get(networkId).forEach(link -> {
364 + if (link.src().equals(src) && link.dst().equals(dst)) {
365 + virtualLinkSet.add(link);
366 + }
367 + });
368 +
347 if (virtualLinkSet != null) { 369 if (virtualLinkSet != null) {
348 networkIdVirtualLinkSetMap.compute(networkId, (id, existingVirtualLinks) -> { 370 networkIdVirtualLinkSetMap.compute(networkId, (id, existingVirtualLinks) -> {
349 if (existingVirtualLinks == null || existingVirtualLinks.isEmpty()) { 371 if (existingVirtualLinks == null || existingVirtualLinks.isEmpty()) {
350 - return ImmutableSet.of(); 372 + return new HashSet<VirtualLink>();
351 } else { 373 } else {
352 - return ImmutableSet.<VirtualLink>builder() 374 + return new HashSet<VirtualLink>(Sets.difference(existingVirtualLinks, virtualLinkSet));
353 - .addAll(Sets.difference(existingVirtualLinks,
354 - ImmutableSet.copyOf(virtualLinkSet)))
355 - .build();
356 } 375 }
357 }); 376 });
358 } 377 }
...@@ -376,16 +395,20 @@ public class DistributedVirtualNetworkStore ...@@ -376,16 +395,20 @@ public class DistributedVirtualNetworkStore
376 @Override 395 @Override
377 public void removePort(NetworkId networkId, DeviceId deviceId, PortNumber portNumber) { 396 public void removePort(NetworkId networkId, DeviceId deviceId, PortNumber portNumber) {
378 checkState(networkExists(networkId), "The network has not been added."); 397 checkState(networkExists(networkId), "The network has not been added.");
379 - Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId); 398 +
399 + Set<VirtualPort> virtualPortSet = new HashSet<>();
400 + networkIdVirtualPortSetMap.get(networkId).forEach(port -> {
401 + if (port.element().id().equals(deviceId) && port.number().equals(portNumber)) {
402 + virtualPortSet.add(port);
403 + }
404 + });
405 +
380 if (virtualPortSet != null) { 406 if (virtualPortSet != null) {
381 networkIdVirtualPortSetMap.compute(networkId, (id, existingVirtualPorts) -> { 407 networkIdVirtualPortSetMap.compute(networkId, (id, existingVirtualPorts) -> {
382 if (existingVirtualPorts == null || existingVirtualPorts.isEmpty()) { 408 if (existingVirtualPorts == null || existingVirtualPorts.isEmpty()) {
383 - return ImmutableSet.of(); 409 + return new HashSet<VirtualPort>();
384 } else { 410 } else {
385 - return ImmutableSet.<VirtualPort>builder() 411 + return new HashSet<VirtualPort>(Sets.difference(existingVirtualPorts, virtualPortSet));
386 - .addAll(Sets.difference(existingVirtualPorts,
387 - ImmutableSet.copyOf(virtualPortSet)))
388 - .build();
389 } 412 }
390 }); 413 });
391 } 414 }
...@@ -415,17 +438,28 @@ public class DistributedVirtualNetworkStore ...@@ -415,17 +438,28 @@ public class DistributedVirtualNetworkStore
415 @Override 438 @Override
416 public Set<VirtualLink> getLinks(NetworkId networkId) { 439 public Set<VirtualLink> getLinks(NetworkId networkId) {
417 checkState(networkExists(networkId), "The network has not been added."); 440 checkState(networkExists(networkId), "The network has not been added.");
418 - Set<VirtualLink> virtualLinkSet = new HashSet<>(); 441 + Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId);
419 - virtualLinkSet.addAll(networkIdVirtualLinkSetMap.get(networkId)); 442 + if (virtualLinkSet == null) {
443 + virtualLinkSet = new HashSet<>();
444 + }
420 return ImmutableSet.copyOf(virtualLinkSet); 445 return ImmutableSet.copyOf(virtualLinkSet);
421 } 446 }
422 447
423 @Override 448 @Override
424 public Set<VirtualPort> getPorts(NetworkId networkId, DeviceId deviceId) { 449 public Set<VirtualPort> getPorts(NetworkId networkId, DeviceId deviceId) {
425 checkState(networkExists(networkId), "The network has not been added."); 450 checkState(networkExists(networkId), "The network has not been added.");
426 - Set<VirtualPort> virtualPortSet = new HashSet<>(); 451 + Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId);
427 - virtualPortSet.addAll(networkIdVirtualPortSetMap.get(networkId)); 452 + if (virtualPortSet == null) {
428 - return ImmutableSet.copyOf(virtualPortSet); 453 + virtualPortSet = new HashSet<>();
454 + }
455 +
456 + Set<VirtualPort> portSet = new HashSet<>();
457 + virtualPortSet.forEach(virtualPort -> {
458 + if (virtualPort.element().id().equals(deviceId)) {
459 + portSet.add(virtualPort);
460 + }
461 + });
462 + return ImmutableSet.copyOf(portSet);
429 } 463 }
430 464
431 /** 465 /**
......