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;
12 import javax.annotation.Nullable;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.KeyFactory;
18 import org.opendaylight.groupbasedpolicy.util.IidFactory;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.AbsoluteLocation;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.AbsoluteLocationBuilder;
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.trace("{} 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);
77 Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
80 public void onSuccess(Void result) {
81 LOG.trace("{} provides 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 Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
114 public void onSuccess(Void result) {
115 LOG.trace("{} removes location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(), provAddrEpLocKey);
119 public void onFailure(Throwable t) {
120 LOG.error("{} failed to remove location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
121 provAddrEpLocKey, t);
126 private static ProviderAddressEndpointLocationKey createProviderAddressEndpointLocationKey(VppEndpoint vppEndpoint) {
127 return new ProviderAddressEndpointLocationKey(vppEndpoint.getAddress(), vppEndpoint.getAddressType(),
128 vppEndpoint.getContextId(), vppEndpoint.getContextType());
131 public void updateExternalNodeLocationForEndpoint(@Nullable String nodePath,
132 @Nonnull AddressEndpointWithLocationKey addrEpWithLocKey) {
133 ProviderAddressEndpointLocationKey provAddrEpLocKey =
134 KeyFactory.providerAddressEndpointLocationKey(addrEpWithLocKey);
135 AbsoluteLocation absoluteLocation = new AbsoluteLocationBuilder()
136 .setLocationType(new ExternalLocationCaseBuilder().setExternalNode(nodePath).build()).build();
137 ProviderAddressEndpointLocation providerAddressEndpointLocation = new ProviderAddressEndpointLocationBuilder()
138 .setKey(provAddrEpLocKey).setAbsoluteLocation(absoluteLocation).build();
139 WriteTransaction wTx = dataProvider.newWriteOnlyTransaction();
140 wTx.merge(LogicalDatastoreType.CONFIGURATION,
141 IidFactory.providerAddressEndpointLocationIid(VPP_ENDPOINT_LOCATION_PROVIDER,
142 providerAddressEndpointLocation.getKey()),
143 providerAddressEndpointLocation);
145 Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
148 public void onSuccess(Void result) {
149 LOG.trace("{} merges location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
150 providerAddressEndpointLocation);
154 public void onFailure(Throwable t) {
155 LOG.error("{} failed to merge location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
156 providerAddressEndpointLocation, t);
162 public void close() throws Exception {
163 WriteTransaction wTx = dataProvider.newWriteOnlyTransaction();
164 wTx.delete(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VPP_ENDPOINT_LOCATION_PROVIDER));