m.rahil | b2e9cd9 | 2019-08-05 23:58:27 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.workflow.cli; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import org.apache.karaf.shell.api.action.Argument; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
| 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.workflow.api.WorkplaceStore; |
| 24 | import org.onosproject.workflow.api.WorkflowStore; |
| 25 | import org.onosproject.workflow.api.WorkflowContext; |
| 26 | import org.onosproject.workflow.api.Workflow; |
| 27 | import org.onosproject.workflow.api.JsonDataModelTree; |
| 28 | import org.onosproject.workflow.api.WorkflowException; |
| 29 | |
| 30 | |
| 31 | import java.util.Objects; |
| 32 | |
| 33 | @Service |
| 34 | @Command(scope = "onos", name = "workflow-nodes", description = "workflow status cli") |
| 35 | public class WorkflowStatusCommand extends AbstractShellCommand { |
| 36 | |
| 37 | static final String STATUS = "status"; |
| 38 | |
| 39 | @Argument(index = 0, name = "cmd", |
| 40 | description = "command(" + STATUS + "eval)", |
| 41 | required = true) |
| 42 | private String cmd = null; |
| 43 | |
| 44 | @Override |
| 45 | protected void doExecute() { |
| 46 | if (Objects.isNull(cmd)) { |
| 47 | error("invalid cmd parameter"); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | switch (cmd) { |
| 53 | case STATUS: |
| 54 | invoke(); |
| 55 | break; |
| 56 | default: |
| 57 | print("Unsupported cmd: " + cmd); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | |
| 62 | private void invoke() { |
| 63 | try { |
| 64 | WorkflowStore workflowStore = get(WorkflowStore.class); |
| 65 | WorkplaceStore workplaceStore = get(WorkplaceStore.class); |
| 66 | System.out.printf("%-25s %-45s %-10s%n", "DEVICEIP", " WORKFLOW NAME", "WORKFLOW STATE"); |
| 67 | for (WorkflowContext context : workplaceStore.getContexts()) { |
| 68 | for (Workflow workflow : workflowStore.getAll()) { |
| 69 | if (context.workflowId().equals(workflow.id())) { |
| 70 | JsonDataModelTree tree = (JsonDataModelTree) context.data(); |
| 71 | JsonNode mgmtIp = tree.nodeAt("/mgmtIp"); |
| 72 | System.out.printf("%-25s %-45s %-10s%n", mgmtIp, context.name(), context.state().toString()); |
| 73 | } |
| 74 | |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | } catch (WorkflowException e) { |
| 79 | e.printStackTrace(); |
| 80 | } |
| 81 | |
| 82 | } |
| 83 | } |
| 84 | |