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 java.util.ArrayList;
11 import java.util.List;
13 import java.util.UUID;
14 import java.util.concurrent.ConcurrentHashMap;
15 import java.util.concurrent.ScheduledExecutorService;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
19 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
20 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.faas.uln.datastore.api.UlnDatastoreApi;
24 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.common.rev151013.Text;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.common.rev151013.Uuid;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.subnets.rev151013.subnets.container.subnets.SubnetBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.subnets.rev151013.subnets.container.subnets.subnet.ExternalGateways;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.subnets.rev151013.subnets.container.subnets.subnet.ExternalGatewaysBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.mapped.entity.MappedSubnet;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.mapped.entity.MappedSubnetBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.Subnet;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.subnet.Gateways;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.subnet.gateways.Prefixes;
38 import org.opendaylight.yangtools.yang.binding.DataObject;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import com.google.common.base.Optional;
45 public class FaasSubnetManagerListener implements DataChangeListener {
47 private static final Logger LOG = LoggerFactory.getLogger(FaasSubnetManagerListener.class);
48 private ConcurrentHashMap<SubnetId, Uuid> mappedSubnets = new ConcurrentHashMap<>();
49 private final ScheduledExecutorService executor;
50 private final DataBroker dataProvider;
51 private final TenantId gbpTenantId;
52 private final Uuid faasTenantId;
54 public FaasSubnetManagerListener(DataBroker dataProvider, TenantId gbpTenantId, Uuid faasTenantId,
55 ScheduledExecutorService executor) {
56 this.executor = executor;
57 this.faasTenantId = faasTenantId;
58 this.gbpTenantId = gbpTenantId;
59 this.dataProvider = dataProvider;
63 public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
64 executor.execute(new Runnable() {
72 private void executeEvent(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
74 for (DataObject dao : change.getCreatedData().values()) {
75 if (dao instanceof Subnet) {
76 Subnet subnet = (Subnet) dao;
77 LOG.debug("Subnet {} is Created.", subnet.getId().getValue());
78 UlnDatastoreApi.submitSubnetToDs(initSubnetBuilder(subnet).build());
82 Map<InstanceIdentifier<?>, DataObject> dao = change.getUpdatedData();
83 for (Map.Entry<InstanceIdentifier<?>, DataObject> entry : dao.entrySet()) {
84 if (entry.getValue() instanceof Subnet) {
85 Subnet subnet = (Subnet) dao;
86 LOG.debug("Subnet {} is Updated.", subnet.getId().getValue());
87 UlnDatastoreApi.submitSubnetToDs(initSubnetBuilder(subnet).build());
91 for (InstanceIdentifier<?> iid : change.getRemovedPaths()) {
92 DataObject old = change.getOriginalData().get(iid);
96 if (old instanceof Subnet) {
97 Subnet subnet = (Subnet) old;
98 ReadWriteTransaction rwTx = dataProvider.newReadWriteTransaction();
99 Optional<MappedSubnet> op = DataStoreHelper.removeIfExists(LogicalDatastoreType.OPERATIONAL,
100 FaasIidFactory.mappedSubnetIid(gbpTenantId, subnet.getId()), rwTx);
101 if (op.isPresent()) {
102 DataStoreHelper.submitToDs(rwTx);
104 Uuid faasSubnetId = mappedSubnets.remove(subnet.getId());
105 if (faasSubnetId != null) {
106 UlnDatastoreApi.removeSubnetFromDsIfExists(faasTenantId, faasSubnetId);
112 public void loadAll(List<Subnet> subnets, List<MappedSubnet> mpSubnets) {
113 if (mpSubnets != null) {
114 for (MappedSubnet mpSubnet : mpSubnets) {
115 mappedSubnets.putIfAbsent(mpSubnet.getGbpSubnetId(), mpSubnet.getFaasSubnetId());
118 if (subnets != null) {
119 for (Subnet subnet : subnets) {
120 LOG.debug("Loading Subnet {}", subnet.getId().getValue());
121 UlnDatastoreApi.submitSubnetToDs(initSubnetBuilder(subnet).build());
126 private SubnetBuilder initSubnetBuilder(Subnet gbpSubnet) {
127 SubnetBuilder builder = new SubnetBuilder();
128 if (gbpSubnet.getGateways() != null) {
129 List<ExternalGateways> gateways = new ArrayList<>();
130 for (Gateways gw : gbpSubnet.getGateways()) {
131 ExternalGatewaysBuilder eb = new ExternalGatewaysBuilder();
132 eb.setExternalGateway(gw.getGateway());
133 if (gw.getPrefixes() != null) {
134 List<IpPrefix> ipPrefixes = new ArrayList<>();
135 for (Prefixes px : gw.getPrefixes()) {
136 ipPrefixes.add(px.getPrefix());
138 eb.setPrefixes(ipPrefixes);
140 gateways.add(eb.build());
142 builder.setExternalGateways(gateways);
145 builder.setIpPrefix(gbpSubnet.getIpPrefix());
146 builder.setUuid(getFaasSubnetId(gbpSubnet.getId()));
147 builder.setName(new Text(gbpSubnet.getId().getValue()));
148 if (gbpSubnet.getDescription() != null)
149 builder.setDescription(new Text("gbp-subnet: " + gbpSubnet.getDescription().getValue()));
151 builder.setDescription(new Text("gbp-subnet"));
152 builder.setTenantId(faasTenantId);
153 builder.setVirtualRouterIp(gbpSubnet.getVirtualRouterIp());
155 builder.setDnsNameservers(null);
157 builder.setEnableDhcp(false);
161 private Uuid getFaasSubnetId(SubnetId subnetId) {
162 Uuid val = mappedSubnets.get(subnetId);
166 Uuid faasSubnetId = null;
167 if (FaasPolicyManager.isUUid(subnetId.getValue())) {
168 faasSubnetId = new Uuid(subnetId.getValue());
170 faasSubnetId = new Uuid(UUID.randomUUID().toString());
172 mappedSubnets.putIfAbsent(subnetId, faasSubnetId);
173 val = mappedSubnets.get(subnetId);
174 MappedSubnetBuilder builder = new MappedSubnetBuilder();
175 builder.setFaasSubnetId(val);
176 builder.setGbpSubnetId(subnetId);
177 WriteTransaction wTx = dataProvider.newWriteOnlyTransaction();
178 MappedSubnet result = builder.build();
179 wTx.put(LogicalDatastoreType.OPERATIONAL,
180 FaasIidFactory.mappedSubnetIid(gbpTenantId, subnetId), result);
181 if (DataStoreHelper.submitToDs(wTx)) {
182 LOG.debug("Cached in Datastore Mapped Subnet {}", result);
184 LOG.error("Couldn't Cache in Datastore Mapped Subnet {}", result);