admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | ''' |
| 3 | Created on 24-June-2013 |
| 4 | |
| 5 | @author: Anil Kumar (anilkumar.s@paxterrasolutions.com) |
| 6 | |
| 7 | TestON is free software: you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation, either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | TestON is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | |
| 21 | ''' |
| 22 | import time |
| 23 | import pexpect |
| 24 | import struct, fcntl, os, sys, signal |
| 25 | import sys |
| 26 | import re |
| 27 | sys.path.append("../") |
| 28 | from drivers.common.clidriver import CLI |
| 29 | |
| 30 | class HPSwitch(CLI): |
| 31 | |
| 32 | def __init__(self): |
| 33 | super(CLI, self).__init__() |
| 34 | |
| 35 | def connect(self,**connectargs): |
| 36 | for key in connectargs: |
| 37 | vars(self)[key] = connectargs[key] |
| 38 | |
| 39 | self.name = self.options['name'] |
| 40 | self.handle = super(HPSwitch,self).connect(user_name = self.user_name, ip_address = self.ip_address,port = self.port, pwd = self.pwd) |
| 41 | |
| 42 | return main.TRUE |
| 43 | |
| 44 | def configure(self): |
| 45 | self.execute(cmd='configure', prompt = '\(config\)',timeout = 3) |
| 46 | if re.search('\(config\)', main.last_response): |
| 47 | main.log.info("Configure mode enabled"+main.last_response) |
| 48 | else : |
| 49 | main.log.warn("Fail to enable configure mode"+main.last_response) |
| 50 | |
| 51 | |
| 52 | def set_up_vlan(self,**vlanargs): |
| 53 | ''' |
| 54 | Configure vlan. |
| 55 | ''' |
| 56 | for key in vlanargs: |
| 57 | vars(self)[key] = vlanargs[key] |
| 58 | |
| 59 | self.execute(cmd='vlan '+self.vlan_id, prompt = '\(vlan-'+self.vlan_id+'\)',timeout = 3) |
| 60 | if re.search('\(vlan-'+self.vlan_id+'\)', main.last_response): |
| 61 | main.log.info("Configuring VLAN "+self.vlan_id) |
| 62 | else : |
| 63 | main.log.warn("Fail to configure Vlan"+self.vlan_id+main.last_response) |
| 64 | return main.FALSE |
| 65 | |
| 66 | if self.vlan_name : |
| 67 | self.execute(cmd='name '+self.vlan_name, prompt = '\(vlan-'+self.vlan_id+'\)',timeout = 3) |
| 68 | if re.search('\(vlan-'+self.vlan_id+'\)', main.last_response): |
| 69 | main.log.info("Configuring "+self.vlan_id) |
| 70 | return main.TRUE |
| 71 | else : |
| 72 | main.log.warn("Fail to configure Vlan"+self.vlan_id+main.last_response) |
| 73 | return main.FALSE |
| 74 | else : |
| 75 | main.log.error("Vlan Name not specified") |
| 76 | return main.FALSE |
| 77 | |
| 78 | def vlan_tagged(self, **taggedargs): |
| 79 | for key in taggedargs: |
| 80 | vars(self)[key] = taggedargs[key] |
| 81 | if self.vlan_id : |
| 82 | self.execute(cmd='vlan '+self.vlan_id, prompt = '\(vlan-'+self.vlan_id+'\)',timeout = 3) |
| 83 | |
| 84 | if re.search('\(vlan-'+self.vlan_id+'\)', main.last_response): |
| 85 | main.log.info("Configuring "+self.vlan_id) |
| 86 | else : |
| 87 | main.log.warn("Fail to configure Vlan"+self.vlan_id+main.last_response) |
| 88 | return main.FALSE |
| 89 | if self.tagged : |
| 90 | self.execute(cmd='tagged '+self.vlan_id, prompt = '\(vlan-'+self.vlan_id+'\)',timeout = 3) |
| 91 | if re.search('\(vlan-'+self.vlan_id+'\)', main.last_response): |
| 92 | main.log.info("VLAN tagged done "+self.tagged) |
| 93 | return main.TRUE |
| 94 | else : |
| 95 | main.log.warn("Fail to tagged Vlan"+self.vlan_id+main.last_response) |
| 96 | return main.FALSE |
| 97 | |
| 98 | def vlan_untagged(self, **untaggedargs): |
| 99 | for key in untaggedargs: |
| 100 | vars(self)[key] = untaggedargs[key] |
| 101 | if self.vlan_id : |
| 102 | self.execute(cmd='vlan '+self.vlan_id, prompt = '\(vlan-'+self.vlan_id+'\)',timeout = 3) |
| 103 | |
| 104 | if re.search('\(vlan-'+self.vlan_id+'\)', main.last_response): |
| 105 | main.log.info("Configuring "+self.vlan_id) |
| 106 | else : |
| 107 | main.log.warn("Fail to configure Vlan"+self.vlan_id+main.last_response) |
| 108 | return main.FALSE |
| 109 | if self.tagged : |
| 110 | self.execute(cmd='untagged '+self.vlan_id, prompt = '\(vlan-'+self.vlan_id+'\)',timeout = 3) |
| 111 | if re.search('\(vlan-'+self.vlan_id+'\)', main.last_response): |
| 112 | main.log.info("VLAN untagged done "+self.tagged) |
| 113 | return main.TRUE |
| 114 | else : |
| 115 | main.log.warn("Fail to untagged Vlan"+self.vlan_id+main.last_response) |
| 116 | return main.FALSE |
| 117 | |
| 118 | def openflow_mode(self): |
| 119 | self.configure() |
| 120 | self.execute(cmd='openflow', prompt = '\(openflow\)',timeout = 3) |
| 121 | if re.search('\(openflow\)', main.last_response): |
| 122 | main.log.info("Openflow mode enabled"+main.last_response) |
| 123 | return main.TRUE |
| 124 | else : |
| 125 | main.log.warn("Fail to enable Openflow mode"+main.last_response) |
| 126 | return main.FALSE |
| 127 | |
| 128 | |
| 129 | def add_openflow_controller(self,**controllerargs): |
| 130 | for key in controllerargs: |
| 131 | vars(self)[key] = controllerargs[key] |
| 132 | |
| 133 | if not self.openflow_mode(): |
| 134 | return main.FALSE |
| 135 | |
| 136 | contoller_details = 'controller-id '+ self.controller_id+'ip '+self.controller_ip + 'controller-interface vlan '+self.interface_vlan_id |
| 137 | self.execute(cmd=contoller_details, prompt = '\(openflow\)',timeout = 3) |
| 138 | |
| 139 | if re.search('already\sconfigured', main.last_response): |
| 140 | main.log.warn("A controller is already configured with this ID."+main.last_response) |
| 141 | return main.FALSE |
| 142 | elif re.search('Incomplete\sinput',main.last_response ) : |
| 143 | main.log.warn("Incomplete\sinput"+main.last_response) |
| 144 | return main.FALSE |
| 145 | else: |
| 146 | main.log.info("Successfully added Openflow Controller") |
| 147 | return main.TRUE |
| 148 | |
| 149 | |
| 150 | def create_openflow_instance(self,**instanceargs): |
| 151 | for key in instanceargs: |
| 152 | vars(self)[key] = instanceargs[key] |
| 153 | |
| 154 | if not self.openflow_mode(): |
| 155 | return main.FALSE |
| 156 | |
| 157 | if self.instance_name : |
| 158 | self.execute(cmd='instance '+self.instance_name, prompt = '\(of-inst-'+self.instance_name+'\)',timeout = 3) |
| 159 | |
| 160 | if re.search('\(of-inst-'+self.instance_name+'\)', main.last_response): |
| 161 | main.log.info("Configuring Openflow instance "+self.instance_name) |
| 162 | else : |
| 163 | main.log.warn("Fail to configure Openflow instance"+self.instance_name+"\n\n"+main.last_response) |
| 164 | return main.FALSE |
| 165 | if self.controller_id : |
| 166 | self.execute(cmd='controller-id '+self.controller_id, prompt = '\(of-inst-'+self.instance_name+'\)',timeout = 3) |
| 167 | main.log.info(main.last_response) |
| 168 | |
| 169 | if self.member : |
| 170 | self.execute(cmd='member vlan '+self.member_vlan_id, prompt = '\(of-inst-'+self.instance_name+'\)',timeout = 3) |
| 171 | main.log.info(main.last_response) |
| 172 | |
| 173 | if self.execute(cmd='enable', prompt = '\(of-inst-'+self.instance_name+'\)',timeout = 3): |
| 174 | return main.TRUE |
| 175 | else : |
| 176 | return main.FALSE |
| 177 | |
| 178 | def pair_vlan_with_openflow_instance(self,vlan_id): |
| 179 | self.configure() |
| 180 | self.execute(cmd='vlan '+vlan_id, prompt = '\(vlan-'+vlan_id+'\)',timeout = 3) |
| 181 | if re.search('\(vlan-'+vlan_id+'\)', main.last_response): |
| 182 | main.log.info("Configuring VLAN "+vlan_id) |
| 183 | else : |
| 184 | main.log.warn("Fail to configure Vlan"+self.vlan_id+main.last_response) |
| 185 | return main.FALSE |
| 186 | |
| 187 | self.execute(cmd='openflow enable', prompt = '\(vlan-'+vlan_id+'\)',timeout = 3) |
| 188 | if re.search('\(vlan-'+vlan_id+'\)', main.last_response): |
| 189 | main.log.info("Configuring VLAN "+vlan_id) |
| 190 | else : |
| 191 | main.log.warn("Fail to configure Vlan"+self.vlan_id+main.last_response) |
| 192 | return main.FALSE |
| 193 | |
| 194 | def show_openflow_instance(self,instance_name): |
| 195 | |
| 196 | self.execute(cmd='show openflow instance '+instance_name, prompt = '#',timeout = 3) |
| 197 | return main.TRUE |
| 198 | |
| 199 | def show(self, command): |
| 200 | self.execute(cmd=command, prompt = '#',timeout = 3) |
| 201 | return main.TRUE |
| 202 | |
| 203 | |
| 204 | def openflow_enable(self): |
| 205 | self.configure() |
| 206 | self.execute(cmd='openflow enable', prompt = '#',timeout = 3) |
| 207 | return main.TRUE |
| 208 | |
| 209 | def openflow_disable(self): |
| 210 | self.configure() |
| 211 | self.execute(cmd='openflow enable', prompt = '#',timeout = 3) |
| 212 | return main.TRUE |
| 213 | |
| 214 | def remove_controller(self,controller_id): |
| 215 | self.configure() |
| 216 | self.execute(cmd='no controller-id '+controller_id, prompt = '#',timeout = 3) |
| 217 | return main.TRUE |
| 218 | |
| 219 | def remove_vlan(self,vlan_id): |
| 220 | self.configure() |
| 221 | if self.execute(cmd='no vlan '+vlan_id, prompt = '#',timeout = 3): |
| 222 | return main.TRUE |
| 223 | else : |
| 224 | self.execute(cmd=' '+vlan_id, prompt = '#',timeout = 3) |
| 225 | return main.TRUE |
| 226 | |
| 227 | def remove_openflow_instance(self,instance_name): |
| 228 | self.configure() |
| 229 | self.execute(cmd='no openflow instance '+instance_name, prompt = '#',timeout = 3) |
| 230 | return main.TRUE |