Skip to content
Snippets Groups Projects
GTURTLEModeling.java 389 KiB
Newer Older
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                TMLArchiCPUNode newcpu = new TMLArchiCPUNode(firewallArchComp.getX() + 100, firewallArchComp.getY() + 100, newarch.tmlap.getMinX(), newarch.tmlap.getMaxX(), newarch.tmlap.getMinY(), newarch.tmlap.getMaxY(), true, null, newarch.tmlap);
                newcpu.setName("CPU" + firewallNode.getName());
                newarch.tmlap.replaceArchComponent(firewallArchComp, newcpu);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                //Add artifact
                TMLArchiArtifact hsmArt = new TMLArchiArtifact(newcpu.getX(), newcpu.getY(), newarch.tmlap.getMinX(), newarch.tmlap.getMaxX(), newarch.tmlap.getMinY(), newarch.tmlap.getMaxY(), true, newcpu, newarch.tmlap);
                newarch.tmlap.addComponent(hsmArt, newcpu.getX(), newcpu.getY(), true, true);
                hsmArt.setFullName(firewallNode.getName(), refTask);
            }
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            //  for (int link = 0; link < links / 2; link++) {
            HashMap<TMLChannel, TMLChannel> inChans = new HashMap<TMLChannel, TMLChannel>();
            HashMap<TMLChannel, TMLChannel> outChans = new HashMap<TMLChannel, TMLChannel>();
            if (TraceManager.devPolicy == TraceManager.TO_CONSOLE) {
                firewallComp = new TMLCPrimitiveComponent(newtcdp.getMaxX() - 100, newtcdp.getMaxY() - 100, newtcdp.getMinX(), newtcdp.getMaxX(), newtcdp.getMinY(), newtcdp.getMaxY(), false, null, newtcdp);
                TAttribute reqIndex = new TAttribute(2, "index", "0", 0);
                firewallComp.getAttributeList().add(reqIndex);
                TAttribute chanIndex = new TAttribute(2, "channelIndex", "0", 0);
                firewallComp.getAttributeList().add(chanIndex);
                TAttribute rule = new TAttribute(2, "rule", "true", 4);
                firewallComp.getAttributeList().add(rule);
                newtcdp.addComponent(firewallComp, 0, 0, false, true);
                firewallComp.setValueWithChange(firewallNode.getName());
                firewallADP = tcp.getTMLActivityDiagramPanel(firewallNode.getName());
            }
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            List<TMLChannel> channelsCopy = tmlm.getChannels();
            List<TMLChannel> toAdd = new ArrayList<TMLChannel>();
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            TMLTask firewall = new TMLTask("TASK__" + firewallNode.getName(), firewallComp, firewallADP);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            // tmlm.addTask(firewall);
            //   map.addTaskToHwExecutionNode(firewall, cpu);
            TMLActivity act = firewall.getActivityDiagram();
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            TraceManager.addDev("FirewallADP " + firewallADP);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            List<TMLTask> modifiedTasks = new ArrayList<TMLTask>();
            //Build requests between every connected task and firewall task
            for (TMLTask task : map.getTMLModeling().getTasks()) {
                TMLCPrimitiveComponent taskcomp = null;
                for (TGComponent tg : tcdp.getComponentList()) {
                    if (tg instanceof TMLCPrimitiveComponent) {
                        if (tg.getValue().equals(task.getName().split("__")[1])) {
                            taskcomp = ((TMLCPrimitiveComponent) tg);
                            break;
                        }
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                    } else if (tg instanceof TMLCCompositeComponent) {
                        TMLCCompositeComponent cc = (TMLCCompositeComponent) tg;
                        List<TMLCPrimitiveComponent> pcomps = cc.getAllPrimitiveComponents();
                        for (TMLCPrimitiveComponent pc : pcomps) {
                            if (pc.getValue().equals(task.getName().split("__")[1])) {
                                taskcomp = pc;
                                break;
                            }
                        }
                    }
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                }
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                if (taskcomp == null) {
                    continue;
                }
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                if (pathExists(map, firewallNode, task) && commPath(map, firewallNode, task)) {
                    modifiedTasks.add(task);
                    //Add Request Ports
                    TMLCChannelOutPort destPort = new TMLCChannelOutPort(taskcomp.getX(), taskcomp.getY(), newtcdp.getMinX(), newtcdp.getMaxX(), newtcdp.getMinY(), newtcdp.getMaxX(), true, firewallComp, newtcdp);
                    TMLCChannelOutPort originPort = new TMLCChannelOutPort(taskcomp.getX(), taskcomp.getY(), newtcdp.getMinX(), newtcdp.getMaxX(), newtcdp.getMinY(), newtcdp.getMaxX(), true, taskcomp, newtcdp);
                    originPort.commName = "req" + firewallNode.getName();
                    originPort.isOrigin = true;
                    destPort.isOrigin = false;

                    destPort.commName = "req" + firewallNode.getName();
                    //Connect Request ports
                    TMLCPortConnector conn = new TMLCPortConnector(0, 0, newtcdp.getMinX(), newtcdp.getMaxX(), newtcdp.getMinY(), newtcdp.getMaxX(), true, null, newtcdp, originPort.getTGConnectingPointAtIndex(0), destPort.getTGConnectingPointAtIndex(0), new Vector<Point>());
                    newtcdp.addComponent(conn, 0, 0, false, true);
                    originPort.typep = 2;
                    destPort.typep = 2;
                    originPort.setParam(0, new TType(1));
                    destPort.setParam(0, new TType(1));
                    newtcdp.addComponent(originPort, taskcomp.getX(), taskcomp.getY(), true, true);
                    newtcdp.addComponent(destPort, firewallComp.getX(), firewallComp.getY(), true, true);

                    //Add updateRules connection to first task
                    if (!updateRulesAdded) {
                        destPort = new TMLCChannelOutPort(taskcomp.getX(), taskcomp.getY(), newtcdp.getMinX(), newtcdp.getMaxX(), newtcdp.getMinY(), newtcdp.getMaxX(), true, firewallComp, newtcdp);
                        originPort = new TMLCChannelOutPort(taskcomp.getX(), taskcomp.getY(), newtcdp.getMinX(), newtcdp.getMaxX(), newtcdp.getMinY(), newtcdp.getMaxX(), true, taskcomp, newtcdp);

                        originPort.commName = "updateRules" + firewallNode.getName();
                        originPort.isOrigin = true;
                        destPort.isOrigin = false;

                        destPort.commName = "updateRules" + firewallNode.getName();

                        //Connect Evt ports
                        conn = new TMLCPortConnector(0, 0, newtcdp.getMinX(), newtcdp.getMaxX(), newtcdp.getMinY(), newtcdp.getMaxX(), true, null, newtcdp, originPort.getTGConnectingPointAtIndex(0), destPort.getTGConnectingPointAtIndex(0), new Vector<Point>());
                        newtcdp.addComponent(conn, 0, 0, false, true);
                        originPort.typep = 1;
                        destPort.typep = 1;
                        originPort.setParam(0, new TType(1));
                        destPort.setParam(0, new TType(1));
                        originPort.setParam(1, new TType(2));
                        destPort.setParam(1, new TType(2));
                        newtcdp.addComponent(originPort, taskcomp.getX(), taskcomp.getY(), true, true);
                        newtcdp.addComponent(destPort, firewallComp.getX(), firewallComp.getY(), true, true);
                        updateRulesAdded = true;
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                    }
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                }
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            }
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            List<String> firewallChans = new ArrayList<String>();
            for (TMLChannel chan : channelsCopy) {
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                TMLTask orig = chan.getOriginTask();
                TMLTask dest = chan.getDestinationTask();
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                //If firewall does not filter these tasks, continue on
                if (!pathExists(map, firewallNode, orig) || !pathExists(map, firewallNode, dest) || !pathIncludes(map, orig, dest, firewallNode)) {
                    //toAdd.add(chan);
                    continue;
                }
                firewallChans.add(chan.getName().split("__")[chan.getName().split("__").length - 1]);
                System.out.println("REMOVING " + chan.getName());
                chansToRemove.add(chan.getName().split("__")[1]);
                channelIndexMap.put(chan.getName().split("__")[1], index);
                index++;
                TMLPort origPort = chan.getOriginPort();
                TMLPort destPort = chan.getDestinationPort();
                TMLChannel wr = new TMLChannel(chan.getName().split("__")[1] + "_firewallIn", chan.getReferenceObject());
                //Specify new channel attributes
                wr.setSize(chan.getSize());
                wr.setMax(chan.getMax());
                wr.setPorts(origPort, destPort);
                wr.setType(TMLChannel.BRBW);
                wr.setPriority(chan.getPriority());
                wr.setTasks(orig, firewall);
                TMLChannel rd = new TMLChannel(chan.getName().split("__")[1] + "_firewallOut", chan.getReferenceObject());
                rd.setTasks(firewall, dest);
                rd.setSize(chan.getSize());
                rd.setMax(chan.getMax());
                rd.setPorts(origPort, destPort);
                rd.setType(TMLChannel.BRBW);
                rd.setPriority(chan.getPriority());
                inChans.put(chan, wr);
                outChans.put(chan, rd);
                toAdd.add(rd);
                toAdd.add(wr);
                map.getCorrespondanceList().addCor(rd, (TGComponent) rd.getReferenceObject());
                map.getCorrespondanceList().addCor(wr, (TGComponent) wr.getReferenceObject());


            }
            List<TGComponent> tmpRemove = new ArrayList<TGComponent>();

            for (TGComponent tg : newtcdp.getComponentList()) {
                if (tg instanceof TMLCPrimitiveComponent) {
                    TMLCPrimitiveComponent taskcomp = (TMLCPrimitiveComponent) tg;
                    for (TMLCPrimitivePort port : taskcomp.getAllChannelsOriginPorts()) {
                        //System.out.println("chantoremove " + chansToRemove + " " + port.getPortName());
                        if (chansToRemove.contains(port.getPortName())) {
                            //System.out.println("Removing " + port);
                            tmpRemove.add(port);
                        }
                    }
                    for (TMLCPrimitivePort port : taskcomp.getAllChannelsDestinationPorts()) {
                        if (chansToRemove.contains(port.getPortName())) {
                            tmpRemove.add(port);
                        }
                    }
                }
            }
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            for (TGComponent tg : tmpRemove) {
                newtcdp.removeComponent(tg);
            }
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            //Build activity diagram
            //Get start state
            adStart = (TMLADStartState) firewallADP.getComponentList().get(0);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            if (inChans.keySet().size() == 0) {
                TMLADStopState stop = new TMLADStopState(100, 100, firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP);
                firewallADP.addComponent(stop, 100, 100, false, true);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                //Connect stop and start
                TGConnector tmp = new TGConnectorTMLAD(100, 100, firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP, null, null, new Vector<Point>());
                tmp.setP1(adStart.getTGConnectingPointAtIndex(0));
                tmp.setP2(stop.getTGConnectingPointAtIndex(0));
                firewallADP.addComponent(tmp, 100, 100, false, true);
                continue;
            }
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            //Add request
            req = new TMLADReadRequestArg(300, 100, firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP);
            firewallADP.addComponent(req, 300, 100, false, true);
            req.setParam(0, "index");
            req.makeValue();
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            TGConnector tmp = new TGConnectorTMLAD(req.getX(), req.getY(), firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP, adStart.getTGConnectingPointAtIndex(0), req.getTGConnectingPointAtIndex(0), new Vector<Point>());
            firewallADP.addComponent(tmp, req.getX(), req.getY(), false, true);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            //add choice between update and channel filter
            adChoiceMain = new TMLADChoice(400, 300, firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP);
            firewallADP.addComponent(adChoiceMain, 400, 300, false, true);
            adChoiceMain.setGuard("[index==0]", 0);
            adChoiceMain.setGuard("[else]", 1);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            tmp = new TGConnectorTMLAD(adChoiceMain.getX(), adChoiceMain.getY(), firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP, req.getTGConnectingPointAtIndex(1), adChoiceMain.getTGConnectingPointAtIndex(0), new Vector<Point>());
            firewallADP.addComponent(tmp, adChoiceMain.getX(), adChoiceMain.getY(), false, true);
            //Add guard
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            //Add choice between channels
            adChoice = new TMLADChoice(800, 400, firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP);
            firewallADP.addComponent(adChoice, 400, 300, false, true);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            tmp = new TGConnectorTMLAD(adChoice.getX(), adChoice.getY(), firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP, adChoiceMain.getTGConnectingPointAtIndex(2), adChoice.getTGConnectingPointAtIndex(0), new Vector<Point>());
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            firewallADP.addComponent(tmp, adChoice.getX(), adChoice.getY(), false, true);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            //Add waitEvent and updateRules activity
            TMLADWaitEvent evt = new TMLADWaitEvent(100, 300, firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP);
            evt.setEventName("updateRules" + firewallNode.getName());
            evt.setParam("channelIndex", 0);
            evt.setParam("rule", 1);
            evt.makeValue();
            firewallADP.addComponent(evt, 100, 300, false, true);
            //Connect choice and waitevent
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            //Add updaterules choice
            TMLADChoice adChoiceRules = new TMLADChoice(100, 400, firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP);
            firewallADP.addComponent(adChoiceRules, 100, 400, false, true);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            tmp = new TGConnectorTMLAD(adChoice.getX(), adChoice.getY(), firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP, adChoiceMain.getTGConnectingPointAtIndex(1), evt.getTGConnectingPointAtIndex(0), new Vector<Point>());
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            firewallADP.addComponent(tmp, adChoice.getX(), adChoice.getY(), false, true);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            adChoiceMain.setGuard("[index==0]", 0);
            adChoiceMain.setGuard("[else]", 1);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            //Add connector between evt and updaterules choice
            tmp = new TGConnectorTMLAD(adChoice.getX(), adChoice.getY(), firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP, evt.getTGConnectingPointAtIndex(1), adChoiceRules.getTGConnectingPointAtIndex(0), new Vector<Point>());
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            firewallADP.addComponent(tmp, adChoice.getX(), adChoice.getY(), false, true);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            //for each channel, add the rule change and guard
            int i = 1;
            for (TMLChannel chan : inChans.keySet()) {
                if (i > 3) {
                    break;
                }
                TMLADActionState action = new TMLADActionState(100 * i, 500, firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP);
                firewallADP.addComponent(action, 100 * i, 500, false, true);
                action.setValue(chan.getName().split("__")[1] + "Allowed=rule");
                //Connect choice and action
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                TAttribute channelRule = new TAttribute(2, chan.getName().split("__")[1] + "Allowed", "true", 4);
                firewallComp.getAttributeList().add(channelRule);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                tmp = new TGConnectorTMLAD(action.getX(), action.getY(), firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP, adChoiceRules.getTGConnectingPointAtIndex(i), action.getTGConnectingPointAtIndex(0), new Vector<Point>());
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                firewallADP.addComponent(tmp, action.getX(), action.getY(), false, true);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                adChoiceRules.setGuard("[channelIndex==" + channelIndexMap.get(chan.getName().split("__")[1]) + "]", i - 1);
                //add stop state
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                adStop = new TMLADStopState(100 * i, 600, firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP);
                firewallADP.addComponent(adStop, 100 * i, 600, false, true);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                //Connect action and stop state
                tmp = new TGConnectorTMLAD(action.getX(), action.getY(), firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP, action.getTGConnectingPointAtIndex(1), adStop.getTGConnectingPointAtIndex(0), new Vector<Point>());
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                firewallADP.addComponent(tmp, action.getX(), action.getY(), false, true);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                i++;
            }
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            for (TMLChannel chan : inChans.keySet()) {
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                TMLChannel newChan = inChans.get(chan);
                TMLCChannelOutPort originPort = new TMLCChannelOutPort(0, 0, tcp.tmlctdp.getMinX(), tcp.tmlctdp.getMaxX(), tcp.tmlctdp.getMinY(), tcp.tmlctdp.getMaxX(), true, null, tcp.tmlctdp);
                TMLCChannelOutPort destPort = new TMLCChannelOutPort(0, 0, tcp.tmlctdp.getMinX(), tcp.tmlctdp.getMaxX(), tcp.tmlctdp.getMinY(), tcp.tmlctdp.getMaxX(), true, null, tcp.tmlctdp);
                for (TGComponent tg : tcp.tmlctdp.getComponentList()) {
                    if (tg instanceof TMLCPrimitiveComponent) {
                        if (tg.getValue().equals(newChan.getOriginTask().getName().split("__")[1])) {
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                            originPort = new TMLCChannelOutPort(tg.getX(), tg.getY(), tcp.tmlctdp.getMinX(), tcp.tmlctdp.getMaxX(), tcp.tmlctdp.getMinY(), tcp.tmlctdp.getMaxX(), true, tg, tcp.tmlctdp);
                            originPort.commName = newChan.getName();
                            tcp.tmlctdp.addComponent(originPort, tg.getX(), tg.getY(), true, true);
                        } else if (tg.getValue().equals(firewallNode.getName())) {
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                            destPort = new TMLCChannelOutPort(tg.getX(), tg.getY(), tcp.tmlctdp.getMinX(), tcp.tmlctdp.getMaxX(), tcp.tmlctdp.getMinY(), tcp.tmlctdp.getMaxX(), true, tg, tcp.tmlctdp);
                            destPort.isOrigin = false;
                            destPort.commName = newChan.getName();
                            tcp.tmlctdp.addComponent(destPort, tg.getX(), tg.getY(), true, true);
                        }
                    }
                }
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                TMLCPortConnector conn = new TMLCPortConnector(0, 0, tcp.tmlctdp.getMinX(), tcp.tmlctdp.getMaxX(), tcp.tmlctdp.getMinY(), tcp.tmlctdp.getMaxX(), true, null, tcp.tmlctdp, originPort.getTGConnectingPointAtIndex(0), destPort.getTGConnectingPointAtIndex(0), new Vector<Point>());
                tcp.tmlctdp.addComponent(conn, 0, 0, false, true);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                originPort = new TMLCChannelOutPort(0, 0, tcp.tmlctdp.getMinX(), tcp.tmlctdp.getMaxX(), tcp.tmlctdp.getMinY(), tcp.tmlctdp.getMaxX(), true, null, tcp.tmlctdp);
                destPort = new TMLCChannelOutPort(0, 0, tcp.tmlctdp.getMinX(), tcp.tmlctdp.getMaxX(), tcp.tmlctdp.getMinY(), tcp.tmlctdp.getMaxX(), true, null, tcp.tmlctdp);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                TMLChannel wrChan = outChans.get(chan);
                for (TGComponent tg : tcp.tmlctdp.getComponentList()) {
                    if (tg instanceof TMLCPrimitiveComponent) {
                        if (tg.getValue().equals(firewallNode.getName())) {
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                            originPort = new TMLCChannelOutPort(tg.getX(), tg.getY(), tcp.tmlctdp.getMinX(), tcp.tmlctdp.getMaxX(), tcp.tmlctdp.getMinY(), tcp.tmlctdp.getMaxX(), true, tg, tcp.tmlctdp);
                            originPort.commName = wrChan.getName();
                            tcp.tmlctdp.addComponent(originPort, tg.getX(), tg.getY(), true, true);
                        } else if (tg.getValue().equals(wrChan.getDestinationTask().getName().split("__")[1])) {
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                            destPort = new TMLCChannelOutPort(tg.getX(), tg.getY(), tcp.tmlctdp.getMinX(), tcp.tmlctdp.getMaxX(), tcp.tmlctdp.getMinY(), tcp.tmlctdp.getMaxX(), true, tg, tcp.tmlctdp);
                            destPort.isOrigin = false;
                            destPort.commName = wrChan.getName();
                            tcp.tmlctdp.addComponent(destPort, tg.getX(), tg.getY(), true, true);
                        }
                    }
                }
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                conn = new TMLCPortConnector(0, 0, tcp.tmlctdp.getMinX(), tcp.tmlctdp.getMaxX(), tcp.tmlctdp.getMinY(), tcp.tmlctdp.getMaxX(), true, null, tcp.tmlctdp, originPort.getTGConnectingPointAtIndex(0), destPort.getTGConnectingPointAtIndex(0), new Vector<Point>());
                tcp.tmlctdp.addComponent(conn, 0, 0, false, true);
                i++;
            }
            int xpos = 700;
            int ypos = 100;
            i = 1;
            for (TMLChannel chan : inChans.keySet()) {
                if (i > 3) {
                    break;
                }
                adChoice.setGuard("[index==" + channelIndexMap.get(chan.getName().split("__")[1]) + "]", i - 1);
                TMLChannel newChan = inChans.get(chan);
                adRC = new TMLADReadChannel(xpos, 450, firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP);
                adRC.setChannelName(newChan.getName());
                adRC.setSamples("1");
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                tmp = new TGConnectorTMLAD(adRC.getX(), adRC.getY(), firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP, adChoice.getTGConnectingPointAtIndex(i), adRC.getTGConnectingPointAtIndex(0), new Vector<Point>());
                firewallADP.addComponent(tmp, adRC.getX(), adRC.getY(), false, true);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                firewallADP.addComponent(adRC, xpos, 450, false, true);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                //Execute for latency value
                exec = new TMLADExecI(xpos, 500, firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                exec.setDelayValue(Integer.toString(firewallNode.latency));
                firewallADP.addComponent(exec, xpos, 500, false, true);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                tmp = new TGConnectorTMLAD(exec.getX(), exec.getY(), firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP, adRC.getTGConnectingPointAtIndex(1), exec.getTGConnectingPointAtIndex(0), new Vector<Point>());
                firewallADP.addComponent(tmp, exec.getX(), exec.getY(), false, true);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                adChoice2 = new TMLADChoice(xpos, 550, firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP);
                firewallADP.addComponent(adChoice2, 550, 300, false, true);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                tmp = new TGConnectorTMLAD(exec.getX(), exec.getY(), firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP, exec.getTGConnectingPointAtIndex(1), adChoice2.getTGConnectingPointAtIndex(0), new Vector<Point>());
                firewallADP.addComponent(tmp, exec.getX(), exec.getY(), false, true);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                TMLChannel wrChan = outChans.get(chan);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                adWC = new TMLADWriteChannel(xpos - 50, 600, firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP);
                adWC.setChannelName(wrChan.getName());
                adWC.setSamples("1");
                firewallADP.addComponent(adWC, xpos - 50, 600, false, true);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                tmp = new TGConnectorTMLAD(exec.getX(), exec.getY(), firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP, adChoice2.getTGConnectingPointAtIndex(1), adWC.getTGConnectingPointAtIndex(0), new Vector<Point>());
                firewallADP.addComponent(tmp, exec.getX(), exec.getY(), false, true);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                adStop = new TMLADStopState(xpos - 50, 650, firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP);
                firewallADP.addComponent(adStop, xpos - 50, 650, false, true);
                tmp = new TGConnectorTMLAD(adStop.getX(), adStop.getY(), firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP, adWC.getTGConnectingPointAtIndex(1), adStop.getTGConnectingPointAtIndex(0), new Vector<Point>());
                firewallADP.addComponent(tmp, adStop.getX(), adStop.getY(), false, true);
                //    } else {
                adStop2 = new TMLADStopState(xpos + 50, 650, firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP);
                firewallADP.addComponent(adStop2, xpos + 50, 650, false, true);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                tmp = new TGConnectorTMLAD(adStop.getX(), adStop.getY(), firewallADP.getMinX(), firewallADP.getMaxX(), firewallADP.getMinY(), firewallADP.getMaxY(), false, null, firewallADP, adChoice2.getTGConnectingPointAtIndex(2), adStop2.getTGConnectingPointAtIndex(0), new Vector<Point>());
                firewallADP.addComponent(tmp, adStop.getX(), adStop.getY(), false, true);
                //   }
                xpos += 200;
                i++;
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            }
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            TMLStartState start = new TMLStartState("start", adStart);
            act.setFirst(start);
            map.getCorrespondanceList().addCor(start, adStart);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            System.out.println("MODIFIED " + modifiedTasks);
            //Replace channel operator with new firewallIn and firewallOut operators
            System.out.println("firewallchans " + firewallChans);
            for (TMLTask task : map.getTMLModeling().getTasks()) {
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                //Get activity diagram
                TMLActivityDiagramPanel tad = tcp.getTMLActivityDiagramPanel(task.getName());
                TGConnector fromStart = new TGConnectorTMLAD(0, 0, tad.getMinX(), tad.getMaxX(), tad.getMinY(), tad.getMaxY(), false, null, tad, null, null, new Vector<Point>());
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                List<TMLADWriteChannel> wrChans = new ArrayList<TMLADWriteChannel>();
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                List<TMLADReadChannel> rdChans = new ArrayList<TMLADReadChannel>();
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                for (TGComponent tg : tad.getComponentList()) {
                    if (tg instanceof TMLADWriteChannel) {
                        TMLADWriteChannel writeChannel = (TMLADWriteChannel) tg;
                        if (fromStart != null) {
                            wrChans.add(writeChannel);
                        }
                    }
                    if (tg instanceof TMLADReadChannel) {
                        TMLADReadChannel readChannel = (TMLADReadChannel) tg;
                        if (fromStart != null) {
                            rdChans.add(readChannel);
                        }
                    }
/* former HEAD
Letitia Li's avatar
Letitia Li committed
				//Get activity diagram
Ludovic Apvrille's avatar
Ludovic Apvrille committed
				TMLActivityDiagramPanel tad = tcp.getTMLActivityDiagramPanel(task.getName());
Letitia Li's avatar
Letitia Li committed
				TGConnector fromStart = new TGConnectorTMLAD(0, 0, tad.getMinX(), tad.getMaxX(), tad.getMinY(), tad.getMaxY(), false, null, tad, null, null, new Vector<Point>());
Letitia Li's avatar
Letitia Li committed
				List<TMLADWriteChannel> wrChans = new ArrayList<TMLADWriteChannel>();
Letitia Li's avatar
Letitia Li committed
				List<TMLADReadChannel> rdChans = new ArrayList<TMLADReadChannel>();
Letitia Li's avatar
Letitia Li committed
				for (TGComponent tg : tad.getComponentList()) {
            	    if (tg instanceof TMLADWriteChannel) {
						TMLADWriteChannel writeChannel = (TMLADWriteChannel) tg;
            	        if (fromStart != null) {
            	        	wrChans.add(writeChannel);
            	        }
            	    }
            	    if (tg instanceof TMLADReadChannel) {
						TMLADReadChannel readChannel = (TMLADReadChannel) tg;
            	        if (fromStart != null) {
            	        	rdChans.add(readChannel);
            	        }
            	    }
        		}
Ludovic Apvrille's avatar
Ludovic Apvrille committed
        		//For each writeChannel, add request operator, rename channel
Letitia Li's avatar
Letitia Li committed
        		for (TMLADWriteChannel wr: wrChans){
Letitia Li's avatar
Letitia Li committed
	        		String chanName = wr.getChannelName();
Letitia Li's avatar
Letitia Li committed
        			xpos = wr.getX();
            	    ypos = wr.getY();
            	    fromStart = tad.findTGConnectorEndingAt(wr.getTGConnectingPointAtIndex(0));
Dominique Blouin's avatar
Dominique Blouin committed
            	    //TGConnectingPoint point = fromStart.getTGConnectingPointP2();
Ludovic Apvrille's avatar
Ludovic Apvrille committed
======= */
                }
                //For each writeChannel, add request operator, rename channel
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                for (TMLADWriteChannel wr : wrChans) {
                    //Check if it goes through the firewall
                    String chanName = wr.getChannelName();
                    if (firewallChans.contains(chanName)) {
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                        xpos = wr.getX();
                        ypos = wr.getY();
                        fromStart = tad.findTGConnectorEndingAt(wr.getTGConnectingPointAtIndex(0));
                        TGConnectingPoint point = fromStart.getTGConnectingPointP2();
//>>>>>>> labsoc_sec
                        int yShift = 50;

                        TMLADSendRequest sendreq = new TMLADSendRequest(xpos, ypos, tad.getMinX(), tad.getMaxX(), tad.getMinY(), tad.getMaxY(), false, null, tad);
                        sendreq.setRequestName("req" + firewallNode.getName());
                        if (channelIndexMap.containsKey(chanName)) {
                            sendreq.setParam(0, Integer.toString(channelIndexMap.get(chanName)));
                        }
                        sendreq.makeValue();

                        tad.addComponent(sendreq, xpos, ypos, false, true);
                        //Re-route connector to sendreq
                        fromStart.setP2(sendreq.getTGConnectingPointAtIndex(0));


                        //Add connector between sendreq and writechannel
                        fromStart = new TGConnectorTMLAD(wr.getX(), wr.getY(), tad.getMinX(), tad.getMaxX(), tad.getMinY(), tad.getMaxY(), false, null, tad, sendreq.getTGConnectingPointAtIndex(1), wr.getTGConnectingPointAtIndex(0), new Vector<Point>());
                        tad.addComponent(fromStart, xpos, ypos, false, true);

                        wr.setChannelName(chanName + "_firewallIn");
                        wr.makeValue();

                        //Shift everything below sendreq down
                        for (TGComponent tg : tad.getComponentList()) {
                            if (tg.getY() >= ypos && tg != sendreq) {
                                tg.setCd(tg.getX(), tg.getY() + yShift);
                            }
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                }
                //for each readchannel, rename channel
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                for (TMLADReadChannel rd : rdChans) {
                    //check if it goes through the firewall
                    String chanName = rd.getChannelName();
                    if (firewallChans.contains(chanName)) {
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                        rd.setChannelName(chanName + "_firewallOut");
                        rd.makeValue();
                    }
                }
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            }
            //Remove old channel ports
Ludovic Apvrille's avatar
Ludovic Apvrille committed
        }


        //Redo syntax checking
        GTMLModeling gtm = new GTMLModeling(newarch, true);
        map = gtm.translateToTMLMapping();
Ludovic Apvrille's avatar
Ludovic Apvrille committed
        return map;
    }

    @SuppressWarnings("unchecked")
    public void setTMLMapping(TMLMapping tmap) {
        this.tmap = tmap;
    }

Ludovic Apvrille's avatar
Ludovic Apvrille committed

    public HashMap<String, HashSet<String>> getCPUTaskMap() {
        HashMap<String, HashSet<String>> cpuTaskMap = new HashMap<String, HashSet<String>>();
        if (tmap == null) {
            return cpuTaskMap;
        }

        for (HwNode node : tmap.getArch().getCPUs()) {
            if (tmap.getMappedTasks(node).size() > 0) {
                cpuTaskMap.put(node.getName(), tmap.getMappedTasks(node));
            }
        }

        return cpuTaskMap;
    }

    public void addHSM(MainGUI gui, Map<String, List<String>> selectedCpuTasks) {
        HSMGeneration hsm = new HSMGeneration(gui, selectedCpuTasks, tmap);
        hsm.startThread();
    }

Ludovic Apvrille's avatar
Ludovic Apvrille committed
    @SuppressWarnings("unchecked")
    public boolean generateGraphicalMapping(TMLMapping map) {
        TURTLEPanel tmlap = tmap.getCorrespondanceList().getTG(tmap.getArch().getFirstCPU()).getTDiagramPanel().tp;
        int arch = mgui.tabs.indexOf(tmlap);
        mgui.cloneRenameTab(arch, "Z3");
Ludovic Apvrille's avatar
Ludovic Apvrille committed
        TMLArchiPanel newArch = (TMLArchiPanel) mgui.tabs.get(mgui.tabs.size() - 1);
        TMLArchiDiagramPanel panel = newArch.tmlap;

        if (panel == null) {
            return false;
        }

        panel.removeAllArtifacts();

Ludovic Apvrille's avatar
Ludovic Apvrille committed
        List<HwExecutionNode> nodes = map.getNodes();
        HwExecutionNode node;
        List<TMLTask> tasks = map.getMappedTasks();
        TMLTask task;
        for (int i = 0; i < nodes.size(); i++) {
Ludovic Apvrille's avatar
Ludovic Apvrille committed
            node = nodes.get(i);
            task = tasks.get(i);
            if ((node != null) && (task != null)) {
                if (!panel.addTaskToNode(node.getName(), task.getName())) {
                    TraceManager.addDev("Could not add " + task.getName() + " to " + node.getName());
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                    return false;
                } else {
                    TraceManager.addDev("Task " + task.getName() + " was added to " + node.getName());
Ludovic Apvrille's avatar
Ludovic Apvrille committed

    public TMLMapping<TGComponent> autoSecure(MainGUI gui, boolean autoConf, boolean autoWeakAuth, boolean autoStrongAuth) {
        //TODO add more options
        //
        if (tmap == null) {
            return null;
        }
        TURTLEPanel tmlap = tmap.getCorrespondanceList().getTG(tmap.getArch().getFirstCPU()).getTDiagramPanel().tp;
        int arch = gui.tabs.indexOf(tmlap);
        gui.cloneRenameTab(arch, "enc");
        TMLArchiPanel newarch = (TMLArchiPanel) gui.tabs.get(gui.tabs.size() - 1);

        return autoSecure(gui, "enc", tmap, newarch, autoConf, autoWeakAuth, autoStrongAuth);
    }

    public TMLMapping<TGComponent> autoSecure(MainGUI gui, String name, TMLMapping<TGComponent> map, TMLArchiPanel newarch) {
        return autoSecure(gui, name, map, newarch, "100", "0", "100", true, false, false, new HashMap<String, java.util.List<String>>());
    }

    public TMLMapping<TGComponent> autoSecure(MainGUI gui, String name, TMLMapping<TGComponent> map, TMLArchiPanel newarch, boolean autoConf,
                                              boolean autoWeakAuth, boolean autoStrongAuth) {
        return autoSecure(gui, name, map, newarch, "100", "0", "100", autoConf, autoWeakAuth, autoStrongAuth, new HashMap<String, java.util.List<String>>());
    }

    public TMLMapping<TGComponent> autoSecure(MainGUI gui, String encComp, String overhead, String decComp) {
        if (tmap == null) {
            return null;
        }
        TURTLEPanel tmlap = tmap.getCorrespondanceList().getTG(tmap.getArch().getFirstCPU()).getTDiagramPanel().tp;
        int arch = gui.tabs.indexOf(tmlap);
        gui.cloneRenameTab(arch, "enc");
        TMLArchiPanel newarch = (TMLArchiPanel) gui.tabs.get(gui.tabs.size() - 1);
        return autoSecure(gui, "enc", tmap, newarch, encComp, overhead, decComp, true, false, false, new HashMap<String, java.util.List<String>>());
    }

    public TMLMapping<TGComponent> autoSecure(MainGUI gui, String encComp, String overhead, String decComp, boolean autoConf, boolean autoWeakAuth,
                                              boolean autoStrongAuth, Map<String, List<String>> selectedCpuTasks) {
        if (tmap == null) {
            return null;
        }
        TURTLEPanel tmlap = tmap.getCorrespondanceList().getTG(tmap.getArch().getFirstCPU()).getTDiagramPanel().tp;
        int arch = gui.tabs.indexOf(tmlap);
        gui.cloneRenameTab(arch, "enc");
        TMLArchiPanel newarch = (TMLArchiPanel) gui.tabs.get(gui.tabs.size() - 1);
        return autoSecure(gui, "enc", tmap, newarch, encComp, overhead, decComp, autoConf, autoWeakAuth, autoStrongAuth, selectedCpuTasks);
    }

    public TMLMapping<TGComponent> autoSecure(MainGUI gui, String name, TMLMapping<TGComponent> map, TMLArchiPanel newarch, String encComp, String
            overhead, String decComp) {
        return autoSecure(gui, name, tmap, newarch, encComp, overhead, decComp, true, false, false, new HashMap<String, java.util.List<String>>());
    }


    public TMLMapping<TGComponent> autoSecure(MainGUI gui, String name, TMLMapping<TGComponent> map, TMLArchiPanel newarch, String encComp, String overhead, String decComp, boolean autoConf, boolean autoWeakAuth, boolean autoStrongAuth, Map<String, List<String>> selectedCpuTasks) {


        //move to another thread
        SecurityGeneration secgen = new SecurityGeneration(gui, name, map, newarch, encComp, overhead, decComp, autoConf, autoWeakAuth, autoStrongAuth, selectedCpuTasks);
        tmap = null;
        tmap = secgen.startThread();
        GTMLModeling gtm = new GTMLModeling(newarch, true);
        tmap = gtm.translateToTMLMapping();
        listE = gtm.getCorrespondanceTable();
        if (tmap != null) {
            autoMapKeys();
        }
        return tmap;
    }


    public boolean securePath(TMLMapping<TGComponent> map, TMLTask t1, TMLTask t2) {
        //Check if a path between two tasks is secure
        boolean secure = true;
        List<HwLink> links = map.getTMLArchitecture().getHwLinks();
apvrille's avatar
apvrille committed
        HwExecutionNode node1 = map.getHwNodeOf(t1);
        HwExecutionNode node2 = map.getHwNodeOf(t2);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
        List<HwNode> found = new ArrayList<HwNode>();
        List<HwNode> done = new ArrayList<HwNode>();
        List<HwNode> path = new ArrayList<HwNode>();
        Map<HwNode, List<HwNode>> pathMap = new HashMap<HwNode, List<HwNode>>();
        TraceManager.addDev("Links " + links);
        if (node1 == node2) {
            return true;
        }
        for (HwLink link : links) {
            if (link.hwnode == node1) {
                found.add(link.bus);
                List<HwNode> tmp = new ArrayList<HwNode>();
                tmp.add(link.bus);
                pathMap.put(link.bus, tmp);
            }
        }
        outerloop:
        while (found.size() > 0) {
            HwNode curr = found.remove(0);
            for (HwLink link : links) {
                if (curr == link.bus) {
                    if (link.hwnode == node2) {
                        path = pathMap.get(curr);
                        break outerloop;
                    }
                    if (!done.contains(link.hwnode) && !found.contains(link.hwnode) && link.hwnode instanceof HwBridge) {
                        found.add(link.hwnode);
                        List<HwNode> tmp = new ArrayList<HwNode>(pathMap.get(curr));
                        tmp.add(link.hwnode);
                        pathMap.put(link.hwnode, tmp);
                    }
                } else if (curr == link.hwnode) {
                    if (!done.contains(link.bus) && !found.contains(link.bus)) {
                        found.add(link.bus);
                        List<HwNode> tmp = new ArrayList<HwNode>(pathMap.get(curr));
                        tmp.add(link.bus);
                        pathMap.put(link.bus, tmp);
                    }
                }
            }
            done.add(curr);
        }
        if (path.size() == 0) {

            return true;
        } else {
            HwBus bus;
            //Check if all buses and bridges are private
            for (HwNode n : path) {
                if (n instanceof HwBus) {
                    bus = (HwBus) n;
                    if (bus.privacy == 0) {
                        return false;
                    }
                }
            }
        }
        return secure;
    }

    public void autoMapKeys() {

        if (tmap == null) {
            return;
        }
        List<HwLink> links = tmap.getArch().getHwLinks();
        //Find all Security Patterns, if they don't have an associated memory at encrypt and decrypt, map them
        TMLModeling<TGComponent> tmlm = tmap.getTMLModeling();
        if (tmlm.securityTaskMap == null) {
            return;
        }
        //
        for (SecurityPattern sp : tmlm.securityTaskMap.keySet()) {
            if (sp.type.contains("Symmetric Encryption") || sp.type.equals("MAC")) {

                for (TMLTask t : tmlm.securityTaskMap.get(sp)) {
                    ArrayList<HwMemory> mems = new ArrayList<HwMemory>();
                    boolean keyFound = false;
apvrille's avatar
apvrille committed
                    HwExecutionNode node1 = tmap.getHwNodeOf(t);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                    //Try to find memory using only private buses
                    List<HwNode> toVisit = new ArrayList<HwNode>();
                    //  List<HwNode> toMemory = new ArrayList<HwNode>();
                    List<HwNode> complete = new ArrayList<HwNode>();
                    for (HwLink link : links) {
                        if (link.hwnode == node1) {
                            if (link.bus.privacy == 1) {
                                toVisit.add(link.bus);
                            }
                        }
                    }
                    memloop:
                    while (toVisit.size() > 0) {
                        HwNode curr = toVisit.remove(0);

                        for (HwLink link : links) {
                            if (curr == link.bus) {
                                if (link.hwnode instanceof HwMemory) {
                                    mems.add((HwMemory) link.hwnode);
                                    TMLArchiMemoryNode memNode = (TMLArchiMemoryNode) listE.getTG(link.hwnode);
                                    List<TMLArchiKey> keys = memNode.getKeyList();
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                                    String patternString = "";
                                    for (TMLArchiKey key : keys) {
                                        if (key.getValue().equals(sp.name)) {

                                            keyFound = true;
                                            break memloop;
                                        }
                                        patternString += key.getValue();
                                        patternString += " ";
                                    }
                                    TraceManager.addDev("Memory " + link.hwnode.getName() + " has currently mapped: " + patternString);
                                }
                                if (!complete.contains(link.hwnode) && !toVisit.contains(link.hwnode) && link.hwnode instanceof HwBridge) {
                                    toVisit.add(link.hwnode);
                                }
                            } else if (curr == link.hwnode) {
                                if (!complete.contains(link.bus) && !toVisit.contains(link.bus)) {
                                    toVisit.add(link.bus);
                                }
                            }
                        }
                        complete.add(curr);
                    }
                    if (!keyFound) {
                        if (mems.size() > 0) {
                            TMLArchiMemoryNode memNode = (TMLArchiMemoryNode) listE.getTG(mems.get(0));
                            TMLArchiKey key = new TMLArchiKey(memNode.x, memNode.y, memNode.tdp.getMinX(), memNode.tdp.getMaxX(), memNode.tdp.getMinY(), memNode.tdp.getMaxY(), false, memNode, memNode.tdp);
                            key.setReferenceKey(sp.name);
                            key.makeFullValue();
                            TraceManager.addDev("Adding " + sp.name + " key to " + memNode.getName());
                            TraceManager.addDev("Adding " + sp + " key to " + memNode.getName());
                            memNode.tdp.addComponent(key, memNode.x, memNode.y, true, true);
                            memNode.tdp.repaint();
                        } else {
                            //
                            //	mgui.issueError("Cannot map key in memory for " + sp.name + " on task " + t.getName(), "Key Mapping Error");
                            UICheckingError ce = new UICheckingError(CheckingError.STRUCTURE_ERROR, "Cannot find secure memory to map key " + sp.name + " for task " + t.getName());
                            ce.setTDiagramPanel(tmap.getCorrespondanceList().getTG(tmap.getArch().getFirstCPU()).getTDiagramPanel());
                            ce.setTGComponent(null);
                            checkingErrors.add(ce);
                        }
                    }
                }
            } else if (sp.type.contains("Asymmetric Encryption")) {
                for (TMLTask t : tmlm.securityTaskMap.get(sp)) {
                    ArrayList<HwMemory> mems = new ArrayList<HwMemory>();
                    boolean keyFound = false;
apvrille's avatar
apvrille committed
                    HwExecutionNode node1 = tmap.getHwNodeOf(t);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                    //Try to find memory using only private buses
                    List<HwNode> toVisit = new ArrayList<HwNode>();
                    //  List<HwNode> toMemory = new ArrayList<HwNode>();
                    List<HwNode> complete = new ArrayList<HwNode>();
                    for (HwLink link : links) {
                        if (link.hwnode == node1) {
                            if (link.bus.privacy == 1) {
                                toVisit.add(link.bus);
                            }
                        }
                    }
                    memloop:
                    while (toVisit.size() > 0) {
                        HwNode curr = toVisit.remove(0);
                        for (HwLink link : links) {
                            if (curr == link.bus) {
                                if (link.hwnode instanceof HwMemory) {
                                    mems.add((HwMemory) link.hwnode);
                                    TMLArchiMemoryNode memNode = (TMLArchiMemoryNode) listE.getTG(link.hwnode);
                                    List<TMLArchiKey> keys = memNode.getKeyList();
Ludovic Apvrille's avatar
Ludovic Apvrille committed
                                    String patternString = "";
                                    for (TMLArchiKey key : keys) {
                                        if (key.getValue().equals(sp.name)) {

                                            keyFound = true;
                                            break memloop;
                                        }
                                        patternString += key.getValue();
                                        patternString += " ";
                                    }
                                    TraceManager.addDev("Memory " + link.hwnode.getName() + " has currently mapped: " + patternString);
                                }
                                if (!complete.contains(link.hwnode) && !toVisit.contains(link.hwnode) && link.hwnode instanceof HwBridge) {
                                    toVisit.add(link.hwnode);
                                }
                            } else if (curr == link.hwnode) {
                                if (!complete.contains(link.bus) && !toVisit.contains(link.bus)) {
                                    toVisit.add(link.bus);
                                }
                            }
                        }
                        complete.add(curr);
                    }
                    if (!keyFound) {
                        if (mems.size() > 0) {
                            TMLArchiMemoryNode memNode = (TMLArchiMemoryNode) listE.getTG(mems.get(0));
                            TMLArchiKey key = new TMLArchiKey(memNode.x, memNode.y, memNode.tdp.getMinX(), memNode.tdp.getMaxX(), memNode.tdp.getMinY(), memNode.tdp.getMaxY(), false, memNode, memNode.tdp);
                            key.setReferenceKey(sp.name);
                            key.makeFullValue();
                            TraceManager.addDev("Adding " + sp.name + " key to " + memNode.getName());
                            TraceManager.addDev("Adding " + sp + " key to " + memNode.getName());
                            memNode.tdp.addComponent(key, memNode.x, memNode.y, true, true);
                            memNode.tdp.repaint();
                        } else {

                            UICheckingError ce = new UICheckingError(CheckingError.STRUCTURE_ERROR, "Key " + sp.name + " on task " + t.getName() + " cannot be mapped since no memory can be accessed surely from this task.");
                            ce.setTDiagramPanel(tmap.getCorrespondanceList().getTG(tmap.getArch().getFirstCPU()).getTDiagramPanel());
                            ce.setTGComponent(null);
                            checkingErrors.add(ce);
                        }
                    }
                }
            }
        }
        TraceManager.addDev("Mapping finished");
    }

    public boolean generateFullAvatarFromTML() {
        /*if (tmlm != null && tmap == null) {
            tmap = tmlm.getDefaultMapping();
        FullTML2Avatar t2a = new FullTML2Avatar(tmlm);
        TraceManager.addDev("Avatar spec generation");
        avatarspec = t2a.generateAvatarSpec("1");
        TraceManager.addDev("Avatar spec generated");

        AvatarSyntaxChecker asc = new AvatarSyntaxChecker();

        if (mgui.isExperimentalOn()) {
            //TraceManager.addDev("Avatar spec:" + avatarspec.toString());
            //mgui.drawAvatarSpecification(avatarspec);
            //TraceManager.addDev("Avatar spec:" + avatarspec.toString());
            //AvatarSpecification av2 = avatarspec.advancedClone();
            //mgui.drawAvatarSpecification(av2);
Ludovic Apvrille's avatar
Ludovic Apvrille committed
    public void generateAvatarFromTML(boolean mc, boolean security) {
        TraceManager.addDev("Generating Avatar from TML");
        if (tmlm != null && tmap == null) {
            tmap = tmlm.getDefaultMapping();
        }
        if (avatarspec != null) {
            return;
        } else if (tmap != null) {
            t2a = new TML2Avatar(tmap, mc, security);
            TraceManager.addDev("Avatar spec generation");
            avatarspec = t2a.generateAvatarSpec("1");
            TraceManager.addDev("Avatar spec generation: done");
            if (mgui.isExperimentalOn()) {
                mgui.drawAvatarSpecification(avatarspec);
            }

    public boolean generateRandomMapping(String[] tasks, String architectureName, String mappingName) {

        TraceManager.addDev("Getting panel");

        // Get the architecture panel
        TURTLEPanel tp = mgui.getTURTLEPanel(architectureName);
        if (tp == null) {
            return false;
        }

        // Clone this panel
        int indexArch = mgui.getIndexOfPanel(tp);
        if (indexArch < 0) {
            return false;
        }
        mgui.cloneRenameTab(indexArch, mappingName);

        TraceManager.addDev("Select last panel");

        // Select the last Panel
        TURTLEPanel newTP = mgui.getTabs().get(mgui.getTabs().size() - 1);

        if (newTP == null) {
            TraceManager.addDev("Null selected panel");
        }

        if (!(newTP instanceof TMLArchiPanel)) {
            return false;
        }

        TraceManager.addDev("add Tasks");

        TMLArchiPanel mapping = (TMLArchiPanel) newTP;
        return mapping.addRandomTasks(tasks);

    }

Ludovic Apvrille's avatar
Ludovic Apvrille committed
    public boolean generateProVerifFromAVATAR(String _path, int _stateReachability, boolean _typed, boolean allowPrivateChannelDuplication, String loopLimit) {
        //
        if (avatarspec != null) {
            //use avspec
        } else if (tmap != null) {
            t2a = new TML2Avatar(tmap, false, true);
            avatarspec = t2a.generateAvatarSpec(loopLimit);
            if (mgui.isExperimentalOn()) {
                drawPanel(avatarspec, mgui.getFirstAvatarDesignPanelFound());
            }
Ludovic Apvrille's avatar
Ludovic Apvrille committed

        } else if (tmlm != null) {
            //Generate default mapping
            tmap = tmlm.getDefaultMapping();

            t2a = new TML2Avatar(tmap, false, true);
            avatarspec = t2a.generateAvatarSpec(loopLimit);