Tests for Iovisor root package
[groupbasedpolicy.git] / renderers / iovisor / src / test / java / org / opendaylight / groupbasedpolicy / renderer / iovisor / ResolvedPolicyListenerTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.groupbasedpolicy.renderer.iovisor;
10
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;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Set;
21
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;
65
66 public class ResolvedPolicyListenerTest extends GbpIovisorDataBrokerTest {
67
68     private DataBroker dataBroker;
69     private ResolvedPolicyListener resolvedPolicyListener;
70     private DataObjectModification<ResolvedPolicy> rootNode;
71     private Set<DataTreeModification<ResolvedPolicy>> changes;
72
73     private TenantId tenantId = new TenantId("tenant1");
74     private EndpointGroupId consEpg = new EndpointGroupId("client");
75     private EndpointGroupId provEpg = new EndpointGroupId("webserver");
76
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());
83
84     private ResolvedPolicy resolvedPolicy;
85     private PolicyRuleGroupWithEndpointConstraints policyRuleGroupWithEndpointConstraints;
86     private ResolvedRule resolvedRule;
87
88     private RestClient restClient = mock(RestClient.class);
89
90     private List<PolicyRuleGroupWithEndpointConstraints> listOfPolicyRuleGroupWithEndpointConstraints =
91             new ArrayList<>();;
92     private List<PolicyRuleGroup> policyRuleGroup;
93     private EndpointManager endpointManager;
94     private IovisorModuleManager iovisorModuleManager;
95
96     private IovisorModuleInstance iomInstance;
97
98     private final String iom1 = "192.168.50.100:5001";
99
100     @SuppressWarnings("unchecked")
101     @Before
102     public void iovisorInit() {
103
104         resolvedRule = new ResolvedRuleBuilder().setAction(action).setClassifier(classifier).build();
105
106         policyRuleGroup = ImmutableList.of(new PolicyRuleGroupBuilder().setContractId(contractId)
107             .setResolvedRule(ImmutableList.of(resolvedRule))
108             .setTenantId(tenantId)
109             .setSubjectName(subjectName)
110             .build());
111
112         policyRuleGroupWithEndpointConstraints = new PolicyRuleGroupWithEndpointConstraintsBuilder()
113             // .setConsumerEndpointConstraints(null)
114             // .setProviderEndpointConstraints(null)
115             .setPolicyRuleGroup(policyRuleGroup).build();
116
117         listOfPolicyRuleGroupWithEndpointConstraints.add(policyRuleGroupWithEndpointConstraints);
118
119         resolvedPolicy = new ResolvedPolicyBuilder().setConsumerEpgId(consEpg)
120             .setConsumerTenantId(tenantId)
121             .setProviderEpgId(provEpg)
122             .setProviderTenantId(tenantId)
123             .setPolicyRuleGroupWithEndpointConstraints(listOfPolicyRuleGroupWithEndpointConstraints)
124             .build();
125
126         // when(restClient.post(any(String.class), any(String.class)));
127
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));
133
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)
139                         .build();
140         DataTreeIdentifier<ResolvedPolicy> rootPath =
141                 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, rootIdentifier);
142         DataTreeModification<ResolvedPolicy> change = mock(DataTreeModification.class);
143
144         when(change.getRootNode()).thenReturn(rootNode);
145         when(change.getRootPath()).thenReturn(rootPath);
146
147         changes = ImmutableSet.of(change);
148
149         when(rootNode.getDataAfter()).thenReturn(resolvedPolicy);
150     }
151
152     @Test(expected = NullPointerException.class)
153     public void onWriteTestNull() {
154         resolvedPolicyListener.processResolvedPolicyNotification(null);
155     }
156
157     @Test
158     public void testOnWrite() {
159         when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.WRITE);
160
161         resolvedPolicyListener.onDataTreeChanged(changes);
162
163         verify(resolvedPolicyListener).processResolvedPolicyNotification(eq(resolvedPolicy));
164     }
165
166     @Test(expected = UnsupportedOperationException.class)
167     public void testOnDelete() {
168         when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.DELETE);
169
170         resolvedPolicyListener.onDataTreeChanged(changes);
171     }
172
173     @Test(expected = UnsupportedOperationException.class)
174     public void testOnSubtreeModified() {
175         when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.SUBTREE_MODIFIED);
176
177         resolvedPolicyListener.onDataTreeChanged(changes);
178     }
179
180     @Test
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));
185     }
186
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);
197     }
198 }