f1943aa0a8dc0d7fd851bd24242fca86573f8660
[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.Mockito.mock;
13 import static org.mockito.Mockito.when;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.junit.Assert;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.groupbasedpolicy.api.EpRendererAugmentationRegistry;
23 import org.opendaylight.groupbasedpolicy.renderer.iovisor.endpoint.EndpointManager;
24 import org.opendaylight.groupbasedpolicy.renderer.iovisor.module.IovisorModuleManager;
25 import org.opendaylight.groupbasedpolicy.renderer.iovisor.restclient.RestClient;
26 import org.opendaylight.groupbasedpolicy.renderer.iovisor.sf.AllowAction;
27 import org.opendaylight.groupbasedpolicy.renderer.iovisor.sf.EtherTypeClassifier;
28 import org.opendaylight.groupbasedpolicy.renderer.iovisor.test.GbpIovisorDataBrokerTest;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContractId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.IovisorModuleId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.iovisor.module.instances.IovisorModuleInstance;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.iovisor.rev151030.iovisor.module.instances.IovisorModuleInstanceBuilder;
37 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;
38 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;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.has.actions.Action;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.has.actions.ActionBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.has.classifiers.Classifier;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.has.classifiers.ClassifierBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.has.resolved.rules.ResolvedRule;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.has.resolved.rules.ResolvedRuleBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.ResolvedPolicy;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.ResolvedPolicyBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.resolved.policy.PolicyRuleGroupWithEndpointConstraints;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.resolved.policy.PolicyRuleGroupWithEndpointConstraintsBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.resolved.policy.policy.rule.group.with.endpoint.constraints.PolicyRuleGroup;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.resolved.policy.policy.rule.group.with.endpoint.constraints.PolicyRuleGroupBuilder;
51
52 import com.google.common.collect.ImmutableList;
53 import com.sun.jersey.api.client.ClientHandlerException;
54
55 public class ResolvedPolicyListenerTest extends GbpIovisorDataBrokerTest {
56
57     private DataBroker dataBroker;
58     private ResolvedPolicyListener resolvedPolicyListener;
59
60     private TenantId tenantId = new TenantId("tenant1");
61     private EndpointGroupId consEpg = new EndpointGroupId("client");
62     private EndpointGroupId provEpg = new EndpointGroupId("webserver");
63
64     private SubjectName subjectName = new SubjectName("subject1");
65     private ContractId contractId = new ContractId("icmp-http-contract");
66     private List<Action> action =
67             ImmutableList.of(new ActionBuilder().setActionDefinitionId(new AllowAction().getId()).build());
68     private List<Classifier> classifier = ImmutableList
69         .of(new ClassifierBuilder().setClassifierDefinitionId(new EtherTypeClassifier(null).getId()).build());
70
71     private ResolvedPolicy resolvedPolicy;
72     private PolicyRuleGroupWithEndpointConstraints policyRuleGroupWithEndpointConstraints;
73     private ResolvedRule resolvedRule;
74
75     private RestClient restClient = mock(RestClient.class);
76
77     private List<PolicyRuleGroupWithEndpointConstraints> listOfPolicyRuleGroupWithEndpointConstraints =
78             new ArrayList<>();;
79     private List<PolicyRuleGroup> policyRuleGroup;
80     private EndpointManager endpointManager;
81     private IovisorModuleManager iovisorModuleManager;
82
83     private IovisorModuleInstance iomInstance;
84
85     private final String iom1 = "192.168.50.100:5001";
86
87     @SuppressWarnings("unchecked")
88     @Before
89     public void iovisorInit() {
90
91         resolvedRule = new ResolvedRuleBuilder().setAction(action).setClassifier(classifier).build();
92
93         policyRuleGroup = ImmutableList.of(new PolicyRuleGroupBuilder().setContractId(contractId)
94             .setResolvedRule(ImmutableList.of(resolvedRule))
95             .setTenantId(tenantId)
96             .setSubjectName(subjectName)
97             .build());
98
99         policyRuleGroupWithEndpointConstraints = new PolicyRuleGroupWithEndpointConstraintsBuilder()
100             // .setConsumerEndpointConstraints(null)
101             // .setProviderEndpointConstraints(null)
102             .setPolicyRuleGroup(policyRuleGroup).build();
103
104         listOfPolicyRuleGroupWithEndpointConstraints.add(policyRuleGroupWithEndpointConstraints);
105
106         resolvedPolicy = new ResolvedPolicyBuilder().setConsumerEpgId(consEpg)
107             .setConsumerTenantId(tenantId)
108             .setProviderEpgId(provEpg)
109             .setProviderTenantId(tenantId)
110             .setPolicyRuleGroupWithEndpointConstraints(listOfPolicyRuleGroupWithEndpointConstraints)
111             .build();
112
113         // when(restClient.post(any(String.class), any(String.class)));
114
115         dataBroker = getDataBroker();
116         endpointManager = new EndpointManager(dataBroker, mock(EpRendererAugmentationRegistry.class));
117         iovisorModuleManager = mock(IovisorModuleManager.class);
118         // iovisorModuleManager = endpointManager.getIovisorModuleManager();
119         resolvedPolicyListener = new ResolvedPolicyListener(dataBroker, iovisorModuleManager);
120     }
121
122     @Test(expected = NullPointerException.class)
123     public void onWriteTestNull() {
124         resolvedPolicyListener.processResolvedPolicyNotification(null);
125     }
126
127     @Test
128     public void policyUriBuilderTest() {
129         String target = "/restconf/operational/resolved-policy:resolved-policies/resolved-policy/" + tenantId.getValue()
130                 + "/" + consEpg.getValue() + "/" + tenantId.getValue() + "/" + provEpg.getValue() + "/";
131         Assert.assertEquals(target, resolvedPolicyListener.buildPolicyUris(resolvedPolicy));
132     }
133
134     // TODO FIXME Need to resolve correct REST MOCK in order to remove expected exception.
135     @Test(expected = ClientHandlerException.class)
136     public void processResolvedPolicyNotificationTest() {
137         iomInstance = new IovisorModuleInstanceBuilder().setId(new IovisorModuleId(iom1)).setUri(new Uri(iom1)).build();
138         List<IovisorModuleInstanceId> listIomId = new ArrayList<>();
139         listIomId.add(new IovisorModuleInstanceIdBuilder().setId(new IovisorModuleId(iom1)).build());
140         when(iovisorModuleManager.getActiveIovisorModule(any(IovisorModuleId.class))).thenReturn(iomInstance);
141         when(iovisorModuleManager.getIovisorModulesByTenantByEpg(any(TenantId.class), any(EndpointGroupId.class)))
142             .thenReturn(listIomId);
143         resolvedPolicyListener.processResolvedPolicyNotification(resolvedPolicy);
144     }
145 }