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.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import com.google.common.base.Preconditions;
35 import com.google.common.util.concurrent.FutureCallback;
36 import com.google.common.util.concurrent.Futures;
38 public class VppEndpointLocationProvider implements AutoCloseable {
40 private static final Logger LOG = LoggerFactory.getLogger(VppEndpointLocationProvider.class);
41 public static final ProviderName VPP_ENDPOINT_LOCATION_PROVIDER =
42 new ProviderName("VPP endpoint location provider");
43 public static final long PROVIDER_PRIORITY = 10L;
44 private final DataBroker dataProvider;
46 public VppEndpointLocationProvider(DataBroker dataProvider) {
47 this.dataProvider = Preconditions.checkNotNull(dataProvider);
48 LocationProvider locationProvider = new LocationProviderBuilder().setProvider(VPP_ENDPOINT_LOCATION_PROVIDER)
49 .setPriority(PROVIDER_PRIORITY)
51 WriteTransaction wTx = dataProvider.newWriteOnlyTransaction();
52 wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VPP_ENDPOINT_LOCATION_PROVIDER),
53 locationProvider, true);
55 Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
58 public void onSuccess(Void result) {
59 LOG.debug("{} was created", VPP_ENDPOINT_LOCATION_PROVIDER.getValue());
63 public void onFailure(Throwable t) {
64 LOG.error("{} was NOT created", VPP_ENDPOINT_LOCATION_PROVIDER.getValue());
69 public void createLocationForVppEndpoint(VppEndpoint vppEndpoint) {
70 ProviderAddressEndpointLocation providerAddressEndpointLocation =
71 createProviderAddressEndpointLocation(vppEndpoint);
72 WriteTransaction wTx = dataProvider.newWriteOnlyTransaction();
73 wTx.put(LogicalDatastoreType.CONFIGURATION,
74 IidFactory.providerAddressEndpointLocationIid(VPP_ENDPOINT_LOCATION_PROVIDER,
75 providerAddressEndpointLocation.getKey()),
76 providerAddressEndpointLocation);
78 Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
81 public void onSuccess(Void result) {
82 LOG.debug("{} provides location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
83 providerAddressEndpointLocation);
87 public void onFailure(Throwable t) {
88 LOG.error("{} failed to provide location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
89 providerAddressEndpointLocation, t);
94 public static ProviderAddressEndpointLocation createProviderAddressEndpointLocation(VppEndpoint vppEndpoint) {
95 String restIfacePath = VppPathMapper.interfaceToRestPath(vppEndpoint.getVppInterfaceName());
96 AbsoluteLocation absoluteLocation = new AbsoluteLocationBuilder()
97 .setLocationType(new ExternalLocationCaseBuilder().setExternalNodeMountPoint(vppEndpoint.getVppNodePath())
98 .setExternalNodeConnector(restIfacePath)
101 return new ProviderAddressEndpointLocationBuilder()
102 .setKey(createProviderAddressEndpointLocationKey(vppEndpoint))
103 .setAbsoluteLocation(absoluteLocation)
107 public void deleteLocationForVppEndpoint(VppEndpoint vppEndpoint) {
108 ProviderAddressEndpointLocationKey provAddrEpLocKey = createProviderAddressEndpointLocationKey(vppEndpoint);
109 WriteTransaction wTx = dataProvider.newWriteOnlyTransaction();
110 wTx.delete(LogicalDatastoreType.CONFIGURATION,
111 IidFactory.providerAddressEndpointLocationIid(VPP_ENDPOINT_LOCATION_PROVIDER, provAddrEpLocKey));
112 Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
115 public void onSuccess(Void result) {
116 LOG.debug("{} removes 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 updateExternalNodeLocationForEndpoint(@Nullable String nodePath,
134 InstanceIdentifier<?> externalNodeMountPoint, @Nonnull AddressEndpointWithLocationKey addrEpWithLocKey) {
135 ProviderAddressEndpointLocationKey provAddrEpLocKey =
136 KeyFactory.providerAddressEndpointLocationKey(addrEpWithLocKey);
137 AbsoluteLocation absoluteLocation =
138 new AbsoluteLocationBuilder().setLocationType(new ExternalLocationCaseBuilder()
139 .setExternalNodeMountPoint(externalNodeMountPoint).setExternalNode(nodePath).build()).build();
140 ProviderAddressEndpointLocation providerAddressEndpointLocation = new ProviderAddressEndpointLocationBuilder()
141 .setKey(provAddrEpLocKey).setAbsoluteLocation(absoluteLocation).build();
142 WriteTransaction wTx = dataProvider.newWriteOnlyTransaction();
143 wTx.merge(LogicalDatastoreType.CONFIGURATION,
144 IidFactory.providerAddressEndpointLocationIid(VPP_ENDPOINT_LOCATION_PROVIDER,
145 providerAddressEndpointLocation.getKey()),
146 providerAddressEndpointLocation);
148 Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
151 public void onSuccess(Void result) {
152 LOG.debug("{} merges location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
153 providerAddressEndpointLocation);
157 public void onFailure(Throwable t) {
158 LOG.error("{} failed to merge location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
159 providerAddressEndpointLocation, t);
165 public void close() throws Exception {
166 WriteTransaction wTx = dataProvider.newWriteOnlyTransaction();
167 wTx.delete(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VPP_ENDPOINT_LOCATION_PROVIDER));