2 * Copyright (c) 2015 Huawei Technologies and others. 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
8 package org.opendaylight.groupbasedpolicy.renderer.faas;
10 import com.google.common.base.Optional;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.List;
14 import java.util.UUID;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.Executor;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
21 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
22 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.faas.uln.datastore.api.UlnDatastoreApi;
25 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
26 import org.opendaylight.groupbasedpolicy.util.IetfModelCodec;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.common.rev151013.Text;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.common.rev151013.Uuid;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.subnets.rev151013.subnets.container.subnets.SubnetBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.subnets.rev151013.subnets.container.subnets.subnet.ExternalGateways;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.subnets.rev151013.subnets.container.subnets.subnet.ExternalGatewaysBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.mapped.entity.MappedSubnet;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.mapped.entity.MappedSubnetBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.Subnet;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.subnet.Gateways;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.subnet.gateways.Prefixes;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class FaasSubnetManagerListener implements DataTreeChangeListener<Subnet> {
44 private static final Logger LOG = LoggerFactory.getLogger(FaasSubnetManagerListener.class);
45 private final ConcurrentHashMap<SubnetId, Uuid> mappedSubnets = new ConcurrentHashMap<>();
46 private final Executor executor;
47 private final DataBroker dataProvider;
48 private final TenantId gbpTenantId;
49 private final Uuid faasTenantId;
51 public FaasSubnetManagerListener(DataBroker dataProvider, TenantId gbpTenantId, Uuid faasTenantId,
53 this.executor = executor;
54 this.faasTenantId = faasTenantId;
55 this.gbpTenantId = gbpTenantId;
56 this.dataProvider = dataProvider;
60 public void onDataTreeChanged(Collection<DataTreeModification<Subnet>> changes) {
61 executor.execute(() -> executeEvent(changes));
64 private void executeEvent(final Collection<DataTreeModification<Subnet>> changes) {
65 for (DataTreeModification<Subnet> change: changes) {
66 DataObjectModification<Subnet> rootNode = change.getRootNode();
67 switch (rootNode.getModificationType()) {
68 case SUBTREE_MODIFIED:
70 Subnet updatedSubnet = rootNode.getDataAfter();
71 LOG.debug("Subnet {} is Updated.", updatedSubnet.getId().getValue());
72 UlnDatastoreApi.submitSubnetToDs(initSubnetBuilder(updatedSubnet).build());
75 Subnet deletedSubnet = rootNode.getDataBefore();
76 ReadWriteTransaction rwTx = dataProvider.newReadWriteTransaction();
77 Optional<MappedSubnet> op = DataStoreHelper.removeIfExists(LogicalDatastoreType.OPERATIONAL,
78 FaasIidFactory.mappedSubnetIid(gbpTenantId, deletedSubnet.getId()), rwTx);
80 DataStoreHelper.submitToDs(rwTx);
82 Uuid faasSubnetId = mappedSubnets.remove(deletedSubnet.getId());
83 if (faasSubnetId != null) {
84 UlnDatastoreApi.removeSubnetFromDsIfExists(faasTenantId, faasSubnetId);
93 public void loadAll(List<Subnet> subnets, List<MappedSubnet> mpSubnets) {
94 if (mpSubnets != null) {
95 for (MappedSubnet mpSubnet : mpSubnets) {
96 mappedSubnets.putIfAbsent(mpSubnet.getGbpSubnetId(), mpSubnet.getFaasSubnetId());
99 if (subnets != null) {
100 for (Subnet subnet : subnets) {
101 LOG.debug("Loading Subnet {}", subnet.getId().getValue());
102 UlnDatastoreApi.submitSubnetToDs(initSubnetBuilder(subnet).build());
107 protected SubnetBuilder initSubnetBuilder(Subnet gbpSubnet) {
108 SubnetBuilder builder = new SubnetBuilder();
109 if (gbpSubnet.getGateways() != null) {
110 List<ExternalGateways> gateways = new ArrayList<>();
111 for (Gateways gw : gbpSubnet.getGateways()) {
112 ExternalGatewaysBuilder eb = new ExternalGatewaysBuilder();
113 eb.setExternalGateway(IetfModelCodec.ipAddress2013(gw.getGateway()));
114 if (gw.getPrefixes() != null) {
115 List<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix> ipPrefixes = new ArrayList<>();
116 for (Prefixes px : gw.getPrefixes()) {
117 ipPrefixes.add(IetfModelCodec.ipPrefix2013(px.getPrefix()));
119 eb.setPrefixes(ipPrefixes);
121 gateways.add(eb.build());
123 builder.setExternalGateways(gateways);
126 builder.setIpPrefix(IetfModelCodec.ipPrefix2013(gbpSubnet.getIpPrefix()));
127 builder.setUuid(getFaasSubnetId(gbpSubnet.getId()));
128 builder.setName(new Text(gbpSubnet.getId().getValue()));
129 if (gbpSubnet.getDescription() != null) {
130 builder.setDescription(new Text("gbp-subnet: " + gbpSubnet.getDescription().getValue()));
132 builder.setDescription(new Text("gbp-subnet"));
134 builder.setTenantId(faasTenantId);
135 builder.setVirtualRouterIp(IetfModelCodec.ipAddress2013(gbpSubnet.getVirtualRouterIp()));
137 builder.setDnsNameservers(null);
139 builder.setEnableDhcp(false);
143 private Uuid getFaasSubnetId(SubnetId subnetId) {
144 Uuid val = mappedSubnets.get(subnetId);
148 Uuid faasSubnetId = null;
149 if (FaasPolicyManager.isUUid(subnetId.getValue())) {
150 faasSubnetId = new Uuid(subnetId.getValue());
152 faasSubnetId = new Uuid(UUID.randomUUID().toString());
154 mappedSubnets.putIfAbsent(subnetId, faasSubnetId);
155 val = mappedSubnets.get(subnetId);
156 MappedSubnetBuilder builder = new MappedSubnetBuilder();
157 builder.setFaasSubnetId(val);
158 builder.setGbpSubnetId(subnetId);
159 WriteTransaction wTx = dataProvider.newWriteOnlyTransaction();
160 MappedSubnet result = builder.build();
161 wTx.put(LogicalDatastoreType.OPERATIONAL,
162 FaasIidFactory.mappedSubnetIid(gbpTenantId, subnetId), result);
163 if (DataStoreHelper.submitToDs(wTx)) {
164 LOG.debug("Cached in Datastore Mapped Subnet {}", result);
166 LOG.error("Couldn't Cache in Datastore Mapped Subnet {}", result);