Initial OpenECOMP Demo commit
[demo.git] / vnfs / honeycomb_plugin / sample_plugin / sample-plugin-impl / src / main / java / io / fd / honeycomb / tutorial / write / PgWriteCustomizer.java
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  /*
18  * Modifications copyright (c) 2017 AT&T Intellectual Property
19  */
20
21 package io.fd.honeycomb.tutorial.write;
22
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;
36
37 import java.util.Arrays;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41
42 /**
43  * Writer for {@link VxlanTunnel} list node from our YANG model.
44  */
45 public final class PgWriteCustomizer implements ListWriterCustomizer<PgStream, PgStreamKey> {
46
47
48     private static final Logger LOG = LoggerFactory.getLogger(PgWriteCustomizer.class);
49
50     /**
51      * JVpp APIs
52      */
53     private final FutureJVppCore jvppCore;
54     /**
55      * Shared vxlan tunnel naming context
56      */
57     private final NamingContext pgStreamNamingContext;
58
59     public PgWriteCustomizer(final FutureJVppCore jvppCore, final NamingContext pgStreamNamingContext) {
60         this.jvppCore = jvppCore;
61         this.pgStreamNamingContext = pgStreamNamingContext;
62     }
63
64     @Override
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!!!");
77         String tempMsg = "";
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];
81              LOG.info(tempMsg);
82              pgEnableDisable.streamName[i] = tempArray[i];
83         }
84
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);
88         LOG.info(logMsg);
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
95
96         try {
97             final PgEnableDisableReply replyForWrite = TranslateUtils
98                     .getReplyForWrite(jvppCore.pgEnableDisable(pgEnableDisable).toCompletableFuture(), id);
99
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);
106         }
107     }
108
109     @Override
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"));
117     }
118
119     @Override
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
129
130         String sName = dataBefore.getId();
131         pgEnableDisable.streamName = sName.getBytes();//public byte[]
132         pgEnableDisable.streamNameLength = sName.length()+1;//public int
133
134         String logMsg1 = "***** Disabling: "+sName+" len: "+sName.length()+" getBytes:" + Arrays.toString(pgEnableDisable.streamName);
135         LOG.info(logMsg1);
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
143
144         try {
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);
153         }
154     }
155 }