// Code generated by go-swagger; DO NOT EDIT.

// Copyright Authors of Cilium
// SPDX-License-Identifier: Apache-2.0

package models

import (
	"context"
	stderrors "errors"

	"github.com/go-openapi/errors"
	"github.com/go-openapi/strfmt"
	"github.com/go-openapi/swag/jsonutils"
	"github.com/go-openapi/swag/typeutils"
	"github.com/go-openapi/validate"
)

// IPAMResponse IPAM configuration of an endpoint
//
// swagger:model IPAMResponse
type IPAMResponse struct {

	// address
	// Required: true
	Address *AddressPair `json:"address"`

	// host addressing
	// Required: true
	HostAddressing *NodeAddressing `json:"host-addressing"`

	// ipv4
	IPv4 *IPAMAddressResponse `json:"ipv4,omitempty"`

	// ipv6
	IPv6 *IPAMAddressResponse `json:"ipv6,omitempty"`
}

// Validate validates this IP a m response
func (m *IPAMResponse) Validate(formats strfmt.Registry) error {
	var res []error

	if err := m.validateAddress(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateHostAddressing(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateIPv4(formats); err != nil {
		res = append(res, err)
	}

	if err := m.validateIPv6(formats); err != nil {
		res = append(res, err)
	}

	if len(res) > 0 {
		return errors.CompositeValidationError(res...)
	}
	return nil
}

func (m *IPAMResponse) validateAddress(formats strfmt.Registry) error {

	if err := validate.Required("address", "body", m.Address); err != nil {
		return err
	}

	if m.Address != nil {
		if err := m.Address.Validate(formats); err != nil {
			ve := new(errors.Validation)
			if stderrors.As(err, &ve) {
				return ve.ValidateName("address")
			}
			ce := new(errors.CompositeError)
			if stderrors.As(err, &ce) {
				return ce.ValidateName("address")
			}

			return err
		}
	}

	return nil
}

func (m *IPAMResponse) validateHostAddressing(formats strfmt.Registry) error {

	if err := validate.Required("host-addressing", "body", m.HostAddressing); err != nil {
		return err
	}

	if m.HostAddressing != nil {
		if err := m.HostAddressing.Validate(formats); err != nil {
			ve := new(errors.Validation)
			if stderrors.As(err, &ve) {
				return ve.ValidateName("host-addressing")
			}
			ce := new(errors.CompositeError)
			if stderrors.As(err, &ce) {
				return ce.ValidateName("host-addressing")
			}

			return err
		}
	}

	return nil
}

func (m *IPAMResponse) validateIPv4(formats strfmt.Registry) error {
	if typeutils.IsZero(m.IPv4) { // not required
		return nil
	}

	if m.IPv4 != nil {
		if err := m.IPv4.Validate(formats); err != nil {
			ve := new(errors.Validation)
			if stderrors.As(err, &ve) {
				return ve.ValidateName("ipv4")
			}
			ce := new(errors.CompositeError)
			if stderrors.As(err, &ce) {
				return ce.ValidateName("ipv4")
			}

			return err
		}
	}

	return nil
}

func (m *IPAMResponse) validateIPv6(formats strfmt.Registry) error {
	if typeutils.IsZero(m.IPv6) { // not required
		return nil
	}

	if m.IPv6 != nil {
		if err := m.IPv6.Validate(formats); err != nil {
			ve := new(errors.Validation)
			if stderrors.As(err, &ve) {
				return ve.ValidateName("ipv6")
			}
			ce := new(errors.CompositeError)
			if stderrors.As(err, &ce) {
				return ce.ValidateName("ipv6")
			}

			return err
		}
	}

	return nil
}

// ContextValidate validate this IP a m response based on the context it is used
func (m *IPAMResponse) ContextValidate(ctx context.Context, formats strfmt.Registry) error {
	var res []error

	if err := m.contextValidateAddress(ctx, formats); err != nil {
		res = append(res, err)
	}

	if err := m.contextValidateHostAddressing(ctx, formats); err != nil {
		res = append(res, err)
	}

	if err := m.contextValidateIPv4(ctx, formats); err != nil {
		res = append(res, err)
	}

	if err := m.contextValidateIPv6(ctx, formats); err != nil {
		res = append(res, err)
	}

	if len(res) > 0 {
		return errors.CompositeValidationError(res...)
	}
	return nil
}

func (m *IPAMResponse) contextValidateAddress(ctx context.Context, formats strfmt.Registry) error {

	if m.Address != nil {

		if err := m.Address.ContextValidate(ctx, formats); err != nil {
			ve := new(errors.Validation)
			if stderrors.As(err, &ve) {
				return ve.ValidateName("address")
			}
			ce := new(errors.CompositeError)
			if stderrors.As(err, &ce) {
				return ce.ValidateName("address")
			}

			return err
		}
	}

	return nil
}

func (m *IPAMResponse) contextValidateHostAddressing(ctx context.Context, formats strfmt.Registry) error {

	if m.HostAddressing != nil {

		if err := m.HostAddressing.ContextValidate(ctx, formats); err != nil {
			ve := new(errors.Validation)
			if stderrors.As(err, &ve) {
				return ve.ValidateName("host-addressing")
			}
			ce := new(errors.CompositeError)
			if stderrors.As(err, &ce) {
				return ce.ValidateName("host-addressing")
			}

			return err
		}
	}

	return nil
}

func (m *IPAMResponse) contextValidateIPv4(ctx context.Context, formats strfmt.Registry) error {

	if m.IPv4 != nil {

		if typeutils.IsZero(m.IPv4) { // not required
			return nil
		}

		if err := m.IPv4.ContextValidate(ctx, formats); err != nil {
			ve := new(errors.Validation)
			if stderrors.As(err, &ve) {
				return ve.ValidateName("ipv4")
			}
			ce := new(errors.CompositeError)
			if stderrors.As(err, &ce) {
				return ce.ValidateName("ipv4")
			}

			return err
		}
	}

	return nil
}

func (m *IPAMResponse) contextValidateIPv6(ctx context.Context, formats strfmt.Registry) error {

	if m.IPv6 != nil {

		if typeutils.IsZero(m.IPv6) { // not required
			return nil
		}

		if err := m.IPv6.ContextValidate(ctx, formats); err != nil {
			ve := new(errors.Validation)
			if stderrors.As(err, &ve) {
				return ve.ValidateName("ipv6")
			}
			ce := new(errors.CompositeError)
			if stderrors.As(err, &ce) {
				return ce.ValidateName("ipv6")
			}

			return err
		}
	}

	return nil
}

// MarshalBinary interface implementation
func (m *IPAMResponse) MarshalBinary() ([]byte, error) {
	if m == nil {
		return nil, nil
	}
	return jsonutils.WriteJSON(m)
}

// UnmarshalBinary interface implementation
func (m *IPAMResponse) UnmarshalBinary(b []byte) error {
	var res IPAMResponse
	if err := jsonutils.ReadJSON(b, &res); err != nil {
		return err
	}
	*m = res
	return nil
}
