import smc.helpers

class SMCElement(object):
    def __init__(self, json):
        self.json = json
        
class Host(object):
    def __init__(self):
        self.name = None
        self.ip = None
        self.secondary_ip = None
        self.comment = None
    
    def create(self):
        host = smc.helpers.get_json_template('host.json')
        host['name'] = self.name
        host['address'] = self.ip
        if self.comment:
            host['comment'] = self.comment
        if self.secondary_ip:
            for addr in self.secondary_ip:
                host['secondary'].append(addr)
        return host 
    
    
class Group(object):
    def __init__(self):
        self.name = None
        self.members = None
        self.comment = None
    
    def create(self):
        group = smc.helpers.get_json_template('group.json')
        group['name'] = self.name
        if self.members:
            group['element'] = self.members
        if self.comment:
            group['comment'] = self.comment
        return group


class Router(object):
    def __init__(self):
        self.name = None
        self.address = None
        self.secondary_ip = None 
    
    def create(self):
        router = smc.helpers.get_json_template('router.json')
        router['name'] = self.name
        router['address'] = self.address
        if self.secondary_ip:
            for addr in self.secondary_ip:
                router['secondary'].append(addr)
        return router   
    

class Network(object):
    def __init__(self):
        self.name = None
        self.ip4_network = None
        self.comment = None
    
    def create(self):
        network = smc.helpers.get_json_template('network.json')
        network['name'] = self.name
        network['ipv4_network'] = self.ip4_network
        if self.comment:
            network['comment'] = self.comment
        return network

        
class SingleFW(object):
    def __init__(self, SMCElement=None):
        self.name = None
        self.mgmt_ip = None
        self.mgmt_network = None
        self.log_server = None
        self.dns = None
        self.fw_license = None
        self.element = SMCElement    #existing json to modify
    
    def add_interface(self, ip, mask):
        """ Add physical interface to firewall
            Args: 
                * ip address of interface
                * mask in cidr format
        """
        interface = smc.helpers.get_json_template('routed_interface.json')
        interface_ids = [] #node_id with latest value for existing interfaces, add 1 for newly created interface
        
        for node_interface in self.element.json['physicalInterfaces']:
            #print node_interface['physical_interface']['interface_id'] #Get next available interface id
            interface_ids.append(node_interface['physical_interface']['interface_id'])
        interface_id = [int(i) for i in interface_ids]  #needed to find max
        interface_id = max(interface_id)+1
        print "Next available interface is: %s" % interface_id

        phys_iface = interface['physical_interface']
        phys_iface['interface_id'] = interface_id
        iface = interface['physical_interface']['interfaces'][0]
        iface = iface['single_node_interface']
        iface['address'] = ip
        iface['nicid'] = str(interface_id)
        iface['network_value'] = mask
        
        self.element.json['physicalInterfaces'].append(interface)
        
        return self.element.json
        
    def create(self):
        single_fw = smc.helpers.get_json_template('single_fw.json')
        
        for k,v in single_fw.iteritems():    
            if k == 'name':
                single_fw[k] = self.name
            elif k == 'nodes':
                single_fw[k][0]['firewall_node']['name'] = self.name + ' node 1'
            elif k == 'physicalInterfaces':
                single_fw[k][0]['physical_interface']['interfaces'][0]['single_node_interface']['address'] = self.mgmt_ip
                single_fw[k][0]['physical_interface']['interfaces'][0]['single_node_interface']['network_value'] = self.mgmt_network
            elif k == 'log_server_ref':
                single_fw[k] = self.log_server
            elif k == 'domain_server_address':
                if self.dns:
                    single_fw[k].append({"rank": 0, "value": self.dns})
        return single_fw
       

def get_element(SMCElement):
    pass

        
        
        
        
@@ -90,24 +90,25 @@
 json = json%0A
+%0A
         %0Acla
@@ -2194,17 +2194,28 @@
 son 
-to
+for
  modif
-y
+ication/add
 %0A   
@@ -2252,16 +2252,29 @@
 ip, mask
+, int_id=None
 ):%0A     
@@ -2354,16 +2354,17 @@
     * ip
+:
  address
@@ -2399,16 +2399,17 @@
   * mask
+:
  in cidr
@@ -2416,16 +2416,70 @@
  format%0A
+                * int_id (optional): id for interface%0A
         
@@ -2589,37 +2589,13 @@
 %5B%5D #
-node_id with latest value f
+st
 or
+e
  exi
@@ -2604,53 +2604,33 @@
 ing 
-interfaces, add 1 for newly created interface
+node_id to find next open
 %0A   
@@ -2710,113 +2710,8 @@
 '%5D:%0A
-            #print node_interface%5B'physical_interface'%5D%5B'interface_id'%5D #Get next available interface id%0A
     
@@ -2920,16 +2920,17 @@
         
+#
 print %22N
@@ -3067,16 +3067,20 @@
 _id'%5D = 
+str(
 interfac
@@ -3083,16 +3083,17 @@
 rface_id
+)
 %0A       
@@ -4328,16 +4328,100 @@
       %0A%0A
+class L2FW(object):%0A    def __init__(self, SMCElement=None):%0A        pass%0A    %0A    %0A
 def get_

remove node_id from json