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 org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.groupbasedpolicy.util.IidFactory;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.AbsoluteLocation;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.AbsoluteLocationBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCaseBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.ProviderName;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.LocationProvider;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.LocationProviderBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocation;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocationBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocationKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.VppEndpoint;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.google.common.base.Preconditions;
29 import com.google.common.util.concurrent.FutureCallback;
30 import com.google.common.util.concurrent.Futures;
32 public class VppEndpointLocationProvider implements AutoCloseable {
34 private static final Logger LOG = LoggerFactory.getLogger(VppEndpointLocationProvider.class);
35 public static final ProviderName VPP_ENDPOINT_LOCATION_PROVIDER =
36 new ProviderName("VPP endpoint location provider");
37 public static final long PROVIDER_PRIORITY = 10L;
38 private static final String INTERFACE_PATH = "/ietf-interfaces:interfaces/ietf-interfaces:interface";
39 private final DataBroker dataProvider;
41 public VppEndpointLocationProvider(DataBroker dataProvider) {
42 this.dataProvider = Preconditions.checkNotNull(dataProvider);
43 LocationProvider locationProvider = new LocationProviderBuilder().setProvider(VPP_ENDPOINT_LOCATION_PROVIDER)
44 .setPriority(PROVIDER_PRIORITY)
46 WriteTransaction wTx = dataProvider.newWriteOnlyTransaction();
47 wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VPP_ENDPOINT_LOCATION_PROVIDER),
48 locationProvider, true);
50 Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
53 public void onSuccess(Void result) {
54 LOG.trace("{} was created", VPP_ENDPOINT_LOCATION_PROVIDER.getValue());
58 public void onFailure(Throwable t) {
59 LOG.error("{} was NOT created", VPP_ENDPOINT_LOCATION_PROVIDER.getValue());
64 public void createLocationForVppEndpoint(VppEndpoint vppEndpoint) {
65 ProviderAddressEndpointLocation providerAddressEndpointLocation =
66 createProviderAddressEndpointLocation(vppEndpoint);
67 WriteTransaction wTx = dataProvider.newWriteOnlyTransaction();
68 wTx.put(LogicalDatastoreType.CONFIGURATION,
69 IidFactory.providerAddressEndpointLocationIid(VPP_ENDPOINT_LOCATION_PROVIDER,
70 providerAddressEndpointLocation.getKey()),
71 providerAddressEndpointLocation);
73 Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
76 public void onSuccess(Void result) {
77 LOG.trace("{} provides location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
78 providerAddressEndpointLocation);
82 public void onFailure(Throwable t) {
83 LOG.error("{} failed to provide location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
84 providerAddressEndpointLocation, t);
89 public static ProviderAddressEndpointLocation createProviderAddressEndpointLocation(VppEndpoint vppEndpoint) {
90 String restIfacePath = INTERFACE_PATH + createRestInterfaceKey(vppEndpoint.getVppInterfaceName());
91 AbsoluteLocation absoluteLocation = new AbsoluteLocationBuilder()
92 .setLocationType(new ExternalLocationCaseBuilder().setExternalNodeMountPoint(vppEndpoint.getVppNodePath())
93 .setExternalNodeConnector(restIfacePath)
96 return new ProviderAddressEndpointLocationBuilder()
97 .setKey(createProviderAddressEndpointLocationKey(vppEndpoint))
98 .setAbsoluteLocation(absoluteLocation)
102 public void deleteLocationForVppEndpoint(VppEndpoint vppEndpoint) {
103 ProviderAddressEndpointLocationKey provAddrEpLocKey = createProviderAddressEndpointLocationKey(vppEndpoint);
104 WriteTransaction wTx = dataProvider.newWriteOnlyTransaction();
105 wTx.delete(LogicalDatastoreType.CONFIGURATION,
106 IidFactory.providerAddressEndpointLocationIid(VPP_ENDPOINT_LOCATION_PROVIDER, provAddrEpLocKey));
107 Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
110 public void onSuccess(Void result) {
111 LOG.trace("{} removes location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(), provAddrEpLocKey);
115 public void onFailure(Throwable t) {
116 LOG.error("{} failed to remove location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
117 provAddrEpLocKey, t);
122 private static ProviderAddressEndpointLocationKey createProviderAddressEndpointLocationKey(VppEndpoint vppEndpoint) {
123 return new ProviderAddressEndpointLocationKey(vppEndpoint.getAddress(), vppEndpoint.getAddressType(),
124 vppEndpoint.getContextId(), vppEndpoint.getContextType());
127 private static String createRestInterfaceKey(String ifaceName) {
128 return "[ietf-interfaces:name='" + ifaceName + "']";
132 public void close() throws Exception {
133 WriteTransaction wTx = dataProvider.newWriteOnlyTransaction();
134 wTx.delete(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VPP_ENDPOINT_LOCATION_PROVIDER));