UT: Added FaaS unit tests
[groupbasedpolicy.git] / renderers / faas / src / main / java / org / opendaylight / groupbasedpolicy / renderer / faas / FaasSubnetManagerListener.java
1 /*
2  * Copyright (c) 2015 Huawei Technologies 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 package org.opendaylight.groupbasedpolicy.renderer.faas;
9
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.UUID;
14 import java.util.concurrent.ConcurrentHashMap;
15 import java.util.concurrent.ScheduledExecutorService;
16
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;
42
43 import com.google.common.base.Optional;
44
45 public class FaasSubnetManagerListener implements DataChangeListener {
46
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;
53
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;
60     }
61
62     @Override
63     public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
64         executor.execute(new Runnable() {
65
66             public void run() {
67                 executeEvent(change);
68             }
69         });
70     }
71
72     private void executeEvent(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
73         // Create
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());
79             }
80         }
81         // Update
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());
88             }
89         }
90         // Remove
91         for (InstanceIdentifier<?> iid : change.getRemovedPaths()) {
92             DataObject old = change.getOriginalData().get(iid);
93             if (old == null) {
94                 continue;
95             }
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);
103                 }
104                 Uuid faasSubnetId = mappedSubnets.remove(subnet.getId());
105                 if (faasSubnetId != null) {
106                     UlnDatastoreApi.removeSubnetFromDsIfExists(faasTenantId, faasSubnetId);
107                 }
108             }
109         }
110     }
111
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());
116             }
117         }
118         if (subnets != null) {
119             for (Subnet subnet : subnets) {
120                 LOG.debug("Loading Subnet {}", subnet.getId().getValue());
121                 UlnDatastoreApi.submitSubnetToDs(initSubnetBuilder(subnet).build());
122             }
123         }
124     }
125
126     protected 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());
137                     }
138                     eb.setPrefixes(ipPrefixes);
139                 }
140                 gateways.add(eb.build());
141             }
142             builder.setExternalGateways(gateways);
143         }
144
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()));
150         else
151             builder.setDescription(new Text("gbp-subnet"));
152         builder.setTenantId(faasTenantId);
153         builder.setVirtualRouterIp(gbpSubnet.getVirtualRouterIp());
154         // TODO DNS servers
155         builder.setDnsNameservers(null);
156         // TODO DHCP server
157         builder.setEnableDhcp(false);
158         return builder;
159     }
160
161     private Uuid getFaasSubnetId(SubnetId subnetId) {
162         Uuid val = mappedSubnets.get(subnetId);
163         if (val != null) {
164             return val;
165         }
166         Uuid faasSubnetId = null;
167         if (FaasPolicyManager.isUUid(subnetId.getValue())) {
168             faasSubnetId = new Uuid(subnetId.getValue());
169         } else {
170             faasSubnetId = new Uuid(UUID.randomUUID().toString());
171         }
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);
183         } else {
184             LOG.error("Couldn't Cache in Datastore Mapped Subnet {}", result);
185         }
186         return val;
187     }
188
189 }