func (e *EndorsementStore) GetHardwareID(args common.QueryArgs) (common.QueryResult, error) {
	var platformID string
	var result []interface{}
	var hwID HardwareIdentity
	hwMap := make(map[string]string)
	platformID, err := ExtractPlatformID(args)
	if err != nil {
		return nil, fmt.Errorf("failed to extract platform_id from query params: %w", err)
	}
	err = e.GetHwIDFromDB(platformID, &hwID)
	if err != nil {
		return nil, fmt.Errorf("for given platform id=%s, failed to fetch the hwID from DB: %w", platformID, err)
	}
	hwMap["hw_id"] = hwID.HardwareRot.HwVer
	result = append(result, hwMap["hw_id"])
	return result, nil
}
GetHardwareID is the query function to fetch the Hardware Id associated to a platform
package main
import (
	"context"
	"encoding/json"
	"fmt"
	"log"
	"github.com/hashicorp/go-plugin"
	"github.com/veraison/common"
)
const (
	HW = "HW"
	SW = "SW"
	Edge = "Edge"
	Rel = "Rel"
)
const (
	mindepth      = "1"
	maxdepth      = "10"
	to            = ".."
	space         = " "
	newline       = "\n"
	filterPatches = "FILTER link.rel == 'patches' "
	filterUpdates = "FILTER link.rel == 'updates' "
)
type EndorsementStore struct {
	common.BaseEndorsementStore
	store Store
}
type Resource struct {
	Type             string `json:"type"`
	MeasType         string `json:"arm.com-PSAMeasurementType"`
	Description      string `json:"arm.com-PSADescription"`
	MeasurementValue string `json:"arm.com-PSAMeasurementValue"`
	SignerID         string `json:"arm.com-PSASignerId"`
}
type ResourceCollection struct {
	Resources []Resource `json:"resource"`
}
type PsaEntity struct {
	Name  string   `json:"entity-name"`
	RegID string   `json:"reg-id"`
	Role  []string `json:"role"`
}
type PsaHardwareRot struct {
	ImplementationID string `json:"implementation-id"`
	HwVer            string `json:"hw-ver"`
}
type SoftwareIdentity struct {
	TagID           string               `json:"tag-id"`
	TagVersion      int                  `json:"tag-version"`
	SoftwareName    string               `json:"software-name"`
	SoftwareVersion string               `json:"software-version"`
	Entity          []PsaEntity          `json:"entity"`
	Payload         []ResourceCollection `json:"payload"`
}
type HardwareIdentity struct {
	TagID       string         `json:"tag-id"`
	Entity      []PsaEntity    `json:"entity"`
	HardwareRot PsaHardwareRot `json:"psa-hardware-rot"`
}
type HardwareIdentityWrapper struct {
	ID      HardwareIdentity `json:"HardwareIdentity"`
	RawData json.RawMessage
}
type SoftwareIdentityWrapper struct {
	ID      SoftwareIdentity `json:"SoftwareIdentity"`
	Rawdata json.RawMessage
}
const FirstIndex = 0
func (e *EndorsementStore) GetName() string {
	return "arangodb"
}
func (e *EndorsementStore) Init(args common.EndorsementStoreParams) error {
	e.Queries = map[string]common.Query{
		"hardware_id":           e.GetHardwareID,
		"software_components":   e.GetSoftwareComponents,           
		"all_sw_components":     e.GetAllSoftwareComponents,        
		"linked_sw_comp_latest": e.GetMostRecentSwVerOfLinkedComp,  
		"sw_component_latest":   e.GetMostRecentSwVerOfAnySWComp,   
	}
	_, found := args["AltAlgorithm"]
	if found {
		e.Queries["software_components"] = e.GetAltSoftwareComponents
	}
	store, found := args["storeInstance"]
	if !found {
		return fmt.Errorf("DB store instance is not provided inside FetcherParams")
	}
	e.store = store.(Store)
	if !e.store.IsInitialised() {
		return fmt.Errorf("uninitialized DB store provided in FetcherParams")
	}
	ctx := context.Background()
	if err := e.store.Connect(ctx); err != nil {
		return fmt.Errorf("DB connection failed: %w", err)
	}
	return nil
}
func (e *EndorsementStore) Close() error {
	ctx := context.Background()
	if err := e.store.Connect(ctx); err != nil {
		return err
	}
	if err := e.store.Close(ctx); err != nil {
		return err
	}
	return nil
}
func (e *EndorsementStore) GetHwIDFromDB(platformID string, hwID *HardwareIdentity) error {
	var hardwareInstance HardwareIdentityWrapper
	ctx := context.Background()
	if err := e.store.Connect(ctx); err != nil {
		return err
	}
	queryArgs := map[string]interface{}{
		"platformId": platformID,
	}
	hwCollection, _ := e.store.GetQueryParam(HW)
	query := "FOR d IN " + hwCollection
	query += " FILTER d.HardwareIdentity.`psa-hardware-rot`.`implementation-id` == @platformId RETURN d"
	log.Printf("input query = %s", query)
	resultList, err := e.store.RunQuery(ctx, query, queryArgs, &hardwareInstance)
	if err != nil {
		return fmt.Errorf("query failed: %w", err)
	}
	for _, resultValue := range resultList {
		hardwareInstance = resultValue.(HardwareIdentityWrapper)
		break
	}
	*hwID = hardwareInstance.ID
	return nil
}
func (e *EndorsementStore) GetSwIDTagsLinkedToHWTagFromDB(hwTag string) ([]SoftwareIdentity, error) {
	hwCollection, _ := e.store.GetQueryParam(HW)
	hwColTag := hwCollection + "/" + hwTag
	hwColTag = "'" + hwColTag + "'"
	edgeCollection, _ := e.store.GetQueryParam(Edge)
	query := "FOR swid, link IN INBOUND " + hwColTag + space + edgeCollection
	query += newline + " FILTER link.rel == 'psa-rot-compound' RETURN swid"
	swidList, err := e.GetSwIDTagsForQueryFromDB(query)
	if err != nil {
		return nil, fmt.Errorf("query GetSwIDTagsForQueryFromDB failed: %w", err)
	}
	return swidList, nil
}