aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/cli/src/main/java/org/onosproject/cli/net/LinkResourceTestCommand.java
blob: d68084c7040d0752c7d78d9c79f58fdf3817ecd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package org.onosproject.cli.net;

import java.util.Set;
import java.util.List;

import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.apache.karaf.shell.commands.Option;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.net.intent.IntentId;
import org.onosproject.net.resource.link.DefaultLinkResourceRequest;
import org.onosproject.net.resource.link.LinkResourceAllocations;
import org.onosproject.net.resource.link.LinkResourceRequest;
import org.onosproject.net.resource.link.LinkResourceService;
import org.onosproject.net.topology.PathService;
import org.onosproject.net.DeviceId;
import org.onosproject.net.Link;
import org.onosproject.net.Path;

import com.google.common.collect.Lists;

/**
 * Commands to test out LinkResourceManager directly.
 */
@Command(scope = "onos", name = "resource-request",
        description = "request or remove resources")
public class LinkResourceTestCommand extends AbstractShellCommand {

    // default is bandwidth.
    @Option(name = "-m", aliases = "--mpls", description = "MPLS resource",
            required = false, multiValued = false)
    private boolean isMPLS = false;

    @Option(name = "-o", aliases = "--optical", description = "Optical resource",
            required = false, multiValued = false)
    private boolean isOptical = false;

    @Option(name = "-d", aliases = "--delete", description = "Delete resource by intent ID",
            required = false, multiValued = false)
    private boolean remove = false;

    @Argument(index = 0, name = "srcString", description = "Link source",
            required = true, multiValued = false)
    String srcString = null;

    @Argument(index = 1, name = "dstString", description = "Link destination",
            required = true, multiValued = false)
    String dstString = null;

    @Argument(index = 2, name = "id", description = "Identifier",
            required = true, multiValued = false)
    int id;

    private LinkResourceService resService;
    private PathService pathService;

    private static final int BANDWIDTH = 1_000_000;

    @Override
    protected void execute() {
        resService = get(LinkResourceService.class);
        pathService = get(PathService.class);

        DeviceId src = DeviceId.deviceId(getDeviceId(srcString));
        DeviceId dst = DeviceId.deviceId(getDeviceId(dstString));
        IntentId intId = IntentId.valueOf(id);

        Set<Path> paths = pathService.getPaths(src, dst);

        if (paths == null || paths.isEmpty()) {
            print("No path between %s and %s", srcString, dstString);
            return;
        }

        if (remove) {
            LinkResourceAllocations lra = resService.getAllocations(intId);
            resService.releaseResources(lra);
            return;
        }

        for (Path p : paths) {
            List<Link> links = p.links();
            LinkResourceRequest.Builder request = null;
            if (isMPLS) {
                List<Link> nlinks = Lists.newArrayList();
                try {
                    nlinks.addAll(links.subList(1, links.size() - 2));
                    request = DefaultLinkResourceRequest.builder(intId, nlinks)
                            .addMplsRequest();
                } catch (IndexOutOfBoundsException e) {
                    log.warn("could not allocate MPLS path", e);
                    continue;
                }
            } else if (isOptical) {
                request = DefaultLinkResourceRequest.builder(intId, links)
                        .addLambdaRequest();
            } else {
                request = DefaultLinkResourceRequest.builder(intId, links)
                        .addBandwidthRequest(BANDWIDTH);
            }

            if (request != null) {
                LinkResourceRequest lrr = request.build();
                LinkResourceAllocations lra = resService.requestResources(lrr);
                if (lra != null) {
                    break;
                }
                print("Allocated:\n%s", lra);
            } else {
                log.info("nothing to request");
            }
        }
    }

    public String getDeviceId(String deviceString) {
        int slash = deviceString.indexOf('/');
        if (slash <= 0) {
            return "";
        }
        return deviceString.substring(0, slash);
    }

}