1+ //
2+ // Licensed to the Apache Software Foundation (ASF) under one
3+ // or more contributor license agreements. See the NOTICE file
4+ // distributed with this work for additional information
5+ // regarding copyright ownership. The ASF licenses this file
6+ // to you under the Apache License, Version 2.0 (the
7+ // "License"); you may not use this file except in compliance
8+ // with the License. You may obtain a copy of the License at
9+ //
10+ // http://www.apache.org/licenses/LICENSE-2.0
11+ //
12+ // Unless required by applicable law or agreed to in writing,
13+ // software distributed under the License is distributed on an
14+ // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+ // KIND, either express or implied. See the License for the
16+ // specific language governing permissions and limitations
17+ // under the License.
18+ //
19+
20+ package com .cloud .hypervisor .kvm .resource .wrapper ;
21+
22+ import java .util .List ;
23+
24+ import org .apache .log4j .Logger ;
25+ import org .libvirt .Connect ;
26+ import org .libvirt .LibvirtException ;
27+
28+ import com .cloud .agent .api .Answer ;
29+ import com .cloud .agent .api .PvlanSetupCommand ;
30+ import com .cloud .hypervisor .kvm .resource .LibvirtComputingResource ;
31+ import com .cloud .hypervisor .kvm .resource .LibvirtVMDef .InterfaceDef ;
32+ import com .cloud .resource .CommandWrapper ;
33+ import com .cloud .utils .script .Script ;
34+
35+ public final class LibvirtPvlanSetupCommandWrapper extends CommandWrapper <PvlanSetupCommand , Answer , LibvirtComputingResource > {
36+
37+ private static final Logger s_logger = Logger .getLogger (LibvirtPvlanSetupCommandWrapper .class );
38+
39+ @ Override
40+ public Answer execute (final PvlanSetupCommand command , final LibvirtComputingResource libvirtComputingResource ) {
41+ final String primaryPvlan = command .getPrimary ();
42+ final String isolatedPvlan = command .getIsolated ();
43+ final String op = command .getOp ();
44+ final String dhcpName = command .getDhcpName ();
45+ final String dhcpMac = command .getDhcpMac ();
46+ final String dhcpIp = command .getDhcpIp ();
47+ final String vmMac = command .getVmMac ();
48+ boolean add = true ;
49+
50+ String opr = "-A" ;
51+ if (op .equals ("delete" )) {
52+ opr = "-D" ;
53+ add = false ;
54+ }
55+
56+ String result = null ;
57+ try {
58+ final String guestBridgeName = libvirtComputingResource .getGuestBridgeName ();
59+ final int timeout = libvirtComputingResource .getTimeout ();
60+
61+ if (command .getType () == PvlanSetupCommand .Type .DHCP ) {
62+ final String ovsPvlanDhcpHostPath = libvirtComputingResource .getOvsPvlanDhcpHostPath ();
63+ final Script script = new Script (ovsPvlanDhcpHostPath , timeout , s_logger );
64+
65+ if (add ) {
66+ final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource .getLibvirtUtilitiesHelper ();
67+ final Connect conn = libvirtUtilitiesHelper .getConnectionByVmName (dhcpName );
68+
69+ final List <InterfaceDef > ifaces = libvirtComputingResource .getInterfaces (conn , dhcpName );
70+ final InterfaceDef guestNic = ifaces .get (0 );
71+ script .add (opr , "-b" , guestBridgeName , "-p" , primaryPvlan , "-i" , isolatedPvlan , "-n" , dhcpName , "-d" , dhcpIp , "-m" , dhcpMac , "-I" ,
72+ guestNic .getDevName ());
73+ } else {
74+ script .add (opr , "-b" , guestBridgeName , "-p" , primaryPvlan , "-i" , isolatedPvlan , "-n" , dhcpName , "-d" , dhcpIp , "-m" , dhcpMac );
75+ }
76+
77+ result = script .execute ();
78+
79+ if (result != null ) {
80+ s_logger .warn ("Failed to program pvlan for dhcp server with mac " + dhcpMac );
81+ return new Answer (command , false , result );
82+ } else {
83+ s_logger .info ("Programmed pvlan for dhcp server with mac " + dhcpMac );
84+ }
85+ } else if (command .getType () == PvlanSetupCommand .Type .VM ) {
86+ final String ovsPvlanVmPath = libvirtComputingResource .getOvsPvlanVmPath ();
87+
88+ final Script script = new Script (ovsPvlanVmPath , timeout , s_logger );
89+ script .add (opr , "-b" , guestBridgeName , "-p" , primaryPvlan , "-i" , isolatedPvlan , "-v" , vmMac );
90+ result = script .execute ();
91+
92+ if (result != null ) {
93+ s_logger .warn ("Failed to program pvlan for vm with mac " + vmMac );
94+ return new Answer (command , false , result );
95+ } else {
96+ s_logger .info ("Programmed pvlan for vm with mac " + vmMac );
97+ }
98+ }
99+ } catch (final LibvirtException e ) {
100+ s_logger .error ("Error whislt executing OVS Setup command! ==> " + e .getMessage ());
101+ return new Answer (command , false , e .getMessage ());
102+ }
103+ return new Answer (command , true , result );
104+ }
105+ }
0 commit comments