Merge "Endpoints"
[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.debug("{} 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         LOG.debug("Creating location for {}", providerAddressEndpointLocation.getKey());
78         Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
79
80             @Override
81             public void onSuccess(Void result) {
82                 LOG.debug("{} provided 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         LOG.debug("Deleting location for {}", provAddrEpLocKey);
113         Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
114
115             @Override
116             public void onSuccess(Void result) {
117                 LOG.debug("{} removed location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(), provAddrEpLocKey);
118             }
119
120             @Override
121             public void onFailure(Throwable t) {
122                 LOG.error("{} failed to remove location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
123                         provAddrEpLocKey, t);
124             }
125         });
126     }
127
128     private static ProviderAddressEndpointLocationKey createProviderAddressEndpointLocationKey(
129             VppEndpoint vppEndpoint) {
130         return new ProviderAddressEndpointLocationKey(vppEndpoint.getAddress(), vppEndpoint.getAddressType(),
131                 vppEndpoint.getContextId(), vppEndpoint.getContextType());
132     }
133
134     public void updateExternalNodeLocationForEndpoint(@Nullable String nodePath,
135             InstanceIdentifier<?> externalNodeMountPoint, @Nonnull AddressEndpointWithLocationKey addrEpWithLocKey) {
136         ProviderAddressEndpointLocationKey provAddrEpLocKey =
137                 KeyFactory.providerAddressEndpointLocationKey(addrEpWithLocKey);
138         AbsoluteLocation absoluteLocation =
139                 new AbsoluteLocationBuilder().setLocationType(new ExternalLocationCaseBuilder()
140                     .setExternalNodeMountPoint(externalNodeMountPoint).setExternalNode(nodePath).build()).build();
141         ProviderAddressEndpointLocation providerAddressEndpointLocation = new ProviderAddressEndpointLocationBuilder()
142             .setKey(provAddrEpLocKey).setAbsoluteLocation(absoluteLocation).build();
143         WriteTransaction wTx = dataProvider.newWriteOnlyTransaction();
144         wTx.merge(LogicalDatastoreType.CONFIGURATION,
145                 IidFactory.providerAddressEndpointLocationIid(VPP_ENDPOINT_LOCATION_PROVIDER,
146                         providerAddressEndpointLocation.getKey()),
147                 providerAddressEndpointLocation);
148         LOG.debug("Updating location for {}", provAddrEpLocKey);
149         Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
150
151             @Override
152             public void onSuccess(Void result) {
153                 LOG.debug("{} merged location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
154                         providerAddressEndpointLocation);
155             }
156
157             @Override
158             public void onFailure(Throwable t) {
159                 LOG.error("{} failed to merge location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
160                         providerAddressEndpointLocation, t);
161             }
162         });
163     }
164
165     @Override
166     public void close() throws Exception {
167         WriteTransaction wTx = dataProvider.newWriteOnlyTransaction();
168         wTx.delete(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VPP_ENDPOINT_LOCATION_PROVIDER));
169         wTx.submit();
170     }
171 }