1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Services.Optimization.ControllerService.Interfaces;
|
---|
6 | using Microsoft.WindowsAzure;
|
---|
7 | using Microsoft.WindowsAzure.StorageClient;
|
---|
8 | using Microsoft.WindowsAzure.ServiceRuntime;
|
---|
9 | using System.Diagnostics;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Services.Optimization.ControllerService.Azure {
|
---|
12 | public static class AzureConstants {
|
---|
13 | public static readonly string SCENARIO_TABLE = "Scenario";
|
---|
14 | public static readonly string SCENARIO_BLOB_CONTAINER = "scenario";
|
---|
15 | public static readonly string CLOUD_SETTINGS_KEY = "Cloudia.WindowsAzure.Storage";
|
---|
16 | }
|
---|
17 |
|
---|
18 | public class ScenarioDao : IScenarioDao {
|
---|
19 | public CloudTableClient TableClient { get; set; }
|
---|
20 |
|
---|
21 | public bool Add(ScenarioEntity entity) {
|
---|
22 | TableServiceContext serviceContext = TableClient.GetDataServiceContext();
|
---|
23 | TableClient.CreateTableIfNotExist(AzureConstants.SCENARIO_TABLE);
|
---|
24 | ScenarioEntity dbEntity = (from e in serviceContext.CreateQuery<ScenarioEntity>(AzureConstants.SCENARIO_TABLE)
|
---|
25 | where e.RowKey == entity.RowKey
|
---|
26 | select e).FirstOrDefault();
|
---|
27 | if (dbEntity != null)
|
---|
28 | return false;
|
---|
29 |
|
---|
30 | serviceContext.AddObject(AzureConstants.SCENARIO_TABLE, entity);
|
---|
31 | serviceContext.SaveChanges();
|
---|
32 | return true;
|
---|
33 | }
|
---|
34 |
|
---|
35 | public bool DeleteByName(string scenarioName) {
|
---|
36 | TableServiceContext serviceContext = TableClient.GetDataServiceContext();
|
---|
37 | TableClient.CreateTableIfNotExist(AzureConstants.SCENARIO_TABLE);
|
---|
38 | ScenarioEntity entity = (from e in serviceContext.CreateQuery<ScenarioEntity>(AzureConstants.SCENARIO_TABLE)
|
---|
39 | where e.RowKey == scenarioName
|
---|
40 | select e).FirstOrDefault();
|
---|
41 | if (entity == null)
|
---|
42 | return false;
|
---|
43 |
|
---|
44 | serviceContext.DeleteObject(entity);
|
---|
45 | serviceContext.SaveChangesWithRetries();
|
---|
46 | return true;
|
---|
47 | }
|
---|
48 |
|
---|
49 | public ScenarioEntity FindByName(string scenarioName) {
|
---|
50 | TableServiceContext serviceContext = TableClient.GetDataServiceContext();
|
---|
51 | TableClient.CreateTableIfNotExist(AzureConstants.SCENARIO_TABLE);
|
---|
52 | ScenarioEntity entity = (from e in serviceContext.CreateQuery<ScenarioEntity>(AzureConstants.SCENARIO_TABLE)
|
---|
53 | where e.RowKey == scenarioName
|
---|
54 | select e).FirstOrDefault();
|
---|
55 | return entity;
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | public IEnumerable<ScenarioEntity> GetAllEntities() {
|
---|
60 | TableServiceContext serviceContext = TableClient.GetDataServiceContext();
|
---|
61 | TableClient.CreateTableIfNotExist(AzureConstants.SCENARIO_TABLE);
|
---|
62 | return (from e in serviceContext.CreateQuery<ScenarioEntity>(AzureConstants.SCENARIO_TABLE)
|
---|
63 | select e).AsEnumerable();
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | public class BlobDao : IBlobDao {
|
---|
68 | public CloudBlobClient BlobClient { get; set; }
|
---|
69 |
|
---|
70 | public bool Add(StringEntry entry) {
|
---|
71 | CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.SCENARIO_BLOB_CONTAINER);
|
---|
72 | container.CreateIfNotExist();
|
---|
73 | var blob = container.GetBlobReference(entry.Key);
|
---|
74 | blob.UploadText(entry.Text);
|
---|
75 | return true;
|
---|
76 | }
|
---|
77 |
|
---|
78 | public bool DeleteByKey(string entryKey) {
|
---|
79 | CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.SCENARIO_BLOB_CONTAINER);
|
---|
80 | container.CreateIfNotExist();
|
---|
81 | var blob = container.GetBlobReference(entryKey);
|
---|
82 | return blob.DeleteIfExists();
|
---|
83 | }
|
---|
84 |
|
---|
85 | public StringEntry FindByKey(string entryKey) {
|
---|
86 | CloudBlobContainer container = BlobClient.GetContainerReference(AzureConstants.SCENARIO_BLOB_CONTAINER);
|
---|
87 | container.CreateIfNotExist();
|
---|
88 | var blob = container.GetBlobReference(entryKey);
|
---|
89 | return new StringEntry() { Key = entryKey, Text = blob.DownloadText() };
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | public class AzureDataAccessLayer : IDataAccessLayer {
|
---|
94 | private IScenarioDao scenarioDao;
|
---|
95 | private IBlobDao blobDao;
|
---|
96 |
|
---|
97 | private CloudStorageAccount storageAccount;
|
---|
98 |
|
---|
99 | private CloudStorageAccount StorageAccount {
|
---|
100 | get {
|
---|
101 | if (storageAccount == null) {
|
---|
102 | try {
|
---|
103 | storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting(AzureConstants.CLOUD_SETTINGS_KEY));
|
---|
104 | }
|
---|
105 | catch (Exception ex) {
|
---|
106 | Trace.WriteLine(ex.Message);
|
---|
107 | storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=optimizationstorage1;AccountKey=n7Leom8ZFWkof/VQ2a4aRSvwOlX+Gwr3uojQF9CFJw1osmGCV0WwaNC8s7nkZ+qteLduAgW2l75WFpbXrkvG4Q==");
|
---|
108 | }
|
---|
109 | }
|
---|
110 | return storageAccount;
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | public IScenarioDao ScenarioDao {
|
---|
115 | get {
|
---|
116 | if (scenarioDao == null) {
|
---|
117 | scenarioDao = new ScenarioDao();
|
---|
118 | }
|
---|
119 | return scenarioDao;
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | public IBlobDao BlobDao {
|
---|
124 | get {
|
---|
125 | if (blobDao == null) {
|
---|
126 | blobDao = new BlobDao();
|
---|
127 | }
|
---|
128 | return blobDao;
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | public IScenarioDao CreateScenarioDao() {
|
---|
133 | return new ScenarioDao() { TableClient = StorageAccount.CreateCloudTableClient() };
|
---|
134 | }
|
---|
135 |
|
---|
136 | public IBlobDao CreateBlobDao() {
|
---|
137 | return new BlobDao() { BlobClient = StorageAccount.CreateCloudBlobClient() };
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|