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 javax.annotation.Nonnull;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.KeyFactory;
17 import org.opendaylight.groupbasedpolicy.util.IidFactory;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.AbsoluteLocation;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.AbsoluteLocationBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.ProviderName;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.LocationProvider;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.LocationProviderBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocationBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocationKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocationKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.VppEndpoint;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import com.google.common.base.Preconditions;
34 import com.google.common.util.concurrent.FutureCallback;
35 import com.google.common.util.concurrent.Futures;
37 public class VppEndpointLocationProvider implements AutoCloseable {
39 private static final Logger LOG = LoggerFactory.getLogger(VppEndpointLocationProvider.class);
40 public static final ProviderName VPP_ENDPOINT_LOCATION_PROVIDER =
41 new ProviderName("VPP endpoint location provider");
42 public static final long PROVIDER_PRIORITY = 10L;
43 private final DataBroker dataProvider;
45 public VppEndpointLocationProvider(DataBroker dataProvider) {
46 this.dataProvider = Preconditions.checkNotNull(dataProvider);
47 LocationProvider locationProvider = new LocationProviderBuilder().setProvider(VPP_ENDPOINT_LOCATION_PROVIDER)
48 .setPriority(PROVIDER_PRIORITY)
50 WriteTransaction wTx = dataProvider.newWriteOnlyTransaction();
51 wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VPP_ENDPOINT_LOCATION_PROVIDER),
52 locationProvider, true);
54 Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
57 public void onSuccess(Void result) {
58 LOG.debug("{} was created", VPP_ENDPOINT_LOCATION_PROVIDER.getValue());
62 public void onFailure(Throwable t) {
63 LOG.error("{} was NOT created", VPP_ENDPOINT_LOCATION_PROVIDER.getValue());
68 public void createLocationForVppEndpoint(VppEndpoint vppEndpoint) {
69 ProviderAddressEndpointLocation providerAddressEndpointLocation =
70 createProviderAddressEndpointLocation(vppEndpoint);
71 WriteTransaction wTx = dataProvider.newWriteOnlyTransaction();
72 wTx.put(LogicalDatastoreType.CONFIGURATION,
73 IidFactory.providerAddressEndpointLocationIid(VPP_ENDPOINT_LOCATION_PROVIDER,
74 providerAddressEndpointLocation.getKey()),
75 providerAddressEndpointLocation);
76 LOG.debug("Creating location for {}", providerAddressEndpointLocation.getKey());
77 Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
80 public void onSuccess(Void result) {
81 LOG.debug("{} provided location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
82 providerAddressEndpointLocation);
86 public void onFailure(Throwable t) {
87 LOG.error("{} failed to provide location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
88 providerAddressEndpointLocation, t);
93 public static ProviderAddressEndpointLocation createProviderAddressEndpointLocation(VppEndpoint vppEndpoint) {
94 String restIfacePath = VppPathMapper.interfaceToRestPath(vppEndpoint.getVppInterfaceName());
95 AbsoluteLocation absoluteLocation = new AbsoluteLocationBuilder()
96 .setLocationType(new ExternalLocationCaseBuilder().setExternalNodeMountPoint(vppEndpoint.getVppNodePath())
97 .setExternalNodeConnector(restIfacePath)
100 return new ProviderAddressEndpointLocationBuilder()
101 .setKey(createProviderAddressEndpointLocationKey(vppEndpoint))
102 .setAbsoluteLocation(absoluteLocation)
106 public void deleteLocationForVppEndpoint(VppEndpoint vppEndpoint) {
107 ProviderAddressEndpointLocationKey provAddrEpLocKey = createProviderAddressEndpointLocationKey(vppEndpoint);
108 WriteTransaction wTx = dataProvider.newWriteOnlyTransaction();
109 wTx.delete(LogicalDatastoreType.CONFIGURATION,
110 IidFactory.providerAddressEndpointLocationIid(VPP_ENDPOINT_LOCATION_PROVIDER, provAddrEpLocKey));
111 LOG.debug("Deleting location for {}", provAddrEpLocKey);
112 Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
115 public void onSuccess(Void result) {
116 LOG.debug("{} removed location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(), provAddrEpLocKey);
120 public void onFailure(Throwable t) {
121 LOG.error("{} failed to remove location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
122 provAddrEpLocKey, t);
127 private static ProviderAddressEndpointLocationKey createProviderAddressEndpointLocationKey(
128 VppEndpoint vppEndpoint) {
129 return new ProviderAddressEndpointLocationKey(vppEndpoint.getAddress(), vppEndpoint.getAddressType(),
130 vppEndpoint.getContextId(), vppEndpoint.getContextType());
133 public void replaceLocationForEndpoint(@Nonnull ExternalLocationCase location, @Nonnull AddressEndpointWithLocationKey addrEpWithLocKey) {
134 ProviderAddressEndpointLocationKey provAddrEpLocKey =
135 KeyFactory.providerAddressEndpointLocationKey(addrEpWithLocKey);
136 AbsoluteLocation absoluteLocation =
137 new AbsoluteLocationBuilder().setLocationType(location).build();
138 ProviderAddressEndpointLocation providerAddressEndpointLocation = new ProviderAddressEndpointLocationBuilder()
139 .setKey(provAddrEpLocKey).setAbsoluteLocation(absoluteLocation).build();
140 WriteTransaction wTx = dataProvider.newWriteOnlyTransaction();
141 wTx.put(LogicalDatastoreType.CONFIGURATION,
142 IidFactory.providerAddressEndpointLocationIid(VPP_ENDPOINT_LOCATION_PROVIDER,
143 providerAddressEndpointLocation.getKey()),
144 providerAddressEndpointLocation);
145 LOG.debug("Updating location for {}", provAddrEpLocKey);
146 Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
149 public void onSuccess(Void result) {
150 LOG.debug("{} replaced location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
151 providerAddressEndpointLocation);
155 public void onFailure(Throwable t) {
156 LOG.error("{} failed to replace location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
157 providerAddressEndpointLocation, t);
163 public void close() throws Exception {
164 WriteTransaction wTx = dataProvider.newWriteOnlyTransaction();
165 wTx.delete(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VPP_ENDPOINT_LOCATION_PROVIDER));