Merge "SFC topology"
[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 static com.google.common.base.Preconditions.checkNotNull;
12
13 import javax.annotation.Nonnull;
14
15 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.CloseOnFailTransactionChain;
20 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.KeyFactory;
21 import org.opendaylight.groupbasedpolicy.util.IidFactory;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.AbsoluteLocation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.AbsoluteLocationBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.ProviderName;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.LocationProvider;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.LocationProviderBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocation;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocationBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocationKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocationKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.VppEndpoint;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import com.google.common.base.Function;
38 import com.google.common.util.concurrent.FutureCallback;
39 import com.google.common.util.concurrent.Futures;
40 import com.google.common.util.concurrent.ListenableFuture;
41
42 public class VppEndpointLocationProvider implements AutoCloseable {
43
44     private static final Logger LOG = LoggerFactory.getLogger(VppEndpointLocationProvider.class);
45     public static final ProviderName VPP_ENDPOINT_LOCATION_PROVIDER =
46             new ProviderName("VPP endpoint location provider");
47     public static final long PROVIDER_PRIORITY = 10L;
48     private final BindingTransactionChain txChain;
49
50     public VppEndpointLocationProvider(DataBroker dataProvider) {
51         LocationProvider locationProvider = new LocationProviderBuilder().setProvider(VPP_ENDPOINT_LOCATION_PROVIDER)
52             .setPriority(PROVIDER_PRIORITY)
53             .build();
54         txChain = checkNotNull(dataProvider)
55             .createTransactionChain(new CloseOnFailTransactionChain(VppEndpointLocationProvider.class.getSimpleName()));
56         WriteTransaction wTx = txChain.newWriteOnlyTransaction();
57         wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VPP_ENDPOINT_LOCATION_PROVIDER),
58                 locationProvider, true);
59
60         Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
61
62             @Override
63             public void onSuccess(Void result) {
64                 LOG.debug("{} was created", VPP_ENDPOINT_LOCATION_PROVIDER.getValue());
65             }
66
67             @Override
68             public void onFailure(Throwable t) {
69                 LOG.error("{} was NOT created", VPP_ENDPOINT_LOCATION_PROVIDER.getValue());
70             }
71         });
72     }
73
74     public ListenableFuture<Void> createLocationForVppEndpoint(VppEndpoint vppEndpoint) {
75         ProviderAddressEndpointLocation providerAddressEndpointLocation = createProviderAddressEndpointLocation(vppEndpoint);
76         WriteTransaction wTx = txChain.newWriteOnlyTransaction();
77         wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.providerAddressEndpointLocationIid(
78                 VPP_ENDPOINT_LOCATION_PROVIDER, providerAddressEndpointLocation.getKey()),
79                 providerAddressEndpointLocation);
80         LOG.debug("Creating location for {}", providerAddressEndpointLocation.getKey());
81         return Futures.transform(wTx.submit(), new Function<Void, Void>() {
82
83             @Override
84             public Void apply(Void input) {
85                 LOG.debug("{} provided location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
86                         providerAddressEndpointLocation);
87                 return null;
88             }
89         });
90     }
91
92     public static ProviderAddressEndpointLocation createProviderAddressEndpointLocation(VppEndpoint vppEndpoint) {
93         String restIfacePath = VppPathMapper.interfaceToRestPath(vppEndpoint.getVppInterfaceName());
94         AbsoluteLocation absoluteLocation = new AbsoluteLocationBuilder()
95             .setLocationType(new ExternalLocationCaseBuilder().setExternalNodeMountPoint(vppEndpoint.getVppNodePath())
96                 .setExternalNodeConnector(restIfacePath)
97                 .build())
98             .build();
99         return new ProviderAddressEndpointLocationBuilder()
100             .setKey(createProviderAddressEndpointLocationKey(vppEndpoint))
101             .setAbsoluteLocation(absoluteLocation)
102             .build();
103     }
104
105     public ListenableFuture<Void> deleteLocationForVppEndpoint(VppEndpoint vppEndpoint) {
106         ProviderAddressEndpointLocationKey provAddrEpLocKey = createProviderAddressEndpointLocationKey(vppEndpoint);
107         WriteTransaction wTx = txChain.newWriteOnlyTransaction();
108         wTx.delete(LogicalDatastoreType.CONFIGURATION,
109                 IidFactory.providerAddressEndpointLocationIid(VPP_ENDPOINT_LOCATION_PROVIDER, provAddrEpLocKey));
110         LOG.debug("Deleting location for {}", provAddrEpLocKey);
111         return Futures.transform(wTx.submit(), new Function<Void, Void>() {
112
113             @Override
114             public Void apply(Void input) {
115                 LOG.debug("{} removed location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(), provAddrEpLocKey);
116                 return null;
117             }
118         });
119     }
120
121     private static ProviderAddressEndpointLocationKey createProviderAddressEndpointLocationKey(
122             VppEndpoint vppEndpoint) {
123         return new ProviderAddressEndpointLocationKey(vppEndpoint.getAddress(), vppEndpoint.getAddressType(),
124                 vppEndpoint.getContextId(), vppEndpoint.getContextType());
125     }
126
127     public ListenableFuture<Void> replaceLocationForEndpoint(@Nonnull ExternalLocationCase location, @Nonnull AddressEndpointWithLocationKey addrEpWithLocKey) {
128         ProviderAddressEndpointLocationKey provAddrEpLocKey =
129                 KeyFactory.providerAddressEndpointLocationKey(addrEpWithLocKey);
130         AbsoluteLocation absoluteLocation =
131                 new AbsoluteLocationBuilder().setLocationType(location).build();
132         ProviderAddressEndpointLocation providerAddressEndpointLocation = new ProviderAddressEndpointLocationBuilder()
133             .setKey(provAddrEpLocKey).setAbsoluteLocation(absoluteLocation).build();
134         WriteTransaction wTx = txChain.newWriteOnlyTransaction();
135         wTx.put(LogicalDatastoreType.CONFIGURATION,
136                 IidFactory.providerAddressEndpointLocationIid(VPP_ENDPOINT_LOCATION_PROVIDER,
137                         providerAddressEndpointLocation.getKey()),
138                 providerAddressEndpointLocation);
139         LOG.debug("Updating location for {}", provAddrEpLocKey);
140         return Futures.transform(wTx.submit(), new Function<Void, Void>() {
141
142             @Override
143             public Void apply(Void input) {
144                 LOG.debug("{} replaced location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
145                         providerAddressEndpointLocation);
146                 return null;
147             }
148         });
149     }
150
151     @Override
152     public void close() throws Exception {
153         WriteTransaction wTx = txChain.newWriteOnlyTransaction();
154         wTx.delete(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VPP_ENDPOINT_LOCATION_PROVIDER));
155         wTx.submit();
156     }
157 }