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.util.concurrent.FutureCallback;
38 import com.google.common.util.concurrent.Futures;
40 public class VppEndpointLocationProvider implements AutoCloseable {
42 private static final Logger LOG = LoggerFactory.getLogger(VppEndpointLocationProvider.class);
43 public static final ProviderName VPP_ENDPOINT_LOCATION_PROVIDER =
44 new ProviderName("VPP endpoint location provider");
45 public static final long PROVIDER_PRIORITY = 10L;
46 private final BindingTransactionChain txChain;
48 public VppEndpointLocationProvider(DataBroker dataProvider) {
49 LocationProvider locationProvider = new LocationProviderBuilder().setProvider(VPP_ENDPOINT_LOCATION_PROVIDER)
50 .setPriority(PROVIDER_PRIORITY)
52 txChain = checkNotNull(dataProvider)
53 .createTransactionChain(new CloseOnFailTransactionChain(VppEndpointLocationProvider.class.getSimpleName()));
54 WriteTransaction wTx = txChain.newWriteOnlyTransaction();
55 wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VPP_ENDPOINT_LOCATION_PROVIDER),
56 locationProvider, true);
58 Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
61 public void onSuccess(Void result) {
62 LOG.debug("{} was created", VPP_ENDPOINT_LOCATION_PROVIDER.getValue());
66 public void onFailure(Throwable t) {
67 LOG.error("{} was NOT created", VPP_ENDPOINT_LOCATION_PROVIDER.getValue());
72 public void createLocationForVppEndpoint(VppEndpoint vppEndpoint) {
73 ProviderAddressEndpointLocation providerAddressEndpointLocation =
74 createProviderAddressEndpointLocation(vppEndpoint);
75 WriteTransaction wTx = txChain.newWriteOnlyTransaction();
76 wTx.put(LogicalDatastoreType.CONFIGURATION,
77 IidFactory.providerAddressEndpointLocationIid(VPP_ENDPOINT_LOCATION_PROVIDER,
78 providerAddressEndpointLocation.getKey()),
79 providerAddressEndpointLocation);
80 LOG.debug("Creating location for {}", providerAddressEndpointLocation.getKey());
81 Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
84 public void onSuccess(Void result) {
85 LOG.debug("{} provided location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
86 providerAddressEndpointLocation);
90 public void onFailure(Throwable t) {
91 LOG.error("{} failed to provide location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
92 providerAddressEndpointLocation, t);
97 public static ProviderAddressEndpointLocation createProviderAddressEndpointLocation(VppEndpoint vppEndpoint) {
98 String restIfacePath = VppPathMapper.interfaceToRestPath(vppEndpoint.getVppInterfaceName());
99 AbsoluteLocation absoluteLocation = new AbsoluteLocationBuilder()
100 .setLocationType(new ExternalLocationCaseBuilder().setExternalNodeMountPoint(vppEndpoint.getVppNodePath())
101 .setExternalNodeConnector(restIfacePath)
104 return new ProviderAddressEndpointLocationBuilder()
105 .setKey(createProviderAddressEndpointLocationKey(vppEndpoint))
106 .setAbsoluteLocation(absoluteLocation)
110 public void deleteLocationForVppEndpoint(VppEndpoint vppEndpoint) {
111 ProviderAddressEndpointLocationKey provAddrEpLocKey = createProviderAddressEndpointLocationKey(vppEndpoint);
112 WriteTransaction wTx = txChain.newWriteOnlyTransaction();
113 wTx.delete(LogicalDatastoreType.CONFIGURATION,
114 IidFactory.providerAddressEndpointLocationIid(VPP_ENDPOINT_LOCATION_PROVIDER, provAddrEpLocKey));
115 LOG.debug("Deleting location for {}", provAddrEpLocKey);
116 Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
119 public void onSuccess(Void result) {
120 LOG.debug("{} removed location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(), provAddrEpLocKey);
124 public void onFailure(Throwable t) {
125 LOG.error("{} failed to remove location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
126 provAddrEpLocKey, t);
131 private static ProviderAddressEndpointLocationKey createProviderAddressEndpointLocationKey(
132 VppEndpoint vppEndpoint) {
133 return new ProviderAddressEndpointLocationKey(vppEndpoint.getAddress(), vppEndpoint.getAddressType(),
134 vppEndpoint.getContextId(), vppEndpoint.getContextType());
137 public void replaceLocationForEndpoint(@Nonnull ExternalLocationCase location, @Nonnull AddressEndpointWithLocationKey addrEpWithLocKey) {
138 ProviderAddressEndpointLocationKey provAddrEpLocKey =
139 KeyFactory.providerAddressEndpointLocationKey(addrEpWithLocKey);
140 AbsoluteLocation absoluteLocation =
141 new AbsoluteLocationBuilder().setLocationType(location).build();
142 ProviderAddressEndpointLocation providerAddressEndpointLocation = new ProviderAddressEndpointLocationBuilder()
143 .setKey(provAddrEpLocKey).setAbsoluteLocation(absoluteLocation).build();
144 WriteTransaction wTx = txChain.newWriteOnlyTransaction();
145 wTx.put(LogicalDatastoreType.CONFIGURATION,
146 IidFactory.providerAddressEndpointLocationIid(VPP_ENDPOINT_LOCATION_PROVIDER,
147 providerAddressEndpointLocation.getKey()),
148 providerAddressEndpointLocation);
149 LOG.debug("Updating location for {}", provAddrEpLocKey);
150 Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
153 public void onSuccess(Void result) {
154 LOG.debug("{} replaced location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
155 providerAddressEndpointLocation);
159 public void onFailure(Throwable t) {
160 LOG.error("{} failed to replace location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
161 providerAddressEndpointLocation, t);
167 public void close() throws Exception {
168 WriteTransaction wTx = txChain.newWriteOnlyTransaction();
169 wTx.delete(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VPP_ENDPOINT_LOCATION_PROVIDER));