Bug 6743: added futures to InterfaceManager and VppNodeManager
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / iface / VppEndpointLocationProvider.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.iface;
10
11 import static com.google.common.base.Preconditions.checkNotNull;
12
13 import javax.annotation.Nonnull;
14 import com.google.common.base.Function;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.CloseOnFailTransactionChain;
23 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.KeyFactory;
24 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppIidFactory;
25 import org.opendaylight.groupbasedpolicy.util.IidFactory;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.AbsoluteLocation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.AbsoluteLocationBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.ProviderName;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.LocationProvider;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.LocationProviderBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocation;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocationBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocationKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocationKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.VppEndpoint;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public class VppEndpointLocationProvider implements AutoCloseable {
44
45     private static final Logger LOG = LoggerFactory.getLogger(VppEndpointLocationProvider.class);
46     public static final ProviderName VPP_ENDPOINT_LOCATION_PROVIDER =
47             new ProviderName("VPP endpoint location provider");
48     public static final long PROVIDER_PRIORITY = 10L;
49     private final BindingTransactionChain txChain;
50
51     public VppEndpointLocationProvider(DataBroker dataProvider) {
52         LocationProvider locationProvider = new LocationProviderBuilder().setProvider(VPP_ENDPOINT_LOCATION_PROVIDER)
53             .setPriority(PROVIDER_PRIORITY)
54             .build();
55         txChain = checkNotNull(dataProvider)
56             .createTransactionChain(new CloseOnFailTransactionChain(VppEndpointLocationProvider.class.getSimpleName()));
57         WriteTransaction wTx = txChain.newWriteOnlyTransaction();
58         wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VPP_ENDPOINT_LOCATION_PROVIDER),
59                 locationProvider, true);
60
61         Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
62
63             @Override
64             public void onSuccess(Void result) {
65                 LOG.debug("{} was created", VPP_ENDPOINT_LOCATION_PROVIDER.getValue());
66             }
67
68             @Override
69             public void onFailure(Throwable t) {
70                 LOG.error("{} was NOT created", VPP_ENDPOINT_LOCATION_PROVIDER.getValue());
71             }
72         });
73     }
74
75     ListenableFuture<Void> createLocationForVppEndpoint(final VppEndpoint vppEndpoint) {
76         final ProviderAddressEndpointLocation providerAddressEndpointLocation =
77                 createProviderAddressEndpointLocation(vppEndpoint);
78         final WriteTransaction wTx = txChain.newWriteOnlyTransaction();
79         wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.providerAddressEndpointLocationIid(
80                 VPP_ENDPOINT_LOCATION_PROVIDER, providerAddressEndpointLocation.getKey()),
81                 providerAddressEndpointLocation);
82         LOG.debug("Creating location for {}", providerAddressEndpointLocation.getKey());
83         return Futures.transform(wTx.submit(), (Function<Void, Void>) input -> {
84             LOG.debug("{} provided location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
85                     providerAddressEndpointLocation);
86             return null;
87         });
88     }
89
90     public static ProviderAddressEndpointLocation createProviderAddressEndpointLocation(VppEndpoint vppEndpoint) {
91         InstanceIdentifier<Node> vppNodeIid = VppIidFactory.getNetconfNodeIid(vppEndpoint.getVppNodeId());
92         String restIfacePath = VppPathMapper.interfaceToRestPath(vppEndpoint.getVppInterfaceName());
93         AbsoluteLocation absoluteLocation = new AbsoluteLocationBuilder()
94             .setLocationType(new ExternalLocationCaseBuilder().setExternalNodeMountPoint(vppNodeIid)
95                 .setExternalNodeConnector(restIfacePath)
96                 .build())
97             .build();
98         return new ProviderAddressEndpointLocationBuilder()
99             .setKey(createProviderAddressEndpointLocationKey(vppEndpoint))
100             .setAbsoluteLocation(absoluteLocation)
101             .build();
102     }
103
104     ListenableFuture<Void> deleteLocationForVppEndpoint(final VppEndpoint vppEndpoint) {
105         final ProviderAddressEndpointLocationKey provAddrEpLocKey =
106                 createProviderAddressEndpointLocationKey(vppEndpoint);
107         final WriteTransaction wTx = txChain.newWriteOnlyTransaction();
108         wTx.delete(LogicalDatastoreType.CONFIGURATION,
109                 IidFactory.providerAddressEndpointLocationIid(VPP_ENDPOINT_LOCATION_PROVIDER, provAddrEpLocKey));
110         LOG.debug("Deleting location for {}", provAddrEpLocKey);
111         return Futures.transform(wTx.submit(), (Function<Void, Void>) input -> {
112             LOG.debug("{} removed location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(), provAddrEpLocKey);
113             return null;
114         });
115     }
116
117     private static ProviderAddressEndpointLocationKey createProviderAddressEndpointLocationKey(
118             VppEndpoint vppEndpoint) {
119         return new ProviderAddressEndpointLocationKey(vppEndpoint.getAddress(), vppEndpoint.getAddressType(),
120                 vppEndpoint.getContextId(), vppEndpoint.getContextType());
121     }
122
123     public ListenableFuture<Void> replaceLocationForEndpoint(@Nonnull ExternalLocationCase location, @Nonnull AddressEndpointWithLocationKey addrEpWithLocKey) {
124         ProviderAddressEndpointLocationKey provAddrEpLocKey =
125                 KeyFactory.providerAddressEndpointLocationKey(addrEpWithLocKey);
126         AbsoluteLocation absoluteLocation =
127                 new AbsoluteLocationBuilder().setLocationType(location).build();
128         ProviderAddressEndpointLocation providerAddressEndpointLocation = new ProviderAddressEndpointLocationBuilder()
129             .setKey(provAddrEpLocKey).setAbsoluteLocation(absoluteLocation).build();
130         WriteTransaction wTx = txChain.newWriteOnlyTransaction();
131         wTx.put(LogicalDatastoreType.CONFIGURATION,
132                 IidFactory.providerAddressEndpointLocationIid(VPP_ENDPOINT_LOCATION_PROVIDER,
133                         providerAddressEndpointLocation.getKey()),
134                 providerAddressEndpointLocation);
135         LOG.debug("Updating location for {}", provAddrEpLocKey);
136         return Futures.transform(wTx.submit(), new Function<Void, Void>() {
137
138             @Override
139             public Void apply(Void input) {
140                 LOG.debug("{} replaced location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
141                         providerAddressEndpointLocation);
142                 return null;
143             }
144         });
145     }
146
147     @Override
148     public void close() throws Exception {
149         WriteTransaction wTx = txChain.newWriteOnlyTransaction();
150         wTx.delete(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VPP_ENDPOINT_LOCATION_PROVIDER));
151         wTx.submit();
152     }
153 }