public DemandProposal SaveDemandProposal(string receivingSubscriptionId, string sendingSubscriptionId, Demand demand, string offerId = null, Proposal.StateEnum state = Proposal.StateEnum.InitialEnum) { if (this.SubscriptionProposals.ContainsKey(receivingSubscriptionId) && this.Subscriptions[receivingSubscriptionId] is OfferSubscription) { var demandProposal = new DemandProposal() { Id = "" + Guid.NewGuid(), InternalId = this.GetNextProposalInternalId(), ReceivingSubscriptionId = receivingSubscriptionId, SendingSubscriptionId = sendingSubscriptionId, OfferId = offerId, Demand = demand, State = state }; // TODO do we need a lock here??? this.SubscriptionProposals[receivingSubscriptionId].Add(demandProposal); this.DemandProposals[demandProposal.Id] = demandProposal; return(demandProposal); } else { throw new Exception($"Offer Subscription Id {receivingSubscriptionId} does not exist!"); } }
public Agreement CreateAgreement(DemandProposal demandProposal, OfferProposal offerProposal, DateTime validTo) { if (this.Agreements.ContainsKey(offerProposal.Id)) { throw new Exception($"Agreement Id {offerProposal.Id} already exists!"); } var agreement = new Agreement() { Id = offerProposal.Id, OfferProposal = offerProposal, DemandProposal = demandProposal, State = AgreementState.Proposal, ValidTo = validTo }; this.Agreements[agreement.Id] = agreement; return(agreement); }
public async Task InMemoryActivityProcessor_Integration_SimplePositiveScenario() { // Test scenario var demand = new DemandProposal() { OfferId = "dummyOfferId", InternalId = 2, Id = "DummyProposalId", Demand = new Demand() { Constraints = "", NodeId = "DummyRequestorNode1", Properties = new Dictionary <string, JToken>() } }; var offer = new OfferProposal() { DemandId = "dummyDemandId", Id = "DummyProposalId", InternalId = 1, Offer = new Offer() { Constraints = "", NodeId = "DummyProviderNode1", Properties = new Dictionary <string, JToken>() } }; var agreement = this.AgreementRepository.CreateAgreement(demand, offer, DateTime.Now); // 1. Create Activity in a known, negotiated agreement var activity = this.RequestorProcessor.CreateActivity(agreement.Id); var provEvents = await this.ProviderProcessor.CollectActivityEventsAsync(offer.Offer.NodeId, 1.0f); // 2. Send a sample ExeScript command batch var batchId = this.RequestorProcessor.ExecAsync(activity.Id, new ExeScript() { Text = "DEPLOY\nSTART\n" }); // 2.1. Simulate ExeScript execution on Provider side Task.Run(async() => { bool isActivityDestroyed = false; do { provEvents = await this.ProviderProcessor.CollectActivityEventsAsync(offer.Offer.NodeId, 1.0f); foreach (var provEvent in provEvents) { switch (provEvent.EventType) { case ActivityProviderEvent.ActivityProviderEventType.Exec: var commands = provEvent.ExeScript.Text.Split('\n'); foreach (var command in commands) { if (string.IsNullOrWhiteSpace(command)) { continue; } if (command.Contains("DEPLOY")) { this.ActivityRepository.SetActivityState(activity.Id, ActivityState.Deploying); Thread.Sleep(1000); this.ActivityRepository.SetActivityState(activity.Id, ActivityState.Ready); } if (command.Contains("START")) { this.ActivityRepository.SetActivityState(activity.Id, ActivityState.Starting); Thread.Sleep(1000); this.ActivityRepository.SetActivityState(activity.Id, ActivityState.Terminated); } activity = this.ActivityRepository.GetActivity(activity.Id); this.ProviderProcessor.SendActivityExecResult(activity.Id, batchId, new ActivityExecResult() { CurrentState = activity.State, Result = ActivityExecResult.ActivityExecResultEnum.OK }); } break; case ActivityProviderEvent.ActivityProviderEventType.DestroyActivity: isActivityDestroyed = true; break; } } }while (isActivityDestroyed != true); }); // 2.2. Process the ExeScript results on Requestor side bool isActivityFinished = false; do { var reqEvents = await this.RequestorProcessor.GetExecBatchResultsAsync(batchId, 1.0f); foreach (var reqEvent in reqEvents) { Assert.AreEqual(ActivityExecResult.ActivityExecResultEnum.OK, reqEvent.ExecResult.Result); if (reqEvent.ExecResult.CurrentState == ActivityState.Terminated) { isActivityFinished = true; } } }while (!isActivityFinished); // 3. Destroy Activity this.RequestorProcessor.DestroyActivity(activity.Id); }