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.renderer.vpp.util.VppIidFactory;
22 import org.opendaylight.groupbasedpolicy.util.IidFactory;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.AbsoluteLocation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.AbsoluteLocationBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.ProviderName;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.LocationProvider;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.LocationProviderBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocation;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocationBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocationKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocationKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.VppEndpoint;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import com.google.common.base.Function;
41 import com.google.common.util.concurrent.FutureCallback;
42 import com.google.common.util.concurrent.Futures;
43 import com.google.common.util.concurrent.ListenableFuture;
45 public class VppEndpointLocationProvider implements AutoCloseable {
47 private static final Logger LOG = LoggerFactory.getLogger(VppEndpointLocationProvider.class);
48 public static final ProviderName VPP_ENDPOINT_LOCATION_PROVIDER =
49 new ProviderName("VPP endpoint location provider");
50 public static final long PROVIDER_PRIORITY = 10L;
51 private final BindingTransactionChain txChain;
53 public VppEndpointLocationProvider(DataBroker dataProvider) {
54 LocationProvider locationProvider = new LocationProviderBuilder().setProvider(VPP_ENDPOINT_LOCATION_PROVIDER)
55 .setPriority(PROVIDER_PRIORITY)
57 txChain = checkNotNull(dataProvider)
58 .createTransactionChain(new CloseOnFailTransactionChain(VppEndpointLocationProvider.class.getSimpleName()));
59 WriteTransaction wTx = txChain.newWriteOnlyTransaction();
60 wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VPP_ENDPOINT_LOCATION_PROVIDER),
61 locationProvider, true);
63 Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
66 public void onSuccess(Void result) {
67 LOG.debug("{} was created", VPP_ENDPOINT_LOCATION_PROVIDER.getValue());
71 public void onFailure(Throwable t) {
72 LOG.error("{} was NOT created", VPP_ENDPOINT_LOCATION_PROVIDER.getValue());
77 public ListenableFuture<Void> createLocationForVppEndpoint(VppEndpoint vppEndpoint) {
78 ProviderAddressEndpointLocation providerAddressEndpointLocation = createProviderAddressEndpointLocation(vppEndpoint);
79 WriteTransaction wTx = txChain.newWriteOnlyTransaction();
80 wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.providerAddressEndpointLocationIid(
81 VPP_ENDPOINT_LOCATION_PROVIDER, providerAddressEndpointLocation.getKey()),
82 providerAddressEndpointLocation);
83 LOG.debug("Creating location for {}", providerAddressEndpointLocation.getKey());
84 return Futures.transform(wTx.submit(), new Function<Void, Void>() {
87 public Void apply(Void input) {
88 LOG.debug("{} provided location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
89 providerAddressEndpointLocation);
95 public static ProviderAddressEndpointLocation createProviderAddressEndpointLocation(VppEndpoint vppEndpoint) {
96 InstanceIdentifier<Node> vppNodeIid = VppIidFactory.getNetconfNodeIid(vppEndpoint.getVppNodeId());
97 String restIfacePath = VppPathMapper.interfaceToRestPath(vppEndpoint.getVppInterfaceName());
98 AbsoluteLocation absoluteLocation = new AbsoluteLocationBuilder()
99 .setLocationType(new ExternalLocationCaseBuilder().setExternalNodeMountPoint(vppNodeIid)
100 .setExternalNodeConnector(restIfacePath)
103 return new ProviderAddressEndpointLocationBuilder()
104 .setKey(createProviderAddressEndpointLocationKey(vppEndpoint))
105 .setAbsoluteLocation(absoluteLocation)
109 public ListenableFuture<Void> deleteLocationForVppEndpoint(VppEndpoint vppEndpoint) {
110 ProviderAddressEndpointLocationKey provAddrEpLocKey = createProviderAddressEndpointLocationKey(vppEndpoint);
111 WriteTransaction wTx = txChain.newWriteOnlyTransaction();
112 wTx.delete(LogicalDatastoreType.CONFIGURATION,
113 IidFactory.providerAddressEndpointLocationIid(VPP_ENDPOINT_LOCATION_PROVIDER, provAddrEpLocKey));
114 LOG.debug("Deleting location for {}", provAddrEpLocKey);
115 return Futures.transform(wTx.submit(), new Function<Void, Void>() {
118 public Void apply(Void input) {
119 LOG.debug("{} removed location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(), provAddrEpLocKey);
125 private static ProviderAddressEndpointLocationKey createProviderAddressEndpointLocationKey(
126 VppEndpoint vppEndpoint) {
127 return new ProviderAddressEndpointLocationKey(vppEndpoint.getAddress(), vppEndpoint.getAddressType(),
128 vppEndpoint.getContextId(), vppEndpoint.getContextType());
131 public ListenableFuture<Void> replaceLocationForEndpoint(@Nonnull ExternalLocationCase location, @Nonnull AddressEndpointWithLocationKey addrEpWithLocKey) {
132 ProviderAddressEndpointLocationKey provAddrEpLocKey =
133 KeyFactory.providerAddressEndpointLocationKey(addrEpWithLocKey);
134 AbsoluteLocation absoluteLocation =
135 new AbsoluteLocationBuilder().setLocationType(location).build();
136 ProviderAddressEndpointLocation providerAddressEndpointLocation = new ProviderAddressEndpointLocationBuilder()
137 .setKey(provAddrEpLocKey).setAbsoluteLocation(absoluteLocation).build();
138 WriteTransaction wTx = txChain.newWriteOnlyTransaction();
139 wTx.put(LogicalDatastoreType.CONFIGURATION,
140 IidFactory.providerAddressEndpointLocationIid(VPP_ENDPOINT_LOCATION_PROVIDER,
141 providerAddressEndpointLocation.getKey()),
142 providerAddressEndpointLocation);
143 LOG.debug("Updating location for {}", provAddrEpLocKey);
144 return Futures.transform(wTx.submit(), new Function<Void, Void>() {
147 public Void apply(Void input) {
148 LOG.debug("{} replaced location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
149 providerAddressEndpointLocation);
156 public void close() throws Exception {
157 WriteTransaction wTx = txChain.newWriteOnlyTransaction();
158 wTx.delete(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VPP_ENDPOINT_LOCATION_PROVIDER));