2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.v3po.translate.v3po.interfaces;
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkNotNull;
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;
40 public class VppInterfaceCustomizer extends VppApiCustomizer
41 implements ChildWriterCustomizer<VppInterfaceAugmentation> {
43 private static final Logger LOG = LoggerFactory.getLogger(VppInterfaceCustomizer.class);
45 public VppInterfaceCustomizer(final org.openvpp.vppjapi.vppApi vppApi) {
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));
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);
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);
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);
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);
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);
93 LOG.info("Deleting interface: {}, type: {}", ifcBefore.getName(), ifcBefore.getType().getSimpleName());
95 if (ifcBefore.getType().isAssignableFrom(EthernetCsmacd.class)) {
96 LOG.error("Interface {}, type: {} cannot be deleted",
97 ifcBefore.getName(), ifcBefore.getType().getSimpleName());
99 /* FIXME: Add additional interface types here.
101 * } else if (swIf.getType().isAssignableFrom(*.class)) {
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);
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);
119 // TODO handle updates properly
121 if (VxlanTunnel.class.isAssignableFrom(ifType)) {
122 updateVxlanTunnelInterface(swIf);
123 } else if (EthernetCsmacd.class.isAssignableFrom(ifType)) {
124 updateEthernetCsmacdInterface(swIf, swIfName, swIfIndex);
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);
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);
140 if (VxlanTunnel.class.isAssignableFrom(ifType)) {
141 createVxlanTunnelInterface(swIf, swIfName);
142 } else if (EthernetCsmacd.class.isAssignableFrom(ifType)) {
143 createEthernetCsmacdInterface(id, swIfName, dataAfter);
147 private void createVxlanTunnelInterface(final Interface swIf, final String swIfName)
148 throws VppApiInvocationException {
149 LOG.debug("Creating {} interface {}", swIf.getType().getSimpleName(), swIf.getName());
151 // FIXME, Vxlan child writer needs to be handled before this is
152 int newSwIfIndex = getVppApi().swIfIndexFromName(swIfName);
154 setInterfaceFlags(swIfName, newSwIfIndex, swIf.isEnabled()
157 setDescription(swIf);
160 private void updateVxlanTunnelInterface(final Interface swIf) {
161 LOG.debug("Updating {} interface {}", swIf.getType().getSimpleName(), swIf.getName());
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);
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()
179 setInterfaceFlags(swIfName, swIfIndex, enabled);
180 setDescription(swIf);
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 */);
187 LOG.debug("Updating interface flags for: {}, index: {}, enabled: {}, ctxId: {}", swIfName, swIfIndex,
190 final int rv = V3poUtils.waitForResponse(ctxId, getVppApi());
192 LOG.warn("Failed to update interface flags for: {}, index: {}, enabled: {}, ctxId: {}", swIfName, swIfIndex,
194 throw new VppApiInvocationException("swInterfaceSetFlags", ctxId, rv);
196 LOG.debug("Interface flags updated successfully for: {}, index: {}, enabled: {}, ctxId: {}",
197 swIfName, swIfIndex, enabled, ctxId);
201 private void setDescription(final Interface swIf) {
202 if (swIf.getDescription() != null) {
203 getVppApi().setInterfaceDescription(swIf.getName(), swIf.getDescription());
205 getVppApi().setInterfaceDescription(swIf.getName(), "");