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

package models

// This file was generated by the swagger tool.
// Editing this file might prove futile when you re-run the swagger generate command

import (
	"context"
	"encoding/json"

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

// IKEPolicyUpdate IKE Policy object used for update
//
// Min Properties: 1
//
// swagger:model IKEPolicyUpdate
type IKEPolicyUpdate struct {

	// authentication
	Authentication IKEPolicyAuthentication `json:"authentication,omitempty"`

	// DH group of the IKE Policy
	// Example: 2
	// Enum: [1 2 5 14 19 20 24]
	DhGroup int64 `json:"dhGroup,omitempty"`

	// encryption of the IKE Policy
	// Example: aes-256-cbc
	// Enum: [aes-256-cbc aes-192-cbc aes-128-cbc aes-256-gcm aes-128-gcm 3des-cbc]
	Encryption string `json:"encryption,omitempty"`

	// key lifetime
	KeyLifetime KeyLifetime `json:"keyLifetime,omitempty"`

	// name of the IKE Policy
	// Example: ikePolicy1
	// Max Length: 47
	// Min Length: 1
	Name string `json:"name,omitempty"`

	// Preshared key used in this IKE Policy (length of preshared key must be even)
	PresharedKey string `json:"presharedKey,omitempty"`

	// version of the IKE Policy
	// Example: 2
	// Enum: [1 2]
	Version int64 `json:"version,omitempty"`

	// i k e policy update additional properties
	IKEPolicyUpdateAdditionalProperties map[string]interface{} `json:"-"`
}

// UnmarshalJSON unmarshals this object with additional properties from JSON
func (m *IKEPolicyUpdate) UnmarshalJSON(data []byte) error {
	// stage 1, bind the properties
	var stage1 struct {

		// authentication
		Authentication IKEPolicyAuthentication `json:"authentication,omitempty"`

		// DH group of the IKE Policy
		// Example: 2
		// Enum: [1 2 5 14 19 20 24]
		DhGroup int64 `json:"dhGroup,omitempty"`

		// encryption of the IKE Policy
		// Example: aes-256-cbc
		// Enum: [aes-256-cbc aes-192-cbc aes-128-cbc aes-256-gcm aes-128-gcm 3des-cbc]
		Encryption string `json:"encryption,omitempty"`

		// key lifetime
		KeyLifetime KeyLifetime `json:"keyLifetime,omitempty"`

		// name of the IKE Policy
		// Example: ikePolicy1
		// Max Length: 47
		// Min Length: 1
		Name string `json:"name,omitempty"`

		// Preshared key used in this IKE Policy (length of preshared key must be even)
		PresharedKey string `json:"presharedKey,omitempty"`

		// version of the IKE Policy
		// Example: 2
		// Enum: [1 2]
		Version int64 `json:"version,omitempty"`
	}
	if err := json.Unmarshal(data, &stage1); err != nil {
		return err
	}
	var rcv IKEPolicyUpdate

	rcv.Authentication = stage1.Authentication
	rcv.DhGroup = stage1.DhGroup
	rcv.Encryption = stage1.Encryption
	rcv.KeyLifetime = stage1.KeyLifetime
	rcv.Name = stage1.Name
	rcv.PresharedKey = stage1.PresharedKey
	rcv.Version = stage1.Version
	*m = rcv

	// stage 2, remove properties and add to map
	stage2 := make(map[string]json.RawMessage)
	if err := json.Unmarshal(data, &stage2); err != nil {
		return err
	}

	delete(stage2, "authentication")
	delete(stage2, "dhGroup")
	delete(stage2, "encryption")
	delete(stage2, "keyLifetime")
	delete(stage2, "name")
	delete(stage2, "presharedKey")
	delete(stage2, "version")
	// stage 3, add additional properties values
	if len(stage2) > 0 {
		result := make(map[string]interface{})
		for k, v := range stage2 {
			var toadd interface{}
			if err := json.Unmarshal(v, &toadd); err != nil {
				return err
			}
			result[k] = toadd
		}
		m.IKEPolicyUpdateAdditionalProperties = result
	}

	return nil
}

// MarshalJSON marshals this object with additional properties into a JSON object
func (m IKEPolicyUpdate) MarshalJSON() ([]byte, error) {
	var stage1 struct {

		// authentication
		Authentication IKEPolicyAuthentication `json:"authentication,omitempty"`

		// DH group of the IKE Policy
		// Example: 2
		// Enum: [1 2 5 14 19 20 24]
		DhGroup int64 `json:"dhGroup,omitempty"`

		// encryption of the IKE Policy
		// Example: aes-256-cbc
		// Enum: [aes-256-cbc aes-192-cbc aes-128-cbc aes-256-gcm aes-128-gcm 3des-cbc]
		Encryption string `json:"encryption,omitempty"`

		// key lifetime
		KeyLifetime KeyLifetime `json:"keyLifetime,omitempty"`

		// name of the IKE Policy
		// Example: ikePolicy1
		// Max Length: 47
		// Min Length: 1
		Name string `json:"name,omitempty"`

		// Preshared key used in this IKE Policy (length of preshared key must be even)
		PresharedKey string `json:"presharedKey,omitempty"`

		// version of the IKE Policy
		// Example: 2
		// Enum: [1 2]
		Version int64 `json:"version,omitempty"`
	}

	stage1.Authentication = m.Authentication
	stage1.DhGroup = m.DhGroup
	stage1.Encryption = m.Encryption
	stage1.KeyLifetime = m.KeyLifetime
	stage1.Name = m.Name
	stage1.PresharedKey = m.PresharedKey
	stage1.Version = m.Version

	// make JSON object for known properties
	props, err := json.Marshal(stage1)
	if err != nil {
		return nil, err
	}

	if len(m.IKEPolicyUpdateAdditionalProperties) == 0 { // no additional properties
		return props, nil
	}

	// make JSON object for the additional properties
	additional, err := json.Marshal(m.IKEPolicyUpdateAdditionalProperties)
	if err != nil {
		return nil, err
	}

	if len(props) < 3 { // "{}": only additional properties
		return additional, nil
	}

	// concatenate the 2 objects
	return swag.ConcatJSON(props, additional), nil
}

// Validate validates this i k e policy update
func (m *IKEPolicyUpdate) Validate(formats strfmt.Registry) error {
	var res []error

	// short circuits minProperties > 0
	if m == nil {
		return errors.TooFewProperties("", "body", 1)
	}

	props := make(map[string]json.RawMessage, 7+10)
	j, err := swag.WriteJSON(m)
	if err != nil {
		return err
	}

	if err = swag.ReadJSON(j, &props); err != nil {
		return err
	}

	nprops := len(props)

	// minProperties: 1
	if nprops < 1 {
		return errors.TooFewProperties("", "body", 1)
	}

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

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

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

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

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

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

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

func (m *IKEPolicyUpdate) validateAuthentication(formats strfmt.Registry) error {
	if swag.IsZero(m.Authentication) { // not required
		return nil
	}

	if err := m.Authentication.Validate(formats); err != nil {
		if ve, ok := err.(*errors.Validation); ok {
			return ve.ValidateName("authentication")
		} else if ce, ok := err.(*errors.CompositeError); ok {
			return ce.ValidateName("authentication")
		}
		return err
	}

	return nil
}

var iKEPolicyUpdateTypeDhGroupPropEnum []interface{}

func init() {
	var res []int64
	if err := json.Unmarshal([]byte(`[1,2,5,14,19,20,24]`), &res); err != nil {
		panic(err)
	}
	for _, v := range res {
		iKEPolicyUpdateTypeDhGroupPropEnum = append(iKEPolicyUpdateTypeDhGroupPropEnum, v)
	}
}

// prop value enum
func (m *IKEPolicyUpdate) validateDhGroupEnum(path, location string, value int64) error {
	if err := validate.EnumCase(path, location, value, iKEPolicyUpdateTypeDhGroupPropEnum, true); err != nil {
		return err
	}
	return nil
}

func (m *IKEPolicyUpdate) validateDhGroup(formats strfmt.Registry) error {
	if swag.IsZero(m.DhGroup) { // not required
		return nil
	}

	// value enum
	if err := m.validateDhGroupEnum("dhGroup", "body", m.DhGroup); err != nil {
		return err
	}

	return nil
}

var iKEPolicyUpdateTypeEncryptionPropEnum []interface{}

func init() {
	var res []string
	if err := json.Unmarshal([]byte(`["aes-256-cbc","aes-192-cbc","aes-128-cbc","aes-256-gcm","aes-128-gcm","3des-cbc"]`), &res); err != nil {
		panic(err)
	}
	for _, v := range res {
		iKEPolicyUpdateTypeEncryptionPropEnum = append(iKEPolicyUpdateTypeEncryptionPropEnum, v)
	}
}

const (

	// IKEPolicyUpdateEncryptionAesDash256DashCbc captures enum value "aes-256-cbc"
	IKEPolicyUpdateEncryptionAesDash256DashCbc string = "aes-256-cbc"

	// IKEPolicyUpdateEncryptionAesDash192DashCbc captures enum value "aes-192-cbc"
	IKEPolicyUpdateEncryptionAesDash192DashCbc string = "aes-192-cbc"

	// IKEPolicyUpdateEncryptionAesDash128DashCbc captures enum value "aes-128-cbc"
	IKEPolicyUpdateEncryptionAesDash128DashCbc string = "aes-128-cbc"

	// IKEPolicyUpdateEncryptionAesDash256DashGcm captures enum value "aes-256-gcm"
	IKEPolicyUpdateEncryptionAesDash256DashGcm string = "aes-256-gcm"

	// IKEPolicyUpdateEncryptionAesDash128DashGcm captures enum value "aes-128-gcm"
	IKEPolicyUpdateEncryptionAesDash128DashGcm string = "aes-128-gcm"

	// IKEPolicyUpdateEncryptionNr3desDashCbc captures enum value "3des-cbc"
	IKEPolicyUpdateEncryptionNr3desDashCbc string = "3des-cbc"
)

// prop value enum
func (m *IKEPolicyUpdate) validateEncryptionEnum(path, location string, value string) error {
	if err := validate.EnumCase(path, location, value, iKEPolicyUpdateTypeEncryptionPropEnum, true); err != nil {
		return err
	}
	return nil
}

func (m *IKEPolicyUpdate) validateEncryption(formats strfmt.Registry) error {
	if swag.IsZero(m.Encryption) { // not required
		return nil
	}

	// value enum
	if err := m.validateEncryptionEnum("encryption", "body", m.Encryption); err != nil {
		return err
	}

	return nil
}

func (m *IKEPolicyUpdate) validateKeyLifetime(formats strfmt.Registry) error {
	if swag.IsZero(m.KeyLifetime) { // not required
		return nil
	}

	if err := m.KeyLifetime.Validate(formats); err != nil {
		if ve, ok := err.(*errors.Validation); ok {
			return ve.ValidateName("keyLifetime")
		} else if ce, ok := err.(*errors.CompositeError); ok {
			return ce.ValidateName("keyLifetime")
		}
		return err
	}

	return nil
}

func (m *IKEPolicyUpdate) validateName(formats strfmt.Registry) error {
	if swag.IsZero(m.Name) { // not required
		return nil
	}

	if err := validate.MinLength("name", "body", m.Name, 1); err != nil {
		return err
	}

	if err := validate.MaxLength("name", "body", m.Name, 47); err != nil {
		return err
	}

	return nil
}

var iKEPolicyUpdateTypeVersionPropEnum []interface{}

func init() {
	var res []int64
	if err := json.Unmarshal([]byte(`[1,2]`), &res); err != nil {
		panic(err)
	}
	for _, v := range res {
		iKEPolicyUpdateTypeVersionPropEnum = append(iKEPolicyUpdateTypeVersionPropEnum, v)
	}
}

// prop value enum
func (m *IKEPolicyUpdate) validateVersionEnum(path, location string, value int64) error {
	if err := validate.EnumCase(path, location, value, iKEPolicyUpdateTypeVersionPropEnum, true); err != nil {
		return err
	}
	return nil
}

func (m *IKEPolicyUpdate) validateVersion(formats strfmt.Registry) error {
	if swag.IsZero(m.Version) { // not required
		return nil
	}

	// value enum
	if err := m.validateVersionEnum("version", "body", m.Version); err != nil {
		return err
	}

	return nil
}

// ContextValidate validate this i k e policy update based on the context it is used
func (m *IKEPolicyUpdate) ContextValidate(ctx context.Context, formats strfmt.Registry) error {
	var res []error

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

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

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

func (m *IKEPolicyUpdate) contextValidateAuthentication(ctx context.Context, formats strfmt.Registry) error {

	if swag.IsZero(m.Authentication) { // not required
		return nil
	}

	if err := m.Authentication.ContextValidate(ctx, formats); err != nil {
		if ve, ok := err.(*errors.Validation); ok {
			return ve.ValidateName("authentication")
		} else if ce, ok := err.(*errors.CompositeError); ok {
			return ce.ValidateName("authentication")
		}
		return err
	}

	return nil
}

func (m *IKEPolicyUpdate) contextValidateKeyLifetime(ctx context.Context, formats strfmt.Registry) error {

	if swag.IsZero(m.KeyLifetime) { // not required
		return nil
	}

	if err := m.KeyLifetime.ContextValidate(ctx, formats); err != nil {
		if ve, ok := err.(*errors.Validation); ok {
			return ve.ValidateName("keyLifetime")
		} else if ce, ok := err.(*errors.CompositeError); ok {
			return ce.ValidateName("keyLifetime")
		}
		return err
	}

	return nil
}

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

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