# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.  The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License.  You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations
# under the License.

class CIMI::Frontend::Network < CIMI::Frontend::Entity

  get '/cimi/networks/:id' do
    network_xml = get_entity('networks', params[:id], credentials)
    @network = CIMI::Model::Network.from_xml(network_xml)
    haml :'networks/show'
  end

  get '/cimi/networks' do
    networks_xml = get_entity_collection('networks', credentials)
    @networks = collection_class_for(:network).from_xml(networks_xml)
    if @networks.operations.find {|o| o.rel == 'add'}
      # note that even if 'add' is supported, both forwarding group and
      # network config are optional in network template
      begin
        forwarding_groups_xml = get_entity_collection('forwarding_groups', credentials)
        @forwarding_groups = collection_class_for(:forwarding_group).from_xml(forwarding_groups_xml)
      rescue RestClient::ResourceNotFound
      end
      begin
        network_config_xml = get_entity_collection('network_configurations', credentials)
        @network_configurations = collection_class_for(:network_configuration).from_xml(network_config_xml)
      rescue RestClient::ResourceNotFound
      end
    end
    haml :'networks/index'
  end

  post '/cimi/networks' do
    network_xml = Nokogiri::XML::Builder.new do |xml|
      xml.NetworkCreate(:xmlns => CIMI::Frontend::CMWG_NAMESPACE) {
        xml.name params[:network][:name]
        xml.description params[:network][:description]
        xml.networkTemplate {
          xml.networkConfig( :href => params[:network][:network_configuration] )
          xml.forwardingGroup( :href => params[:network][:forwarding_group] )
        }
      }
    end.to_xml
    begin
      result = create_entity('networks', network_xml, credentials)
      network = collection_class_for(:network).from_xml(result)
      flash[:success] = "Network was successfully created."
      redirect "/cimi/networks/#{href_to_id(network.id)}", 302
    rescue => e
      flash[:error] = "Network cannot be created: #{e.message}"
    end
  end

end
