2 * Copyright (c) 2015 Cisco Systems. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.groupbasedpolicy.renderer.iovisor;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.eq;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.spy;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
18 import java.util.ArrayList;
19 import java.util.List;
22 import com.google.common.collect.ImmutableList;
23 import com.google.common.collect.ImmutableSet;
24 import com.sun.jersey.api.client.ClientHandlerException;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
29 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
30 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
31 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
32 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
33 import org.opendaylight.groupbasedpolicy.api.EpRendererAugmentationRegistry;
34 import org.opendaylight.groupbasedpolicy.renderer.iovisor.endpoint.EndpointManager;
35 import org.opendaylight.groupbasedpolicy.renderer.iovisor.module.IovisorModuleManager;
36 import org.opendaylight.groupbasedpolicy.renderer.iovisor.restclient.RestClient;
37 import org.opendaylight.groupbasedpolicy.renderer.iovisor.sf.AllowAction;
38 import org.opendaylight.groupbasedpolicy.renderer.iovisor.sf.EtherTypeClassifier;
39 import org.opendaylight.groupbasedpolicy.renderer.iovisor.test.GbpIovisorDataBrokerTest;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContractId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.IovisorModuleId;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.iovisor.module.instances.IovisorModuleInstance;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.iovisor.module.instances.IovisorModuleInstanceBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.iovisor.modules.by.tenant.by.endpointgroup.id.iovisor.module.by.tenant.by.endpointgroup.id.IovisorModuleInstanceId;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.iovisor.modules.by.tenant.by.endpointgroup.id.iovisor.module.by.tenant.by.endpointgroup.id.IovisorModuleInstanceIdBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.ResolvedPolicies;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.has.actions.Action;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.has.actions.ActionBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.has.classifiers.Classifier;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.has.classifiers.ClassifierBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.has.resolved.rules.ResolvedRule;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.has.resolved.rules.ResolvedRuleBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.ResolvedPolicy;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.ResolvedPolicyBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.ResolvedPolicyKey;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.resolved.policy.PolicyRuleGroupWithEndpointConstraints;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.resolved.policy.PolicyRuleGroupWithEndpointConstraintsBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.resolved.policy.policy.rule.group.with.endpoint.constraints.PolicyRuleGroup;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.resolved.policy.policy.rule.group.with.endpoint.constraints.PolicyRuleGroupBuilder;
64 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
66 public class ResolvedPolicyListenerTest extends GbpIovisorDataBrokerTest {
68 private DataBroker dataBroker;
69 private ResolvedPolicyListener resolvedPolicyListener;
70 private DataObjectModification<ResolvedPolicy> rootNode;
71 private Set<DataTreeModification<ResolvedPolicy>> changes;
73 private TenantId tenantId = new TenantId("tenant1");
74 private EndpointGroupId consEpg = new EndpointGroupId("client");
75 private EndpointGroupId provEpg = new EndpointGroupId("webserver");
77 private SubjectName subjectName = new SubjectName("subject1");
78 private ContractId contractId = new ContractId("icmp-http-contract");
79 private List<Action> action =
80 ImmutableList.of(new ActionBuilder().setActionDefinitionId(new AllowAction().getId()).build());
81 private List<Classifier> classifier = ImmutableList
82 .of(new ClassifierBuilder().setClassifierDefinitionId(new EtherTypeClassifier(null).getId()).build());
84 private ResolvedPolicy resolvedPolicy;
85 private PolicyRuleGroupWithEndpointConstraints policyRuleGroupWithEndpointConstraints;
86 private ResolvedRule resolvedRule;
88 private RestClient restClient = mock(RestClient.class);
90 private List<PolicyRuleGroupWithEndpointConstraints> listOfPolicyRuleGroupWithEndpointConstraints =
92 private List<PolicyRuleGroup> policyRuleGroup;
93 private EndpointManager endpointManager;
94 private IovisorModuleManager iovisorModuleManager;
96 private IovisorModuleInstance iomInstance;
98 private final String iom1 = "192.168.50.100:5001";
100 @SuppressWarnings("unchecked")
102 public void iovisorInit() {
104 resolvedRule = new ResolvedRuleBuilder().setAction(action).setClassifier(classifier).build();
106 policyRuleGroup = ImmutableList.of(new PolicyRuleGroupBuilder().setContractId(contractId)
107 .setResolvedRule(ImmutableList.of(resolvedRule))
108 .setTenantId(tenantId)
109 .setSubjectName(subjectName)
112 policyRuleGroupWithEndpointConstraints = new PolicyRuleGroupWithEndpointConstraintsBuilder()
113 // .setConsumerEndpointConstraints(null)
114 // .setProviderEndpointConstraints(null)
115 .setPolicyRuleGroup(policyRuleGroup).build();
117 listOfPolicyRuleGroupWithEndpointConstraints.add(policyRuleGroupWithEndpointConstraints);
119 resolvedPolicy = new ResolvedPolicyBuilder().setConsumerEpgId(consEpg)
120 .setConsumerTenantId(tenantId)
121 .setProviderEpgId(provEpg)
122 .setProviderTenantId(tenantId)
123 .setPolicyRuleGroupWithEndpointConstraints(listOfPolicyRuleGroupWithEndpointConstraints)
126 // when(restClient.post(any(String.class), any(String.class)));
128 dataBroker = getDataBroker();
129 endpointManager = new EndpointManager(dataBroker, mock(EpRendererAugmentationRegistry.class));
130 iovisorModuleManager = mock(IovisorModuleManager.class);
131 // iovisorModuleManager = endpointManager.getIovisorModuleManager();
132 resolvedPolicyListener = spy(new ResolvedPolicyListener(dataBroker, iovisorModuleManager));
134 ResolvedPolicyKey key = mock(ResolvedPolicyKey.class);
135 rootNode = mock(DataObjectModification.class);
136 InstanceIdentifier<ResolvedPolicy> rootIdentifier =
137 InstanceIdentifier.builder(ResolvedPolicies.class)
138 .child(ResolvedPolicy.class, key)
140 DataTreeIdentifier<ResolvedPolicy> rootPath =
141 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, rootIdentifier);
142 DataTreeModification<ResolvedPolicy> change = mock(DataTreeModification.class);
144 when(change.getRootNode()).thenReturn(rootNode);
145 when(change.getRootPath()).thenReturn(rootPath);
147 changes = ImmutableSet.of(change);
149 when(rootNode.getDataAfter()).thenReturn(resolvedPolicy);
152 @Test(expected = NullPointerException.class)
153 public void onWriteTestNull() {
154 resolvedPolicyListener.processResolvedPolicyNotification(null);
158 public void testOnWrite() {
159 when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.WRITE);
161 resolvedPolicyListener.onDataTreeChanged(changes);
163 verify(resolvedPolicyListener).processResolvedPolicyNotification(eq(resolvedPolicy));
166 @Test(expected = UnsupportedOperationException.class)
167 public void testOnDelete() {
168 when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.DELETE);
170 resolvedPolicyListener.onDataTreeChanged(changes);
173 @Test(expected = UnsupportedOperationException.class)
174 public void testOnSubtreeModified() {
175 when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.SUBTREE_MODIFIED);
177 resolvedPolicyListener.onDataTreeChanged(changes);
181 public void policyUriBuilderTest() {
182 String target = "/restconf/operational/resolved-policy:resolved-policies/resolved-policy/" + tenantId.getValue()
183 + "/" + consEpg.getValue() + "/" + tenantId.getValue() + "/" + provEpg.getValue() + "/";
184 Assert.assertEquals(target, resolvedPolicyListener.buildPolicyUris(resolvedPolicy));
187 // TODO FIXME Need to resolve correct REST MOCK in order to remove expected exception.
188 @Test(expected = ClientHandlerException.class)
189 public void processResolvedPolicyNotificationTest() {
190 iomInstance = new IovisorModuleInstanceBuilder().setId(new IovisorModuleId(iom1)).setUri(new Uri(iom1)).build();
191 List<IovisorModuleInstanceId> listIomId = new ArrayList<>();
192 listIomId.add(new IovisorModuleInstanceIdBuilder().setId(new IovisorModuleId(iom1)).build());
193 when(iovisorModuleManager.getActiveIovisorModule(any(IovisorModuleId.class))).thenReturn(iomInstance);
194 when(iovisorModuleManager.getIovisorModulesByTenantByEpg(any(TenantId.class), any(EndpointGroupId.class)))
195 .thenReturn(listIomId);
196 resolvedPolicyListener.processResolvedPolicyNotification(resolvedPolicy);