<commit_before>// Copyright (c) 2016 Intel Corporation
//
// Licensed 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.
//

package datastore

import (
	"fmt"

	"github.com/01org/ciao/ciao-controller/types"
	"github.com/01org/ciao/payloads"
	"github.com/01org/ciao/ssntp/uuid"
)

// MemoryDB is a memory backed persistentStore implementation for unit testing
type MemoryDB struct {
	tenants         map[string]*tenant
	nodes           map[string]*node
	instances       map[string]*types.Instance
	tenantUsage     map[string][]types.CiaoUsage
	blockDevices    map[string]types.BlockData
	attachments     map[string]types.StorageAttachment
	instanceVolumes map[attachment]string
	logEntries      []*types.LogEntry
	cnciWorkload    *types.Workload

	tableInitPath string
	workloadsPath string
}

func (db *MemoryDB) fillWorkloads() error {
	// add dummy public tenant.
	_ = db.addTenant("public", "")

	// add a public workload for test cases
	testConfig := `
---
#cloud-config
users:
  - name: demouser
    gecos: CIAO Demo User
    lock-passwd: false
    passwd: <PASSWORD>OYJXJ<PASSWORD>XHzKXTAO.
    sudo: ALL=(ALL) NOPASSWD:ALL
    ssh-authorized-keys:
    - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDerQfD+qkb0V0XdQs8SBWqy4sQmqYFP96n/kI4Cq162w4UE8pTxy0ozAPldOvBJjljMvgaNKSAddknkhGcrNUvvJsUcZFm2qkafi32WyBdGFvIc45A+8O7vsxPXgHEsS9E3ylEALXAC3D0eX7pPtRiAbasLlY+VcACRqr3bPDSZTfpCmIkV2334uZD9iwOvTVeR+FjGDqsfju4DyzoAIqpPasE0+wk4Vbog7osP+qvn1gj5kQyusmr62+t0wx+bs2dF5QemksnFOswUrv9PGLhZgSMmDQrRYuvEfIAC7IdN/hfjTn0OokzljBiuWQ4WIIba/7xTYLVujJV65qH3heaSMxJJD7eH9QZs9RdbbdTXMFuJFsHV2OF6wZRp18tTNZZJMqiHZZSndC5WP1WrUo3Au/9a+ighSaOiVddHsPG07C/TOEnr3IrwU7c9yIHeeRFHmcQs9K0+n9XtrmrQxDQ9/mLkfje80Ko25VJ/QpAQPzCKh2KfQ4RD+/PxBUScx/lHIHOIhTSCh57ic629zWgk0coSQDi4MKSa5guDr3cuDvt4RihGviDM6V68ewsl0gh6Z9c0Hw7hU0vky4oxak5AiySiPz0FtsOnAzIL0UON+yMuKzrJgLjTKodwLQ0wlBXu43cD+P8VXwQYeqNSzfrhBnHqsrMf4lTLtc7kDDTcw== ciao@ciao
...
	`
	cpus := payloads.RequestedResource{
		Type:      payloads.VCPUs,
		Value:     2,
		Mandatory: false,
	}

	mem := payloads.RequestedResource{
		Type:      payloads.MemMB,
		Value:     512,
		Mandatory: false,
	}

	storage := types.StorageResource{
		ID:        "",
		Ephemeral: true,
		Size:      20,
	}

	wl := types.Workload{
		ID:          uuid.Generate().String(),
		TenantID:    "public",
		Description: "testWorkload",
		FWType:      string(payloads.EFI),
		VMType:      payloads.QEMU,
		ImageID:     uuid.Generate().String(),
		ImageName:   "",
		Config:      testConfig,
		Defaults:    []payloads.RequestedResource{cpus, mem},
		Storage:     []types.StorageResource{storage},
	}

	return db.updateWorkload(wl)
}

func (db *MemoryDB) init(config Config) error {
	db.tenants = make(map[string]*tenant)
	db.nodes = make(map[string]*node)
	db.instances = make(map[string]*types.Instance)
	db.tenantUsage = make(map[string][]types.CiaoUsage)
	db.blockDevices = make(map[string]types.BlockData)
	db.attachments = make(map[string]types.StorageAttachment)
	db.instanceVolumes = make(map[attachment]string)

	db.workloadsPath = config.InitWorkloadsPath
	return db.fillWorkloads()
}

func (db *MemoryDB) disconnect() {

}

func (db *MemoryDB) logEvent(tenantID string, eventType string, message string) error {
	entry := types.LogEntry{
		TenantID:  tenantID,
		EventType: eventType,
		Message:   message,
	}

	db.logEntries = append(db.logEntries, &entry)

	return nil
}

func (db *MemoryDB) clearLog() error {
	db.logEntries = nil
	return nil
}

func (db *MemoryDB) getEventLog() ([]*types.LogEntry, error) {
	return db.logEntries, nil
}

func (db *MemoryDB) getCNCIWorkloadID() (string, error) {
	if db.cnciWorkload != nil {
		return db.cnciWorkload.ID, nil
	}
	return "", fmt.Errorf("CNCI not found")
}

func (db *MemoryDB) addLimit(tenantID string, resourceID int, limit int) error {
	return nil
}

func (db *MemoryDB) addTenant(id string, MAC string) error {
	t := &tenant{
		Tenant: types.Tenant{
			ID:      id,
			CNCIMAC: MAC,
		},
		network:   make(map[int]map[int]bool),
		instances: make(map[string]*types.Instance),
		devices:   make(map[string]types.BlockData),<commit_msg>memorydb: Remove unused workload related code<commit_after>// Copyright (c) 2016 Intel Corporation
//
// Licensed 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.
//

package datastore

import (
	"fmt"

	"github.com/01org/ciao/ciao-controller/types"
	"github.com/01org/ciao/payloads"
	"github.com/01org/ciao/ssntp/uuid"
)

// MemoryDB is a memory backed persistentStore implementation for unit testing
type MemoryDB struct {
	tenants         map[string]*tenant
	nodes           map[string]*node
	instances       map[string]*types.Instance
	tenantUsage     map[string][]types.CiaoUsage
	blockDevices    map[string]types.BlockData
	attachments     map[string]types.StorageAttachment
	instanceVolumes map[attachment]string
	logEntries      []*types.LogEntry

	workloadsPath string
}

func (db *MemoryDB) fillWorkloads() error {
	// add dummy public tenant.
	_ = db.addTenant("public", "")

	// add a public workload for test cases
	testConfig := `
---
#cloud-config
users:
  - name: demouser
    gecos: CIAO Demo User
    lock-passwd: false
    passwd: <PASSWORD>.
    sudo: ALL=(ALL) NOPASSWD:ALL
    ssh-authorized-keys:
    - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDerQfD+qkb0V0XdQs8SBWqy4sQmqYFP96n/kI4Cq162w4UE8pTxy0ozAPldOvBJjljMvgaNKSAddknkhGcrNUvvJsUcZFm2qkafi32WyBdGFvIc45A+8O7vsxPXgHEsS9E3ylEALXAC3D0eX7pPtRiAbasLlY+VcACRqr3bPDSZTfpCmIkV2334uZD9iwOvTVeR+FjGDqsfju4DyzoAIqpPasE0+wk4Vbog7osP+qvn1gj5kQyusmr62+t0wx+bs2dF5QemksnFOswUrv9PGLhZgSMmDQrRYuvEfIAC7IdN/hfjTn0OokzljBiuWQ4WIIba/7xTYLVujJV65qH3heaSMxJJD7eH9QZs9RdbbdTXMFuJFsHV2OF6wZRp18tTNZZJMqiHZZSndC5WP1WrUo3Au/9a+ighSaOiVddHsPG07C/TOEnr3IrwU7c9yIHeeRFHmcQs9K0+n9XtrmrQxDQ9/mLkfje80Ko25VJ/QpAQPzCKh2KfQ4RD+/PxBUScx/lHIHOIhTSCh57ic629zWgk0coSQDi4MKSa5guDr3cuDvt4RihGviDM6V68ewsl0gh6Z9c0Hw7hU0vky4oxak5AiySiPz0FtsOnAzIL0UON+yMuKzrJgLjTKodwLQ0wlBXu43cD+P8VXwQYeqNSzfrhBnHqsrMf4lTLtc7kDDTcw== ciao@ciao
...
	`
	cpus := payloads.RequestedResource{
		Type:      payloads.VCPUs,
		Value:     2,
		Mandatory: false,
	}

	mem := payloads.RequestedResource{
		Type:      payloads.MemMB,
		Value:     512,
		Mandatory: false,
	}

	storage := types.StorageResource{
		ID:        "",
		Ephemeral: true,
		Size:      20,
	}

	wl := types.Workload{
		ID:          uuid.Generate().String(),
		TenantID:    "public",
		Description: "testWorkload",
		FWType:      string(payloads.EFI),
		VMType:      payloads.QEMU,
		ImageID:     uuid.Generate().String(),
		ImageName:   "",
		Config:      testConfig,
		Defaults:    []payloads.RequestedResource{cpus, mem},
		Storage:     []types.StorageResource{storage},
	}

	return db.updateWorkload(wl)
}

func (db *MemoryDB) init(config Config) error {
	db.tenants = make(map[string]*tenant)
	db.nodes = make(map[string]*node)
	db.instances = make(map[string]*types.Instance)
	db.tenantUsage = make(map[string][]types.CiaoUsage)
	db.blockDevices = make(map[string]types.BlockData)
	db.attachments = make(map[string]types.StorageAttachment)
	db.instanceVolumes = make(map[attachment]string)

	db.workloadsPath = config.InitWorkloadsPath
	return db.fillWorkloads()
}

func (db *MemoryDB) disconnect() {

}

func (db *MemoryDB) logEvent(tenantID string, eventType string, message string) error {
	entry := types.LogEntry{
		TenantID:  tenantID,
		EventType: eventType,
		Message:   message,
	}

	db.logEntries = append(db.logEntries, &entry)

	return nil
}

func (db *MemoryDB) clearLog() error {
	db.logEntries = nil
	return nil
}

func (db *MemoryDB) getEventLog() ([]*types.LogEntry, error) {
	return db.logEntries, nil
}

func (db *MemoryDB) addLimit(tenantID string, resourceID int, limit int) error {
	return nil
}

func (db *MemoryDB) addTenant(id string, MAC string) error {
	t := &tenant{
		Tenant: types.Tenant{
			ID:      id,
			CNCIMAC: MAC,
		},
		network:   make(map[int]map[int]bool),
		instances: make(map[string]*types.Instance),
		devices:   make(map[string]types.BlockData),
{'fraction_non_alphanumeric': 0.10711493354182955, 'fraction_numerical': 0.028593767452250642, 'mean_word_length': 4.527160493827161, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6193549', 'n_tokens_mistral': 3755, 'n_tokens_neox': 3567, 'n_words': 642}