84107cec9389b2549e3edf9b33f45d4daeedb862
[unimgr.git] / cli / src / main / java / org / opendaylight / unimgr / cli / EvcAddShellCommand.java
1 /*
2  * Copyright (c) 2015 CableLabs and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.unimgr.cli;
10
11
12 import java.util.ArrayList;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.ListIterator;
16
17 import org.apache.karaf.shell.commands.Command;
18 import org.apache.karaf.shell.commands.Option;
19 import org.apache.karaf.shell.console.OsgiCommandSupport;
20 import org.opendaylight.unimgr.api.IUnimgrConsoleProvider;
21 import org.opendaylight.unimgr.impl.UnimgrConstants;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentationBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.EgressBw;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.IngressBw;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDest;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDestBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDestKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSource;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSourceBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSourceKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed100MBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10GBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed10MBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.service.speed.speed.Speed1GBuilder;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 @Command(name = "evc-add",
42 scope = "uni",
43 description = "Add evc to the controller.")
44
45 public class EvcAddShellCommand extends OsgiCommandSupport {
46
47     private static final Logger LOG = LoggerFactory.getLogger(EvcAddShellCommand.class);
48     protected IUnimgrConsoleProvider provider;
49
50     @Option(name = "-IPs",
51             aliases = { "--IP-Address-source-uni" },
52             description = "The IP address of the source UNI.\n-IPs / --IP Address source uni",
53             required = true,
54             multiValued = false)
55     private String IPs = "";
56
57     @Option(name = "-IPd",
58             aliases = { "--IP-Address-destenation-uni" },
59             description = "The IP address of the destenation UNI.\n-IPs / --IP Address destenation uni",
60             required = true,
61             multiValued = false)
62     private String IPd = "";
63
64     @Option(name = "-egress",
65             aliases = { "--egress-speed" },
66             description = "egress speed.\n-s / --speed 10M/100M/1G/10G",
67             required = false,
68             multiValued = true)
69     private String egress = "";
70
71     @Option(name = "-ingress",
72             aliases = { "--ingress-speed" },
73             description = "ingress speed.\n-s / --speed 10M/100M/1G/10G",
74             required = false,
75             multiValued = true)
76     private String ingress = "";
77
78     private Object getSpeed(String speed) {
79         Object speedObject = null;
80         if (speed.equals("10M")) {
81             speedObject = new Speed10MBuilder().build();
82         }
83         else if (speed.equals("100M")) {
84             speedObject = new Speed100MBuilder().build();
85         }
86         else if (speed.equals("1G")) {
87             speedObject = new Speed1GBuilder().build();
88         }
89         else if (speed.equals("10G")) {
90             speedObject = new Speed10GBuilder().build();
91         }
92         return speedObject;
93     }
94
95     public EvcAddShellCommand(IUnimgrConsoleProvider provider) {
96         this.provider = provider;
97     }
98
99     @Override
100     protected Object doExecute() throws Exception {
101         Short order = new Short("0");
102         IpAddress ipAddreSource = new IpAddress(IPs.toCharArray());
103         UniSource uniSource = new UniSourceBuilder()
104                                   .setIpAddress(ipAddreSource)
105                                   .setKey(new UniSourceKey(order))
106                                   .setOrder(order)
107                                   .build();
108         List<UniSource> uniSourceList = new ArrayList<UniSource>();
109         uniSourceList.add(uniSource);
110         IpAddress ipAddreDest = new IpAddress(IPd.toCharArray());
111         UniDest uniDest = new UniDestBuilder()
112                           .setOrder(order)
113                           .setKey(new UniDestKey(order))
114                           .setIpAddress(ipAddreDest)
115                           .build();
116         List<UniDest> uniDestList = new ArrayList<UniDest>();
117         uniDestList.add(uniDest);
118         EvcAugmentation evcAug = new EvcAugmentationBuilder()
119                                      .setCosId(UnimgrConstants.EVC_PREFIX + 1)
120                                      .setEgressBw((EgressBw) getSpeed(egress))
121                                      .setIngressBw((IngressBw) getSpeed(ingress))
122                                      .setUniDest(uniDestList)
123                                      .setUniSource(uniSourceList)
124                                      .build();
125         if (provider.addEvc(evcAug)) {
126             return new String("Evc with Source Uni " +IPs+" and destenation Uni " +IPd+" created");
127         } else {
128             return new String("Error creating new Evc");
129         }
130     }
131 }