Bug 8608 - quick fix for async transaction creation
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / policy / acl / SourceMapper.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. 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.vpp.policy.acl;
10
11 import java.net.UnknownHostException;
12 import java.util.List;
13
14 import org.opendaylight.groupbasedpolicy.renderer.vpp.policy.acl.AccessListUtil.ACE_DIRECTION;
15 import org.opendaylight.groupbasedpolicy.util.EndpointUtils;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.containment.endpoints.ContainmentEndpoint;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.parent.child.endpoints.parent.endpoint.choice.parent.endpoint._case.ParentEndpoint;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev160427.L3Context;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocation;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 class SourceMapper extends AddressMapper {
24
25     private static final Logger LOG = LoggerFactory.getLogger(SourceMapper.class);
26
27     SourceMapper(ACE_DIRECTION direction) {
28         super(direction);
29     }
30
31     @Override
32     List<GbpAceBuilder> updateExtRules(List<GbpAceBuilder> rules, AddressEndpointWithLocation localEp,
33             ContainmentEndpoint cEp) {
34         // TODO external networking as a next step.
35         return rules;
36     }
37
38     @Override
39     void updateRule(AddressEndpointWithLocation addrEp, GbpAceBuilder aclRuleBuilder) {
40         String address;
41         if (addrEp.getContextType().isAssignableFrom(L3Context.class)) {
42             address = addrEp.getAddress();
43         } else {
44             List<ParentEndpoint> parentEndpoints = EndpointUtils.getParentEndpoints(addrEp.getParentEndpointChoice());
45             if (parentEndpoints == null || parentEndpoints.isEmpty()
46                     || !parentEndpoints.get(0).getContextType().isAssignableFrom(L3Context.class)) {
47                 LOG.warn("Cannot resolve IP address for endpoint {}", addrEp);
48                 return;
49             }
50             address = parentEndpoints.get(0).getAddress();
51         }
52         LOG.trace("Setting src IP address {} in rule {}", address, aclRuleBuilder);
53         try {
54             AccessListUtil.setSourceL3Address(aclRuleBuilder, address);
55         } catch (UnknownHostException e) {
56             LOG.error("Failed to parse address {}. Cannot apply ACL entry {}. {}", address, aclRuleBuilder, e);
57         }
58     }
59 }