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.
18 * Modifications copyright (c) 2017 AT&T Intellectual Property
21 package io.fd.honeycomb.tutorial.write;
23 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
24 import io.fd.honeycomb.translate.v3po.util.NamingContext;
25 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
26 import io.fd.honeycomb.translate.write.WriteContext;
27 import io.fd.honeycomb.translate.write.WriteFailedException;
28 import javax.annotation.Nonnull;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.sample.plugin.rev160918.sample.plugin.params.pg.streams.PgStream;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.sample.plugin.rev160918.sample.plugin.params.pg.streams.PgStreamKey;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.openvpp.jvpp.VppBaseCallException;
33 import org.openvpp.jvpp.core.dto.PgEnableDisable;
34 import org.openvpp.jvpp.core.dto.PgEnableDisableReply;
35 import org.openvpp.jvpp.core.future.FutureJVppCore;
37 import java.util.Arrays;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
43 * Writer for {@link VxlanTunnel} list node from our YANG model.
45 public final class PgWriteCustomizer implements ListWriterCustomizer<PgStream, PgStreamKey> {
48 private static final Logger LOG = LoggerFactory.getLogger(PgWriteCustomizer.class);
53 private final FutureJVppCore jvppCore;
55 * Shared vxlan tunnel naming context
57 private final NamingContext pgStreamNamingContext;
59 public PgWriteCustomizer(final FutureJVppCore jvppCore, final NamingContext pgStreamNamingContext) {
60 this.jvppCore = jvppCore;
61 this.pgStreamNamingContext = pgStreamNamingContext;
65 public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<PgStream> id,
66 @Nonnull final PgStream dataAfter,
67 @Nonnull final WriteContext writeContext) throws WriteFailedException {
68 // Create and set vxlan tunnel add request
69 final PgEnableDisable pgEnableDisable = new PgEnableDisable();
70 // 1 for add, 0 for delete
71 //look into this file: ~/vpp/build-root/build-vpp-native/vpp-api/java/jvpp-core/org/openvpp/jvpp/core/dto/PgEnableDisable.java
72 pgEnableDisable.isEnabled = 1;//public byte
73 String sName = dataAfter.getId();
74 //pgEnableDisable.streamName = sName.getBytes();//public byte[]
75 byte[] tempArray = sName.getBytes();
76 LOG.info("Going to copy array!!!");
78 pgEnableDisable.streamName = new byte[tempArray.length+1];
79 for(int i = 0; i < tempArray.length; i++){
80 tempMsg = "copying: i= "+i+" value: "+tempArray[i];
82 pgEnableDisable.streamName[i] = tempArray[i];
85 //System.arraycopy( sName.getBytes(), 0, pgEnableDisable.streamName, 0, sName.length());
86 pgEnableDisable.streamNameLength = sName.length() + 1;//public int
87 String logMsg = "######***** Enabling: "+sName+" len: "+sName.length()+" getBytes:" + Arrays.toString(pgEnableDisable.streamName);
89 // dataAfter is the new vxlanTunnel configuration
90 //final boolean isIpv6 = dataAfter.getSrc().getIpv6Address() != null;
91 //vxlanAddDelTunnel.isIpv6 = TranslateUtils.booleanToByte(isIpv6);
92 //vxlanAddDelTunnel.srcAddress = TranslateUtils.ipAddressToArray(isIpv6, dataAfter.getSrc());
93 //vxlanAddDelTunnel.dstAddress = TranslateUtils.ipAddressToArray(isIpv6, dataAfter.getDst());
94 // There are other input parameters that are not exposed by our YANG model, default values will be used
97 final PgEnableDisableReply replyForWrite = TranslateUtils
98 .getReplyForWrite(jvppCore.pgEnableDisable(pgEnableDisable).toCompletableFuture(), id);
100 // VPP returns the index of new vxlan tunnel
101 //final int newVxlanTunnelIndex = replyForWrite.swIfIndex;
102 // It's important to store it in context so that reader knows to which name a vxlan tunnel is mapped
103 pgStreamNamingContext.addName(1, dataAfter.getId(), writeContext.getMappingContext());
104 } catch (VppBaseCallException e) {
105 throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
110 public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<PgStream> id,
111 @Nonnull final PgStream dataBefore,
112 @Nonnull final PgStream dataAfter, @Nonnull final WriteContext writeContext)
113 throws WriteFailedException {
114 // Not supported at VPP API level, throw exception
115 throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter,
116 new UnsupportedOperationException("Vxlan tunnel update is not supported by VPP"));
120 public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<PgStream> id,
121 @Nonnull final PgStream dataBefore,
122 @Nonnull final WriteContext writeContext) throws WriteFailedException {
123 // Create and set vxlan tunnel add request
124 //final VxlanAddDelTunnel vxlanAddDelTunnel = new VxlanAddDelTunnel();
125 final PgEnableDisable pgEnableDisable = new PgEnableDisable();
126 // 1 for add, 0 for delete
127 //vxlanAddDelTunnel.isAdd = 0;
128 pgEnableDisable.isEnabled = 0;//public byte
130 String sName = dataBefore.getId();
131 pgEnableDisable.streamName = sName.getBytes();//public byte[]
132 pgEnableDisable.streamNameLength = sName.length()+1;//public int
134 String logMsg1 = "***** Disabling: "+sName+" len: "+sName.length()+" getBytes:" + Arrays.toString(pgEnableDisable.streamName);
136 // Vxlan tunnel is identified by its attributes when deleting, not index, so set all attributes
137 // dataBefore is the vxlan tunnel that's being deleted
138 //final boolean isIpv6 = dataBefore.getSrc().getIpv6Address() != null;
139 //vxlanAddDelTunnel.isIpv6 = TranslateUtils.booleanToByte(isIpv6);
140 //vxlanAddDelTunnel.srcAddress = TranslateUtils.ipAddressToArray(isIpv6, dataBefore.getSrc());
141 //vxlanAddDelTunnel.dstAddress = TranslateUtils.ipAddressToArray(isIpv6, dataBefore.getDst());
142 // There are other input parameters that are not exposed by our YANG model, default values will be used
145 // final VxlanAddDelTunnelReply replyForWrite = TranslateUtils
146 // .getReplyForWrite(jvppCore.vxlanAddDelTunnel(vxlanAddDelTunnel).toCompletableFuture(), id);
147 final PgEnableDisableReply replyForWrite = TranslateUtils
148 .getReplyForWrite(jvppCore.pgEnableDisable(pgEnableDisable).toCompletableFuture(), id);
149 // It's important to remove the mapping from context
150 pgStreamNamingContext.removeName(dataBefore.getId(), writeContext.getMappingContext());
151 } catch (VppBaseCallException e) {
152 throw new WriteFailedException.DeleteFailedException(id, e);