Skip to content

Commit 616c529

Browse files
setting up foundation to submit api cmds
1 parent 803728b commit 616c529

2 files changed

Lines changed: 98 additions & 8 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.managed.context;
18+
19+
public class ManagedContextExecutor extends ManagedContextRunnable {
20+
private final Runnable task;
21+
22+
public ManagedContextExecutor(Runnable task) {
23+
this.task = task;
24+
}
25+
26+
@Override
27+
protected void runInContext() {
28+
task.run();
29+
}
30+
31+
public static void execute(Runnable task) {
32+
new ManagedContextExecutor(task).run();
33+
}
34+
}

plugins/iac/nimble/src/main/java/org/apache/cloudstack/tosca/orchestrator/ToscaOrchestrator.java

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,30 @@
1717
package org.apache.cloudstack.tosca.orchestrator;
1818

1919
import com.cloud.api.ApiDispatcher;
20+
import com.cloud.api.ApiGsonHelper;
21+
import com.cloud.api.ApiServer;
2022
import com.cloud.exception.InvalidParameterValueException;
2123
import com.cloud.utils.UuidUtils;
2224
import com.cloud.utils.component.ComponentContext;
2325
import org.apache.cloudstack.api.command.user.vmgroup.CreateVMGroupCmd;
26+
import org.apache.cloudstack.api.command.user.vpc.CreateVPCCmd;
2427
import org.apache.cloudstack.context.CallContext;
28+
import org.apache.cloudstack.framework.jobs.AsyncJobDispatcher;
29+
import org.apache.cloudstack.framework.jobs.AsyncJobManager;
30+
import org.apache.cloudstack.framework.jobs.impl.AsyncJobVO;
31+
import org.apache.cloudstack.managed.context.ManagedContextExecutor;
2532
import org.apache.cloudstack.persistence.iactemplatesprofile.IacResourceTypeVO;
2633
import org.apache.cloudstack.tosca.model.ToscaNodeTemplate;
2734
import org.apache.cloudstack.tosca.model.ToscaNodeType;
2835
import org.apache.cloudstack.tosca.model.ToscaServiceTemplate;
2936
import org.apache.cloudstack.tosca.parser.ToscaParser;
37+
import org.apache.commons.lang3.ObjectUtils;
3038
import org.apache.logging.log4j.LogManager;
3139
import org.apache.logging.log4j.Logger;
3240
import org.apache.logging.log4j.ThreadContext;
3341

3442
import javax.inject.Inject;
43+
import javax.inject.Named;
3544
import java.util.Collections;
3645
import java.util.HashMap;
3746
import java.util.HashSet;
@@ -56,6 +65,16 @@ public class ToscaOrchestrator {
5665
@Inject
5766
private ApiDispatcher apiDispatcher;
5867

68+
@Inject
69+
private AsyncJobManager asyncJobManager;
70+
71+
@Inject
72+
@Named("ApiAsyncJobDispatcher")
73+
private AsyncJobDispatcher asyncJobDispatcher;
74+
75+
@Inject
76+
private ApiServer apiServer;
77+
5978
private Map<String, ToscaNodeType> toscaProfile;
6079

6180
private ExecutorService executorPool;
@@ -150,21 +169,58 @@ private CompletableFuture<String> provisionNode(ToscaNodeTemplate nodeTemplate)
150169

151170
Random random = new Random();
152171
int randomInt = random.nextInt((4000 - 500) + 1) + 500;
153-
logger.debug("sleeping for randomInt: [{}] ms", randomInt);
154-
try {
155-
dispatchProvisioningCommand(callerContext, randomInt);
156-
Thread.sleep(randomInt);
157-
} catch (InterruptedException e) {
158-
throw new RuntimeException(e);
159-
}
172+
173+
logger.info("Getting API Class: [{}]", apiServer.getCmdClass("listAnnotations").getName());
174+
175+
ManagedContextExecutor.execute(() -> {
176+
dispatchProvisioningSynchronousCommand(callerContext, randomInt);
177+
try {
178+
logger.debug("sleeping for randomInt: [{}] ms", randomInt);
179+
Thread.sleep(randomInt);
180+
} catch (InterruptedException e) {
181+
throw new RuntimeException(e);
182+
}
183+
});
184+
185+
ManagedContextExecutor.execute(() -> {
186+
dispatchProvisioningAsynchronousCommand(callerContext, randomInt);
187+
});
160188

161189
logger.debug("Here you'll be able to populate the attributes");
162190
// ThreadContext.put("logcontextid", currentLogContextId);
163191
return "res-" + nodeTemplate.getName();
164192
}, executorPool);
165193
}
166194

167-
private void dispatchProvisioningCommand(CallContext ctx, int randomInt) {
195+
private void dispatchProvisioningAsynchronousCommand(CallContext ctx, int randomInt) {
196+
Map<String, String> params = new HashMap<>(Map.of(
197+
"zoneid", "309ea14d-ce26-44eb-ac05-53106b0ccb17",
198+
"name", "vpc-" + randomInt,
199+
"vpcofferingid", "3e70fd9b-bc5a-4d4b-89f1-40dc756e8058",
200+
"cidr", "10.0.0.0/16",
201+
"ctxUserId", String.valueOf(ctx.getCallingUserId()),
202+
"ctxAccountId", String.valueOf(ctx.getCallingAccountId())
203+
));
204+
CreateVPCCmd cmd = new CreateVPCCmd();
205+
cmd = ComponentContext.inject(cmd);
206+
try {
207+
apiDispatcher.dispatchCreateCmd(cmd, params);
208+
params.put("ctxStartEventId", "1");
209+
Long objectId = ObjectUtils.defaultIfNull(cmd.getEntityId(), cmd.getApiResourceId());
210+
params.put("id", objectId.toString());
211+
AsyncJobVO job = new AsyncJobVO("", ctx.getCallingUserId(), ctx.getCallingAccountId(), CreateVPCCmd.class.getName(),
212+
ApiGsonHelper.getBuilder().create().toJson(params), objectId,
213+
cmd.getApiResourceType() != null ? cmd.getApiResourceType().toString() : null,
214+
null);
215+
job.setDispatcher(asyncJobDispatcher.getName());
216+
long jobId = asyncJobManager.submitAsyncJob(job);
217+
logger.info("Submitted async job with id: {}", jobId);
218+
} catch (Exception e) {
219+
throw new RuntimeException(e);
220+
}
221+
}
222+
223+
private void dispatchProvisioningSynchronousCommand(CallContext ctx, int randomInt) {
168224
logger.debug("Constructing provisioning command");
169225
CallContext.register(ctx, null);
170226
CreateVMGroupCmd cmd = new CreateVMGroupCmd();

0 commit comments

Comments
 (0)