89bc8f48cab0dea1e27dc461a479e214d608bd39
[groupbasedpolicy.git] / sxp-mapper / src / main / java / org / opendaylight / groupbasedpolicy / sxp / mapper / impl / SxpMapperReactorImpl.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.sxp.mapper.impl;
10
11 import java.util.Collections;
12
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
17 import org.opendaylight.groupbasedpolicy.sxp.mapper.api.SxpMapperReactor;
18 import org.opendaylight.groupbasedpolicy.sxp.mapper.impl.util.SxpListenerUtil;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.BaseEndpointService;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.Endpoints;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.RegisterEndpointInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.RegisterEndpointInputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.common.endpoint.fields.NetworkContainment;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.common.endpoint.fields.NetworkContainmentBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.AddressEndpoints;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpoint;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpointKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.register.endpoint.input.AddressEndpointReg;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.register.endpoint.input.AddressEndpointRegBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContextId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev160427.IpPrefixType;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev160427.L3Context;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.mapper.model.rev160302.sxp.mapper.EndpointForwardingTemplateBySubnet;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.mapper.model.rev160302.sxp.mapper.EndpointPolicyTemplateBySgt;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.database.rev160308.master.database.fields.MasterDatabaseBinding;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.common.RpcResult;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 import com.google.common.base.Optional;
44 import com.google.common.base.Preconditions;
45 import com.google.common.util.concurrent.AsyncFunction;
46 import com.google.common.util.concurrent.CheckedFuture;
47 import com.google.common.util.concurrent.Futures;
48 import com.google.common.util.concurrent.JdkFutureAdapters;
49 import com.google.common.util.concurrent.ListenableFuture;
50
51 /**
52  * Purpose: exclusively processes sxp master database changes and EGP templates changes
53  */
54 public class SxpMapperReactorImpl implements SxpMapperReactor {
55
56     private static final Logger LOG = LoggerFactory.getLogger(SxpMapperReactorImpl.class);
57
58     private final BaseEndpointService l3EndpointService;
59     private final DataBroker dataBroker;
60
61     public SxpMapperReactorImpl(final BaseEndpointService l3EndpointService, final DataBroker dataBroker) {
62         this.l3EndpointService = Preconditions.checkNotNull(l3EndpointService, "l3Endpoint service missing");
63         this.dataBroker = Preconditions.checkNotNull(dataBroker, "dataBroker missing");
64     }
65
66     @Override
67     public ListenableFuture<RpcResult<Void>> processTemplatesAndSxpMasterDB(
68             final EndpointPolicyTemplateBySgt epPolicyTemplate,
69             final EndpointForwardingTemplateBySubnet epForwardingTemplate,
70             final MasterDatabaseBinding masterDBBinding) {
71         LOG.debug("processing ep-templates + sxpMasterDB entry: {} - {}",
72                 masterDBBinding.getSecurityGroupTag(), masterDBBinding.getIpPrefix());
73         // apply sxpMasterDB to policy template
74         final Ipv4Prefix address = new Ipv4Prefix(epForwardingTemplate.getIpPrefix().getIpv4Prefix().getValue());
75         final NetworkContainment networkContainment = new NetworkContainmentBuilder()
76             .setNetworkDomainType(epForwardingTemplate.getNetworkContainment().getNetworkDomainType())
77             .setNetworkDomainId(epForwardingTemplate.getNetworkContainment().getNetworkDomainId())
78             .build();
79         final RegisterEndpointInput epInput = new RegisterEndpointInputBuilder()
80                 .setAddressEndpointReg(Collections.singletonList(new AddressEndpointRegBuilder()
81                         .setAddressType(IpPrefixType.class)
82                         .setAddress(address.getValue())
83                         .setContextType(L3Context.class)
84                         .setContextId(epForwardingTemplate.getL3Context())
85                         .setNetworkContainment(networkContainment)
86                         .setCondition(epPolicyTemplate.getConditions())
87                         .setTenant(epPolicyTemplate.getTenant())
88                         .setEndpointGroup(epPolicyTemplate.getEndpointGroups())
89                         .build()))
90                 .build();
91                 epForwardingTemplate.getL3Context();
92
93         return chainL3EPServiceIfEpAbsent(epInput);
94     }
95
96     private CheckedFuture<Optional<AddressEndpoint>, ReadFailedException> findExistingEndPoint(final ContextId containment,
97                                                                                           final String address) {
98         KeyedInstanceIdentifier<AddressEndpoint, AddressEndpointKey> addressEndpointPath =
99                 InstanceIdentifier.create(Endpoints.class).child(AddressEndpoints.class).child(AddressEndpoint.class,
100                         new AddressEndpointKey(address, IpPrefixType.class, containment, L3Context.class));
101         final ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
102         final CheckedFuture<Optional<AddressEndpoint>, ReadFailedException> read = rTx.read(
103                 LogicalDatastoreType.OPERATIONAL, addressEndpointPath);
104         Futures.addCallback(read, SxpListenerUtil.createTxCloseCallback(rTx));
105         return read;
106     }
107
108     private ListenableFuture<RpcResult<Void>> chainL3EPServiceIfEpAbsent(final RegisterEndpointInput epInput) {
109         AddressEndpointReg addressEndpoint = epInput.getAddressEndpointReg().get(0);
110         CheckedFuture<Optional<AddressEndpoint>, ReadFailedException> existingEndpointFuture =
111                 findExistingEndPoint(addressEndpoint.getContextId(), addressEndpoint.getAddress());
112
113         return Futures.transform(existingEndpointFuture, new AsyncFunction<Optional<AddressEndpoint>, RpcResult<Void>>() {
114             @Override
115             public ListenableFuture<RpcResult<Void>> apply(final Optional<AddressEndpoint> input) throws Exception {
116                 final ListenableFuture<RpcResult<Void>> nextResult;
117                 if (input == null || !input.isPresent()) {
118                     // invoke service
119                     return JdkFutureAdapters.listenInPoolThread(l3EndpointService.registerEndpoint(epInput));
120                 } else {
121                     final String existingL3EpMsg = String.format("address-endpoint for given key already exists: %s | %s",
122                             addressEndpoint.getContextId(), addressEndpoint.getAddress() );
123                     nextResult = Futures.immediateFailedFuture(new IllegalStateException(existingL3EpMsg));
124                 }
125                 return nextResult;
126             }
127         });
128     }
129
130 }
131