3ea3f9743afc0bb7c611dde5432078d0ef6edcb0
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.v3po.translate.v3po.interfaces;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkNotNull;
21
22 import com.google.common.base.Optional;
23 import io.fd.honeycomb.v3po.translate.Context;
24 import io.fd.honeycomb.v3po.translate.spi.write.ChildWriterCustomizer;
25 import io.fd.honeycomb.v3po.translate.v3po.util.VppApiCustomizer;
26 import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException;
27 import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils;
28 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
29 import javax.annotation.Nonnull;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.EthernetCsmacd;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfaceType;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VxlanTunnel;
35 import org.opendaylight.yangtools.yang.binding.DataObject;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public class VppInterfaceCustomizer extends VppApiCustomizer
41     implements ChildWriterCustomizer<VppInterfaceAugmentation> {
42
43     private static final Logger LOG = LoggerFactory.getLogger(VppInterfaceCustomizer.class);
44
45     public VppInterfaceCustomizer(final org.openvpp.vppjapi.vppApi vppApi) {
46         super(vppApi);
47     }
48
49     @Nonnull
50     @Override
51     public Optional<VppInterfaceAugmentation> extract(
52         @Nonnull final InstanceIdentifier<VppInterfaceAugmentation> currentId,
53         @Nonnull final DataObject parentData) {
54         return Optional.fromNullable(((Interface) parentData).getAugmentation(VppInterfaceAugmentation.class));
55     }
56
57     @Override
58     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<VppInterfaceAugmentation> id,
59                                        @Nonnull final VppInterfaceAugmentation dataAfter,
60                                        @Nonnull final Context writeContext)
61         throws WriteFailedException {
62         final Interface ifc = (Interface) writeContext.get(InterfaceCustomizer.IFC_AFTER_CTX);
63         try {
64             setInterface(id, ifc, dataAfter);
65         } catch (VppApiInvocationException e) {
66             LOG.warn("Update of VppInterfaceAugment failed", e);
67             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
68         }
69     }
70
71     @Override
72     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<VppInterfaceAugmentation> id,
73                                         @Nonnull final VppInterfaceAugmentation dataBefore,
74                                         @Nonnull final VppInterfaceAugmentation dataAfter,
75                                         @Nonnull final Context writeContext)
76         throws WriteFailedException.UpdateFailedException {
77         final Interface ifcBefore = (Interface) writeContext.get(InterfaceCustomizer.IFC_BEFORE_CTX);
78         final Interface ifc = (Interface) writeContext.get(InterfaceCustomizer.IFC_AFTER_CTX);
79         try {
80             updateInterface(id, ifc, dataBefore, dataAfter);
81         } catch (VppApiInvocationException e) {
82             LOG.warn("Update of VppInterfaceAugment failed", e);
83             throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter, e);
84         }
85     }
86
87     @Override
88     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<VppInterfaceAugmentation> id,
89                                         @Nonnull final VppInterfaceAugmentation dataBefore,
90                                         @Nonnull final Context writeContext) {
91         final Interface ifcBefore = (Interface) writeContext.get(InterfaceCustomizer.IFC_BEFORE_CTX);
92
93         LOG.info("Deleting interface: {}, type: {}", ifcBefore.getName(), ifcBefore.getType().getSimpleName());
94
95         if (ifcBefore.getType().isAssignableFrom(EthernetCsmacd.class)) {
96             LOG.error("Interface {}, type: {} cannot be deleted",
97                 ifcBefore.getName(), ifcBefore.getType().getSimpleName());
98
99         /* FIXME: Add additional interface types here.
100          *
101          * } else if (swIf.getType().isAssignableFrom(*.class)) {
102          */
103
104         }
105     }
106
107
108     private void updateInterface(final InstanceIdentifier<VppInterfaceAugmentation> id, final Interface swIf,
109                                  final VppInterfaceAugmentation dataBefore,
110                                  final VppInterfaceAugmentation dataAfter) throws VppApiInvocationException {
111         LOG.info("Updating interface {}, type: {}", swIf.getName(), swIf.getType().getSimpleName());
112         LOG.debug("Updating interface {}", swIf);
113
114         Class<? extends InterfaceType> ifType = checkNotNull(swIf.getType(), "Interface type missing for %s", swIf);
115         String swIfName = swIf.getName();
116         int swIfIndex = getVppApi().swIfIndexFromName(swIfName);
117         checkArgument(swIfIndex != -1, "Updating non-existing vpp interface: %s", swIfName);
118
119         // TODO handle updates properly
120
121         if (VxlanTunnel.class.isAssignableFrom(ifType)) {
122             updateVxlanTunnelInterface(swIf);
123         } else if (EthernetCsmacd.class.isAssignableFrom(ifType)) {
124             updateEthernetCsmacdInterface(swIf, swIfName, swIfIndex);
125         }
126     }
127
128
129     private void setInterface(final InstanceIdentifier<VppInterfaceAugmentation> id, final Interface swIf,
130                               final VppInterfaceAugmentation dataAfter)
131         throws VppApiInvocationException, WriteFailedException {
132         LOG.info("Setting interface {}, type: {}", swIf.getName(), swIf.getType().getSimpleName());
133         LOG.debug("Setting interface {}", swIf);
134
135         Class<? extends InterfaceType> ifType = checkNotNull(swIf.getType(), "Interface type missing for %s", swIf);
136         String swIfName = swIf.getName();
137         int swIfIndex = getVppApi().swIfIndexFromName(swIfName);
138         checkArgument(swIfIndex == -1, "Creating already-existing vpp interface: %s", swIfName);
139
140         if (VxlanTunnel.class.isAssignableFrom(ifType)) {
141             createVxlanTunnelInterface(swIf, swIfName);
142         } else if (EthernetCsmacd.class.isAssignableFrom(ifType)) {
143             createEthernetCsmacdInterface(id, swIfName, dataAfter);
144         }
145     }
146
147     private void createVxlanTunnelInterface(final Interface swIf, final String swIfName)
148         throws VppApiInvocationException {
149         LOG.debug("Creating {} interface {}", swIf.getType().getSimpleName(), swIf.getName());
150
151         // FIXME, Vxlan child writer needs to be handled before this is
152         int newSwIfIndex = getVppApi().swIfIndexFromName(swIfName);
153
154         setInterfaceFlags(swIfName, newSwIfIndex, swIf.isEnabled()
155             ? (byte) 1
156             : (byte) 0);
157         setDescription(swIf);
158     }
159
160     private void updateVxlanTunnelInterface(final Interface swIf) {
161         LOG.debug("Updating {} interface {}", swIf.getType().getSimpleName(), swIf.getName());
162
163         // TODO handle
164     }
165
166     private void createEthernetCsmacdInterface(final InstanceIdentifier<VppInterfaceAugmentation> id,
167                                                final String swIfName, final VppInterfaceAugmentation dataAfter) throws WriteFailedException {
168         LOG.warn("Unable to create interface: {}, type: {}", swIfName, EthernetCsmacd.class);
169         throw new WriteFailedException.CreateFailedException(id, dataAfter);
170     }
171
172     private void updateEthernetCsmacdInterface(final Interface swIf,
173                                                final String swIfName, final int swIfIndex)
174         throws VppApiInvocationException {
175         LOG.debug("Updating {} interface {}", swIf.getType().getSimpleName(), swIf.getName());
176         byte enabled = swIf.isEnabled()
177             ? (byte) 1
178             : (byte) 0;
179         setInterfaceFlags(swIfName, swIfIndex, enabled);
180         setDescription(swIf);
181     }
182
183     private void setInterfaceFlags(final String swIfName, final int swIfIndex, final byte enabled)
184         throws VppApiInvocationException {
185         int ctxId = getVppApi().swInterfaceSetFlags(swIfIndex, enabled, enabled, (byte) 0 /* deleted */);
186
187         LOG.debug("Updating interface flags for: {}, index: {}, enabled: {}, ctxId: {}", swIfName, swIfIndex,
188             enabled, ctxId);
189
190         final int rv = V3poUtils.waitForResponse(ctxId, getVppApi());
191         if (rv < 0) {
192             LOG.warn("Failed to update interface flags for: {}, index: {}, enabled: {}, ctxId: {}", swIfName, swIfIndex,
193                 enabled, ctxId);
194             throw new VppApiInvocationException("swInterfaceSetFlags", ctxId, rv);
195         } else {
196             LOG.debug("Interface flags updated successfully for: {}, index: {}, enabled: {}, ctxId: {}",
197                 swIfName, swIfIndex, enabled, ctxId);
198         }
199     }
200
201     private void setDescription(final Interface swIf) {
202         if (swIf.getDescription() != null) {
203             getVppApi().setInterfaceDescription(swIf.getName(), swIf.getDescription());
204         } else {
205             getVppApi().setInterfaceDescription(swIf.getName(), "");
206         }
207     }
208
209 }
210