Implementing VBD API in Vpp renderer
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / iface / VppEndpointLocationProvider.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.iface;
10
11 import javax.annotation.Nonnull;
12 import javax.annotation.Nullable;
13
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;
33
34 import com.google.common.base.Preconditions;
35 import com.google.common.util.concurrent.FutureCallback;
36 import com.google.common.util.concurrent.Futures;
37
38 public class VppEndpointLocationProvider implements AutoCloseable {
39
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;
45
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)
50             .build();
51         WriteTransaction wTx = dataProvider.newWriteOnlyTransaction();
52         wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VPP_ENDPOINT_LOCATION_PROVIDER),
53                 locationProvider, true);
54
55         Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
56
57             @Override
58             public void onSuccess(Void result) {
59                 LOG.trace("{} was created", VPP_ENDPOINT_LOCATION_PROVIDER.getValue());
60             }
61
62             @Override
63             public void onFailure(Throwable t) {
64                 LOG.error("{} was NOT created", VPP_ENDPOINT_LOCATION_PROVIDER.getValue());
65             }
66         });
67     }
68
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);
77
78         Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
79
80             @Override
81             public void onSuccess(Void result) {
82                 LOG.trace("{} provides location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
83                         providerAddressEndpointLocation);
84             }
85
86             @Override
87             public void onFailure(Throwable t) {
88                 LOG.error("{} failed to provide location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
89                         providerAddressEndpointLocation, t);
90             }
91         });
92     }
93
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)
99                 .build())
100             .build();
101         return new ProviderAddressEndpointLocationBuilder()
102             .setKey(createProviderAddressEndpointLocationKey(vppEndpoint))
103             .setAbsoluteLocation(absoluteLocation)
104             .build();
105     }
106
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>() {
113
114             @Override
115             public void onSuccess(Void result) {
116                 LOG.trace("{} removes location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(), provAddrEpLocKey);
117             }
118
119             @Override
120             public void onFailure(Throwable t) {
121                 LOG.error("{} failed to remove location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
122                         provAddrEpLocKey, t);
123             }
124         });
125     }
126
127     private static ProviderAddressEndpointLocationKey createProviderAddressEndpointLocationKey(
128             VppEndpoint vppEndpoint) {
129         return new ProviderAddressEndpointLocationKey(vppEndpoint.getAddress(), vppEndpoint.getAddressType(),
130                 vppEndpoint.getContextId(), vppEndpoint.getContextType());
131     }
132
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);
147
148         Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
149
150             @Override
151             public void onSuccess(Void result) {
152                 LOG.trace("{} merges location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
153                         providerAddressEndpointLocation);
154             }
155
156             @Override
157             public void onFailure(Throwable t) {
158                 LOG.error("{} failed to merge location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
159                         providerAddressEndpointLocation, t);
160             }
161         });
162     }
163
164     @Override
165     public void close() throws Exception {
166         WriteTransaction wTx = dataProvider.newWriteOnlyTransaction();
167         wTx.delete(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VPP_ENDPOINT_LOCATION_PROVIDER));
168         wTx.submit();
169     }
170 }