IntentShowShellCommand extends OsgiCommandSupport { @Override protected Object doExecute() throws Exception { StringBuilder sb = new StringBuilder(); Uuid uuid = Uuid.getDefaultInstance(id); Intent intent = provider.getIntent(uuid); if (intent != null) { sb.append(String.format("Intent Id: <%s>\n", intent.getId().getValue())); sb.append(String.format("Subjects: \n")); for (Subjects subjects : intent.getSubjects()) { EndPointGroup endPointGroup = (EndPointGroup) subjects.getSubject(); sb.append(String.format(" Order: %d\n", subjects.getOrder())); sb.append(String.format(VALUE , endPointGroup.getEndPointGroup().getName())); sb.append("\n"); } sb.append(String.format("Actions: \n")); for (Actions actions : intent.getActions()) { Action action = actions.getAction(); sb.append(String.format(" Order: %d\n", actions.getOrder())); if (action instanceof Allow) { sb.append(String.format(VALUE , NicProvider.ACTION_ALLOW)); } else if (action instanceof Block) { sb.append(String.format(VALUE , NicProvider.ACTION_BLOCK)); } else if (action instanceof Log) { sb.append(String.format(VALUE , NicProvider.ACTION_LOG)); } else if (action instanceof Redirect) { sb.append(String.format(VALUE , NicProvider.ACTION_REDIRECT)); } else { sb.append(String.format(VALUE , "UNKNOWN")); } } if (intent.getConstraints() != null) { sb.append(String.format("\nConstraints: \n")); for (Constraints constraints : intent.getConstraints()) { org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.constraints.Constraints constraint = constraints.getConstraints(); sb.append(String.format(" Order: %d\n", constraints.getOrder())); if (constraint instanceof QosConstraint) { sb.append(String.format(VALUE , ((QosConstraint) constraints.getConstraints()).getQosConstraint().getQosName())); } else if (constraint instanceof ClassificationConstraint) { sb.append(String.format(VALUE , ((ClassificationConstraint) constraints.getConstraints()) .getClassificationConstraint().getClassifier())); } else { sb.append(String.format(VALUE , "UNKNOWN YET")); } sb.append("\n"); } } return sb.toString(); } else { return String.format("No intent found. Check the logs for more details."); } } IntentShowShellCommand(NicConsoleProvider provider);   }
@Test public final void testDoExecute() throws Exception { final String id = UUID.randomUUID().toString(); final String failResult = "No intent found. Check the logs for more details."; final String noIntent = "No intent found"; final Uuid uuid = new Uuid(id); IntentKey intentkey = new IntentKey(uuid); Allow allow = new AllowBuilder().build(); Block block = new BlockBuilder().build(); Actions actionAllow = new ActionsBuilder().setAction(allow).build(); Actions actionBlock = new ActionsBuilder().setAction(block).build(); Actions actionNull = new ActionsBuilder().setAction(null).build(); final List<Actions> actionlist = new ArrayList<Actions>(); actionlist.add(actionAllow); actionlist.add(actionBlock); actionlist.add(actionNull); EndPointGroup src = new EndPointGroupBuilder().setName(SOURCE_IP).build(); EndPointGroup dest = new EndPointGroupBuilder().setName(DEST_IP).build(); Subject srcAddress = new org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject.EndPointGroupBuilder().setEndPointGroup(src).build(); Subject destAddress = new org.opendaylight.yang.gen.v1.urn.opendaylight.intent.rev150122.intent.subjects.subject.EndPointGroupBuilder().setEndPointGroup(dest).build(); Subjects srcsubject = new SubjectsBuilder().setSubject(srcAddress).build(); Subjects destsubject = new SubjectsBuilder().setSubject(destAddress).build(); final List<Subjects> subjectslist = new ArrayList<Subjects>(); subjectslist.add(srcsubject); subjectslist.add(destsubject); final StringBuilder expected = new StringBuilder(); expected.append("Intent Id: <"+id+">\n").append("Subjects: \n").append(" Order: null\n"); expected.append(" Value: "+SOURCE_IP+"\n").append("\n").append(" Order: null\n").append(" Value: "+DEST_IP+"\n").append("\n"); expected.append("Actions: \n").append(" Order: null\n").append(" Value: ALLOW\n").append(" Order: null\n"); expected.append(" Value: BLOCK\n").append(" Order: null\n").append(" Value: UNKNOWN\n"); Intent intent = new IntentBuilder().setKey(intentkey).setActions(actionlist).setSubjects(subjectslist).build(); PowerMockito.stub(PowerMockito.method(Uuid.class, "getDefaultInstance")).toReturn(uuid); when(provider.getIntent(uuid)).thenReturn(intent); Object actual = intentShowShellCommand.doExecute(); assertEquals(expected.toString(), actual); when(provider.getIntent(uuid)).thenReturn(null); Object result = intentShowShellCommand.doExecute(); assertEquals("No intent found", failResult, result); }