2 * Copyright (c) 2016 Cisco Systems, Inc. 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
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.iface;
11 import static com.google.common.base.Preconditions.checkNotNull;
13 import javax.annotation.Nonnull;
15 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.CloseOnFailTransactionChain;
20 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.KeyFactory;
21 import org.opendaylight.groupbasedpolicy.util.IidFactory;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.AbsoluteLocation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.AbsoluteLocationBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.ProviderName;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.LocationProvider;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.LocationProviderBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocation;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocationBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocationKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocationKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.VppEndpoint;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import com.google.common.base.Function;
38 import com.google.common.util.concurrent.FutureCallback;
39 import com.google.common.util.concurrent.Futures;
40 import com.google.common.util.concurrent.ListenableFuture;
42 public class VppEndpointLocationProvider implements AutoCloseable {
44 private static final Logger LOG = LoggerFactory.getLogger(VppEndpointLocationProvider.class);
45 public static final ProviderName VPP_ENDPOINT_LOCATION_PROVIDER =
46 new ProviderName("VPP endpoint location provider");
47 public static final long PROVIDER_PRIORITY = 10L;
48 private final BindingTransactionChain txChain;
50 public VppEndpointLocationProvider(DataBroker dataProvider) {
51 LocationProvider locationProvider = new LocationProviderBuilder().setProvider(VPP_ENDPOINT_LOCATION_PROVIDER)
52 .setPriority(PROVIDER_PRIORITY)
54 txChain = checkNotNull(dataProvider)
55 .createTransactionChain(new CloseOnFailTransactionChain(VppEndpointLocationProvider.class.getSimpleName()));
56 WriteTransaction wTx = txChain.newWriteOnlyTransaction();
57 wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VPP_ENDPOINT_LOCATION_PROVIDER),
58 locationProvider, true);
60 Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
63 public void onSuccess(Void result) {
64 LOG.debug("{} was created", VPP_ENDPOINT_LOCATION_PROVIDER.getValue());
68 public void onFailure(Throwable t) {
69 LOG.error("{} was NOT created", VPP_ENDPOINT_LOCATION_PROVIDER.getValue());
74 public ListenableFuture<Void> createLocationForVppEndpoint(VppEndpoint vppEndpoint) {
75 ProviderAddressEndpointLocation providerAddressEndpointLocation = createProviderAddressEndpointLocation(vppEndpoint);
76 WriteTransaction wTx = txChain.newWriteOnlyTransaction();
77 wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.providerAddressEndpointLocationIid(
78 VPP_ENDPOINT_LOCATION_PROVIDER, providerAddressEndpointLocation.getKey()),
79 providerAddressEndpointLocation);
80 LOG.debug("Creating location for {}", providerAddressEndpointLocation.getKey());
81 return Futures.transform(wTx.submit(), new Function<Void, Void>() {
84 public Void apply(Void input) {
85 LOG.debug("{} provided location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
86 providerAddressEndpointLocation);
92 public static ProviderAddressEndpointLocation createProviderAddressEndpointLocation(VppEndpoint vppEndpoint) {
93 String restIfacePath = VppPathMapper.interfaceToRestPath(vppEndpoint.getVppInterfaceName());
94 AbsoluteLocation absoluteLocation = new AbsoluteLocationBuilder()
95 .setLocationType(new ExternalLocationCaseBuilder().setExternalNodeMountPoint(vppEndpoint.getVppNodePath())
96 .setExternalNodeConnector(restIfacePath)
99 return new ProviderAddressEndpointLocationBuilder()
100 .setKey(createProviderAddressEndpointLocationKey(vppEndpoint))
101 .setAbsoluteLocation(absoluteLocation)
105 public ListenableFuture<Void> deleteLocationForVppEndpoint(VppEndpoint vppEndpoint) {
106 ProviderAddressEndpointLocationKey provAddrEpLocKey = createProviderAddressEndpointLocationKey(vppEndpoint);
107 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(), new Function<Void, Void>() {
114 public Void apply(Void input) {
115 LOG.debug("{} removed location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(), provAddrEpLocKey);
121 private static ProviderAddressEndpointLocationKey createProviderAddressEndpointLocationKey(
122 VppEndpoint vppEndpoint) {
123 return new ProviderAddressEndpointLocationKey(vppEndpoint.getAddress(), vppEndpoint.getAddressType(),
124 vppEndpoint.getContextId(), vppEndpoint.getContextType());
127 public ListenableFuture<Void> replaceLocationForEndpoint(@Nonnull ExternalLocationCase location, @Nonnull AddressEndpointWithLocationKey addrEpWithLocKey) {
128 ProviderAddressEndpointLocationKey provAddrEpLocKey =
129 KeyFactory.providerAddressEndpointLocationKey(addrEpWithLocKey);
130 AbsoluteLocation absoluteLocation =
131 new AbsoluteLocationBuilder().setLocationType(location).build();
132 ProviderAddressEndpointLocation providerAddressEndpointLocation = new ProviderAddressEndpointLocationBuilder()
133 .setKey(provAddrEpLocKey).setAbsoluteLocation(absoluteLocation).build();
134 WriteTransaction wTx = txChain.newWriteOnlyTransaction();
135 wTx.put(LogicalDatastoreType.CONFIGURATION,
136 IidFactory.providerAddressEndpointLocationIid(VPP_ENDPOINT_LOCATION_PROVIDER,
137 providerAddressEndpointLocation.getKey()),
138 providerAddressEndpointLocation);
139 LOG.debug("Updating location for {}", provAddrEpLocKey);
140 return Futures.transform(wTx.submit(), new Function<Void, Void>() {
143 public Void apply(Void input) {
144 LOG.debug("{} replaced location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
145 providerAddressEndpointLocation);
152 public void close() throws Exception {
153 WriteTransaction wTx = txChain.newWriteOnlyTransaction();
154 wTx.delete(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VPP_ENDPOINT_LOCATION_PROVIDER));