Merge "Updated BgpManager for Be"
[vpnservice.git] / neutronvpn / neutronvpn-shell / src / main / java / org / opendaylight / vpnservice / neutronvpn / shell / ConfigureL3VpnCommand.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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.vpnservice.neutronvpn.shell;
10
11 import org.apache.karaf.shell.commands.Command;
12 import org.apache.karaf.shell.commands.Option;
13 import org.apache.karaf.shell.console.OsgiCommandSupport;
14 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
15 import org.opendaylight.vpnservice.neutronvpn.interfaces.INeutronVpnManager;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.CreateL3VPNInputBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.CreateL3VPNOutput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DeleteL3VPNInputBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DeleteL3VPNOutput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.NeutronvpnService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.createl3vpn.input.L3vpn;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.createl3vpn.input.L3vpnBuilder;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
31 import java.util.concurrent.ExecutionException;
32 import java.util.concurrent.Future;
33
34 @Command(scope = "vpnservice", name = "configure-l3vpn", description = "Create/Delete Neutron L3VPN")
35 public class ConfigureL3VpnCommand extends OsgiCommandSupport {
36
37     final Logger Logger = LoggerFactory.getLogger(ConfigureL3VpnCommand.class);
38
39     private INeutronVpnManager neutronVpnManager;
40     private RpcProviderRegistry rpcProviderRegistry;
41     private NeutronvpnService neutronvpnService;
42
43     public void setNeutronVpnManager(INeutronVpnManager neutronVpnManager) {
44         this.neutronVpnManager = neutronVpnManager;
45     }
46
47     public void setRpcRegistry(RpcProviderRegistry rpcProviderRegistry) {
48         this.rpcProviderRegistry = rpcProviderRegistry;
49     }
50
51     @Option(name = "-op", aliases = {"--operation"}, description = "create-l3-vpn/delete-l3-vpn",
52             required = false, multiValued = false)
53     String op;
54
55     @Option(name = "-vid", aliases = {"--vpnid"}, description = "VPN ID", required = false, multiValued = false)
56     String vid;
57
58     @Option(name = "-n", aliases = {"--name"}, description = "VPN Name", required = false, multiValued = false)
59     String name;
60
61     @Option(name = "-tid", aliases = {"--tenantid"}, description = "Tenant ID", required = false, multiValued = false)
62     String tid;
63
64     @Option(name = "-rd", aliases = {"--rd"}, description = "Route Distinguisher", required = false, multiValued =
65             false)
66     String rd;
67
68     @Option(name = "-irt", aliases = {"--import-rts"}, description = "List of Import RTs", required = false,
69             multiValued = false)
70     String irt;
71
72     @Option(name = "-ert", aliases = {"--export-rts"}, description = "List of Export RTs", required = false,
73             multiValued = false)
74     String ert;
75
76     @Option(name = "-sid", aliases = {"--subnet-uuid"}, description = "List of Subnet IDs", required = false,
77             multiValued = false)
78     String sid;
79
80     @Override
81     protected Object doExecute() throws Exception {
82
83         if (rpcProviderRegistry != null) {
84             neutronvpnService = rpcProviderRegistry.getRpcService(NeutronvpnService.class);
85             if (neutronvpnService != null) {
86                 if (op != null) {
87                     switch (op) {
88                         case "create-l3-vpn":
89                             createL3VpnCLI();
90                             break;
91                         case "delete-l3-vpn":
92                             deleteL3VpnCLI();
93                             break;
94                         default:
95                             System.out.println("Invalid argument.");
96                             System.out.println(getHelp(""));
97                             break;
98                     }
99                 } else {
100                     System.out.println("Too few arguments");
101                     System.out.println(getHelp(""));
102                 }
103             } else {
104                 System.out.println("neutronvpnservice not initialized");
105             }
106         } else {
107             System.out.println("rpcProviderRegistryService not initialized");
108         }
109         return null;
110     }
111
112     public void createL3VpnCLI() {
113
114         if (vid == null) {
115             System.out.println("Please supply a valid VPN ID");
116             System.out.println(getHelp("create"));
117             return;
118         }
119
120         if (rd == null) {
121             System.out.println("Please supply a valid RD");
122             System.out.println(getHelp("create"));
123             return;
124         }
125
126         if (irt == null) {
127             System.out.println("Please supply a valid list of import RTs separated by {,}");
128             System.out.println(getHelp("create"));
129             return;
130         }
131
132         if (ert == null) {
133             System.out.println("Please supply a valid list of export RTs separated by {,}");
134             System.out.println(getHelp("create"));
135             return;
136         }
137
138         Uuid vuuid = new Uuid(vid);
139
140         try {
141             ArrayList<String> rdList = new ArrayList<String>(Arrays.asList(rd.split(",")));
142             ArrayList<String> irtList = new ArrayList<String>(Arrays.asList(irt.split(",")));
143             ArrayList<String> ertList = new ArrayList<String>(Arrays.asList(ert.split(",")));
144             Uuid tuuid = null;
145
146             if (tid != null) {
147                 tuuid = new Uuid(tid);
148             }
149
150             List<L3vpn> l3vpns = new ArrayList<L3vpn>();
151             L3vpn l3vpn = new L3vpnBuilder().setId(vuuid).setName(name).setRouteDistinguisher(rdList).setImportRT
152                     (irtList)
153                     .setExportRT(ertList).setTenantId(tuuid).build();
154             l3vpns.add(l3vpn);
155             Future<RpcResult<CreateL3VPNOutput>> result =
156                     neutronvpnService.createL3VPN(new CreateL3VPNInputBuilder().setL3vpn(l3vpns).build());
157             RpcResult<CreateL3VPNOutput> rpcResult = result.get();
158             if (rpcResult.isSuccessful()) {
159                 System.out.println("L3VPN created successfully");
160                 Logger.trace("createl3vpn: {}", result);
161             } else {
162                 System.out.println("error populating createL3VPN : " + result.get().getErrors());
163                 System.out.println(getHelp("create"));
164             }
165         } catch (InterruptedException | ExecutionException e) {
166             Logger.trace("error populating createL3VPN", e);
167             System.out.println("error populating createL3VPN : " + e.getMessage());
168             System.out.println(getHelp("create"));
169         }
170
171         try {
172             List<Uuid> sidList = new ArrayList<Uuid>();
173
174             if (sid != null) {
175                 for (String sidStr : sid.split(",")) {
176                     Uuid suuid = new Uuid(sidStr);
177                     sidList.add(suuid);
178                 }
179             }
180             for (Uuid subnet : sidList) {
181                 neutronVpnManager.addSubnetToVpn(vuuid, subnet);
182             }
183
184         } catch (Exception e) {
185             Logger.trace("error in adding subnet to VPN", e);
186             System.out.println("error in adding subnet to VPN : " + e.getMessage());
187         }
188     }
189
190     public void deleteL3VpnCLI() {
191
192         if (vid == null) {
193             System.out.println("Please supply a valid VPN ID");
194             System.out.println(getHelp("delete"));
195             return;
196         }
197         Uuid vpnid = new Uuid(vid);
198         try {
199             List<Uuid> sidList = neutronVpnManager.getSubnetsforVpn(vpnid);
200             if (sidList != null) {
201                 for (Uuid subnet : sidList) {
202                     neutronVpnManager.removeSubnetFromVpn(vpnid, subnet);
203                 }
204             }
205         } catch (Exception e) {
206             Logger.trace("error in deleting subnet from VPN", e);
207             System.out.println("error in deleting subnet from VPN : " + e.getMessage());
208         }
209
210         try {
211             List<Uuid> vpnIdList = new ArrayList<Uuid>();
212             vpnIdList.add(vpnid);
213
214             Future<RpcResult<DeleteL3VPNOutput>> result =
215                     neutronvpnService.deleteL3VPN(new DeleteL3VPNInputBuilder().setId(vpnIdList).build());
216             RpcResult<DeleteL3VPNOutput> rpcResult = result.get();
217             if (rpcResult.isSuccessful()) {
218                 System.out.println("L3VPN deleted successfully");
219                 Logger.trace("deletel3vpn: {}", result);
220             } else {
221                 System.out.println("error populating deleteL3VPN : " + result.get().getErrors());
222                 System.out.println(getHelp("delete"));
223             }
224         } catch (InterruptedException | ExecutionException e) {
225             Logger.trace("error populating deleteL3VPN", e);
226             System.out.println("error populating deleteL3VPN : " + e.getMessage());
227             System.out.println(getHelp("delete"));
228         }
229     }
230
231     private String getHelp(String cmd) {
232         StringBuilder help = new StringBuilder("Usage:");
233         switch (cmd) {
234             case "create":
235                 help.append("exec configure-vpn -op/--operation create-l3-vpn -vid/--vpnid <id>\n");
236                 help.append("-rd/--rd <rd> -irt/--import-rts <irt1,irt2,..> -ert/--export-rts <ert1,ert2,..>\n");
237                 help.append("[-sid/--subnet-uuid <subnet1,subnet2,..>]\n");
238                 break;
239             case "delete":
240                 help.append("exec configure-vpn -op/--operation delete-l3-vpn -vid/--vpnid <id> \n");
241                 break;
242             case "":
243                 help.append("exec configure-vpn -op/--operation create-l3-vpn -vid/--vpnid <id>\n");
244                 help.append("-rd/--rd <rd> -irt/--import-rts <irt1,irt2,..> -ert/--export-rts <ert1,ert2,..>\n");
245                 help.append("[-sid/--subnet-uuid <subnet1,subnet2,..>]\n");
246                 help.append("exec configure-vpn -op/--operation delete-l3-vpn -vid/--vpnid <id> \n");
247         }
248         return help.toString();
249     }
250
251 }