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;
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;
43 public class VppEndpointLocationProvider implements AutoCloseable {
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;
51 public VppEndpointLocationProvider(DataBroker dataProvider) {
52 LocationProvider locationProvider = new LocationProviderBuilder().setProvider(VPP_ENDPOINT_LOCATION_PROVIDER)
53 .setPriority(PROVIDER_PRIORITY)
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);
61 Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
64 public void onSuccess(Void result) {
65 LOG.debug("{} was created", VPP_ENDPOINT_LOCATION_PROVIDER.getValue());
69 public void onFailure(Throwable t) {
70 LOG.error("{} was NOT created", VPP_ENDPOINT_LOCATION_PROVIDER.getValue());
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);
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)
98 return new ProviderAddressEndpointLocationBuilder()
99 .setKey(createProviderAddressEndpointLocationKey(vppEndpoint))
100 .setAbsoluteLocation(absoluteLocation)
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);
117 private static ProviderAddressEndpointLocationKey createProviderAddressEndpointLocationKey(
118 VppEndpoint vppEndpoint) {
119 return new ProviderAddressEndpointLocationKey(vppEndpoint.getAddress(), vppEndpoint.getAddressType(),
120 vppEndpoint.getContextId(), vppEndpoint.getContextType());
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>() {
139 public Void apply(Void input) {
140 LOG.debug("{} replaced location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
141 providerAddressEndpointLocation);
148 public void close() throws Exception {
149 WriteTransaction wTx = txChain.newWriteOnlyTransaction();
150 wTx.delete(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VPP_ENDPOINT_LOCATION_PROVIDER));