Update Honeycomb to Rel1810 for vFW
[demo.git] / vnfs / honeycomb_plugin / stream-count / stream-count-impl / src / main / java / org / onap / vnf / vfw / write / ElementCustomizer.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) 2019 AT&T Intellectual Property
19  */
20
21 package org.onap.vnf.vfw.write;
22
23 import org.onap.vnf.vfw.CrudService;
24 import io.fd.honeycomb.translate.spi.write.WriterCustomizer;
25 import io.fd.honeycomb.translate.write.WriteContext;
26 import io.fd.honeycomb.translate.write.WriteFailedException;
27 import javax.annotation.Nonnull;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.stream.count.rev190118.stream.count.params.Streams;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import java.io.IOException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * Writer for {@link Element} list node from our YANG model.
36  */
37 public final class ElementCustomizer implements WriterCustomizer<Streams> {
38
39     private static final Logger LOG = LoggerFactory.getLogger(ElementCustomizer.class);
40
41     private final CrudService<Streams> crudService;
42
43     public ElementCustomizer(@Nonnull final CrudService<Streams> crudService) {
44         this.crudService = crudService;
45     }
46
47     @Override
48     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<Streams> id, @Nonnull final Streams dataAfter,
49                                        @Nonnull final WriteContext writeContext) throws WriteFailedException {
50         //perform write of data,or throw exception
51         //invoked by PUT operation,if provided data doesn't exist in Config data
52         crudService.writeData(id, dataAfter);
53         try {
54             runScript(dataAfter.getActiveStreams().getValue());
55         }
56         catch (IOException e) {
57             String message = "Write operation failed " + e;
58             LOG.error(message);
59         }
60     }
61
62     @Override
63     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Streams> id,
64                                         @Nonnull final Streams dataBefore,
65                                         @Nonnull final Streams dataAfter, @Nonnull final WriteContext writeContext)
66             throws WriteFailedException {
67         //invoked by PUT operation,if provided data does exist in Config data
68         crudService.updateData(id, dataBefore, dataAfter);
69         try {
70             runScript(dataAfter.getActiveStreams().getValue());
71         }
72         catch (IOException e) {
73             String message = "Write operation failed " + e;
74             LOG.error(message);
75         }
76     }
77
78     @Override
79     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Streams> id,
80                                         @Nonnull final Streams dataBefore,
81                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
82         //perform delete of data,or throw exception
83         // Not supported, throw exception
84         throw new WriteFailedException.DeleteFailedException(id,
85                 new UnsupportedOperationException("Delete operation not supported"));
86     }
87
88     //Update the number of running streams running a custom script that uses the old vPacketGen REST APIs
89     private void runScript(long streams) throws IOException {
90
91         String script = new String("bash /opt/enable_disable_streams.sh " + streams);
92         Runtime.getRuntime().exec(script);
93         String message = "Number of running streams updated to " + streams;
94         LOG.info(message);
95     }
96 }